CGuiHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 "../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. void CGuiHandler::popInt( IShowActivatable *top )
  39. {
  40. assert(listInt.front() == top);
  41. top->deactivate();
  42. listInt.pop_front();
  43. objsToBlit -= top;
  44. if(listInt.size())
  45. listInt.front()->activate();
  46. totalRedraw();
  47. }
  48. void CGuiHandler::popIntTotally( IShowActivatable *top )
  49. {
  50. assert(listInt.front() == top);
  51. popInt(top);
  52. delete top;
  53. fakeMouseMove();
  54. }
  55. void CGuiHandler::pushInt( IShowActivatable *newInt )
  56. {
  57. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  58. screenBuf = screen2;
  59. if(listInt.size())
  60. listInt.front()->deactivate();
  61. listInt.push_front(newInt);
  62. newInt->activate();
  63. objsToBlit.push_back(newInt);
  64. totalRedraw();
  65. }
  66. void CGuiHandler::popInts( int howMany )
  67. {
  68. if(!howMany) return; //senseless but who knows...
  69. assert(listInt.size() >= howMany);
  70. listInt.front()->deactivate();
  71. for(int i=0; i < howMany; i++)
  72. {
  73. objsToBlit -= listInt.front();
  74. delete listInt.front();
  75. listInt.pop_front();
  76. }
  77. if(listInt.size())
  78. {
  79. listInt.front()->activate();
  80. totalRedraw();
  81. }
  82. fakeMouseMove();
  83. }
  84. IShowActivatable * CGuiHandler::topInt()
  85. {
  86. if(!listInt.size())
  87. return NULL;
  88. else
  89. return listInt.front();
  90. }
  91. void CGuiHandler::totalRedraw()
  92. {
  93. for(int i=0;i<objsToBlit.size();i++)
  94. objsToBlit[i]->showAll(screen2);
  95. blitAt(screen2,0,0,screen);
  96. }
  97. void CGuiHandler::updateTime()
  98. {
  99. int ms = mainFPSmng->getElapsedMilliseconds();
  100. std::list<CIntObject*> hlp = timeinterested;
  101. for (std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  102. {
  103. if(!vstd::contains(timeinterested,*i)) continue;
  104. (*i)->onTimer(ms);
  105. }
  106. }
  107. void CGuiHandler::handleEvents()
  108. {
  109. while(true)
  110. {
  111. SDL_Event ev;
  112. boost::unique_lock<boost::mutex> lock(eventsM);
  113. if(!events.size())
  114. {
  115. return;
  116. }
  117. else
  118. {
  119. ev = events.front();
  120. events.pop();
  121. }
  122. handleEvent(&ev);
  123. }
  124. }
  125. void CGuiHandler::handleEvent(SDL_Event *sEvent)
  126. {
  127. current = sEvent;
  128. bool prev;
  129. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  130. {
  131. SDL_KeyboardEvent key = sEvent->key;
  132. //translate numpad keys
  133. if(key.keysym.sym == SDLK_KP_ENTER)
  134. {
  135. key.keysym.sym = (SDLKey)SDLK_RETURN;
  136. }
  137. bool keysCaptured = false;
  138. for(std::list<CIntObject*>::iterator i=keyinterested.begin(); i != keyinterested.end() && current; i++)
  139. {
  140. if((*i)->captureThisEvent(key))
  141. {
  142. keysCaptured = true;
  143. break;
  144. }
  145. }
  146. std::list<CIntObject*> miCopy = keyinterested;
  147. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end() && current; i++)
  148. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureThisEvent(key)))
  149. (**i).keyPressed(key);
  150. }
  151. else if(sEvent->type==SDL_MOUSEMOTION)
  152. {
  153. CCS->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  154. handleMouseMotion(sEvent);
  155. }
  156. else if (sEvent->type==SDL_MOUSEBUTTONDOWN)
  157. {
  158. if(sEvent->button.button == SDL_BUTTON_LEFT)
  159. {
  160. if(lastClick == sEvent->motion && (SDL_GetTicks() - lastClickTime) < 300)
  161. {
  162. std::list<CIntObject*> hlp = doubleClickInterested;
  163. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  164. {
  165. if(!vstd::contains(doubleClickInterested,*i)) continue;
  166. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  167. {
  168. (*i)->onDoubleClick();
  169. }
  170. }
  171. }
  172. lastClick = sEvent->motion;
  173. lastClickTime = SDL_GetTicks();
  174. std::list<CIntObject*> hlp = lclickable;
  175. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  176. {
  177. if(!vstd::contains(lclickable,*i)) continue;
  178. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  179. {
  180. prev = (*i)->pressedL;
  181. (*i)->pressedL = true;
  182. (*i)->clickLeft(true, prev);
  183. }
  184. }
  185. }
  186. else if (sEvent->button.button == SDL_BUTTON_RIGHT)
  187. {
  188. std::list<CIntObject*> hlp = rclickable;
  189. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  190. {
  191. if(!vstd::contains(rclickable,*i)) continue;
  192. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  193. {
  194. prev = (*i)->pressedR;
  195. (*i)->pressedR = true;
  196. (*i)->clickRight(true, prev);
  197. }
  198. }
  199. }
  200. else if(sEvent->button.button == SDL_BUTTON_WHEELDOWN || sEvent->button.button == SDL_BUTTON_WHEELUP)
  201. {
  202. std::list<CIntObject*> hlp = wheelInterested;
  203. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  204. {
  205. if(!vstd::contains(wheelInterested,*i)) continue;
  206. (*i)->wheelScrolled(sEvent->button.button == SDL_BUTTON_WHEELDOWN, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  207. }
  208. }
  209. }
  210. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  211. {
  212. std::list<CIntObject*> hlp = lclickable;
  213. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  214. {
  215. if(!vstd::contains(lclickable,*i)) continue;
  216. prev = (*i)->pressedL;
  217. (*i)->pressedL = false;
  218. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  219. {
  220. (*i)->clickLeft(false, prev);
  221. }
  222. else
  223. (*i)->clickLeft(boost::logic::indeterminate, prev);
  224. }
  225. }
  226. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  227. {
  228. std::list<CIntObject*> hlp = rclickable;
  229. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  230. {
  231. if(!vstd::contains(rclickable,*i)) continue;
  232. prev = (*i)->pressedR;
  233. (*i)->pressedR = false;
  234. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  235. {
  236. (*i)->clickRight(false, prev);
  237. }
  238. else
  239. (*i)->clickRight(boost::logic::indeterminate, prev);
  240. }
  241. }
  242. current = NULL;
  243. } //event end
  244. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  245. {
  246. //sending active, hovered hoverable objects hover() call
  247. std::vector<CIntObject*> hlp;
  248. for(std::list<CIntObject*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  249. {
  250. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  251. {
  252. if (!(*i)->hovered)
  253. hlp.push_back((*i));
  254. }
  255. else if ((*i)->hovered)
  256. {
  257. (*i)->hover(false);
  258. (*i)->hovered = false;
  259. }
  260. }
  261. for(int i=0; i<hlp.size();i++)
  262. {
  263. hlp[i]->hover(true);
  264. hlp[i]->hovered = true;
  265. }
  266. handleMoveInterested(sEvent->motion);
  267. }
  268. void CGuiHandler::simpleRedraw()
  269. {
  270. //update only top interface and draw background
  271. if(objsToBlit.size() > 1)
  272. blitAt(screen2,0,0,screen); //blit background
  273. objsToBlit.back()->show(screen); //blit active interface/window
  274. }
  275. void CGuiHandler::handleMoveInterested( const SDL_MouseMotionEvent & motion )
  276. {
  277. //sending active, MotionInterested objects mouseMoved() call
  278. std::list<CIntObject*> miCopy = motioninterested;
  279. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  280. {
  281. if ((*i)->strongInterest || isItIn(&(*i)->pos, motion.x, motion.y))
  282. {
  283. (*i)->mouseMoved(motion);
  284. }
  285. }
  286. }
  287. void CGuiHandler::fakeMouseMove()
  288. {
  289. SDL_Event evnt;
  290. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0};
  291. int x, y;
  292. sme.state = SDL_GetMouseState(&x, &y);
  293. sme.x = x;
  294. sme.y = y;
  295. evnt.motion = sme;
  296. current = &evnt;
  297. handleMouseMotion(&evnt);
  298. }
  299. void CGuiHandler::run()
  300. {
  301. setThreadName("CGuiHandler::run");
  302. inGuiThread.reset(new bool(true));
  303. try
  304. {
  305. if (settings["video"]["fullscreen"].Bool())
  306. CCS->curh->centerCursor();
  307. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  308. while(!terminate)
  309. {
  310. if(curInt)
  311. curInt->update(); // calls a update and drawing process of the loaded game interface object at the moment
  312. mainFPSmng->framerateDelay(); // holds a constant FPS
  313. }
  314. } HANDLE_EXCEPTION
  315. }
  316. CGuiHandler::CGuiHandler()
  317. :lastClick(-500, -500)
  318. {
  319. curInt = NULL;
  320. current = NULL;
  321. terminate = false;
  322. statusbar = NULL;
  323. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  324. mainFPSmng = new CFramerateManager(48);
  325. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  326. }
  327. CGuiHandler::~CGuiHandler()
  328. {
  329. delete mainFPSmng;
  330. }
  331. void CGuiHandler::breakEventHandling()
  332. {
  333. current = NULL;
  334. }
  335. void CGuiHandler::drawFPSCounter()
  336. {
  337. const static SDL_Color yellow = {255, 255, 0, 0};
  338. static SDL_Rect overlay = { 0, 0, 64, 32};
  339. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  340. SDL_FillRect(screen, &overlay, black);
  341. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  342. CSDL_Ext::printAt(fps, 10, 10, FONT_BIG, yellow, screen);
  343. }
  344. SDLKey CGuiHandler::arrowToNum( SDLKey key )
  345. {
  346. switch(key)
  347. {
  348. case SDLK_DOWN:
  349. return SDLK_KP2;
  350. case SDLK_UP:
  351. return SDLK_KP8;
  352. case SDLK_LEFT:
  353. return SDLK_KP4;
  354. case SDLK_RIGHT:
  355. return SDLK_KP6;
  356. default:
  357. assert(0);
  358. }
  359. throw std::runtime_error("Wrong key!");
  360. }
  361. SDLKey CGuiHandler::numToDigit( SDLKey key )
  362. {
  363. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  364. return SDLKey(key - SDLK_KP0 + SDLK_0);
  365. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  366. switch(key)
  367. {
  368. REMOVE_KP(PERIOD)
  369. REMOVE_KP(MINUS)
  370. REMOVE_KP(PLUS)
  371. REMOVE_KP(EQUALS)
  372. case SDLK_KP_MULTIPLY:
  373. return SDLK_ASTERISK;
  374. case SDLK_KP_DIVIDE:
  375. return SDLK_SLASH;
  376. case SDLK_KP_ENTER:
  377. return SDLK_RETURN;
  378. default:
  379. //tlog3 << "Illegal numkey conversion!" << std::endl;
  380. return SDLK_UNKNOWN;
  381. }
  382. #undef REMOVE_KP
  383. }
  384. bool CGuiHandler::isNumKey( SDLKey key, bool number )
  385. {
  386. if(number)
  387. return key >= SDLK_KP0 && key <= SDLK_KP9;
  388. else
  389. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  390. }
  391. bool CGuiHandler::isArrowKey( SDLKey key )
  392. {
  393. return key >= SDLK_UP && key <= SDLK_LEFT;
  394. }
  395. bool CGuiHandler::amIGuiThread()
  396. {
  397. return inGuiThread.get() && *inGuiThread;
  398. }
  399. void CGuiHandler::pushSDLEvent(int type, int usercode)
  400. {
  401. SDL_Event event;
  402. event.type = type;
  403. event.user.code = usercode; // not necessarily used
  404. SDL_PushEvent(&event);
  405. }
  406. CFramerateManager::CFramerateManager(int rate)
  407. {
  408. this->rate = rate;
  409. this->rateticks = (1000.0 / rate);
  410. this->fps = 0;
  411. }
  412. void CFramerateManager::init()
  413. {
  414. this->lastticks = SDL_GetTicks();
  415. }
  416. void CFramerateManager::framerateDelay()
  417. {
  418. ui32 currentTicks = SDL_GetTicks();
  419. timeElapsed = currentTicks - lastticks;
  420. // FPS is higher than it should be, then wait some time
  421. if (timeElapsed < rateticks)
  422. {
  423. SDL_Delay(ceil(this->rateticks) - timeElapsed);
  424. }
  425. currentTicks = SDL_GetTicks();
  426. fps = ceil(1000.0 / timeElapsed);
  427. //recalculate timeElapsed for external calls via getElapsed()
  428. timeElapsed = currentTicks - lastticks;
  429. lastticks = SDL_GetTicks();
  430. }