CGuiHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. boost::unique_lock<boost::mutex> lock(eventsM);
  148. while(!events.empty())
  149. {
  150. SDL_Event ev = events.front();
  151. events.pop();
  152. this->handleEvent(&ev);
  153. }
  154. }
  155. void CGuiHandler::handleEvent(SDL_Event *sEvent)
  156. {
  157. current = sEvent;
  158. bool prev;
  159. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  160. {
  161. SDL_KeyboardEvent key = sEvent->key;
  162. //translate numpad keys
  163. if(key.keysym.sym == SDLK_KP_ENTER)
  164. {
  165. key.keysym.sym = (SDLKey)SDLK_RETURN;
  166. #ifndef VCMI_SDL1
  167. key.keysym.scancode = SDL_SCANCODE_RETURN;
  168. #endif // VCMI_SDL1
  169. }
  170. bool keysCaptured = false;
  171. for(auto 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(auto 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(auto 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(auto 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(auto 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. #ifdef VCMI_SDL1 //SDL1x only events
  234. else if(sEvent->button.button == SDL_BUTTON_WHEELDOWN || sEvent->button.button == SDL_BUTTON_WHEELUP)
  235. {
  236. std::list<CIntObject*> hlp = wheelInterested;
  237. for(auto i=hlp.begin(); i != hlp.end() && current; i++)
  238. {
  239. if(!vstd::contains(wheelInterested,*i)) continue;
  240. (*i)->wheelScrolled(sEvent->button.button == SDL_BUTTON_WHEELDOWN, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  241. }
  242. }
  243. #endif
  244. }
  245. #ifndef VCMI_SDL1 //SDL2x only events
  246. else if (sEvent->type == SDL_MOUSEWHEEL)
  247. {
  248. std::list<CIntObject*> hlp = wheelInterested;
  249. for(auto i=hlp.begin(); i != hlp.end() && current; i++)
  250. {
  251. if(!vstd::contains(wheelInterested,*i)) continue;
  252. (*i)->wheelScrolled(sEvent->wheel.y < 0, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  253. }
  254. }
  255. else if(sEvent->type == SDL_TEXTINPUT)
  256. {
  257. for(auto it : textInterested)
  258. {
  259. it->textInputed(sEvent->text);
  260. }
  261. }
  262. else if(sEvent->type == SDL_TEXTEDITING)
  263. {
  264. for(auto it : textInterested)
  265. {
  266. it->textEdited(sEvent->edit);
  267. }
  268. }
  269. #endif // VCMI_SDL1
  270. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  271. {
  272. std::list<CIntObject*> hlp = lclickable;
  273. for(auto i=hlp.begin(); i != hlp.end() && current; i++)
  274. {
  275. if(!vstd::contains(lclickable,*i)) continue;
  276. prev = (*i)->pressedL;
  277. (*i)->pressedL = false;
  278. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  279. {
  280. (*i)->clickLeft(false, prev);
  281. }
  282. else
  283. (*i)->clickLeft(boost::logic::indeterminate, prev);
  284. }
  285. }
  286. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  287. {
  288. std::list<CIntObject*> hlp = rclickable;
  289. for(auto i=hlp.begin(); i != hlp.end() && current; i++)
  290. {
  291. if(!vstd::contains(rclickable,*i)) continue;
  292. prev = (*i)->pressedR;
  293. (*i)->pressedR = false;
  294. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  295. {
  296. (*i)->clickRight(false, prev);
  297. }
  298. else
  299. (*i)->clickRight(boost::logic::indeterminate, prev);
  300. }
  301. }
  302. current = nullptr;
  303. } //event end
  304. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  305. {
  306. //sending active, hovered hoverable objects hover() call
  307. std::vector<CIntObject*> hlp;
  308. for(auto & elem : hoverable)
  309. {
  310. if (isItIn(&(elem)->pos,sEvent->motion.x,sEvent->motion.y))
  311. {
  312. if (!(elem)->hovered)
  313. hlp.push_back((elem));
  314. }
  315. else if ((elem)->hovered)
  316. {
  317. (elem)->hover(false);
  318. (elem)->hovered = false;
  319. }
  320. }
  321. for(auto & elem : hlp)
  322. {
  323. elem->hover(true);
  324. elem->hovered = true;
  325. }
  326. handleMoveInterested(sEvent->motion);
  327. }
  328. void CGuiHandler::simpleRedraw()
  329. {
  330. //update only top interface and draw background
  331. if(objsToBlit.size() > 1)
  332. blitAt(screen2,0,0,screen); //blit background
  333. objsToBlit.back()->show(screen); //blit active interface/window
  334. }
  335. void CGuiHandler::handleMoveInterested( const SDL_MouseMotionEvent & motion )
  336. {
  337. //sending active, MotionInterested objects mouseMoved() call
  338. std::list<CIntObject*> miCopy = motioninterested;
  339. for(auto & elem : miCopy)
  340. {
  341. if ((elem)->strongInterest || isItIn(&(elem)->pos, motion.x, motion.y))
  342. {
  343. (elem)->mouseMoved(motion);
  344. }
  345. }
  346. }
  347. void CGuiHandler::fakeMouseMove()
  348. {
  349. SDL_Event evnt;
  350. #ifdef VCMI_SDL1
  351. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0};
  352. #else
  353. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0, 0, 0};
  354. #endif
  355. int x, y;
  356. sme.state = SDL_GetMouseState(&x, &y);
  357. sme.x = x;
  358. sme.y = y;
  359. evnt.motion = sme;
  360. current = &evnt;
  361. handleMouseMotion(&evnt);
  362. }
  363. void CGuiHandler::renderFrame()
  364. {
  365. if(!terminate)
  366. {
  367. if(curInt)
  368. curInt->update(); // calls a update and drawing process of the loaded game interface object at the moment
  369. #ifndef VCMI_SDL1
  370. if(0 != SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr))
  371. logGlobal->errorStream() << __FUNCTION__ << " SDL_RenderCopy " << SDL_GetError();
  372. SDL_RenderPresent(mainRenderer);
  373. #endif
  374. mainFPSmng->framerateDelay(); // holds a constant FPS
  375. }
  376. }
  377. CGuiHandler::CGuiHandler()
  378. :lastClick(-500, -500)
  379. {
  380. curInt = nullptr;
  381. current = nullptr;
  382. terminate = false;
  383. statusbar = nullptr;
  384. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  385. mainFPSmng = new CFramerateManager(48);
  386. //do not init CFramerateManager here --AVS
  387. }
  388. CGuiHandler::~CGuiHandler()
  389. {
  390. delete mainFPSmng;
  391. }
  392. void CGuiHandler::breakEventHandling()
  393. {
  394. current = nullptr;
  395. }
  396. void CGuiHandler::drawFPSCounter()
  397. {
  398. const static SDL_Color yellow = {255, 255, 0, 0};
  399. static SDL_Rect overlay = { 0, 0, 64, 32};
  400. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  401. SDL_FillRect(screen, &overlay, black);
  402. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  403. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, yellow, Point(10, 10));
  404. }
  405. SDLKey CGuiHandler::arrowToNum( SDLKey key )
  406. {
  407. #ifdef VCMI_SDL1
  408. switch(key)
  409. {
  410. case SDLK_DOWN:
  411. return SDLK_KP2;
  412. case SDLK_UP:
  413. return SDLK_KP8;
  414. case SDLK_LEFT:
  415. return SDLK_KP4;
  416. case SDLK_RIGHT:
  417. return SDLK_KP6;
  418. default:
  419. assert(0);
  420. }
  421. #else
  422. switch(key)
  423. {
  424. case SDLK_DOWN:
  425. return SDLK_KP_2;
  426. case SDLK_UP:
  427. return SDLK_KP_8;
  428. case SDLK_LEFT:
  429. return SDLK_KP_4;
  430. case SDLK_RIGHT:
  431. return SDLK_KP_6;
  432. default:
  433. assert(0);
  434. }
  435. #endif // 0
  436. throw std::runtime_error("Wrong key!");
  437. }
  438. SDLKey CGuiHandler::numToDigit( SDLKey key )
  439. {
  440. #ifdef VCMI_SDL1
  441. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  442. return SDLKey(key - SDLK_KP0 + SDLK_0);
  443. #endif // 0
  444. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  445. switch(key)
  446. {
  447. #ifndef VCMI_SDL1
  448. REMOVE_KP(0)
  449. REMOVE_KP(1)
  450. REMOVE_KP(2)
  451. REMOVE_KP(3)
  452. REMOVE_KP(4)
  453. REMOVE_KP(5)
  454. REMOVE_KP(6)
  455. REMOVE_KP(7)
  456. REMOVE_KP(8)
  457. REMOVE_KP(9)
  458. #endif // VCMI_SDL1
  459. REMOVE_KP(PERIOD)
  460. REMOVE_KP(MINUS)
  461. REMOVE_KP(PLUS)
  462. REMOVE_KP(EQUALS)
  463. case SDLK_KP_MULTIPLY:
  464. return SDLK_ASTERISK;
  465. case SDLK_KP_DIVIDE:
  466. return SDLK_SLASH;
  467. case SDLK_KP_ENTER:
  468. return SDLK_RETURN;
  469. default:
  470. return SDLK_UNKNOWN;
  471. }
  472. #undef REMOVE_KP
  473. }
  474. bool CGuiHandler::isNumKey( SDLKey key, bool number )
  475. {
  476. #ifdef VCMI_SDL1
  477. if(number)
  478. return key >= SDLK_KP0 && key <= SDLK_KP9;
  479. else
  480. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  481. #else
  482. if(number)
  483. return key >= SDLK_KP_1 && key <= SDLK_KP_0;
  484. else
  485. return (key >= SDLK_KP_1 && key <= SDLK_KP_0) || key == SDLK_KP_MINUS || key == SDLK_KP_PLUS || key == SDLK_KP_EQUALS;
  486. #endif // 0
  487. }
  488. bool CGuiHandler::isArrowKey( SDLKey key )
  489. {
  490. #ifdef VCMI_SDL1
  491. return key >= SDLK_UP && key <= SDLK_LEFT;
  492. #else
  493. return key == SDLK_UP || key == SDLK_DOWN || key == SDLK_LEFT || key == SDLK_RIGHT;
  494. #endif
  495. }
  496. bool CGuiHandler::amIGuiThread()
  497. {
  498. return inGuiThread.get() && *inGuiThread;
  499. }
  500. void CGuiHandler::pushSDLEvent(int type, int usercode)
  501. {
  502. SDL_Event event;
  503. event.type = type;
  504. event.user.code = usercode; // not necessarily used
  505. SDL_PushEvent(&event);
  506. }
  507. CFramerateManager::CFramerateManager(int rate)
  508. {
  509. this->rate = rate;
  510. this->rateticks = (1000.0 / rate);
  511. this->fps = 0;
  512. }
  513. void CFramerateManager::init()
  514. {
  515. this->lastticks = SDL_GetTicks();
  516. }
  517. void CFramerateManager::framerateDelay()
  518. {
  519. ui32 currentTicks = SDL_GetTicks();
  520. timeElapsed = currentTicks - lastticks;
  521. // FPS is higher than it should be, then wait some time
  522. if (timeElapsed < rateticks)
  523. {
  524. SDL_Delay(ceil(this->rateticks) - timeElapsed);
  525. }
  526. currentTicks = SDL_GetTicks();
  527. fps = ceil(1000.0 / timeElapsed);
  528. // recalculate timeElapsed for external calls via getElapsed()
  529. // limit it to 1000 ms to avoid breaking animation in case of huge lag (e.g. triggered breakpoint)
  530. timeElapsed = std::min<ui32>(currentTicks - lastticks, 1000);
  531. lastticks = SDL_GetTicks();
  532. }