GUIBase.cpp 23 KB

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