GUIBase.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. {
  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. }
  123. handleEvent(ev);
  124. delete ev;
  125. }
  126. }
  127. void CGuiHandler::handleEvent(SDL_Event *sEvent)
  128. {
  129. current = sEvent;
  130. bool prev;
  131. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  132. {
  133. SDL_KeyboardEvent key = sEvent->key;
  134. //translate numpad keys
  135. if(key.keysym.sym == SDLK_KP_ENTER)
  136. {
  137. key.keysym.sym = (SDLKey)SDLK_RETURN;
  138. }
  139. bool keysCaptured = false;
  140. for(std::list<CIntObject*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
  141. {
  142. if((*i)->captureAllKeys)
  143. {
  144. keysCaptured = true;
  145. break;
  146. }
  147. }
  148. std::list<CIntObject*> miCopy = keyinterested;
  149. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  150. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  151. (**i).keyPressed(key);
  152. }
  153. else if(sEvent->type==SDL_MOUSEMOTION)
  154. {
  155. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  156. handleMouseMotion(sEvent);
  157. }
  158. else if (sEvent->type==SDL_MOUSEBUTTONDOWN)
  159. {
  160. if(sEvent->button.button == SDL_BUTTON_LEFT)
  161. {
  162. if(lastClick == sEvent->motion && (SDL_GetTicks() - lastClickTime) < 300)
  163. {
  164. std::list<CIntObject*> hlp = doubleClickInterested;
  165. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  166. {
  167. if(!vstd::contains(doubleClickInterested,*i)) continue;
  168. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  169. {
  170. (*i)->onDoubleClick();
  171. }
  172. }
  173. }
  174. lastClick = sEvent->motion;
  175. lastClickTime = SDL_GetTicks();
  176. std::list<CIntObject*> hlp = lclickable;
  177. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  178. {
  179. if(!vstd::contains(lclickable,*i)) continue;
  180. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  181. {
  182. prev = (*i)->pressedL;
  183. (*i)->pressedL = true;
  184. (*i)->clickLeft(true, prev);
  185. }
  186. }
  187. }
  188. else if (sEvent->button.button == SDL_BUTTON_RIGHT)
  189. {
  190. std::list<CIntObject*> hlp = rclickable;
  191. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  192. {
  193. if(!vstd::contains(rclickable,*i)) continue;
  194. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  195. {
  196. prev = (*i)->pressedR;
  197. (*i)->pressedR = true;
  198. (*i)->clickRight(true, prev);
  199. }
  200. }
  201. }
  202. else if(sEvent->button.button == SDL_BUTTON_WHEELDOWN || sEvent->button.button == SDL_BUTTON_WHEELUP)
  203. {
  204. std::list<CIntObject*> hlp = wheelInterested;
  205. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  206. {
  207. if(!vstd::contains(wheelInterested,*i)) continue;
  208. (*i)->wheelScrolled(sEvent->button.button == SDL_BUTTON_WHEELDOWN, isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y));
  209. }
  210. }
  211. }
  212. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  213. {
  214. std::list<CIntObject*> hlp = lclickable;
  215. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  216. {
  217. if(!vstd::contains(lclickable,*i)) continue;
  218. prev = (*i)->pressedL;
  219. (*i)->pressedL = false;
  220. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  221. {
  222. (*i)->clickLeft(false, prev);
  223. }
  224. else
  225. (*i)->clickLeft(boost::logic::indeterminate, prev);
  226. }
  227. }
  228. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  229. {
  230. std::list<CIntObject*> hlp = rclickable;
  231. for(std::list<CIntObject*>::iterator i=hlp.begin(); i != hlp.end();i++)
  232. {
  233. if(!vstd::contains(rclickable,*i)) continue;
  234. prev = (*i)->pressedR;
  235. (*i)->pressedR = false;
  236. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  237. {
  238. (*i)->clickRight(false, prev);
  239. }
  240. else
  241. (*i)->clickRight(boost::logic::indeterminate, prev);
  242. }
  243. }
  244. current = NULL;
  245. } //event end
  246. void CGuiHandler::handleMouseMotion(SDL_Event *sEvent)
  247. {
  248. //sending active, hovered hoverable objects hover() call
  249. std::vector<CIntObject*> hlp;
  250. for(std::list<CIntObject*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  251. {
  252. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  253. {
  254. if (!(*i)->hovered)
  255. hlp.push_back((*i));
  256. }
  257. else if ((*i)->hovered)
  258. {
  259. (*i)->hover(false);
  260. (*i)->hovered = false;
  261. }
  262. }
  263. for(int i=0; i<hlp.size();i++)
  264. {
  265. hlp[i]->hover(true);
  266. hlp[i]->hovered = true;
  267. }
  268. handleMoveInterested(sEvent->motion);
  269. }
  270. void CGuiHandler::simpleRedraw()
  271. {
  272. //update only top interface and draw background
  273. if(objsToBlit.size() > 1)
  274. blitAt(screen2,0,0,screen); //blit background
  275. objsToBlit.back()->show(screen); //blit active interface/window
  276. }
  277. void CGuiHandler::handleMoveInterested( const SDL_MouseMotionEvent & motion )
  278. {
  279. //sending active, MotionInterested objects mouseMoved() call
  280. std::list<CIntObject*> miCopy = motioninterested;
  281. for(std::list<CIntObject*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  282. {
  283. if ((*i)->strongInterest || isItIn(&(*i)->pos, motion.x, motion.y))
  284. {
  285. (*i)->mouseMoved(motion);
  286. }
  287. }
  288. }
  289. void CGuiHandler::fakeMouseMove()
  290. {
  291. SDL_Event evnt;
  292. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0};
  293. int x, y;
  294. sme.state = SDL_GetMouseState(&x, &y);
  295. sme.x = x;
  296. sme.y = y;
  297. evnt.motion = sme;
  298. current = &evnt;
  299. handleMoveInterested(sme);
  300. }
  301. void CIntObject::activateLClick()
  302. {
  303. GH.lclickable.push_front(this);
  304. active |= LCLICK;
  305. }
  306. void CIntObject::deactivateLClick()
  307. {
  308. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  309. assert(hlp != GH.lclickable.end());
  310. GH.lclickable.erase(hlp);
  311. active &= ~LCLICK;
  312. }
  313. void CIntObject::clickLeft(tribool down, bool previousState)
  314. {
  315. }
  316. void CIntObject::activateRClick()
  317. {
  318. GH.rclickable.push_front(this);
  319. active |= RCLICK;
  320. }
  321. void CIntObject::deactivateRClick()
  322. {
  323. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  324. assert(hlp != GH.rclickable.end());
  325. GH.rclickable.erase(hlp);
  326. active &= ~RCLICK;
  327. }
  328. void CIntObject::clickRight(tribool down, bool previousState)
  329. {
  330. }
  331. void CIntObject::activateHover()
  332. {
  333. GH.hoverable.push_front(this);
  334. active |= HOVER;
  335. }
  336. void CIntObject::deactivateHover()
  337. {
  338. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  339. assert(hlp != GH.hoverable.end());
  340. GH.hoverable.erase(hlp);
  341. active &= ~HOVER;
  342. }
  343. void CIntObject::hover( bool on )
  344. {
  345. }
  346. void CIntObject::activateKeys()
  347. {
  348. GH.keyinterested.push_front(this);
  349. active |= KEYBOARD;
  350. }
  351. void CIntObject::deactivateKeys()
  352. {
  353. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  354. assert(hlp != GH.keyinterested.end());
  355. GH.keyinterested.erase(hlp);
  356. active &= ~KEYBOARD;
  357. }
  358. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  359. {
  360. }
  361. void CIntObject::activateMouseMove()
  362. {
  363. GH.motioninterested.push_front(this);
  364. active |= MOVE;
  365. }
  366. void CIntObject::deactivateMouseMove()
  367. {
  368. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  369. assert(hlp != GH.motioninterested.end());
  370. GH.motioninterested.erase(hlp);
  371. active &= ~MOVE;
  372. }
  373. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  374. {
  375. }
  376. void CIntObject::activateTimer()
  377. {
  378. GH.timeinterested.push_back(this);
  379. active |= TIME;
  380. }
  381. void CIntObject::deactivateTimer()
  382. {
  383. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  384. assert(hlp != GH.timeinterested.end());
  385. GH.timeinterested.erase(hlp);
  386. active &= ~TIME;
  387. }
  388. void CIntObject::tick()
  389. {
  390. }
  391. CIntObject::CIntObject()
  392. {
  393. pressedL = pressedR = hovered = captureAllKeys = strongInterest = toNextTick = active = used = 0;
  394. recActions = defActions = GH.defActionsDef;
  395. pos.x = 0;
  396. pos.y = 0;
  397. pos.w = 0;
  398. pos.h = 0;
  399. if(GH.captureChildren)
  400. {
  401. assert(GH.createdObj.size());
  402. parent = GH.createdObj.front();
  403. parent->children.push_back(this);
  404. if(parent->defActions & SHARE_POS)
  405. {
  406. pos.x = parent->pos.x;
  407. pos.y = parent->pos.y;
  408. }
  409. }
  410. }
  411. void CIntObject::show( SDL_Surface * to )
  412. {
  413. if(defActions & UPDATE)
  414. for(size_t i = 0; i < children.size(); i++)
  415. if(children[i]->recActions & UPDATE)
  416. children[i]->show(to);
  417. }
  418. void CIntObject::showAll( SDL_Surface * to )
  419. {
  420. if(defActions & SHOWALL)
  421. {
  422. for(size_t i = 0; i < children.size(); i++)
  423. if(children[i]->recActions & SHOWALL)
  424. children[i]->showAll(to);
  425. }
  426. else
  427. show(to);
  428. }
  429. void CIntObject::activate()
  430. {
  431. assert(!active);
  432. active |= GENERAL;
  433. if(used & LCLICK)
  434. activateLClick();
  435. if(used & RCLICK)
  436. activateRClick();
  437. if(used & HOVER)
  438. activateHover();
  439. if(used & MOVE)
  440. activateMouseMove();
  441. if(used & KEYBOARD)
  442. activateKeys();
  443. if(used & TIME)
  444. activateTimer();
  445. if(used & WHEEL)
  446. activateWheel();
  447. if(used & DOUBLECLICK)
  448. activateDClick();
  449. if(defActions & ACTIVATE)
  450. for(size_t i = 0; i < children.size(); i++)
  451. if(children[i]->recActions & ACTIVATE)
  452. children[i]->activate();
  453. }
  454. void CIntObject::deactivate()
  455. {
  456. assert(active);
  457. active &= ~ GENERAL;
  458. if(used & LCLICK)
  459. deactivateLClick();
  460. if(used & RCLICK)
  461. deactivateRClick();
  462. if(used & HOVER)
  463. deactivateHover();
  464. if(used & MOVE)
  465. deactivateMouseMove();
  466. if(used & KEYBOARD)
  467. deactivateKeys();
  468. if(active & TIME) // TIME is special
  469. deactivateTimer();
  470. if(used & WHEEL)
  471. deactivateWheel();
  472. if(used & DOUBLECLICK)
  473. deactivateDClick();
  474. assert(!active);
  475. if(defActions & DEACTIVATE)
  476. for(size_t i = 0; i < children.size(); i++)
  477. if(children[i]->recActions & DEACTIVATE)
  478. children[i]->deactivate();
  479. }
  480. CIntObject::~CIntObject()
  481. {
  482. assert(!active); //do not delete active obj
  483. if(defActions & DISPOSE)
  484. for(size_t i = 0; i < children.size(); i++)
  485. if(children[i]->recActions & DISPOSE)
  486. delete children[i];
  487. }
  488. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  489. {
  490. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  491. }
  492. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  493. {
  494. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  495. }
  496. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  497. {
  498. blitAt(src, pos.x + x, pos.y + y, dst);
  499. }
  500. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh /*= false*/ )
  501. {
  502. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst, refrsh);
  503. }
  504. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh /*= false*/ )
  505. {
  506. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  507. }
  508. void CIntObject::disable()
  509. {
  510. if(active)
  511. deactivate();
  512. recActions = DISPOSE;
  513. }
  514. void CIntObject::enable(bool activation)
  515. {
  516. if(!active && activation)
  517. activate();
  518. recActions = 255;
  519. }
  520. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  521. {
  522. return isItIn(&rect, x - pos.x, y - pos.y);
  523. }
  524. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  525. {
  526. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  527. }
  528. void CIntObject::activateWheel()
  529. {
  530. GH.wheelInterested.push_front(this);
  531. active |= WHEEL;
  532. }
  533. void CIntObject::deactivateWheel()
  534. {
  535. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  536. assert(hlp != GH.wheelInterested.end());
  537. GH.wheelInterested.erase(hlp);
  538. active &= ~WHEEL;
  539. }
  540. void CIntObject::wheelScrolled(bool down, bool in)
  541. {
  542. }
  543. void CIntObject::activateDClick()
  544. {
  545. GH.doubleClickInterested.push_front(this);
  546. active |= DOUBLECLICK;
  547. }
  548. void CIntObject::deactivateDClick()
  549. {
  550. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  551. assert(hlp != GH.doubleClickInterested.end());
  552. GH.doubleClickInterested.erase(hlp);
  553. active &= ~DOUBLECLICK;
  554. }
  555. void CIntObject::onDoubleClick()
  556. {
  557. }
  558. const Rect & CIntObject::center( const Rect &r )
  559. {
  560. pos.w = r.w;
  561. pos.h = r.h;
  562. pos.x = screen->w/2 - r.w/2;
  563. pos.y = screen->h/2 - r.h/2;
  564. return pos;
  565. }
  566. const Rect & CIntObject::center()
  567. {
  568. return center(pos);
  569. }
  570. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  571. {
  572. pos.x += p.x;
  573. pos.y += p.y;
  574. if(propagate)
  575. for(size_t i = 0; i < children.size(); i++)
  576. children[i]->moveBy(p, propagate);
  577. }
  578. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  579. {
  580. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  581. }
  582. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  583. {
  584. bg = BG;
  585. freeSurf = Free;
  586. pos.x += x;
  587. pos.y += y;
  588. pos.w = BG->w;
  589. pos.h = BG->h;
  590. }
  591. CPicture::CPicture( const std::string &bmpname, int x, int y )
  592. {
  593. bg = BitmapHandler::loadBitmap(bmpname);
  594. freeSurf = true;;
  595. pos.x += x;
  596. pos.y += y;
  597. if(bg)
  598. {
  599. pos.w = bg->w;
  600. pos.h = bg->h;
  601. }
  602. else
  603. {
  604. pos.w = pos.h = 0;
  605. }
  606. }
  607. CPicture::~CPicture()
  608. {
  609. if(freeSurf)
  610. SDL_FreeSurface(bg);
  611. }
  612. void CPicture::showAll( SDL_Surface * to )
  613. {
  614. if(bg)
  615. blitAt(bg, pos, to);
  616. }
  617. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  618. :myObj(obj)
  619. {
  620. GH.createdObj.push_front(obj);
  621. GH.captureChildren = true;
  622. }
  623. ObjectConstruction::~ObjectConstruction()
  624. {
  625. assert(GH.createdObj.size());
  626. assert(GH.createdObj.front() == myObj);
  627. GH.createdObj.pop_front();
  628. GH.captureChildren = GH.createdObj.size();
  629. }
  630. SetCaptureState::SetCaptureState(bool allow, ui8 actions)
  631. {
  632. previousCapture = GH.captureChildren;
  633. GH.captureChildren = false;
  634. prevActions = GH.defActionsDef;
  635. GH.defActionsDef = actions;
  636. }
  637. SetCaptureState::~SetCaptureState()
  638. {
  639. GH.captureChildren = previousCapture;
  640. GH.defActionsDef = prevActions;
  641. }
  642. void IShowable::redraw()
  643. {
  644. showAll(screenBuf);
  645. if(screenBuf != screen)
  646. showAll(screen);
  647. }
  648. SDLKey arrowToNum( SDLKey key )
  649. {
  650. switch(key)
  651. {
  652. case SDLK_DOWN:
  653. return SDLK_KP2;
  654. case SDLK_UP:
  655. return SDLK_KP8;
  656. case SDLK_LEFT:
  657. return SDLK_KP4;
  658. case SDLK_RIGHT:
  659. return SDLK_KP6;
  660. default:
  661. assert(0);
  662. }
  663. throw std::string("Wrong key!");
  664. }
  665. SDLKey numToDigit( SDLKey key )
  666. {
  667. return SDLKey(key - SDLK_KP0 + SDLK_0);
  668. }
  669. bool isNumKey( SDLKey key, bool number )
  670. {
  671. if(number)
  672. return key >= SDLK_KP0 && key <= SDLK_KP9;
  673. else
  674. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  675. }
  676. bool isArrowKey( SDLKey key )
  677. {
  678. return key >= SDLK_UP && key <= SDLK_LEFT;
  679. }
  680. Rect Rect::createCentered( int w, int h )
  681. {
  682. return Rect(screen->w/2 - w/2, screen->h/2 - h/2, w, h);
  683. }