GUIBase.cpp 24 KB

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