2
0

GUIBase.cpp 24 KB

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