2
0

CGuiHandler.cpp 15 KB

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