GUIBase.cpp 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. #include "SDL_Extensions.h"
  2. #include <cassert>
  3. #include <boost/thread/locks.hpp>
  4. #include "GUIBase.h"
  5. #include <boost/thread/mutex.hpp>
  6. #include <queue>
  7. #include "CGameInfo.h"
  8. #include "CCursorHandler.h"
  9. #include "CBitmapHandler.h"
  10. #include "Graphics.h"
  11. #include "../CThreadHelper.h"
  12. /*
  13. * GUIBase.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. extern std::queue<SDL_Event*> events;
  22. extern boost::mutex eventsM;
  23. void KeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  24. {
  25. if(vstd::contains(assignedKeys,key.keysym.sym))
  26. {
  27. bool prev = pressedL;
  28. if(key.state == SDL_PRESSED)
  29. {
  30. pressedL = true;
  31. clickLeft(true, prev);
  32. }
  33. else
  34. {
  35. pressedL = false;
  36. clickLeft(false, prev);
  37. }
  38. }
  39. }
  40. void CGuiHandler::popInt( IShowActivable *top )
  41. {
  42. assert(listInt.front() == top);
  43. top->deactivate();
  44. listInt.pop_front();
  45. objsToBlit -= top;
  46. if(listInt.size())
  47. listInt.front()->activate();
  48. totalRedraw();
  49. fakeMouseMove();
  50. }
  51. void CGuiHandler::popIntTotally( IShowActivable *top )
  52. {
  53. assert(listInt.front() == top);
  54. popInt(top);
  55. delete top;
  56. }
  57. void CGuiHandler::pushInt( IShowActivable *newInt )
  58. {
  59. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  60. screenBuf = screen2;
  61. if(listInt.size())
  62. listInt.front()->deactivate();
  63. listInt.push_front(newInt);
  64. newInt->activate();
  65. objsToBlit.push_back(newInt);
  66. totalRedraw();
  67. }
  68. void CGuiHandler::popInts( int howMany )
  69. {
  70. if(!howMany) return; //senseless but who knows...
  71. assert(listInt.size() >= howMany);
  72. listInt.front()->deactivate();
  73. for(int i=0; i < howMany; i++)
  74. {
  75. objsToBlit -= listInt.front();
  76. delete listInt.front();
  77. listInt.pop_front();
  78. }
  79. if(listInt.size())
  80. {
  81. listInt.front()->activate();
  82. totalRedraw();
  83. }
  84. }
  85. IShowActivable * CGuiHandler::topInt()
  86. {
  87. if(!listInt.size())
  88. return NULL;
  89. else
  90. return listInt.front();
  91. }
  92. void CGuiHandler::totalRedraw()
  93. {
  94. this->invalidateTotalRedraw = true;
  95. }
  96. void CGuiHandler::internalTotalRedraw()
  97. {
  98. for(int i=0;i<objsToBlit.size();i++)
  99. objsToBlit[i]->showAll(screen2);
  100. blitAt(screen2,0,0,screen);
  101. if(objsToBlit.size())
  102. objsToBlit.back()->showAll(screen);
  103. this->invalidateTotalRedraw = false;
  104. this->invalidateSimpleRedraw = false;
  105. }
  106. void CGuiHandler::updateTime()
  107. {
  108. int tv = th.getDif();
  109. std::list<CIntObject*> hlp = timeinterested;
  110. for (std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  111. {
  112. if(!vstd::contains(timeinterested,*i)) continue;
  113. if ((*i)->toNextTick>=0)
  114. (*i)->toNextTick-=tv;
  115. if ((*i)->toNextTick<0)
  116. (*i)->tick();
  117. }
  118. }
  119. void CGuiHandler::handleEvents()
  120. {
  121. while(true)
  122. {
  123. SDL_Event *ev = NULL;
  124. boost::unique_lock<boost::mutex> lock(eventsM);
  125. if(!events.size())
  126. {
  127. return;
  128. }
  129. else
  130. {
  131. ev = events.front();
  132. events.pop();
  133. }
  134. handleEvent(ev);
  135. delete ev;
  136. }
  137. }
  138. void CGuiHandler::handleEvent(SDL_Event *sEvent)
  139. {
  140. current = sEvent;
  141. bool prev;
  142. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  143. {
  144. SDL_KeyboardEvent key = sEvent->key;
  145. //translate numpad keys
  146. if(key.keysym.sym == SDLK_KP_ENTER)
  147. {
  148. key.keysym.sym = (SDLKey)SDLK_RETURN;
  149. }
  150. bool keysCaptured = false;
  151. for(std::list<CIntObject*>::iterator i=keyinterested.begin(); i != keyinterested.end() && current; i++)
  152. {
  153. if((*i)->captureAllKeys)
  154. {
  155. keysCaptured = true;
  156. break;
  157. }
  158. }
  159. std::list<CIntObject*> miCopy = keyinterested;
  160. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end() && current; i++)
  161. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  162. (**i).keyPressed(key);
  163. }
  164. else if(sEvent->type==SDL_MOUSEMOTION)
  165. {
  166. CCS->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  167. handleMouseMotion(sEvent);
  168. }
  169. else if (sEvent->type==SDL_MOUSEBUTTONDOWN)
  170. {
  171. if(sEvent->button.button == SDL_BUTTON_LEFT)
  172. {
  173. if(lastClick == sEvent->motion && (SDL_GetTicks() - lastClickTime) < 300)
  174. {
  175. std::list<CIntObject*> hlp = doubleClickInterested;
  176. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  177. {
  178. if(!vstd::contains(doubleClickInterested,*i)) continue;
  179. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  180. {
  181. (*i)->onDoubleClick();
  182. }
  183. }
  184. }
  185. lastClick = sEvent->motion;
  186. lastClickTime = SDL_GetTicks();
  187. std::list<CIntObject*> hlp = lclickable;
  188. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  189. {
  190. if(!vstd::contains(lclickable,*i)) continue;
  191. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  192. {
  193. prev = (*i)->pressedL;
  194. (*i)->pressedL = true;
  195. (*i)->clickLeft(true, prev);
  196. }
  197. }
  198. }
  199. else if (sEvent->button.button == SDL_BUTTON_RIGHT)
  200. {
  201. std::list<CIntObject*> hlp = rclickable;
  202. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  203. {
  204. if(!vstd::contains(rclickable,*i)) continue;
  205. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  206. {
  207. prev = (*i)->pressedR;
  208. (*i)->pressedR = true;
  209. (*i)->clickRight(true, prev);
  210. }
  211. }
  212. }
  213. else if(sEvent->button.button == SDL_BUTTON_WHEELDOWN || sEvent->button.button == SDL_BUTTON_WHEELUP)
  214. {
  215. std::list<CIntObject*> hlp = wheelInterested;
  216. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  217. {
  218. if(!vstd::contains(wheelInterested,*i)) continue;
  219. (*i)->wheelScrolled(sEvent->button.button == SDL_BUTTON_WHEELDOWN, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  220. }
  221. }
  222. }
  223. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  224. {
  225. std::list<CIntObject*> hlp = lclickable;
  226. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  227. {
  228. if(!vstd::contains(lclickable,*i)) continue;
  229. prev = (*i)->pressedL;
  230. (*i)->pressedL = false;
  231. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  232. {
  233. (*i)->clickLeft(false, prev);
  234. }
  235. else
  236. (*i)->clickLeft(boost::logic::indeterminate, prev);
  237. }
  238. }
  239. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  240. {
  241. std::list<CIntObject*> hlp = rclickable;
  242. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end() && current; i++)
  243. {
  244. if(!vstd::contains(rclickable,*i)) continue;
  245. prev = (*i)->pressedR;
  246. (*i)->pressedR = false;
  247. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  248. {
  249. (*i)->clickRight(false, prev);
  250. }
  251. else
  252. (*i)->clickRight(boost::logic::indeterminate, prev);
  253. }
  254. }
  255. current = NULL;
  256. } //event end
  257. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  258. {
  259. //sending active, hovered hoverable objects hover() call
  260. std::vector<CIntObject*> hlp;
  261. for(std::list<CIntObject*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  262. {
  263. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  264. {
  265. if (!(*i)->hovered)
  266. hlp.push_back((*i));
  267. }
  268. else if ((*i)->hovered)
  269. {
  270. (*i)->hover(false);
  271. (*i)->hovered = false;
  272. }
  273. }
  274. for(int i=0; i<hlp.size();i++)
  275. {
  276. hlp[i]->hover(true);
  277. hlp[i]->hovered = true;
  278. }
  279. handleMoveInterested(sEvent->motion);
  280. }
  281. void CGuiHandler::simpleRedraw()
  282. {
  283. this->invalidateSimpleRedraw = true;
  284. }
  285. void CGuiHandler::internalSimpleRedraw()
  286. {
  287. //update only top interface and draw background
  288. if(objsToBlit.size() > 1)
  289. blitAt(screen2,0,0,screen); //blit background
  290. objsToBlit.back()->show(screen); //blit active interface/window
  291. this->invalidateSimpleRedraw = false;
  292. }
  293. void CGuiHandler::handleMoveInterested( const SDL_MouseMotionEvent & motion )
  294. {
  295. //sending active, MotionInterested objects mouseMoved() call
  296. std::list<CIntObject*> miCopy = motioninterested;
  297. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  298. {
  299. if ((*i)->strongInterest || isItIn(&(*i)->pos, motion.x, motion.y))
  300. {
  301. (*i)->mouseMoved(motion);
  302. }
  303. }
  304. }
  305. void CGuiHandler::fakeMouseMove()
  306. {
  307. SDL_Event evnt;
  308. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0};
  309. int x, y;
  310. sme.state = SDL_GetMouseState(&x, &y);
  311. sme.x = x;
  312. sme.y = y;
  313. evnt.motion = sme;
  314. current = &evnt;
  315. handleMouseMotion(&evnt);
  316. }
  317. void CGuiHandler::run()
  318. {
  319. setThreadName(-1, "CGuiHandler::run");
  320. try
  321. {
  322. CCS->curh->centerCursor();
  323. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  324. while(!terminate)
  325. {
  326. if(curInt)
  327. curInt->update(); // calls a update and drawing process of the loaded game interface object at the moment
  328. // Handles mouse and key input
  329. GH.updateTime();
  330. GH.handleEvents();
  331. // Redraws the GUI only once during rendering
  332. if (this->invalidateTotalRedraw == true)
  333. internalTotalRedraw();
  334. if (this->invalidateSimpleRedraw == true)
  335. internalSimpleRedraw();
  336. if (SHOW_FPS)
  337. drawFPSCounter();
  338. mainFPSmng->framerateDelay(); // holds a constant FPS
  339. // draw the mouse cursor and update the screen
  340. // todo: bad way of updating the cursor, update screen should be the last statement of the rendering process
  341. CCS->curh->draw1();
  342. CSDL_Ext::update(screen);
  343. CCS->curh->draw2();
  344. }
  345. } HANDLE_EXCEPTION
  346. }
  347. CGuiHandler::CGuiHandler()
  348. :lastClick(-500, -500)
  349. {
  350. curInt = NULL;
  351. current = NULL;
  352. terminate = false;
  353. statusbar = NULL;
  354. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  355. mainFPSmng = new FPSManager(48);
  356. }
  357. CGuiHandler::~CGuiHandler()
  358. {
  359. delete mainFPSmng;
  360. }
  361. void CGuiHandler::breakEventHandling()
  362. {
  363. current = NULL;
  364. }
  365. void CGuiHandler::drawFPSCounter()
  366. {
  367. const static SDL_Color yellow = {255, 255, 0, 0};
  368. static SDL_Rect overlay = { 0, 0, 64, 32};
  369. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  370. SDL_FillRect(screen, &overlay, black);
  371. std::string fps = toString(mainFPSmng->fps);
  372. CSDL_Ext::printAt(fps, 10, 10, FONT_BIG, yellow, screen);
  373. }
  374. void CIntObject::activateLClick()
  375. {
  376. GH.lclickable.push_front(this);
  377. active |= LCLICK;
  378. }
  379. void CIntObject::deactivateLClick()
  380. {
  381. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  382. assert(hlp != GH.lclickable.end());
  383. GH.lclickable.erase(hlp);
  384. active &= ~LCLICK;
  385. }
  386. void CIntObject::clickLeft(tribool down, bool previousState)
  387. {
  388. }
  389. void CIntObject::activateRClick()
  390. {
  391. GH.rclickable.push_front(this);
  392. active |= RCLICK;
  393. }
  394. void CIntObject::deactivateRClick()
  395. {
  396. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  397. assert(hlp != GH.rclickable.end());
  398. GH.rclickable.erase(hlp);
  399. active &= ~RCLICK;
  400. }
  401. void CIntObject::clickRight(tribool down, bool previousState)
  402. {
  403. }
  404. void CIntObject::activateHover()
  405. {
  406. GH.hoverable.push_front(this);
  407. active |= HOVER;
  408. }
  409. void CIntObject::deactivateHover()
  410. {
  411. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  412. assert(hlp != GH.hoverable.end());
  413. GH.hoverable.erase(hlp);
  414. active &= ~HOVER;
  415. }
  416. void CIntObject::hover( bool on )
  417. {
  418. }
  419. void CIntObject::activateKeys()
  420. {
  421. GH.keyinterested.push_front(this);
  422. active |= KEYBOARD;
  423. }
  424. void CIntObject::deactivateKeys()
  425. {
  426. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  427. assert(hlp != GH.keyinterested.end());
  428. GH.keyinterested.erase(hlp);
  429. active &= ~KEYBOARD;
  430. }
  431. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  432. {
  433. }
  434. void CIntObject::activateMouseMove()
  435. {
  436. GH.motioninterested.push_front(this);
  437. active |= MOVE;
  438. }
  439. void CIntObject::deactivateMouseMove()
  440. {
  441. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  442. assert(hlp != GH.motioninterested.end());
  443. GH.motioninterested.erase(hlp);
  444. active &= ~MOVE;
  445. }
  446. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  447. {
  448. }
  449. void CIntObject::activateTimer()
  450. {
  451. GH.timeinterested.push_back(this);
  452. active |= TIME;
  453. }
  454. void CIntObject::deactivateTimer()
  455. {
  456. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  457. assert(hlp != GH.timeinterested.end());
  458. GH.timeinterested.erase(hlp);
  459. active &= ~TIME;
  460. }
  461. void CIntObject::tick()
  462. {
  463. }
  464. CIntObject::CIntObject()
  465. {
  466. pressedL = pressedR = hovered = captureAllKeys = strongInterest = false;
  467. toNextTick = active = used = 0;
  468. recActions = defActions = GH.defActionsDef;
  469. pos.x = 0;
  470. pos.y = 0;
  471. pos.w = 0;
  472. pos.h = 0;
  473. if(GH.captureChildren)
  474. {
  475. assert(GH.createdObj.size());
  476. parent = GH.createdObj.front();
  477. parent->children.push_back(this);
  478. if(parent->defActions & SHARE_POS)
  479. {
  480. pos.x = parent->pos.x;
  481. pos.y = parent->pos.y;
  482. }
  483. }
  484. else
  485. parent = NULL;
  486. }
  487. void CIntObject::show( SDL_Surface * to )
  488. {
  489. if(defActions & UPDATE)
  490. for(size_t i = 0; i < children.size(); i++)
  491. if(children[i]->recActions & UPDATE)
  492. children[i]->show(to);
  493. }
  494. void CIntObject::showAll( SDL_Surface * to )
  495. {
  496. if(defActions & SHOWALL)
  497. {
  498. for(size_t i = 0; i < children.size(); i++)
  499. if(children[i]->recActions & SHOWALL)
  500. children[i]->showAll(to);
  501. }
  502. else
  503. show(to);
  504. }
  505. void CIntObject::activate()
  506. {
  507. assert(!active);
  508. active |= GENERAL;
  509. activate(used);
  510. if(defActions & ACTIVATE)
  511. for(size_t i = 0; i < children.size(); i++)
  512. if(children[i]->recActions & ACTIVATE)
  513. children[i]->activate();
  514. }
  515. void CIntObject::activate(ui16 what)
  516. {
  517. if(what & LCLICK)
  518. activateLClick();
  519. if(what & RCLICK)
  520. activateRClick();
  521. if(what & HOVER)
  522. activateHover();
  523. if(what & MOVE)
  524. activateMouseMove();
  525. if(what & KEYBOARD)
  526. activateKeys();
  527. if(what & TIME)
  528. activateTimer();
  529. if(what & WHEEL)
  530. activateWheel();
  531. if(what & DOUBLECLICK)
  532. activateDClick();
  533. }
  534. void CIntObject::deactivate()
  535. {
  536. assert(active);
  537. active &= ~ GENERAL;
  538. deactivate(used);
  539. assert(!active);
  540. if(defActions & DEACTIVATE)
  541. for(size_t i = 0; i < children.size(); i++)
  542. if(children[i]->recActions & DEACTIVATE)
  543. children[i]->deactivate();
  544. }
  545. void CIntObject::deactivate(ui16 what)
  546. {
  547. if(what & LCLICK)
  548. deactivateLClick();
  549. if(what & RCLICK)
  550. deactivateRClick();
  551. if(what & HOVER)
  552. deactivateHover();
  553. if(what & MOVE)
  554. deactivateMouseMove();
  555. if(what & KEYBOARD)
  556. deactivateKeys();
  557. if(what & TIME) // TIME is special
  558. deactivateTimer();
  559. if(what & WHEEL)
  560. deactivateWheel();
  561. if(what & DOUBLECLICK)
  562. deactivateDClick();
  563. }
  564. CIntObject::~CIntObject()
  565. {
  566. assert(!active); //do not delete active obj
  567. if(defActions & DISPOSE)
  568. for(size_t i = 0; i < children.size(); i++)
  569. if(children[i]->recActions & DISPOSE)
  570. delete children[i];
  571. if(parent && GH.createdObj.size()) //temporary object destroyed
  572. parent->children -= this;
  573. }
  574. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  575. {
  576. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst);
  577. }
  578. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  579. {
  580. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst);
  581. }
  582. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  583. {
  584. printAtMiddleLoc(text, p.x, p.y, font, kolor, dst);
  585. }
  586. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  587. {
  588. blitAt(src, pos.x + x, pos.y + y, dst);
  589. }
  590. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  591. {
  592. blitAtLoc(src, p.x, p.y, dst);
  593. }
  594. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  595. {
  596. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst);
  597. }
  598. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst )
  599. {
  600. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst);
  601. }
  602. void CIntObject::disable()
  603. {
  604. if(active)
  605. deactivate();
  606. recActions = DISPOSE;
  607. }
  608. void CIntObject::enable(bool activation)
  609. {
  610. if(!active && activation)
  611. activate();
  612. recActions = 255;
  613. }
  614. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  615. {
  616. return isItIn(&rect, x - pos.x, y - pos.y);
  617. }
  618. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  619. {
  620. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  621. }
  622. void CIntObject::activateWheel()
  623. {
  624. GH.wheelInterested.push_front(this);
  625. active |= WHEEL;
  626. }
  627. void CIntObject::deactivateWheel()
  628. {
  629. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  630. assert(hlp != GH.wheelInterested.end());
  631. GH.wheelInterested.erase(hlp);
  632. active &= ~WHEEL;
  633. }
  634. void CIntObject::wheelScrolled(bool down, bool in)
  635. {
  636. }
  637. void CIntObject::activateDClick()
  638. {
  639. GH.doubleClickInterested.push_front(this);
  640. active |= DOUBLECLICK;
  641. }
  642. void CIntObject::deactivateDClick()
  643. {
  644. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  645. assert(hlp != GH.doubleClickInterested.end());
  646. GH.doubleClickInterested.erase(hlp);
  647. active &= ~DOUBLECLICK;
  648. }
  649. void CIntObject::onDoubleClick()
  650. {
  651. }
  652. const Rect & CIntObject::center( const Rect &r, bool propagate )
  653. {
  654. pos.w = r.w;
  655. pos.h = r.h;
  656. return center(Point(screen->w/2, screen->h/2), propagate);
  657. }
  658. const Rect & CIntObject::center( bool propagate )
  659. {
  660. return center(pos, propagate);
  661. }
  662. const Rect & CIntObject::center(const Point &p, bool propagate /*= true*/)
  663. {
  664. moveBy(Point(p.x - pos.w/2 - pos.x,
  665. p.y - pos.h/2 - pos.y),
  666. propagate);
  667. return pos;
  668. }
  669. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  670. {
  671. Point newPos = pos.topLeft();
  672. amax(newPos.x, borderWidth);
  673. amax(newPos.y, borderWidth);
  674. amin(newPos.x, screen->w - borderWidth - pos.w);
  675. amin(newPos.y, screen->h - borderWidth - pos.h);
  676. if (newPos != pos.topLeft())
  677. moveTo(newPos, propagate);
  678. }
  679. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  680. {
  681. pos.x += p.x;
  682. pos.y += p.y;
  683. if(propagate)
  684. for(size_t i = 0; i < children.size(); i++)
  685. children[i]->moveBy(p, propagate);
  686. }
  687. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  688. {
  689. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  690. }
  691. void CIntObject::delChild(CIntObject *child)
  692. {
  693. children -= child;
  694. delete child;
  695. }
  696. void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
  697. {
  698. assert(!vstd::contains(children, child));
  699. assert(child->parent == NULL);
  700. children.push_back(child);
  701. child->parent = this;
  702. if(adjustPosition)
  703. child->pos += pos;
  704. }
  705. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  706. {
  707. assert(vstd::contains(children, child));
  708. assert(child->parent == this);
  709. children -= child;
  710. child->parent = NULL;
  711. if(adjustPosition)
  712. child->pos -= pos;
  713. }
  714. void CIntObject::changeUsedEvents(ui16 what, bool enable, bool adjust /*= true*/)
  715. {
  716. if(enable)
  717. {
  718. used |= what;
  719. if(adjust && active)
  720. activate(what);
  721. }
  722. else
  723. {
  724. used &= ~what;
  725. if(adjust && active)
  726. deactivate(what);
  727. }
  728. }
  729. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  730. {
  731. init();
  732. bg = BG;
  733. freeSurf = Free;
  734. pos.x += x;
  735. pos.y += y;
  736. pos.w = BG->w;
  737. pos.h = BG->h;
  738. }
  739. CPicture::CPicture( const std::string &bmpname, int x, int y )
  740. {
  741. init();
  742. bg = BitmapHandler::loadBitmap(bmpname);
  743. freeSurf = true;;
  744. pos.x += x;
  745. pos.y += y;
  746. if(bg)
  747. {
  748. pos.w = bg->w;
  749. pos.h = bg->h;
  750. }
  751. else
  752. {
  753. pos.w = pos.h = 0;
  754. }
  755. }
  756. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  757. {
  758. init();
  759. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  760. }
  761. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  762. {
  763. init();
  764. createSimpleRect(r, screenFormat, color);
  765. }
  766. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  767. {
  768. needRefresh = false;
  769. srcRect = new Rect(SrcRect);
  770. pos.x += x;
  771. pos.y += y;
  772. bg = BG;
  773. freeSurf = free;
  774. }
  775. CPicture::~CPicture()
  776. {
  777. if(freeSurf)
  778. SDL_FreeSurface(bg);
  779. delete srcRect;
  780. }
  781. void CPicture::init()
  782. {
  783. needRefresh = false;
  784. srcRect = NULL;
  785. }
  786. void CPicture::show( SDL_Surface * to )
  787. {
  788. if (needRefresh)
  789. showAll(to);
  790. }
  791. void CPicture::showAll( SDL_Surface * to )
  792. {
  793. if(bg)
  794. {
  795. if(srcRect)
  796. {
  797. SDL_Rect srcRectCpy = *srcRect;
  798. SDL_Rect dstRect = srcRectCpy;
  799. dstRect.x = pos.x;
  800. dstRect.y = pos.y;
  801. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  802. }
  803. else
  804. blitAt(bg, pos, to);
  805. }
  806. }
  807. void CPicture::convertToScreenBPP()
  808. {
  809. SDL_Surface *hlp = bg;
  810. bg = SDL_ConvertSurface(hlp,screen->format,0);
  811. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  812. SDL_FreeSurface(hlp);
  813. }
  814. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  815. {
  816. pos += r;
  817. pos.w = r.w;
  818. pos.h = r.h;
  819. if(screenFormat)
  820. bg = CSDL_Ext::newSurface(r.w, r.h);
  821. else
  822. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  823. SDL_FillRect(bg, NULL, color);
  824. freeSurf = true;
  825. }
  826. void CPicture::colorizeAndConvert(int player)
  827. {
  828. assert(bg);
  829. colorize(player);
  830. convertToScreenBPP();
  831. }
  832. void CPicture::colorize(int player)
  833. {
  834. assert(bg);
  835. assert(bg->format->BitsPerPixel == 8);
  836. graphics->blueToPlayersAdv(bg, player);
  837. }
  838. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  839. :myObj(obj)
  840. {
  841. GH.createdObj.push_front(obj);
  842. GH.captureChildren = true;
  843. }
  844. ObjectConstruction::~ObjectConstruction()
  845. {
  846. assert(GH.createdObj.size());
  847. assert(GH.createdObj.front() == myObj);
  848. GH.createdObj.pop_front();
  849. GH.captureChildren = GH.createdObj.size();
  850. }
  851. SetCaptureState::SetCaptureState(bool allow, ui8 actions)
  852. {
  853. previousCapture = GH.captureChildren;
  854. GH.captureChildren = false;
  855. prevActions = GH.defActionsDef;
  856. GH.defActionsDef = actions;
  857. }
  858. SetCaptureState::~SetCaptureState()
  859. {
  860. GH.captureChildren = previousCapture;
  861. GH.defActionsDef = prevActions;
  862. }
  863. void IShowable::redraw()
  864. {
  865. showAll(screenBuf);
  866. if(screenBuf != screen)
  867. showAll(screen);
  868. }
  869. SDLKey arrowToNum( SDLKey key )
  870. {
  871. switch(key)
  872. {
  873. case SDLK_DOWN:
  874. return SDLK_KP2;
  875. case SDLK_UP:
  876. return SDLK_KP8;
  877. case SDLK_LEFT:
  878. return SDLK_KP4;
  879. case SDLK_RIGHT:
  880. return SDLK_KP6;
  881. default:
  882. assert(0);
  883. }
  884. throw std::string("Wrong key!");
  885. }
  886. SDLKey numToDigit( SDLKey key )
  887. {
  888. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  889. return SDLKey(key - SDLK_KP0 + SDLK_0);
  890. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  891. switch(key)
  892. {
  893. REMOVE_KP(PERIOD)
  894. REMOVE_KP(MINUS)
  895. REMOVE_KP(PLUS)
  896. REMOVE_KP(EQUALS)
  897. case SDLK_KP_MULTIPLY:
  898. return SDLK_ASTERISK;
  899. case SDLK_KP_DIVIDE:
  900. return SDLK_SLASH;
  901. case SDLK_KP_ENTER:
  902. return SDLK_RETURN;
  903. default:
  904. tlog3 << "Illegal numkey conversion!" << std::endl;
  905. return SDLK_UNKNOWN;
  906. }
  907. #undef REMOVE_KP
  908. }
  909. bool isNumKey( SDLKey key, bool number )
  910. {
  911. if(number)
  912. return key >= SDLK_KP0 && key <= SDLK_KP9;
  913. else
  914. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  915. }
  916. bool isArrowKey( SDLKey key )
  917. {
  918. return key >= SDLK_UP && key <= SDLK_LEFT;
  919. }
  920. CIntObject * moveChild(CIntObject *obj, CIntObject *from, CIntObject *to, bool adjustPos)
  921. {
  922. from->removeChild(obj, adjustPos);
  923. to->addChild(obj, adjustPos);
  924. return obj;
  925. }
  926. Rect Rect::createCentered( int w, int h )
  927. {
  928. return Rect(screen->w/2 - w/2, screen->h/2 - h/2, w, h);
  929. }
  930. Rect Rect::around(const Rect &r, int width /*= 1*/) /*creates rect around another */
  931. {
  932. return Rect(r.x - width, r.y - width, r.w + width * 2, r.h + width * 2);
  933. }
  934. Rect Rect::centerIn(const Rect &r)
  935. {
  936. return Rect(r.x + (r.w - w) / 2, r.y + (r.h - h) / 2, w, h);
  937. }