GUIBase.cpp 19 KB

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