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