GUIBase.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include "SDL_Extensions.h"
  2. #include <cassert>
  3. #include <boost/thread/locks.hpp>
  4. #include "GUIBase.h"
  5. #include <boost/thread/mutex.hpp>
  6. #include <queue>
  7. #include "CGameInfo.h"
  8. #include "CCursorHandler.h"
  9. /*
  10. * GUIBase.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. extern std::queue<SDL_Event*> events;
  19. extern boost::mutex eventsM;
  20. void KeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  21. {
  22. if(vstd::contains(assignedKeys,key.keysym.sym))
  23. {
  24. if(key.state == SDL_PRESSED)
  25. clickLeft(true);
  26. else
  27. clickLeft(false);
  28. }
  29. }
  30. CButtonBase::CButtonBase()
  31. {
  32. bitmapOffset = 0;
  33. curimg=0;
  34. type=-1;
  35. abs=false;
  36. active=false;
  37. notFreeButton = false;
  38. ourObj=NULL;
  39. state=0;
  40. }
  41. CButtonBase::~CButtonBase()
  42. {
  43. if(notFreeButton)
  44. return;
  45. for(int i =0; i<imgs.size();i++)
  46. for(int j=0;j<imgs[i].size();j++)
  47. SDL_FreeSurface(imgs[i][j]);
  48. }
  49. void CButtonBase::show(SDL_Surface * to)
  50. {
  51. int img = std::min(state+bitmapOffset,int(imgs[curimg].size()-1));
  52. img = std::max(0, img);
  53. if (abs)
  54. {
  55. blitAt(imgs[curimg][img],pos.x,pos.y,to);
  56. }
  57. else
  58. {
  59. blitAt(imgs[curimg][img],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y,to);
  60. }
  61. }
  62. ClickableL::ClickableL()
  63. {
  64. pressedL=false;
  65. }
  66. ClickableL::~ClickableL()
  67. {
  68. }
  69. void ClickableL::clickLeft(boost::logic::tribool down)
  70. {
  71. if (down)
  72. pressedL=true;
  73. else
  74. pressedL=false;
  75. }
  76. void ClickableL::activate()
  77. {
  78. GH.lclickable.push_front(this);
  79. }
  80. void ClickableL::deactivate()
  81. {
  82. GH.lclickable.erase(std::find(GH.lclickable.begin(),GH.lclickable.end(),this));
  83. }
  84. ClickableR::ClickableR()
  85. {
  86. pressedR=false;
  87. }
  88. ClickableR::~ClickableR()
  89. {}
  90. void ClickableR::clickRight(boost::logic::tribool down)
  91. {
  92. if (down)
  93. pressedR=true;
  94. else
  95. pressedR=false;
  96. }
  97. void ClickableR::activate()
  98. {
  99. GH.rclickable.push_front(this);
  100. }
  101. void ClickableR::deactivate()
  102. {
  103. GH.rclickable.erase(std::find(GH.rclickable.begin(),GH.rclickable.end(),this));
  104. }
  105. //ClickableR
  106. Hoverable::~Hoverable()
  107. {}
  108. void Hoverable::activate()
  109. {
  110. GH.hoverable.push_front(this);
  111. }
  112. void Hoverable::deactivate()
  113. {
  114. GH.hoverable.erase(std::find(GH.hoverable.begin(),GH.hoverable.end(),this));
  115. }
  116. void Hoverable::hover(bool on)
  117. {
  118. hovered=on;
  119. }
  120. //Hoverable
  121. KeyInterested::~KeyInterested()
  122. {}
  123. void KeyInterested::activate()
  124. {
  125. GH.keyinterested.push_front(this);
  126. }
  127. void KeyInterested::deactivate()
  128. {
  129. GH.keyinterested.erase(std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this));
  130. }
  131. //KeyInterested
  132. void MotionInterested::activate()
  133. {
  134. GH.motioninterested.push_front(this);
  135. }
  136. void MotionInterested::deactivate()
  137. {
  138. GH.motioninterested.erase(std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this));
  139. }
  140. void TimeInterested::activate()
  141. {
  142. GH.timeinterested.push_back(this);
  143. }
  144. void TimeInterested::deactivate()
  145. {
  146. GH.timeinterested.erase(std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this));
  147. }
  148. void CGuiHandler::popInt( IShowActivable *top )
  149. {
  150. assert(listInt.front() == top);
  151. top->deactivate();
  152. listInt.pop_front();
  153. objsToBlit -= top;
  154. if(listInt.size())
  155. listInt.front()->activate();
  156. totalRedraw();
  157. }
  158. void CGuiHandler::popIntTotally( IShowActivable *top )
  159. {
  160. assert(listInt.front() == top);
  161. popInt(top);
  162. delete top;
  163. }
  164. void CGuiHandler::pushInt( IShowActivable *newInt )
  165. {
  166. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  167. screenBuf = screen2;
  168. if(listInt.size())
  169. listInt.front()->deactivate();
  170. listInt.push_front(newInt);
  171. newInt->activate();
  172. objsToBlit.push_back(newInt);
  173. totalRedraw();
  174. }
  175. void CGuiHandler::popInts( int howMany )
  176. {
  177. if(!howMany) return; //senseless but who knows...
  178. assert(listInt.size() > howMany);
  179. listInt.front()->deactivate();
  180. for(int i=0; i < howMany; i++)
  181. {
  182. objsToBlit -= listInt.front();
  183. delete listInt.front();
  184. listInt.pop_front();
  185. }
  186. listInt.front()->activate();
  187. totalRedraw();
  188. }
  189. IShowActivable * CGuiHandler::topInt()
  190. {
  191. if(!listInt.size())
  192. return NULL;
  193. else
  194. return listInt.front();
  195. }
  196. void CGuiHandler::totalRedraw()
  197. {
  198. for(int i=0;i<objsToBlit.size();i++)
  199. objsToBlit[i]->showAll(screen2);
  200. blitAt(screen2,0,0,screen);
  201. if(objsToBlit.size())
  202. objsToBlit.back()->showAll(screen);
  203. }
  204. void CGuiHandler::updateTime()
  205. {
  206. int tv = th.getDif();
  207. std::list<TimeInterested*> hlp = timeinterested;
  208. for (std::list<TimeInterested*>::iterator i=hlp.begin(); i != hlp.end();i++)
  209. {
  210. if(!vstd::contains(timeinterested,*i)) continue;
  211. if ((*i)->toNextTick>=0)
  212. (*i)->toNextTick-=tv;
  213. if ((*i)->toNextTick<0)
  214. (*i)->tick();
  215. }
  216. }
  217. void CGuiHandler::handleEvents()
  218. {
  219. while(true)
  220. {
  221. SDL_Event *ev = NULL;
  222. {
  223. boost::unique_lock<boost::mutex> lock(eventsM);
  224. if(!events.size())
  225. {
  226. return;
  227. }
  228. else
  229. {
  230. ev = events.front();
  231. events.pop();
  232. }
  233. }
  234. handleEvent(ev);
  235. delete ev;
  236. }
  237. }
  238. void CGuiHandler::handleEvent(SDL_Event *sEvent)
  239. {
  240. current = sEvent;
  241. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  242. {
  243. SDL_KeyboardEvent key = sEvent->key;
  244. //translate numpad keys
  245. if (key.keysym.sym >= SDLK_KP0 && key.keysym.sym <= SDLK_KP9)
  246. {
  247. key.keysym.sym = (SDLKey) (key.keysym.sym - SDLK_KP0 + SDLK_0);
  248. }
  249. else if(key.keysym.sym == SDLK_KP_ENTER)
  250. {
  251. key.keysym.sym = (SDLKey)SDLK_RETURN;
  252. }
  253. bool keysCaptured = false;
  254. for(std::list<KeyInterested*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
  255. {
  256. if((*i)->captureAllKeys)
  257. {
  258. keysCaptured = true;
  259. break;
  260. }
  261. }
  262. std::list<KeyInterested*> miCopy = keyinterested;
  263. for(std::list<KeyInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  264. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  265. (**i).keyPressed(key);
  266. }
  267. else if(sEvent->type==SDL_MOUSEMOTION)
  268. {
  269. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  270. handleMouseMotion(sEvent);
  271. }
  272. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  273. {
  274. std::list<ClickableL*> hlp = lclickable;
  275. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  276. {
  277. if(!vstd::contains(lclickable,*i)) continue;
  278. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  279. {
  280. (*i)->clickLeft(true);
  281. }
  282. }
  283. }
  284. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  285. {
  286. std::list<ClickableL*> hlp = lclickable;
  287. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  288. {
  289. if(!vstd::contains(lclickable,*i)) continue;
  290. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  291. {
  292. (*i)->clickLeft(false);
  293. }
  294. else
  295. (*i)->clickLeft(boost::logic::indeterminate);
  296. }
  297. }
  298. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  299. {
  300. std::list<ClickableR*> hlp = rclickable;
  301. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  302. {
  303. if(!vstd::contains(rclickable,*i)) continue;
  304. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  305. {
  306. (*i)->clickRight(true);
  307. }
  308. }
  309. }
  310. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  311. {
  312. std::list<ClickableR*> hlp = rclickable;
  313. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  314. {
  315. if(!vstd::contains(rclickable,*i)) continue;
  316. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  317. {
  318. (*i)->clickRight(false);
  319. }
  320. else
  321. (*i)->clickRight(boost::logic::indeterminate);
  322. }
  323. }
  324. current = NULL;
  325. } //event end
  326. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  327. {
  328. //sending active, hovered hoverable objects hover() call
  329. std::vector<Hoverable*> hlp;
  330. for(std::list<Hoverable*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  331. {
  332. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  333. {
  334. if (!(*i)->hovered)
  335. hlp.push_back((*i));
  336. }
  337. else if ((*i)->hovered)
  338. {
  339. (*i)->hover(false);
  340. }
  341. }
  342. for(int i=0; i<hlp.size();i++)
  343. hlp[i]->hover(true);
  344. //sending active, MotionInterested objects mouseMoved() call
  345. std::list<MotionInterested*> miCopy = motioninterested;
  346. for(std::list<MotionInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  347. {
  348. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  349. {
  350. (*i)->mouseMoved(sEvent->motion);
  351. }
  352. }
  353. }
  354. void CGuiHandler::simpleRedraw()
  355. {
  356. //update only top interface and draw background
  357. if(objsToBlit.size() > 1)
  358. blitAt(screen2,0,0,screen); //blit background
  359. objsToBlit.back()->show(screen); //blit active interface/window
  360. }