GUIBase.cpp 23 KB

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