2
0

CGuiHandler.cpp 12 KB

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