GUIBase.cpp 22 KB

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