GUIBase.cpp 13 KB

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