GUIBase.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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, pressedL);
  26. pressedL = true;
  27. } else {
  28. clickLeft(false, pressedL);
  29. pressedL = false;
  30. }
  31. }
  32. }
  33. void CGuiHandler::popInt( IShowActivable *top )
  34. {
  35. assert(listInt.front() == top);
  36. top->deactivate();
  37. listInt.pop_front();
  38. objsToBlit -= top;
  39. if(listInt.size())
  40. listInt.front()->activate();
  41. totalRedraw();
  42. }
  43. void CGuiHandler::popIntTotally( IShowActivable *top )
  44. {
  45. assert(listInt.front() == top);
  46. popInt(top);
  47. delete top;
  48. }
  49. void CGuiHandler::pushInt( IShowActivable *newInt )
  50. {
  51. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  52. screenBuf = screen2;
  53. if(listInt.size())
  54. listInt.front()->deactivate();
  55. listInt.push_front(newInt);
  56. newInt->activate();
  57. objsToBlit.push_back(newInt);
  58. totalRedraw();
  59. }
  60. void CGuiHandler::popInts( int howMany )
  61. {
  62. if(!howMany) return; //senseless but who knows...
  63. assert(listInt.size() > howMany);
  64. listInt.front()->deactivate();
  65. for(int i=0; i < howMany; i++)
  66. {
  67. objsToBlit -= listInt.front();
  68. delete listInt.front();
  69. listInt.pop_front();
  70. }
  71. listInt.front()->activate();
  72. totalRedraw();
  73. }
  74. IShowActivable * CGuiHandler::topInt()
  75. {
  76. if(!listInt.size())
  77. return NULL;
  78. else
  79. return listInt.front();
  80. }
  81. void CGuiHandler::totalRedraw()
  82. {
  83. for(int i=0;i<objsToBlit.size();i++)
  84. objsToBlit[i]->showAll(screen2);
  85. blitAt(screen2,0,0,screen);
  86. if(objsToBlit.size())
  87. objsToBlit.back()->showAll(screen);
  88. }
  89. void CGuiHandler::updateTime()
  90. {
  91. int tv = th.getDif();
  92. std::list<CIntObject*> hlp = timeinterested;
  93. for (std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  94. {
  95. if(!vstd::contains(timeinterested,*i)) continue;
  96. if ((*i)->toNextTick>=0)
  97. (*i)->toNextTick-=tv;
  98. if ((*i)->toNextTick<0)
  99. (*i)->tick();
  100. }
  101. }
  102. void CGuiHandler::handleEvents()
  103. {
  104. while(true)
  105. {
  106. SDL_Event *ev = NULL;
  107. {
  108. boost::unique_lock<boost::mutex> lock(eventsM);
  109. if(!events.size())
  110. {
  111. return;
  112. }
  113. else
  114. {
  115. ev = events.front();
  116. events.pop();
  117. }
  118. }
  119. handleEvent(ev);
  120. delete ev;
  121. }
  122. }
  123. void CGuiHandler::handleEvent(SDL_Event *sEvent)
  124. {
  125. current = sEvent;
  126. bool prev;
  127. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  128. {
  129. SDL_KeyboardEvent key = sEvent->key;
  130. //translate numpad keys
  131. if (key.keysym.sym >= SDLK_KP0 && key.keysym.sym <= SDLK_KP9)
  132. {
  133. key.keysym.sym = (SDLKey) (key.keysym.sym - SDLK_KP0 + SDLK_0);
  134. }
  135. else if(key.keysym.sym == SDLK_KP_ENTER)
  136. {
  137. key.keysym.sym = (SDLKey)SDLK_RETURN;
  138. }
  139. bool keysCaptured = false;
  140. for(std::list<CIntObject*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
  141. {
  142. if((*i)->captureAllKeys)
  143. {
  144. keysCaptured = true;
  145. break;
  146. }
  147. }
  148. std::list<CIntObject*> miCopy = keyinterested;
  149. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  150. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  151. (**i).keyPressed(key);
  152. }
  153. else if(sEvent->type==SDL_MOUSEMOTION)
  154. {
  155. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  156. handleMouseMotion(sEvent);
  157. }
  158. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  159. {
  160. std::list<CIntObject*> hlp = lclickable;
  161. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  162. {
  163. if(!vstd::contains(lclickable,*i)) continue;
  164. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  165. {
  166. prev = (*i)->pressedL;
  167. (*i)->pressedL = true;
  168. (*i)->clickLeft(true, prev);
  169. }
  170. }
  171. }
  172. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  173. {
  174. std::list<CIntObject*> hlp = lclickable;
  175. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  176. {
  177. if(!vstd::contains(lclickable,*i)) continue;
  178. prev = (*i)->pressedL;
  179. (*i)->pressedL = false;
  180. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  181. {
  182. (*i)->clickLeft(false, prev);
  183. }
  184. else
  185. (*i)->clickLeft(boost::logic::indeterminate, prev);
  186. }
  187. }
  188. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  189. {
  190. std::list<CIntObject*> hlp = rclickable;
  191. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  192. {
  193. if(!vstd::contains(rclickable,*i)) continue;
  194. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  195. {
  196. prev = (*i)->pressedR;
  197. (*i)->pressedR = true;
  198. (*i)->clickRight(true, prev);
  199. }
  200. }
  201. }
  202. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  203. {
  204. std::list<CIntObject*> hlp = rclickable;
  205. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  206. {
  207. if(!vstd::contains(rclickable,*i)) continue;
  208. prev = (*i)->pressedR;
  209. (*i)->pressedR = false;
  210. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  211. {
  212. (*i)->clickRight(false, prev);
  213. }
  214. else
  215. (*i)->clickRight(boost::logic::indeterminate, prev);
  216. }
  217. }
  218. current = NULL;
  219. } //event end
  220. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  221. {
  222. //sending active, hovered hoverable objects hover() call
  223. std::vector<CIntObject*> hlp;
  224. for(std::list<CIntObject*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  225. {
  226. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  227. {
  228. if (!(*i)->hovered)
  229. hlp.push_back((*i));
  230. }
  231. else if ((*i)->hovered)
  232. {
  233. (*i)->hover(false);
  234. (*i)->hovered = false;
  235. }
  236. }
  237. for(int i=0; i<hlp.size();i++)
  238. {
  239. hlp[i]->hover(true);
  240. hlp[i]->hovered = true;
  241. }
  242. //sending active, MotionInterested objects mouseMoved() call
  243. std::list<CIntObject*> miCopy = motioninterested;
  244. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  245. {
  246. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  247. {
  248. (*i)->mouseMoved(sEvent->motion);
  249. }
  250. }
  251. }
  252. void CGuiHandler::simpleRedraw()
  253. {
  254. //update only top interface and draw background
  255. if(objsToBlit.size() > 1)
  256. blitAt(screen2,0,0,screen); //blit background
  257. objsToBlit.back()->show(screen); //blit active interface/window
  258. }
  259. void CIntObject::activateLClick()
  260. {
  261. GH.lclickable.push_front(this);
  262. active |= LCLICK;
  263. }
  264. void CIntObject::deactivateLClick()
  265. {
  266. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  267. assert(hlp != GH.lclickable.end());
  268. GH.lclickable.erase(hlp);
  269. active &= ~LCLICK;
  270. }
  271. void CIntObject::clickLeft(tribool down, bool previousState)
  272. {
  273. }
  274. void CIntObject::activateRClick()
  275. {
  276. GH.rclickable.push_front(this);
  277. active |= RCLICK;
  278. }
  279. void CIntObject::deactivateRClick()
  280. {
  281. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  282. assert(hlp != GH.rclickable.end());
  283. GH.rclickable.erase(hlp);
  284. active &= ~RCLICK;
  285. }
  286. void CIntObject::clickRight(tribool down, bool previousState)
  287. {
  288. }
  289. void CIntObject::activateHover()
  290. {
  291. GH.hoverable.push_front(this);
  292. active |= HOVER;
  293. }
  294. void CIntObject::deactivateHover()
  295. {
  296. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  297. assert(hlp != GH.hoverable.end());
  298. GH.hoverable.erase(hlp);
  299. active &= ~HOVER;
  300. }
  301. void CIntObject::hover( bool on )
  302. {
  303. }
  304. void CIntObject::activateKeys()
  305. {
  306. GH.keyinterested.push_front(this);
  307. active |= KEYBOARD;
  308. }
  309. void CIntObject::deactivateKeys()
  310. {
  311. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  312. assert(hlp != GH.keyinterested.end());
  313. GH.keyinterested.erase(hlp);
  314. active &= ~KEYBOARD;
  315. }
  316. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  317. {
  318. }
  319. void CIntObject::activateMouseMove()
  320. {
  321. GH.motioninterested.push_front(this);
  322. active |= MOVE;
  323. }
  324. void CIntObject::deactivateMouseMove()
  325. {
  326. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  327. assert(hlp != GH.motioninterested.end());
  328. GH.motioninterested.erase(hlp);
  329. active &= ~MOVE;
  330. }
  331. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  332. {
  333. }
  334. void CIntObject::activateTimer()
  335. {
  336. GH.timeinterested.push_back(this);
  337. active |= TIME;
  338. }
  339. void CIntObject::deactivateTimer()
  340. {
  341. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  342. assert(hlp != GH.timeinterested.end());
  343. GH.timeinterested.erase(hlp);
  344. active &= ~TIME;
  345. }
  346. void CIntObject::tick()
  347. {
  348. }
  349. CIntObject::CIntObject()
  350. {
  351. pressedL = pressedR = hovered = captureAllKeys = strongInterest = toNextTick = active = used = 0;
  352. recActions = defActions = GH.defActionsDef;
  353. pos.x = 0;
  354. pos.y = 0;
  355. pos.w = 0;
  356. pos.h = 0;
  357. if(GH.captureChildren)
  358. {
  359. assert(GH.createdObj.size());
  360. parent = GH.createdObj.front();
  361. parent->children.push_back(this);
  362. if(parent->defActions & SHARE_POS)
  363. {
  364. pos.x = parent->pos.x;
  365. pos.y = parent->pos.y;
  366. }
  367. }
  368. }
  369. void CIntObject::show( SDL_Surface * to )
  370. {
  371. if(defActions & UPDATE)
  372. for(size_t i = 0; i < children.size(); i++)
  373. if(children[i]->recActions & UPDATE)
  374. children[i]->show(to);
  375. }
  376. void CIntObject::showAll( SDL_Surface * to )
  377. {
  378. if(defActions & SHOWALL)
  379. {
  380. for(size_t i = 0; i < children.size(); i++)
  381. if(children[i]->recActions & SHOWALL)
  382. children[i]->showAll(to);
  383. }
  384. else
  385. show(to);
  386. }
  387. void CIntObject::activate()
  388. {
  389. assert(!active);
  390. active |= GENERAL;
  391. if(used & LCLICK)
  392. activateLClick();
  393. if(used & RCLICK)
  394. activateRClick();
  395. if(used & HOVER)
  396. activateHover();
  397. if(used & MOVE)
  398. activateMouseMove();
  399. if(used & KEYBOARD)
  400. activateKeys();
  401. if(used & TIME)
  402. activateTimer();
  403. if(defActions & ACTIVATE)
  404. for(size_t i = 0; i < children.size(); i++)
  405. if(children[i]->recActions & ACTIVATE)
  406. children[i]->activate();
  407. }
  408. void CIntObject::deactivate()
  409. {
  410. assert(active);
  411. active &= ~ GENERAL;
  412. if(used & LCLICK)
  413. deactivateLClick();
  414. if(used & RCLICK)
  415. deactivateRClick();
  416. if(used & HOVER)
  417. deactivateHover();
  418. if(used & MOVE)
  419. deactivateMouseMove();
  420. if(used & KEYBOARD)
  421. deactivateKeys();
  422. if(used & TIME)
  423. deactivateTimer();
  424. if(defActions & DEACTIVATE)
  425. for(size_t i = 0; i < children.size(); i++)
  426. if(children[i]->recActions & DEACTIVATE)
  427. children[i]->deactivate();
  428. }
  429. CIntObject::~CIntObject()
  430. {
  431. assert(!active); //do not delete active obj
  432. if(defActions & DISPOSE)
  433. for(size_t i = 0; i < children.size(); i++)
  434. if(children[i]->recActions & DISPOSE)
  435. delete children[i];
  436. }
  437. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  438. {
  439. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  440. }
  441. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  442. {
  443. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  444. }
  445. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  446. {
  447. blitAt(src, pos.x + x, pos.y + y, dst);
  448. }
  449. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh /*= false*/ )
  450. {
  451. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst, refrsh);
  452. }
  453. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh /*= false*/ )
  454. {
  455. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  456. }
  457. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  458. {
  459. bg = BG;
  460. freeSurf = Free;
  461. pos.x += x;
  462. pos.y += y;
  463. pos.w = BG->w;
  464. pos.h = BG->h;
  465. }
  466. CPicture::~CPicture()
  467. {
  468. if(freeSurf)
  469. SDL_FreeSurface(bg);
  470. }
  471. void CPicture::showAll( SDL_Surface * to )
  472. {
  473. blitAt(bg, pos, to);
  474. }
  475. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  476. :myObj(obj)
  477. {
  478. GH.createdObj.push_front(obj);
  479. GH.captureChildren = true;
  480. }
  481. ObjectConstruction::~ObjectConstruction()
  482. {
  483. assert(GH.createdObj.size());
  484. assert(GH.createdObj.front() == myObj);
  485. GH.createdObj.pop_front();
  486. GH.captureChildren = GH.createdObj.size();
  487. }
  488. BlockCapture::BlockCapture()
  489. {
  490. previous = GH.captureChildren;
  491. GH.captureChildren = false;
  492. }
  493. BlockCapture::~BlockCapture()
  494. {
  495. GH.captureChildren = previous;
  496. }
  497. void IShowable::redraw()
  498. {
  499. showAll(screenBuf);
  500. if(screenBuf != screen)
  501. showAll(screen);
  502. }