GUIBase.cpp 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. CCS->curh->centerCursor();
  320. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  321. while(!terminate)
  322. {
  323. if(curInt)
  324. curInt->update(); // calls a update and drawing process of the loaded game interface object at the moment
  325. mainFPSmng->framerateDelay(); // holds a constant FPS
  326. }
  327. } HANDLE_EXCEPTION
  328. }
  329. CGuiHandler::CGuiHandler()
  330. :lastClick(-500, -500)
  331. {
  332. curInt = NULL;
  333. current = NULL;
  334. terminate = false;
  335. statusbar = NULL;
  336. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  337. mainFPSmng = new FPSManager(48);
  338. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  339. }
  340. CGuiHandler::~CGuiHandler()
  341. {
  342. delete mainFPSmng;
  343. }
  344. void CGuiHandler::breakEventHandling()
  345. {
  346. current = NULL;
  347. }
  348. void CGuiHandler::drawFPSCounter()
  349. {
  350. const static SDL_Color yellow = {255, 255, 0, 0};
  351. static SDL_Rect overlay = { 0, 0, 64, 32};
  352. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  353. SDL_FillRect(screen, &overlay, black);
  354. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  355. CSDL_Ext::printAt(fps, 10, 10, FONT_BIG, yellow, screen);
  356. }
  357. void CIntObject::activateLClick()
  358. {
  359. GH.lclickable.push_front(this);
  360. active |= LCLICK;
  361. }
  362. void CIntObject::deactivateLClick()
  363. {
  364. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  365. assert(hlp != GH.lclickable.end());
  366. GH.lclickable.erase(hlp);
  367. active &= ~LCLICK;
  368. }
  369. void CIntObject::clickLeft(tribool down, bool previousState)
  370. {
  371. }
  372. void CIntObject::activateRClick()
  373. {
  374. GH.rclickable.push_front(this);
  375. active |= RCLICK;
  376. }
  377. void CIntObject::deactivateRClick()
  378. {
  379. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  380. assert(hlp != GH.rclickable.end());
  381. GH.rclickable.erase(hlp);
  382. active &= ~RCLICK;
  383. }
  384. void CIntObject::clickRight(tribool down, bool previousState)
  385. {
  386. }
  387. void CIntObject::activateHover()
  388. {
  389. GH.hoverable.push_front(this);
  390. active |= HOVER;
  391. }
  392. void CIntObject::deactivateHover()
  393. {
  394. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  395. assert(hlp != GH.hoverable.end());
  396. GH.hoverable.erase(hlp);
  397. active &= ~HOVER;
  398. }
  399. void CIntObject::hover( bool on )
  400. {
  401. }
  402. void CIntObject::activateKeys()
  403. {
  404. GH.keyinterested.push_front(this);
  405. active |= KEYBOARD;
  406. }
  407. void CIntObject::deactivateKeys()
  408. {
  409. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  410. assert(hlp != GH.keyinterested.end());
  411. GH.keyinterested.erase(hlp);
  412. active &= ~KEYBOARD;
  413. }
  414. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  415. {
  416. }
  417. void CIntObject::activateMouseMove()
  418. {
  419. GH.motioninterested.push_front(this);
  420. active |= MOVE;
  421. }
  422. void CIntObject::deactivateMouseMove()
  423. {
  424. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  425. assert(hlp != GH.motioninterested.end());
  426. GH.motioninterested.erase(hlp);
  427. active &= ~MOVE;
  428. }
  429. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  430. {
  431. }
  432. void CIntObject::activateTimer()
  433. {
  434. GH.timeinterested.push_back(this);
  435. active |= TIME;
  436. }
  437. void CIntObject::deactivateTimer()
  438. {
  439. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  440. assert(hlp != GH.timeinterested.end());
  441. GH.timeinterested.erase(hlp);
  442. active &= ~TIME;
  443. }
  444. void CIntObject::tick()
  445. {
  446. }
  447. CIntObject::CIntObject()
  448. {
  449. pressedL = pressedR = hovered = captureAllKeys = strongInterest = false;
  450. toNextTick = active = used = 0;
  451. recActions = defActions = GH.defActionsDef;
  452. pos.x = 0;
  453. pos.y = 0;
  454. pos.w = 0;
  455. pos.h = 0;
  456. if(GH.captureChildren)
  457. {
  458. assert(GH.createdObj.size());
  459. parent = GH.createdObj.front();
  460. parent->children.push_back(this);
  461. if(parent->defActions & SHARE_POS)
  462. {
  463. pos.x = parent->pos.x;
  464. pos.y = parent->pos.y;
  465. }
  466. }
  467. else
  468. parent = NULL;
  469. }
  470. void CIntObject::show( SDL_Surface * to )
  471. {
  472. if(defActions & UPDATE)
  473. for(size_t i = 0; i < children.size(); i++)
  474. if(children[i]->recActions & UPDATE)
  475. children[i]->show(to);
  476. }
  477. void CIntObject::showAll( SDL_Surface * to )
  478. {
  479. if(defActions & SHOWALL)
  480. {
  481. for(size_t i = 0; i < children.size(); i++)
  482. if(children[i]->recActions & SHOWALL)
  483. children[i]->showAll(to);
  484. }
  485. else
  486. show(to);
  487. }
  488. void CIntObject::activate()
  489. {
  490. assert(!active);
  491. active |= GENERAL;
  492. activate(used);
  493. if(defActions & ACTIVATE)
  494. for(size_t i = 0; i < children.size(); i++)
  495. if(children[i]->recActions & ACTIVATE)
  496. children[i]->activate();
  497. }
  498. void CIntObject::activate(ui16 what)
  499. {
  500. if(what & LCLICK)
  501. activateLClick();
  502. if(what & RCLICK)
  503. activateRClick();
  504. if(what & HOVER)
  505. activateHover();
  506. if(what & MOVE)
  507. activateMouseMove();
  508. if(what & KEYBOARD)
  509. activateKeys();
  510. if(what & TIME)
  511. activateTimer();
  512. if(what & WHEEL)
  513. activateWheel();
  514. if(what & DOUBLECLICK)
  515. activateDClick();
  516. }
  517. void CIntObject::deactivate()
  518. {
  519. assert(active);
  520. active &= ~ GENERAL;
  521. deactivate(used);
  522. assert(!active);
  523. if(defActions & DEACTIVATE)
  524. for(size_t i = 0; i < children.size(); i++)
  525. if(children[i]->recActions & DEACTIVATE)
  526. children[i]->deactivate();
  527. }
  528. void CIntObject::deactivate(ui16 what)
  529. {
  530. if(what & LCLICK)
  531. deactivateLClick();
  532. if(what & RCLICK)
  533. deactivateRClick();
  534. if(what & HOVER)
  535. deactivateHover();
  536. if(what & MOVE)
  537. deactivateMouseMove();
  538. if(what & KEYBOARD)
  539. deactivateKeys();
  540. if(what & TIME) // TIME is special
  541. deactivateTimer();
  542. if(what & WHEEL)
  543. deactivateWheel();
  544. if(what & DOUBLECLICK)
  545. deactivateDClick();
  546. }
  547. CIntObject::~CIntObject()
  548. {
  549. assert(!active); //do not delete active obj
  550. if(defActions & DISPOSE)
  551. for(size_t i = 0; i < children.size(); i++)
  552. if(children[i]->recActions & DISPOSE)
  553. delete children[i];
  554. if(parent && GH.createdObj.size()) //temporary object destroyed
  555. parent->children -= this;
  556. }
  557. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  558. {
  559. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst);
  560. }
  561. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  562. {
  563. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst);
  564. }
  565. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  566. {
  567. printAtMiddleLoc(text, p.x, p.y, font, kolor, dst);
  568. }
  569. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  570. {
  571. blitAt(src, pos.x + x, pos.y + y, dst);
  572. }
  573. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  574. {
  575. blitAtLoc(src, p.x, p.y, dst);
  576. }
  577. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  578. {
  579. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst);
  580. }
  581. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst )
  582. {
  583. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst);
  584. }
  585. void CIntObject::disable()
  586. {
  587. if(active)
  588. deactivate();
  589. recActions = DISPOSE;
  590. }
  591. void CIntObject::enable(bool activation)
  592. {
  593. if(!active && activation)
  594. activate();
  595. recActions = 255;
  596. }
  597. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  598. {
  599. return isItIn(&rect, x - pos.x, y - pos.y);
  600. }
  601. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  602. {
  603. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  604. }
  605. void CIntObject::activateWheel()
  606. {
  607. GH.wheelInterested.push_front(this);
  608. active |= WHEEL;
  609. }
  610. void CIntObject::deactivateWheel()
  611. {
  612. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  613. assert(hlp != GH.wheelInterested.end());
  614. GH.wheelInterested.erase(hlp);
  615. active &= ~WHEEL;
  616. }
  617. void CIntObject::wheelScrolled(bool down, bool in)
  618. {
  619. }
  620. void CIntObject::activateDClick()
  621. {
  622. GH.doubleClickInterested.push_front(this);
  623. active |= DOUBLECLICK;
  624. }
  625. void CIntObject::deactivateDClick()
  626. {
  627. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  628. assert(hlp != GH.doubleClickInterested.end());
  629. GH.doubleClickInterested.erase(hlp);
  630. active &= ~DOUBLECLICK;
  631. }
  632. void CIntObject::onDoubleClick()
  633. {
  634. }
  635. const Rect & CIntObject::center( const Rect &r, bool propagate )
  636. {
  637. pos.w = r.w;
  638. pos.h = r.h;
  639. return center(Point(screen->w/2, screen->h/2), propagate);
  640. }
  641. const Rect & CIntObject::center( bool propagate )
  642. {
  643. return center(pos, propagate);
  644. }
  645. const Rect & CIntObject::center(const Point &p, bool propagate /*= true*/)
  646. {
  647. moveBy(Point(p.x - pos.w/2 - pos.x,
  648. p.y - pos.h/2 - pos.y),
  649. propagate);
  650. return pos;
  651. }
  652. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  653. {
  654. Point newPos = pos.topLeft();
  655. amax(newPos.x, borderWidth);
  656. amax(newPos.y, borderWidth);
  657. amin(newPos.x, screen->w - borderWidth - pos.w);
  658. amin(newPos.y, screen->h - borderWidth - pos.h);
  659. if (newPos != pos.topLeft())
  660. moveTo(newPos, propagate);
  661. }
  662. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  663. {
  664. pos.x += p.x;
  665. pos.y += p.y;
  666. if(propagate)
  667. for(size_t i = 0; i < children.size(); i++)
  668. children[i]->moveBy(p, propagate);
  669. }
  670. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  671. {
  672. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  673. }
  674. void CIntObject::delChild(CIntObject *child)
  675. {
  676. children -= child;
  677. delete child;
  678. }
  679. void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
  680. {
  681. assert(!vstd::contains(children, child));
  682. assert(child->parent == NULL);
  683. children.push_back(child);
  684. child->parent = this;
  685. if(adjustPosition)
  686. child->pos += pos;
  687. }
  688. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  689. {
  690. assert(vstd::contains(children, child));
  691. assert(child->parent == this);
  692. children -= child;
  693. child->parent = NULL;
  694. if(adjustPosition)
  695. child->pos -= pos;
  696. }
  697. void CIntObject::changeUsedEvents(ui16 what, bool enable, bool adjust /*= true*/)
  698. {
  699. if(enable)
  700. {
  701. used |= what;
  702. if(adjust && active)
  703. activate(what);
  704. }
  705. else
  706. {
  707. used &= ~what;
  708. if(adjust && active)
  709. deactivate(what);
  710. }
  711. }
  712. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  713. {
  714. init();
  715. bg = BG;
  716. freeSurf = Free;
  717. pos.x += x;
  718. pos.y += y;
  719. pos.w = BG->w;
  720. pos.h = BG->h;
  721. }
  722. CPicture::CPicture( const std::string &bmpname, int x, int y )
  723. {
  724. init();
  725. bg = BitmapHandler::loadBitmap(bmpname);
  726. freeSurf = true;;
  727. pos.x += x;
  728. pos.y += y;
  729. if(bg)
  730. {
  731. pos.w = bg->w;
  732. pos.h = bg->h;
  733. }
  734. else
  735. {
  736. pos.w = pos.h = 0;
  737. }
  738. }
  739. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  740. {
  741. init();
  742. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  743. }
  744. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  745. {
  746. init();
  747. createSimpleRect(r, screenFormat, color);
  748. }
  749. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  750. {
  751. needRefresh = false;
  752. srcRect = new Rect(SrcRect);
  753. pos.x += x;
  754. pos.y += y;
  755. pos.w = srcRect->w;
  756. pos.h = srcRect->h;
  757. bg = BG;
  758. freeSurf = free;
  759. }
  760. CPicture::~CPicture()
  761. {
  762. if(freeSurf)
  763. SDL_FreeSurface(bg);
  764. delete srcRect;
  765. }
  766. void CPicture::init()
  767. {
  768. needRefresh = false;
  769. srcRect = NULL;
  770. }
  771. void CPicture::show( SDL_Surface * to )
  772. {
  773. if (needRefresh)
  774. showAll(to);
  775. }
  776. void CPicture::showAll( SDL_Surface * to )
  777. {
  778. if(bg)
  779. {
  780. if(srcRect)
  781. {
  782. SDL_Rect srcRectCpy = *srcRect;
  783. SDL_Rect dstRect = srcRectCpy;
  784. dstRect.x = pos.x;
  785. dstRect.y = pos.y;
  786. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  787. }
  788. else
  789. blitAt(bg, pos, to);
  790. }
  791. }
  792. void CPicture::convertToScreenBPP()
  793. {
  794. SDL_Surface *hlp = bg;
  795. bg = SDL_ConvertSurface(hlp,screen->format,0);
  796. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  797. SDL_FreeSurface(hlp);
  798. }
  799. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  800. {
  801. pos += r;
  802. pos.w = r.w;
  803. pos.h = r.h;
  804. if(screenFormat)
  805. bg = CSDL_Ext::newSurface(r.w, r.h);
  806. else
  807. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  808. SDL_FillRect(bg, NULL, color);
  809. freeSurf = true;
  810. }
  811. void CPicture::colorizeAndConvert(int player)
  812. {
  813. assert(bg);
  814. colorize(player);
  815. convertToScreenBPP();
  816. }
  817. void CPicture::colorize(int player)
  818. {
  819. assert(bg);
  820. assert(bg->format->BitsPerPixel == 8);
  821. graphics->blueToPlayersAdv(bg, player);
  822. }
  823. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  824. :myObj(obj)
  825. {
  826. GH.createdObj.push_front(obj);
  827. GH.captureChildren = true;
  828. }
  829. ObjectConstruction::~ObjectConstruction()
  830. {
  831. assert(GH.createdObj.size());
  832. assert(GH.createdObj.front() == myObj);
  833. GH.createdObj.pop_front();
  834. GH.captureChildren = GH.createdObj.size();
  835. }
  836. SetCaptureState::SetCaptureState(bool allow, ui8 actions)
  837. {
  838. previousCapture = GH.captureChildren;
  839. GH.captureChildren = false;
  840. prevActions = GH.defActionsDef;
  841. GH.defActionsDef = actions;
  842. }
  843. SetCaptureState::~SetCaptureState()
  844. {
  845. GH.captureChildren = previousCapture;
  846. GH.defActionsDef = prevActions;
  847. }
  848. void IShowable::redraw()
  849. {
  850. showAll(screenBuf);
  851. if(screenBuf != screen)
  852. showAll(screen);
  853. }
  854. SDLKey arrowToNum( SDLKey key )
  855. {
  856. switch(key)
  857. {
  858. case SDLK_DOWN:
  859. return SDLK_KP2;
  860. case SDLK_UP:
  861. return SDLK_KP8;
  862. case SDLK_LEFT:
  863. return SDLK_KP4;
  864. case SDLK_RIGHT:
  865. return SDLK_KP6;
  866. default:
  867. assert(0);
  868. }
  869. throw std::string("Wrong key!");
  870. }
  871. SDLKey numToDigit( SDLKey key )
  872. {
  873. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  874. return SDLKey(key - SDLK_KP0 + SDLK_0);
  875. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  876. switch(key)
  877. {
  878. REMOVE_KP(PERIOD)
  879. REMOVE_KP(MINUS)
  880. REMOVE_KP(PLUS)
  881. REMOVE_KP(EQUALS)
  882. case SDLK_KP_MULTIPLY:
  883. return SDLK_ASTERISK;
  884. case SDLK_KP_DIVIDE:
  885. return SDLK_SLASH;
  886. case SDLK_KP_ENTER:
  887. return SDLK_RETURN;
  888. default:
  889. tlog3 << "Illegal numkey conversion!" << std::endl;
  890. return SDLK_UNKNOWN;
  891. }
  892. #undef REMOVE_KP
  893. }
  894. bool isNumKey( SDLKey key, bool number )
  895. {
  896. if(number)
  897. return key >= SDLK_KP0 && key <= SDLK_KP9;
  898. else
  899. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  900. }
  901. bool isArrowKey( SDLKey key )
  902. {
  903. return key >= SDLK_UP && key <= SDLK_LEFT;
  904. }
  905. CIntObject * moveChild(CIntObject *obj, CIntObject *from, CIntObject *to, bool adjustPos)
  906. {
  907. from->removeChild(obj, adjustPos);
  908. to->addChild(obj, adjustPos);
  909. return obj;
  910. }
  911. Rect Rect::createCentered( int w, int h )
  912. {
  913. return Rect(screen->w/2 - w/2, screen->h/2 - h/2, w, h);
  914. }
  915. Rect Rect::around(const Rect &r, int width /*= 1*/) /*creates rect around another */
  916. {
  917. return Rect(r.x - width, r.y - width, r.w + width * 2, r.h + width * 2);
  918. }
  919. Rect Rect::centerIn(const Rect &r)
  920. {
  921. return Rect(r.x + (r.w - w) / 2, r.y + (r.h - h) / 2, w, h);
  922. }