CGuiHandler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include "StdInc.h"
  2. #include "CGuiHandler.h"
  3. #include "SDL_Extensions.h"
  4. #include "CIntObject.h"
  5. #include "../CGameInfo.h"
  6. #include "CCursorHandler.h"
  7. #include "../../lib/CThreadHelper.h"
  8. #include "../../lib/CConfigHandler.h"
  9. extern SDL_Surface * screenBuf, * screen2, * screen;
  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. }
  55. void CGuiHandler::handleElementActivate(CIntObject * elem, ui16 activityFlag)
  56. {
  57. processLists(activityFlag,[&](std::list<CIntObject*> * lst){
  58. lst->push_front(elem);
  59. });
  60. elem->active_m |= activityFlag;
  61. }
  62. void CGuiHandler::handleElementDeActivate(CIntObject * elem, ui16 activityFlag)
  63. {
  64. processLists(activityFlag,[&](std::list<CIntObject*> * lst){
  65. std::list<CIntObject*>::iterator hlp = std::find(lst->begin(),lst->end(),elem);
  66. assert(hlp != lst->end());
  67. lst->erase(hlp);
  68. });
  69. elem->active_m &= ~activityFlag;
  70. }
  71. void CGuiHandler::popInt( IShowActivatable *top )
  72. {
  73. assert(listInt.front() == top);
  74. top->deactivate();
  75. listInt.pop_front();
  76. objsToBlit -= top;
  77. if(listInt.size())
  78. listInt.front()->activate();
  79. totalRedraw();
  80. }
  81. void CGuiHandler::popIntTotally( IShowActivatable *top )
  82. {
  83. assert(listInt.front() == top);
  84. popInt(top);
  85. delete top;
  86. fakeMouseMove();
  87. }
  88. void CGuiHandler::pushInt( IShowActivatable *newInt )
  89. {
  90. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  91. screenBuf = screen2;
  92. if(listInt.size())
  93. listInt.front()->deactivate();
  94. listInt.push_front(newInt);
  95. newInt->activate();
  96. objsToBlit.push_back(newInt);
  97. totalRedraw();
  98. }
  99. void CGuiHandler::popInts( int howMany )
  100. {
  101. if(!howMany) return; //senseless but who knows...
  102. assert(listInt.size() >= howMany);
  103. listInt.front()->deactivate();
  104. for(int i=0; i < howMany; i++)
  105. {
  106. objsToBlit -= listInt.front();
  107. delete listInt.front();
  108. listInt.pop_front();
  109. }
  110. if(listInt.size())
  111. {
  112. listInt.front()->activate();
  113. totalRedraw();
  114. }
  115. fakeMouseMove();
  116. }
  117. IShowActivatable * CGuiHandler::topInt()
  118. {
  119. if(!listInt.size())
  120. return NULL;
  121. else
  122. return listInt.front();
  123. }
  124. void CGuiHandler::totalRedraw()
  125. {
  126. for(int i=0;i<objsToBlit.size();i++)
  127. objsToBlit[i]->showAll(screen2);
  128. blitAt(screen2,0,0,screen);
  129. }
  130. void CGuiHandler::updateTime()
  131. {
  132. int ms = mainFPSmng->getElapsedMilliseconds();
  133. std::list<CIntObject*> hlp = timeinterested;
  134. for (std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  135. {
  136. if(!vstd::contains(timeinterested,*i)) continue;
  137. (*i)->onTimer(ms);
  138. }
  139. }
  140. void CGuiHandler::handleEvents()
  141. {
  142. while(true)
  143. {
  144. SDL_Event ev;
  145. boost::unique_lock<boost::mutex> lock(eventsM);
  146. if(!events.size())
  147. {
  148. return;
  149. }
  150. else
  151. {
  152. ev = events.front();
  153. events.pop();
  154. }
  155. 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. }
  170. bool keysCaptured = false;
  171. for(std::list<CIntObject*>::iterator 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(std::list<CIntObject*>::iterator 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(std::list<CIntObject*>::iterator 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(std::list<CIntObject*>::iterator 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(std::list<CIntObject*>::iterator 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. else if(sEvent->button.button == SDL_BUTTON_WHEELDOWN || sEvent->button.button == SDL_BUTTON_WHEELUP)
  234. {
  235. std::list<CIntObject*> hlp = wheelInterested;
  236. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  237. {
  238. if(!vstd::contains(wheelInterested,*i)) continue;
  239. (*i)->wheelScrolled(sEvent->button.button == SDL_BUTTON_WHEELDOWN, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  240. }
  241. }
  242. }
  243. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  244. {
  245. std::list<CIntObject*> hlp = lclickable;
  246. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  247. {
  248. if(!vstd::contains(lclickable,*i)) continue;
  249. prev = (*i)->pressedL;
  250. (*i)->pressedL = false;
  251. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  252. {
  253. (*i)->clickLeft(false, prev);
  254. }
  255. else
  256. (*i)->clickLeft(boost::logic::indeterminate, prev);
  257. }
  258. }
  259. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  260. {
  261. std::list<CIntObject*> hlp = rclickable;
  262. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  263. {
  264. if(!vstd::contains(rclickable,*i)) continue;
  265. prev = (*i)->pressedR;
  266. (*i)->pressedR = false;
  267. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  268. {
  269. (*i)->clickRight(false, prev);
  270. }
  271. else
  272. (*i)->clickRight(boost::logic::indeterminate, prev);
  273. }
  274. }
  275. current = NULL;
  276. } //event end
  277. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  278. {
  279. //sending active, hovered hoverable objects hover() call
  280. std::vector<CIntObject*> hlp;
  281. for(std::list<CIntObject*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  282. {
  283. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  284. {
  285. if (!(*i)->hovered)
  286. hlp.push_back((*i));
  287. }
  288. else if ((*i)->hovered)
  289. {
  290. (*i)->hover(false);
  291. (*i)->hovered = false;
  292. }
  293. }
  294. for(int i=0; i<hlp.size();i++)
  295. {
  296. hlp[i]->hover(true);
  297. hlp[i]->hovered = true;
  298. }
  299. handleMoveInterested(sEvent->motion);
  300. }
  301. void CGuiHandler::simpleRedraw()
  302. {
  303. //update only top interface and draw background
  304. if(objsToBlit.size() > 1)
  305. blitAt(screen2,0,0,screen); //blit background
  306. objsToBlit.back()->show(screen); //blit active interface/window
  307. }
  308. void CGuiHandler::handleMoveInterested( const SDL_MouseMotionEvent & motion )
  309. {
  310. //sending active, MotionInterested objects mouseMoved() call
  311. std::list<CIntObject*> miCopy = motioninterested;
  312. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  313. {
  314. if ((*i)->strongInterest || isItIn(&(*i)->pos, motion.x, motion.y))
  315. {
  316. (*i)->mouseMoved(motion);
  317. }
  318. }
  319. }
  320. void CGuiHandler::fakeMouseMove()
  321. {
  322. SDL_Event evnt;
  323. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0};
  324. int x, y;
  325. sme.state = SDL_GetMouseState(&x, &y);
  326. sme.x = x;
  327. sme.y = y;
  328. evnt.motion = sme;
  329. current = &evnt;
  330. handleMouseMotion(&evnt);
  331. }
  332. void CGuiHandler::run()
  333. {
  334. setThreadName("CGuiHandler::run");
  335. inGuiThread.reset(new bool(true));
  336. try
  337. {
  338. if (settings["video"]["fullscreen"].Bool())
  339. CCS->curh->centerCursor();
  340. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  341. while(!terminate)
  342. {
  343. if(curInt)
  344. curInt->update(); // calls a update and drawing process of the loaded game interface object at the moment
  345. mainFPSmng->framerateDelay(); // holds a constant FPS
  346. }
  347. } HANDLE_EXCEPTION
  348. }
  349. CGuiHandler::CGuiHandler()
  350. :lastClick(-500, -500)
  351. {
  352. curInt = NULL;
  353. current = NULL;
  354. terminate = false;
  355. statusbar = NULL;
  356. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  357. mainFPSmng = new CFramerateManager(48);
  358. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  359. }
  360. CGuiHandler::~CGuiHandler()
  361. {
  362. delete mainFPSmng;
  363. }
  364. void CGuiHandler::breakEventHandling()
  365. {
  366. current = NULL;
  367. }
  368. void CGuiHandler::drawFPSCounter()
  369. {
  370. const static SDL_Color yellow = {255, 255, 0, 0};
  371. static SDL_Rect overlay = { 0, 0, 64, 32};
  372. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  373. SDL_FillRect(screen, &overlay, black);
  374. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  375. CSDL_Ext::printAt(fps, 10, 10, FONT_BIG, yellow, screen);
  376. }
  377. SDLKey CGuiHandler::arrowToNum( SDLKey key )
  378. {
  379. switch(key)
  380. {
  381. case SDLK_DOWN:
  382. return SDLK_KP2;
  383. case SDLK_UP:
  384. return SDLK_KP8;
  385. case SDLK_LEFT:
  386. return SDLK_KP4;
  387. case SDLK_RIGHT:
  388. return SDLK_KP6;
  389. default:
  390. assert(0);
  391. }
  392. throw std::runtime_error("Wrong key!");
  393. }
  394. SDLKey CGuiHandler::numToDigit( SDLKey key )
  395. {
  396. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  397. return SDLKey(key - SDLK_KP0 + SDLK_0);
  398. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  399. switch(key)
  400. {
  401. REMOVE_KP(PERIOD)
  402. REMOVE_KP(MINUS)
  403. REMOVE_KP(PLUS)
  404. REMOVE_KP(EQUALS)
  405. case SDLK_KP_MULTIPLY:
  406. return SDLK_ASTERISK;
  407. case SDLK_KP_DIVIDE:
  408. return SDLK_SLASH;
  409. case SDLK_KP_ENTER:
  410. return SDLK_RETURN;
  411. default:
  412. //tlog3 << "Illegal numkey conversion!" << std::endl;
  413. return SDLK_UNKNOWN;
  414. }
  415. #undef REMOVE_KP
  416. }
  417. bool CGuiHandler::isNumKey( SDLKey key, bool number )
  418. {
  419. if(number)
  420. return key >= SDLK_KP0 && key <= SDLK_KP9;
  421. else
  422. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  423. }
  424. bool CGuiHandler::isArrowKey( SDLKey key )
  425. {
  426. return key >= SDLK_UP && key <= SDLK_LEFT;
  427. }
  428. bool CGuiHandler::amIGuiThread()
  429. {
  430. return inGuiThread.get() && *inGuiThread;
  431. }
  432. void CGuiHandler::pushSDLEvent(int type, int usercode)
  433. {
  434. SDL_Event event;
  435. event.type = type;
  436. event.user.code = usercode; // not necessarily used
  437. SDL_PushEvent(&event);
  438. }
  439. CFramerateManager::CFramerateManager(int rate)
  440. {
  441. this->rate = rate;
  442. this->rateticks = (1000.0 / rate);
  443. this->fps = 0;
  444. }
  445. void CFramerateManager::init()
  446. {
  447. this->lastticks = SDL_GetTicks();
  448. }
  449. void CFramerateManager::framerateDelay()
  450. {
  451. ui32 currentTicks = SDL_GetTicks();
  452. timeElapsed = currentTicks - lastticks;
  453. // FPS is higher than it should be, then wait some time
  454. if (timeElapsed < rateticks)
  455. {
  456. SDL_Delay(ceil(this->rateticks) - timeElapsed);
  457. }
  458. currentTicks = SDL_GetTicks();
  459. fps = ceil(1000.0 / timeElapsed);
  460. //recalculate timeElapsed for external calls via getElapsed()
  461. timeElapsed = currentTicks - lastticks;
  462. lastticks = SDL_GetTicks();
  463. }