CGuiHandler.cpp 13 KB

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