CGuiHandler.cpp 14 KB

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