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