CGuiHandler.cpp 15 KB

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