CGuiHandler.cpp 15 KB

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