GUIBase.cpp 22 KB

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