CGuiHandler.cpp 15 KB

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