CGuiHandler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include "StdInc.h"
  2. #include "CGuiHandler.h"
  3. #include "SDL_Extensions.h"
  4. #include "CIntObject.h"
  5. #include "../CGameInfo.h"
  6. #include "CCursorHandler.h"
  7. #include "../../lib/CThreadHelper.h"
  8. #include "../../lib/CConfigHandler.h"
  9. #include "../CMT.h"
  10. extern std::queue<SDL_Event> events;
  11. extern boost::mutex eventsM;
  12. boost::thread_specific_ptr<bool> inGuiThread;
  13. SObjectConstruction::SObjectConstruction( CIntObject *obj )
  14. :myObj(obj)
  15. {
  16. GH.createdObj.push_front(obj);
  17. GH.captureChildren = true;
  18. }
  19. SObjectConstruction::~SObjectConstruction()
  20. {
  21. assert(GH.createdObj.size());
  22. assert(GH.createdObj.front() == myObj);
  23. GH.createdObj.pop_front();
  24. GH.captureChildren = GH.createdObj.size();
  25. }
  26. SSetCaptureState::SSetCaptureState(bool allow, ui8 actions)
  27. {
  28. previousCapture = GH.captureChildren;
  29. GH.captureChildren = false;
  30. prevActions = GH.defActionsDef;
  31. GH.defActionsDef = actions;
  32. }
  33. SSetCaptureState::~SSetCaptureState()
  34. {
  35. GH.captureChildren = previousCapture;
  36. GH.defActionsDef = prevActions;
  37. }
  38. static inline void
  39. processList(const ui16 mask, const ui16 flag, std::list<CIntObject*> *lst, std::function<void (std::list<CIntObject*> *)> cb)
  40. {
  41. if (mask & flag)
  42. cb(lst);
  43. }
  44. void CGuiHandler::processLists(const ui16 activityFlag, std::function<void (std::list<CIntObject*> *)> cb)
  45. {
  46. processList(CIntObject::LCLICK,activityFlag,&lclickable,cb);
  47. processList(CIntObject::RCLICK,activityFlag,&rclickable,cb);
  48. processList(CIntObject::HOVER,activityFlag,&hoverable,cb);
  49. processList(CIntObject::MOVE,activityFlag,&motioninterested,cb);
  50. processList(CIntObject::KEYBOARD,activityFlag,&keyinterested,cb);
  51. processList(CIntObject::TIME,activityFlag,&timeinterested,cb);
  52. processList(CIntObject::WHEEL,activityFlag,&wheelInterested,cb);
  53. processList(CIntObject::DOUBLECLICK,activityFlag,&doubleClickInterested,cb);
  54. }
  55. void CGuiHandler::handleElementActivate(CIntObject * elem, ui16 activityFlag)
  56. {
  57. processLists(activityFlag,[&](std::list<CIntObject*> * lst){
  58. lst->push_front(elem);
  59. });
  60. elem->active_m |= activityFlag;
  61. }
  62. void CGuiHandler::handleElementDeActivate(CIntObject * elem, ui16 activityFlag)
  63. {
  64. processLists(activityFlag,[&](std::list<CIntObject*> * lst){
  65. std::list<CIntObject*>::iterator hlp = std::find(lst->begin(),lst->end(),elem);
  66. assert(hlp != lst->end());
  67. lst->erase(hlp);
  68. });
  69. elem->active_m &= ~activityFlag;
  70. }
  71. void CGuiHandler::popInt( IShowActivatable *top )
  72. {
  73. assert(listInt.front() == top);
  74. top->deactivate();
  75. listInt.pop_front();
  76. objsToBlit -= top;
  77. if(listInt.size())
  78. listInt.front()->activate();
  79. totalRedraw();
  80. }
  81. void CGuiHandler::popIntTotally( IShowActivatable *top )
  82. {
  83. assert(listInt.front() == top);
  84. popInt(top);
  85. delete top;
  86. fakeMouseMove();
  87. }
  88. void CGuiHandler::pushInt( IShowActivatable *newInt )
  89. {
  90. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  91. screenBuf = screen2;
  92. if(listInt.size())
  93. listInt.front()->deactivate();
  94. listInt.push_front(newInt);
  95. newInt->activate();
  96. objsToBlit.push_back(newInt);
  97. totalRedraw();
  98. }
  99. void CGuiHandler::popInts( int howMany )
  100. {
  101. if(!howMany) return; //senseless but who knows...
  102. assert(listInt.size() >= howMany);
  103. listInt.front()->deactivate();
  104. for(int i=0; i < howMany; i++)
  105. {
  106. objsToBlit -= listInt.front();
  107. delete listInt.front();
  108. listInt.pop_front();
  109. }
  110. if(listInt.size())
  111. {
  112. listInt.front()->activate();
  113. totalRedraw();
  114. }
  115. fakeMouseMove();
  116. }
  117. IShowActivatable * CGuiHandler::topInt()
  118. {
  119. if(!listInt.size())
  120. return nullptr;
  121. else
  122. return listInt.front();
  123. }
  124. void CGuiHandler::totalRedraw()
  125. {
  126. for(int i=0;i<objsToBlit.size();i++)
  127. objsToBlit[i]->showAll(screen2);
  128. blitAt(screen2,0,0,screen);
  129. }
  130. void CGuiHandler::updateTime()
  131. {
  132. int ms = mainFPSmng->getElapsedMilliseconds();
  133. std::list<CIntObject*> hlp = timeinterested;
  134. for (std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  135. {
  136. if(!vstd::contains(timeinterested,*i)) continue;
  137. (*i)->onTimer(ms);
  138. }
  139. }
  140. void CGuiHandler::handleEvents()
  141. {
  142. while(true)
  143. {
  144. SDL_Event ev;
  145. boost::unique_lock<boost::mutex> lock(eventsM);
  146. if(!events.size())
  147. {
  148. return;
  149. }
  150. else
  151. {
  152. ev = events.front();
  153. events.pop();
  154. }
  155. handleEvent(&ev);
  156. }
  157. }
  158. void CGuiHandler::handleEvent(SDL_Event *sEvent)
  159. {
  160. current = sEvent;
  161. bool prev;
  162. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  163. {
  164. SDL_KeyboardEvent key = sEvent->key;
  165. //translate numpad keys
  166. if(key.keysym.sym == SDLK_KP_ENTER)
  167. {
  168. key.keysym.sym = (SDLKey)SDLK_RETURN;
  169. }
  170. bool keysCaptured = false;
  171. for(std::list<CIntObject*>::iterator i=keyinterested.begin(); i != keyinterested.end() && current; i++)
  172. {
  173. if((*i)->captureThisEvent(key))
  174. {
  175. keysCaptured = true;
  176. break;
  177. }
  178. }
  179. std::list<CIntObject*> miCopy = keyinterested;
  180. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end() && current; i++)
  181. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureThisEvent(key)))
  182. (**i).keyPressed(key);
  183. }
  184. else if(sEvent->type==SDL_MOUSEMOTION)
  185. {
  186. CCS->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  187. handleMouseMotion(sEvent);
  188. }
  189. else if (sEvent->type==SDL_MOUSEBUTTONDOWN)
  190. {
  191. if(sEvent->button.button == SDL_BUTTON_LEFT)
  192. {
  193. if(lastClick == sEvent->motion && (SDL_GetTicks() - lastClickTime) < 300)
  194. {
  195. std::list<CIntObject*> hlp = doubleClickInterested;
  196. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  197. {
  198. if(!vstd::contains(doubleClickInterested,*i)) continue;
  199. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  200. {
  201. (*i)->onDoubleClick();
  202. }
  203. }
  204. }
  205. lastClick = sEvent->motion;
  206. lastClickTime = SDL_GetTicks();
  207. std::list<CIntObject*> hlp = lclickable;
  208. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  209. {
  210. if(!vstd::contains(lclickable,*i)) continue;
  211. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  212. {
  213. prev = (*i)->pressedL;
  214. (*i)->pressedL = true;
  215. (*i)->clickLeft(true, prev);
  216. }
  217. }
  218. }
  219. else if (sEvent->button.button == SDL_BUTTON_RIGHT)
  220. {
  221. std::list<CIntObject*> hlp = rclickable;
  222. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  223. {
  224. if(!vstd::contains(rclickable,*i)) continue;
  225. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  226. {
  227. prev = (*i)->pressedR;
  228. (*i)->pressedR = true;
  229. (*i)->clickRight(true, prev);
  230. }
  231. }
  232. }
  233. else if(sEvent->button.button == SDL_BUTTON_WHEELDOWN || sEvent->button.button == SDL_BUTTON_WHEELUP)
  234. {
  235. std::list<CIntObject*> hlp = wheelInterested;
  236. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  237. {
  238. if(!vstd::contains(wheelInterested,*i)) continue;
  239. (*i)->wheelScrolled(sEvent->button.button == SDL_BUTTON_WHEELDOWN, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  240. }
  241. }
  242. }
  243. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  244. {
  245. std::list<CIntObject*> hlp = lclickable;
  246. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  247. {
  248. if(!vstd::contains(lclickable,*i)) continue;
  249. prev = (*i)->pressedL;
  250. (*i)->pressedL = false;
  251. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  252. {
  253. (*i)->clickLeft(false, prev);
  254. }
  255. else
  256. (*i)->clickLeft(boost::logic::indeterminate, prev);
  257. }
  258. }
  259. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  260. {
  261. std::list<CIntObject*> hlp = rclickable;
  262. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  263. {
  264. if(!vstd::contains(rclickable,*i)) continue;
  265. prev = (*i)->pressedR;
  266. (*i)->pressedR = false;
  267. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  268. {
  269. (*i)->clickRight(false, prev);
  270. }
  271. else
  272. (*i)->clickRight(boost::logic::indeterminate, prev);
  273. }
  274. }
  275. current = nullptr;
  276. } //event end
  277. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  278. {
  279. //sending active, hovered hoverable objects hover() call
  280. std::vector<CIntObject*> hlp;
  281. for(std::list<CIntObject*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  282. {
  283. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  284. {
  285. if (!(*i)->hovered)
  286. hlp.push_back((*i));
  287. }
  288. else if ((*i)->hovered)
  289. {
  290. (*i)->hover(false);
  291. (*i)->hovered = false;
  292. }
  293. }
  294. for(int i=0; i<hlp.size();i++)
  295. {
  296. hlp[i]->hover(true);
  297. hlp[i]->hovered = true;
  298. }
  299. handleMoveInterested(sEvent->motion);
  300. }
  301. void CGuiHandler::simpleRedraw()
  302. {
  303. //update only top interface and draw background
  304. if(objsToBlit.size() > 1)
  305. blitAt(screen2,0,0,screen); //blit background
  306. objsToBlit.back()->show(screen); //blit active interface/window
  307. }
  308. void CGuiHandler::handleMoveInterested( const SDL_MouseMotionEvent & motion )
  309. {
  310. //sending active, MotionInterested objects mouseMoved() call
  311. std::list<CIntObject*> miCopy = motioninterested;
  312. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  313. {
  314. if ((*i)->strongInterest || isItIn(&(*i)->pos, motion.x, motion.y))
  315. {
  316. (*i)->mouseMoved(motion);
  317. }
  318. }
  319. }
  320. void CGuiHandler::fakeMouseMove()
  321. {
  322. SDL_Event evnt;
  323. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0};
  324. int x, y;
  325. sme.state = SDL_GetMouseState(&x, &y);
  326. sme.x = x;
  327. sme.y = y;
  328. evnt.motion = sme;
  329. current = &evnt;
  330. handleMouseMotion(&evnt);
  331. }
  332. void CGuiHandler::run()
  333. {
  334. setThreadName("CGuiHandler::run");
  335. inGuiThread.reset(new bool(true));
  336. try
  337. {
  338. if(settings["video"]["fullscreen"].Bool())
  339. CCS->curh->centerCursor();
  340. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  341. while(!terminate)
  342. {
  343. if(curInt)
  344. curInt->update(); // calls a update and drawing process of the loaded game interface object at the moment
  345. mainFPSmng->framerateDelay(); // holds a constant FPS
  346. }
  347. }
  348. catch(const std::exception & e)
  349. {
  350. logGlobal->errorStream() << "Error: " << e.what();
  351. exit(EXIT_FAILURE);
  352. }
  353. }
  354. CGuiHandler::CGuiHandler()
  355. :lastClick(-500, -500)
  356. {
  357. curInt = nullptr;
  358. current = nullptr;
  359. terminate = false;
  360. statusbar = nullptr;
  361. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  362. mainFPSmng = new CFramerateManager(48);
  363. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  364. }
  365. CGuiHandler::~CGuiHandler()
  366. {
  367. delete mainFPSmng;
  368. }
  369. void CGuiHandler::breakEventHandling()
  370. {
  371. current = nullptr;
  372. }
  373. void CGuiHandler::drawFPSCounter()
  374. {
  375. const static SDL_Color yellow = {255, 255, 0, 0};
  376. static SDL_Rect overlay = { 0, 0, 64, 32};
  377. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  378. SDL_FillRect(screen, &overlay, black);
  379. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  380. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, yellow, Point(10, 10));
  381. }
  382. SDLKey CGuiHandler::arrowToNum( SDLKey key )
  383. {
  384. switch(key)
  385. {
  386. case SDLK_DOWN:
  387. return SDLK_KP2;
  388. case SDLK_UP:
  389. return SDLK_KP8;
  390. case SDLK_LEFT:
  391. return SDLK_KP4;
  392. case SDLK_RIGHT:
  393. return SDLK_KP6;
  394. default:
  395. assert(0);
  396. }
  397. throw std::runtime_error("Wrong key!");
  398. }
  399. SDLKey CGuiHandler::numToDigit( SDLKey key )
  400. {
  401. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  402. return SDLKey(key - SDLK_KP0 + SDLK_0);
  403. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  404. switch(key)
  405. {
  406. REMOVE_KP(PERIOD)
  407. REMOVE_KP(MINUS)
  408. REMOVE_KP(PLUS)
  409. REMOVE_KP(EQUALS)
  410. case SDLK_KP_MULTIPLY:
  411. return SDLK_ASTERISK;
  412. case SDLK_KP_DIVIDE:
  413. return SDLK_SLASH;
  414. case SDLK_KP_ENTER:
  415. return SDLK_RETURN;
  416. default:
  417. return SDLK_UNKNOWN;
  418. }
  419. #undef REMOVE_KP
  420. }
  421. bool CGuiHandler::isNumKey( SDLKey key, bool number )
  422. {
  423. if(number)
  424. return key >= SDLK_KP0 && key <= SDLK_KP9;
  425. else
  426. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  427. }
  428. bool CGuiHandler::isArrowKey( SDLKey key )
  429. {
  430. return key >= SDLK_UP && key <= SDLK_LEFT;
  431. }
  432. bool CGuiHandler::amIGuiThread()
  433. {
  434. return inGuiThread.get() && *inGuiThread;
  435. }
  436. void CGuiHandler::pushSDLEvent(int type, int usercode)
  437. {
  438. SDL_Event event;
  439. event.type = type;
  440. event.user.code = usercode; // not necessarily used
  441. SDL_PushEvent(&event);
  442. }
  443. CFramerateManager::CFramerateManager(int rate)
  444. {
  445. this->rate = rate;
  446. this->rateticks = (1000.0 / rate);
  447. this->fps = 0;
  448. }
  449. void CFramerateManager::init()
  450. {
  451. this->lastticks = SDL_GetTicks();
  452. }
  453. void CFramerateManager::framerateDelay()
  454. {
  455. ui32 currentTicks = SDL_GetTicks();
  456. timeElapsed = currentTicks - lastticks;
  457. // FPS is higher than it should be, then wait some time
  458. if (timeElapsed < rateticks)
  459. {
  460. SDL_Delay(ceil(this->rateticks) - timeElapsed);
  461. }
  462. currentTicks = SDL_GetTicks();
  463. fps = ceil(1000.0 / timeElapsed);
  464. //recalculate timeElapsed for external calls via getElapsed()
  465. timeElapsed = currentTicks - lastticks;
  466. lastticks = SDL_GetTicks();
  467. }