CGuiHandler.cpp 11 KB

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