CGuiHandler.cpp 12 KB

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