CGuiHandler.cpp 11 KB

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