GUIBase.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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)
  162. {
  163. if(sEvent->button.button == SDL_BUTTON_LEFT)
  164. {
  165. if(lastClick == sEvent->motion && (SDL_GetTicks() - lastClickTime) < 300)
  166. {
  167. std::list<CIntObject*> hlp = doubleClickInterested;
  168. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  169. {
  170. if(!vstd::contains(doubleClickInterested,*i)) continue;
  171. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  172. {
  173. (*i)->onDoubleClick();
  174. }
  175. }
  176. }
  177. lastClick = sEvent->motion;
  178. lastClickTime = SDL_GetTicks();
  179. std::list<CIntObject*> hlp = lclickable;
  180. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  181. {
  182. if(!vstd::contains(lclickable,*i)) continue;
  183. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  184. {
  185. prev = (*i)->pressedL;
  186. (*i)->pressedL = true;
  187. (*i)->clickLeft(true, prev);
  188. }
  189. }
  190. }
  191. else if (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->button.button == SDL_BUTTON_WHEELDOWN || sEvent->button.button == SDL_BUTTON_WHEELUP)
  206. {
  207. std::list<CIntObject*> hlp = wheelInterested;
  208. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  209. {
  210. if(!vstd::contains(wheelInterested,*i)) continue;
  211. (*i)->wheelScrolled(sEvent->button.button == SDL_BUTTON_WHEELDOWN, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  212. }
  213. }
  214. }
  215. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  216. {
  217. std::list<CIntObject*> hlp = lclickable;
  218. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  219. {
  220. if(!vstd::contains(lclickable,*i)) continue;
  221. prev = (*i)->pressedL;
  222. (*i)->pressedL = false;
  223. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  224. {
  225. (*i)->clickLeft(false, prev);
  226. }
  227. else
  228. (*i)->clickLeft(boost::logic::indeterminate, prev);
  229. }
  230. }
  231. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  232. {
  233. std::list<CIntObject*> hlp = rclickable;
  234. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  235. {
  236. if(!vstd::contains(rclickable,*i)) continue;
  237. prev = (*i)->pressedR;
  238. (*i)->pressedR = false;
  239. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  240. {
  241. (*i)->clickRight(false, prev);
  242. }
  243. else
  244. (*i)->clickRight(boost::logic::indeterminate, prev);
  245. }
  246. }
  247. current = NULL;
  248. } //event end
  249. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  250. {
  251. //sending active, hovered hoverable objects hover() call
  252. std::vector<CIntObject*> hlp;
  253. for(std::list<CIntObject*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  254. {
  255. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  256. {
  257. if (!(*i)->hovered)
  258. hlp.push_back((*i));
  259. }
  260. else if ((*i)->hovered)
  261. {
  262. (*i)->hover(false);
  263. (*i)->hovered = false;
  264. }
  265. }
  266. for(int i=0; i<hlp.size();i++)
  267. {
  268. hlp[i]->hover(true);
  269. hlp[i]->hovered = true;
  270. }
  271. //sending active, MotionInterested objects mouseMoved() call
  272. std::list<CIntObject*> miCopy = motioninterested;
  273. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  274. {
  275. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  276. {
  277. (*i)->mouseMoved(sEvent->motion);
  278. }
  279. }
  280. }
  281. void CGuiHandler::simpleRedraw()
  282. {
  283. //update only top interface and draw background
  284. if(objsToBlit.size() > 1)
  285. blitAt(screen2,0,0,screen); //blit background
  286. objsToBlit.back()->show(screen); //blit active interface/window
  287. }
  288. void CIntObject::activateLClick()
  289. {
  290. GH.lclickable.push_front(this);
  291. active |= LCLICK;
  292. }
  293. void CIntObject::deactivateLClick()
  294. {
  295. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  296. assert(hlp != GH.lclickable.end());
  297. GH.lclickable.erase(hlp);
  298. active &= ~LCLICK;
  299. }
  300. void CIntObject::clickLeft(tribool down, bool previousState)
  301. {
  302. }
  303. void CIntObject::activateRClick()
  304. {
  305. GH.rclickable.push_front(this);
  306. active |= RCLICK;
  307. }
  308. void CIntObject::deactivateRClick()
  309. {
  310. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  311. assert(hlp != GH.rclickable.end());
  312. GH.rclickable.erase(hlp);
  313. active &= ~RCLICK;
  314. }
  315. void CIntObject::clickRight(tribool down, bool previousState)
  316. {
  317. }
  318. void CIntObject::activateHover()
  319. {
  320. GH.hoverable.push_front(this);
  321. active |= HOVER;
  322. }
  323. void CIntObject::deactivateHover()
  324. {
  325. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  326. assert(hlp != GH.hoverable.end());
  327. GH.hoverable.erase(hlp);
  328. active &= ~HOVER;
  329. }
  330. void CIntObject::hover( bool on )
  331. {
  332. }
  333. void CIntObject::activateKeys()
  334. {
  335. GH.keyinterested.push_front(this);
  336. active |= KEYBOARD;
  337. }
  338. void CIntObject::deactivateKeys()
  339. {
  340. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  341. assert(hlp != GH.keyinterested.end());
  342. GH.keyinterested.erase(hlp);
  343. active &= ~KEYBOARD;
  344. }
  345. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  346. {
  347. }
  348. void CIntObject::activateMouseMove()
  349. {
  350. GH.motioninterested.push_front(this);
  351. active |= MOVE;
  352. }
  353. void CIntObject::deactivateMouseMove()
  354. {
  355. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  356. assert(hlp != GH.motioninterested.end());
  357. GH.motioninterested.erase(hlp);
  358. active &= ~MOVE;
  359. }
  360. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  361. {
  362. }
  363. void CIntObject::activateTimer()
  364. {
  365. GH.timeinterested.push_back(this);
  366. active |= TIME;
  367. }
  368. void CIntObject::deactivateTimer()
  369. {
  370. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  371. assert(hlp != GH.timeinterested.end());
  372. GH.timeinterested.erase(hlp);
  373. active &= ~TIME;
  374. }
  375. void CIntObject::tick()
  376. {
  377. }
  378. CIntObject::CIntObject()
  379. {
  380. pressedL = pressedR = hovered = captureAllKeys = strongInterest = toNextTick = active = used = 0;
  381. recActions = defActions = GH.defActionsDef;
  382. pos.x = 0;
  383. pos.y = 0;
  384. pos.w = 0;
  385. pos.h = 0;
  386. if(GH.captureChildren)
  387. {
  388. assert(GH.createdObj.size());
  389. parent = GH.createdObj.front();
  390. parent->children.push_back(this);
  391. if(parent->defActions & SHARE_POS)
  392. {
  393. pos.x = parent->pos.x;
  394. pos.y = parent->pos.y;
  395. }
  396. }
  397. }
  398. void CIntObject::show( SDL_Surface * to )
  399. {
  400. if(defActions & UPDATE)
  401. for(size_t i = 0; i < children.size(); i++)
  402. if(children[i]->recActions & UPDATE)
  403. children[i]->show(to);
  404. }
  405. void CIntObject::showAll( SDL_Surface * to )
  406. {
  407. if(defActions & SHOWALL)
  408. {
  409. for(size_t i = 0; i < children.size(); i++)
  410. if(children[i]->recActions & SHOWALL)
  411. children[i]->showAll(to);
  412. }
  413. else
  414. show(to);
  415. }
  416. void CIntObject::activate()
  417. {
  418. assert(!active);
  419. active |= GENERAL;
  420. if(used & LCLICK)
  421. activateLClick();
  422. if(used & RCLICK)
  423. activateRClick();
  424. if(used & HOVER)
  425. activateHover();
  426. if(used & MOVE)
  427. activateMouseMove();
  428. if(used & KEYBOARD)
  429. activateKeys();
  430. if(used & TIME)
  431. activateTimer();
  432. if(used & WHEEL)
  433. activateWheel();
  434. if(used & DOUBLECLICK)
  435. activateDClick();
  436. if(defActions & ACTIVATE)
  437. for(size_t i = 0; i < children.size(); i++)
  438. if(children[i]->recActions & ACTIVATE)
  439. children[i]->activate();
  440. }
  441. void CIntObject::deactivate()
  442. {
  443. assert(active);
  444. active &= ~ GENERAL;
  445. if(used & LCLICK)
  446. deactivateLClick();
  447. if(used & RCLICK)
  448. deactivateRClick();
  449. if(used & HOVER)
  450. deactivateHover();
  451. if(used & MOVE)
  452. deactivateMouseMove();
  453. if(used & KEYBOARD)
  454. deactivateKeys();
  455. if(used & TIME)
  456. deactivateTimer();
  457. if(used & WHEEL)
  458. deactivateWheel();
  459. if(used & DOUBLECLICK)
  460. deactivateDClick();
  461. if(defActions & DEACTIVATE)
  462. for(size_t i = 0; i < children.size(); i++)
  463. if(children[i]->recActions & DEACTIVATE)
  464. children[i]->deactivate();
  465. }
  466. CIntObject::~CIntObject()
  467. {
  468. assert(!active); //do not delete active obj
  469. if(defActions & DISPOSE)
  470. for(size_t i = 0; i < children.size(); i++)
  471. if(children[i]->recActions & DISPOSE)
  472. delete children[i];
  473. }
  474. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  475. {
  476. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  477. }
  478. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  479. {
  480. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  481. }
  482. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  483. {
  484. blitAt(src, pos.x + x, pos.y + y, dst);
  485. }
  486. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh /*= false*/ )
  487. {
  488. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst, refrsh);
  489. }
  490. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh /*= false*/ )
  491. {
  492. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  493. }
  494. void CIntObject::disable()
  495. {
  496. if(active)
  497. deactivate();
  498. recActions = DISPOSE;
  499. }
  500. void CIntObject::enable(bool activation)
  501. {
  502. if(!active && activation)
  503. activate();
  504. recActions = 255;
  505. }
  506. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  507. {
  508. return isItIn(&rect, x - pos.x, y - pos.y);
  509. }
  510. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  511. {
  512. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  513. }
  514. void CIntObject::activateWheel()
  515. {
  516. GH.wheelInterested.push_front(this);
  517. active |= WHEEL;
  518. }
  519. void CIntObject::deactivateWheel()
  520. {
  521. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  522. assert(hlp != GH.wheelInterested.end());
  523. GH.wheelInterested.erase(hlp);
  524. active &= ~WHEEL;
  525. }
  526. void CIntObject::wheelScrolled(bool down, bool in)
  527. {
  528. }
  529. void CIntObject::activateDClick()
  530. {
  531. GH.doubleClickInterested.push_front(this);
  532. active |= DOUBLECLICK;
  533. }
  534. void CIntObject::deactivateDClick()
  535. {
  536. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  537. assert(hlp != GH.doubleClickInterested.end());
  538. GH.doubleClickInterested.erase(hlp);
  539. active &= ~DOUBLECLICK;
  540. }
  541. void CIntObject::onDoubleClick()
  542. {
  543. }
  544. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  545. {
  546. bg = BG;
  547. freeSurf = Free;
  548. pos.x += x;
  549. pos.y += y;
  550. pos.w = BG->w;
  551. pos.h = BG->h;
  552. }
  553. CPicture::~CPicture()
  554. {
  555. if(freeSurf)
  556. SDL_FreeSurface(bg);
  557. }
  558. void CPicture::showAll( SDL_Surface * to )
  559. {
  560. blitAt(bg, pos, to);
  561. }
  562. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  563. :myObj(obj)
  564. {
  565. GH.createdObj.push_front(obj);
  566. GH.captureChildren = true;
  567. }
  568. ObjectConstruction::~ObjectConstruction()
  569. {
  570. assert(GH.createdObj.size());
  571. assert(GH.createdObj.front() == myObj);
  572. GH.createdObj.pop_front();
  573. GH.captureChildren = GH.createdObj.size();
  574. }
  575. BlockCapture::BlockCapture()
  576. {
  577. previous = GH.captureChildren;
  578. GH.captureChildren = false;
  579. }
  580. BlockCapture::~BlockCapture()
  581. {
  582. GH.captureChildren = previous;
  583. }
  584. void IShowable::redraw()
  585. {
  586. showAll(screenBuf);
  587. if(screenBuf != screen)
  588. showAll(screen);
  589. }