GUIBase.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 CIntObject::activateLClick()
  328. {
  329. GH.lclickable.push_front(this);
  330. active |= LCLICK;
  331. }
  332. void CIntObject::deactivateLClick()
  333. {
  334. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  335. assert(hlp != GH.lclickable.end());
  336. GH.lclickable.erase(hlp);
  337. active &= ~LCLICK;
  338. }
  339. void CIntObject::clickLeft(tribool down, bool previousState)
  340. {
  341. }
  342. void CIntObject::activateRClick()
  343. {
  344. GH.rclickable.push_front(this);
  345. active |= RCLICK;
  346. }
  347. void CIntObject::deactivateRClick()
  348. {
  349. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  350. assert(hlp != GH.rclickable.end());
  351. GH.rclickable.erase(hlp);
  352. active &= ~RCLICK;
  353. }
  354. void CIntObject::clickRight(tribool down, bool previousState)
  355. {
  356. }
  357. void CIntObject::activateHover()
  358. {
  359. GH.hoverable.push_front(this);
  360. active |= HOVER;
  361. }
  362. void CIntObject::deactivateHover()
  363. {
  364. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  365. assert(hlp != GH.hoverable.end());
  366. GH.hoverable.erase(hlp);
  367. active &= ~HOVER;
  368. }
  369. void CIntObject::hover( bool on )
  370. {
  371. }
  372. void CIntObject::activateKeys()
  373. {
  374. GH.keyinterested.push_front(this);
  375. active |= KEYBOARD;
  376. }
  377. void CIntObject::deactivateKeys()
  378. {
  379. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  380. assert(hlp != GH.keyinterested.end());
  381. GH.keyinterested.erase(hlp);
  382. active &= ~KEYBOARD;
  383. }
  384. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  385. {
  386. }
  387. void CIntObject::activateMouseMove()
  388. {
  389. GH.motioninterested.push_front(this);
  390. active |= MOVE;
  391. }
  392. void CIntObject::deactivateMouseMove()
  393. {
  394. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  395. assert(hlp != GH.motioninterested.end());
  396. GH.motioninterested.erase(hlp);
  397. active &= ~MOVE;
  398. }
  399. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  400. {
  401. }
  402. void CIntObject::activateTimer()
  403. {
  404. GH.timeinterested.push_back(this);
  405. active |= TIME;
  406. }
  407. void CIntObject::deactivateTimer()
  408. {
  409. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  410. assert(hlp != GH.timeinterested.end());
  411. GH.timeinterested.erase(hlp);
  412. active &= ~TIME;
  413. }
  414. void CIntObject::tick()
  415. {
  416. }
  417. CIntObject::CIntObject()
  418. {
  419. pressedL = pressedR = hovered = captureAllKeys = strongInterest = toNextTick = active = used = 0;
  420. recActions = defActions = GH.defActionsDef;
  421. pos.x = 0;
  422. pos.y = 0;
  423. pos.w = 0;
  424. pos.h = 0;
  425. if(GH.captureChildren)
  426. {
  427. assert(GH.createdObj.size());
  428. parent = GH.createdObj.front();
  429. parent->children.push_back(this);
  430. if(parent->defActions & SHARE_POS)
  431. {
  432. pos.x = parent->pos.x;
  433. pos.y = parent->pos.y;
  434. }
  435. }
  436. else
  437. parent = NULL;
  438. }
  439. void CIntObject::show( SDL_Surface * to )
  440. {
  441. if(defActions & UPDATE)
  442. for(size_t i = 0; i < children.size(); i++)
  443. if(children[i]->recActions & UPDATE)
  444. children[i]->show(to);
  445. }
  446. void CIntObject::showAll( SDL_Surface * to )
  447. {
  448. if(defActions & SHOWALL)
  449. {
  450. for(size_t i = 0; i < children.size(); i++)
  451. if(children[i]->recActions & SHOWALL)
  452. children[i]->showAll(to);
  453. }
  454. else
  455. show(to);
  456. }
  457. void CIntObject::activate()
  458. {
  459. assert(!active);
  460. active |= GENERAL;
  461. if(used & LCLICK)
  462. activateLClick();
  463. if(used & RCLICK)
  464. activateRClick();
  465. if(used & HOVER)
  466. activateHover();
  467. if(used & MOVE)
  468. activateMouseMove();
  469. if(used & KEYBOARD)
  470. activateKeys();
  471. if(used & TIME)
  472. activateTimer();
  473. if(used & WHEEL)
  474. activateWheel();
  475. if(used & DOUBLECLICK)
  476. activateDClick();
  477. if(defActions & ACTIVATE)
  478. for(size_t i = 0; i < children.size(); i++)
  479. if(children[i]->recActions & ACTIVATE)
  480. children[i]->activate();
  481. }
  482. void CIntObject::deactivate()
  483. {
  484. assert(active);
  485. active &= ~ GENERAL;
  486. if(used & LCLICK)
  487. deactivateLClick();
  488. if(used & RCLICK)
  489. deactivateRClick();
  490. if(used & HOVER)
  491. deactivateHover();
  492. if(used & MOVE)
  493. deactivateMouseMove();
  494. if(used & KEYBOARD)
  495. deactivateKeys();
  496. if(active & TIME) // TIME is special
  497. deactivateTimer();
  498. if(used & WHEEL)
  499. deactivateWheel();
  500. if(used & DOUBLECLICK)
  501. deactivateDClick();
  502. assert(!active);
  503. if(defActions & DEACTIVATE)
  504. for(size_t i = 0; i < children.size(); i++)
  505. if(children[i]->recActions & DEACTIVATE)
  506. children[i]->deactivate();
  507. }
  508. CIntObject::~CIntObject()
  509. {
  510. assert(!active); //do not delete active obj
  511. if(defActions & DISPOSE)
  512. for(size_t i = 0; i < children.size(); i++)
  513. if(children[i]->recActions & DISPOSE)
  514. delete children[i];
  515. if(parent && GH.createdObj.size()) //temporary object destroyed
  516. parent->children -= this;
  517. }
  518. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  519. {
  520. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  521. }
  522. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  523. {
  524. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  525. }
  526. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh /*= false*/)
  527. {
  528. printAtMiddleLoc(text, p.x, p.y, font, kolor, dst, refresh);
  529. }
  530. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  531. {
  532. blitAt(src, pos.x + x, pos.y + y, dst);
  533. }
  534. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  535. {
  536. blitAtLoc(src, p.x, p.y, dst);
  537. }
  538. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh /*= false*/ )
  539. {
  540. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst, refrsh);
  541. }
  542. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh /*= false*/ )
  543. {
  544. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  545. }
  546. void CIntObject::disable()
  547. {
  548. if(active)
  549. deactivate();
  550. recActions = DISPOSE;
  551. }
  552. void CIntObject::enable(bool activation)
  553. {
  554. if(!active && activation)
  555. activate();
  556. recActions = 255;
  557. }
  558. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  559. {
  560. return isItIn(&rect, x - pos.x, y - pos.y);
  561. }
  562. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  563. {
  564. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  565. }
  566. void CIntObject::activateWheel()
  567. {
  568. GH.wheelInterested.push_front(this);
  569. active |= WHEEL;
  570. }
  571. void CIntObject::deactivateWheel()
  572. {
  573. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  574. assert(hlp != GH.wheelInterested.end());
  575. GH.wheelInterested.erase(hlp);
  576. active &= ~WHEEL;
  577. }
  578. void CIntObject::wheelScrolled(bool down, bool in)
  579. {
  580. }
  581. void CIntObject::activateDClick()
  582. {
  583. GH.doubleClickInterested.push_front(this);
  584. active |= DOUBLECLICK;
  585. }
  586. void CIntObject::deactivateDClick()
  587. {
  588. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  589. assert(hlp != GH.doubleClickInterested.end());
  590. GH.doubleClickInterested.erase(hlp);
  591. active &= ~DOUBLECLICK;
  592. }
  593. void CIntObject::onDoubleClick()
  594. {
  595. }
  596. const Rect & CIntObject::center( const Rect &r )
  597. {
  598. pos.w = r.w;
  599. pos.h = r.h;
  600. pos.x = screen->w/2 - r.w/2;
  601. pos.y = screen->h/2 - r.h/2;
  602. return pos;
  603. }
  604. const Rect & CIntObject::center()
  605. {
  606. return center(pos);
  607. }
  608. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  609. {
  610. pos.x += p.x;
  611. pos.y += p.y;
  612. if(propagate)
  613. for(size_t i = 0; i < children.size(); i++)
  614. children[i]->moveBy(p, propagate);
  615. }
  616. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  617. {
  618. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  619. }
  620. void CIntObject::delChild(CIntObject *child)
  621. {
  622. children -= child;
  623. delete child;
  624. }
  625. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  626. {
  627. bg = BG;
  628. freeSurf = Free;
  629. pos.x += x;
  630. pos.y += y;
  631. pos.w = BG->w;
  632. pos.h = BG->h;
  633. }
  634. CPicture::CPicture( const std::string &bmpname, int x, int y )
  635. {
  636. bg = BitmapHandler::loadBitmap(bmpname);
  637. freeSurf = true;;
  638. pos.x += x;
  639. pos.y += y;
  640. if(bg)
  641. {
  642. pos.w = bg->w;
  643. pos.h = bg->h;
  644. }
  645. else
  646. {
  647. pos.w = pos.h = 0;
  648. }
  649. }
  650. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  651. {
  652. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  653. }
  654. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  655. {
  656. createSimpleRect(r, screenFormat, color);
  657. }
  658. CPicture::~CPicture()
  659. {
  660. if(freeSurf)
  661. SDL_FreeSurface(bg);
  662. }
  663. void CPicture::showAll( SDL_Surface * to )
  664. {
  665. if(bg)
  666. blitAt(bg, pos, to);
  667. }
  668. void CPicture::convertToScreenBPP()
  669. {
  670. SDL_Surface *hlp = bg;
  671. bg = SDL_ConvertSurface(hlp,screen->format,0);
  672. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  673. SDL_FreeSurface(hlp);
  674. }
  675. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  676. {
  677. pos += r;
  678. pos.w = r.w;
  679. pos.h = r.h;
  680. if(screenFormat)
  681. bg = CSDL_Ext::newSurface(r.w, r.h);
  682. else
  683. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  684. SDL_FillRect(bg, NULL, color);
  685. }
  686. void CPicture::colorizeAndConvert(int player)
  687. {
  688. assert(bg);
  689. assert(bg->format->BitsPerPixel == 8);
  690. graphics->blueToPlayersAdv(bg, player);
  691. convertToScreenBPP();
  692. }
  693. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  694. :myObj(obj)
  695. {
  696. GH.createdObj.push_front(obj);
  697. GH.captureChildren = true;
  698. }
  699. ObjectConstruction::~ObjectConstruction()
  700. {
  701. assert(GH.createdObj.size());
  702. assert(GH.createdObj.front() == myObj);
  703. GH.createdObj.pop_front();
  704. GH.captureChildren = GH.createdObj.size();
  705. }
  706. SetCaptureState::SetCaptureState(bool allow, ui8 actions)
  707. {
  708. previousCapture = GH.captureChildren;
  709. GH.captureChildren = false;
  710. prevActions = GH.defActionsDef;
  711. GH.defActionsDef = actions;
  712. }
  713. SetCaptureState::~SetCaptureState()
  714. {
  715. GH.captureChildren = previousCapture;
  716. GH.defActionsDef = prevActions;
  717. }
  718. void IShowable::redraw()
  719. {
  720. showAll(screenBuf);
  721. if(screenBuf != screen)
  722. showAll(screen);
  723. }
  724. SDLKey arrowToNum( SDLKey key )
  725. {
  726. switch(key)
  727. {
  728. case SDLK_DOWN:
  729. return SDLK_KP2;
  730. case SDLK_UP:
  731. return SDLK_KP8;
  732. case SDLK_LEFT:
  733. return SDLK_KP4;
  734. case SDLK_RIGHT:
  735. return SDLK_KP6;
  736. default:
  737. assert(0);
  738. }
  739. throw std::string("Wrong key!");
  740. }
  741. SDLKey numToDigit( SDLKey key )
  742. {
  743. return SDLKey(key - SDLK_KP0 + SDLK_0);
  744. }
  745. bool isNumKey( SDLKey key, bool number )
  746. {
  747. if(number)
  748. return key >= SDLK_KP0 && key <= SDLK_KP9;
  749. else
  750. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  751. }
  752. bool isArrowKey( SDLKey key )
  753. {
  754. return key >= SDLK_UP && key <= SDLK_LEFT;
  755. }
  756. CIntObject * moveChildren(CIntObject *obj, CIntObject *from, CIntObject *to, bool adjustPos)
  757. {
  758. assert(vstd::contains(from->children, obj));
  759. assert(obj->parent == from);
  760. from->children -= obj;
  761. to->children.push_back(obj);
  762. obj->parent = to;
  763. if(adjustPos)
  764. obj->pos -= from->pos - to->pos;
  765. return obj;
  766. }
  767. Rect Rect::createCentered( int w, int h )
  768. {
  769. return Rect(screen->w/2 - w/2, screen->h/2 - h/2, w, h);
  770. }