GUIBase.cpp 19 KB

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