GUIBase.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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_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. handleMoveInterested(sme);
  297. }
  298. void CIntObject::activateLClick()
  299. {
  300. GH.lclickable.push_front(this);
  301. active |= LCLICK;
  302. }
  303. void CIntObject::deactivateLClick()
  304. {
  305. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  306. assert(hlp != GH.lclickable.end());
  307. GH.lclickable.erase(hlp);
  308. active &= ~LCLICK;
  309. }
  310. void CIntObject::clickLeft(tribool down, bool previousState)
  311. {
  312. }
  313. void CIntObject::activateRClick()
  314. {
  315. GH.rclickable.push_front(this);
  316. active |= RCLICK;
  317. }
  318. void CIntObject::deactivateRClick()
  319. {
  320. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  321. assert(hlp != GH.rclickable.end());
  322. GH.rclickable.erase(hlp);
  323. active &= ~RCLICK;
  324. }
  325. void CIntObject::clickRight(tribool down, bool previousState)
  326. {
  327. }
  328. void CIntObject::activateHover()
  329. {
  330. GH.hoverable.push_front(this);
  331. active |= HOVER;
  332. }
  333. void CIntObject::deactivateHover()
  334. {
  335. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  336. assert(hlp != GH.hoverable.end());
  337. GH.hoverable.erase(hlp);
  338. active &= ~HOVER;
  339. }
  340. void CIntObject::hover( bool on )
  341. {
  342. }
  343. void CIntObject::activateKeys()
  344. {
  345. GH.keyinterested.push_front(this);
  346. active |= KEYBOARD;
  347. }
  348. void CIntObject::deactivateKeys()
  349. {
  350. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  351. assert(hlp != GH.keyinterested.end());
  352. GH.keyinterested.erase(hlp);
  353. active &= ~KEYBOARD;
  354. }
  355. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  356. {
  357. }
  358. void CIntObject::activateMouseMove()
  359. {
  360. GH.motioninterested.push_front(this);
  361. active |= MOVE;
  362. }
  363. void CIntObject::deactivateMouseMove()
  364. {
  365. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  366. assert(hlp != GH.motioninterested.end());
  367. GH.motioninterested.erase(hlp);
  368. active &= ~MOVE;
  369. }
  370. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  371. {
  372. }
  373. void CIntObject::activateTimer()
  374. {
  375. GH.timeinterested.push_back(this);
  376. active |= TIME;
  377. }
  378. void CIntObject::deactivateTimer()
  379. {
  380. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  381. assert(hlp != GH.timeinterested.end());
  382. GH.timeinterested.erase(hlp);
  383. active &= ~TIME;
  384. }
  385. void CIntObject::tick()
  386. {
  387. }
  388. CIntObject::CIntObject()
  389. {
  390. pressedL = pressedR = hovered = captureAllKeys = strongInterest = toNextTick = active = used = 0;
  391. recActions = defActions = GH.defActionsDef;
  392. pos.x = 0;
  393. pos.y = 0;
  394. pos.w = 0;
  395. pos.h = 0;
  396. if(GH.captureChildren)
  397. {
  398. assert(GH.createdObj.size());
  399. parent = GH.createdObj.front();
  400. parent->children.push_back(this);
  401. if(parent->defActions & SHARE_POS)
  402. {
  403. pos.x = parent->pos.x;
  404. pos.y = parent->pos.y;
  405. }
  406. }
  407. }
  408. void CIntObject::show( SDL_Surface * to )
  409. {
  410. if(defActions & UPDATE)
  411. for(size_t i = 0; i < children.size(); i++)
  412. if(children[i]->recActions & UPDATE)
  413. children[i]->show(to);
  414. }
  415. void CIntObject::showAll( SDL_Surface * to )
  416. {
  417. if(defActions & SHOWALL)
  418. {
  419. for(size_t i = 0; i < children.size(); i++)
  420. if(children[i]->recActions & SHOWALL)
  421. children[i]->showAll(to);
  422. }
  423. else
  424. show(to);
  425. }
  426. void CIntObject::activate()
  427. {
  428. assert(!active);
  429. active |= GENERAL;
  430. if(used & LCLICK)
  431. activateLClick();
  432. if(used & RCLICK)
  433. activateRClick();
  434. if(used & HOVER)
  435. activateHover();
  436. if(used & MOVE)
  437. activateMouseMove();
  438. if(used & KEYBOARD)
  439. activateKeys();
  440. if(used & TIME)
  441. activateTimer();
  442. if(used & WHEEL)
  443. activateWheel();
  444. if(used & DOUBLECLICK)
  445. activateDClick();
  446. if(defActions & ACTIVATE)
  447. for(size_t i = 0; i < children.size(); i++)
  448. if(children[i]->recActions & ACTIVATE)
  449. children[i]->activate();
  450. }
  451. void CIntObject::deactivate()
  452. {
  453. assert(active);
  454. active &= ~ GENERAL;
  455. if(used & LCLICK)
  456. deactivateLClick();
  457. if(used & RCLICK)
  458. deactivateRClick();
  459. if(used & HOVER)
  460. deactivateHover();
  461. if(used & MOVE)
  462. deactivateMouseMove();
  463. if(used & KEYBOARD)
  464. deactivateKeys();
  465. if(used & TIME)
  466. deactivateTimer();
  467. if(used & WHEEL)
  468. deactivateWheel();
  469. if(used & DOUBLECLICK)
  470. deactivateDClick();
  471. if(defActions & DEACTIVATE)
  472. for(size_t i = 0; i < children.size(); i++)
  473. if(children[i]->recActions & DEACTIVATE)
  474. children[i]->deactivate();
  475. }
  476. CIntObject::~CIntObject()
  477. {
  478. assert(!active); //do not delete active obj
  479. if(defActions & DISPOSE)
  480. for(size_t i = 0; i < children.size(); i++)
  481. if(children[i]->recActions & DISPOSE)
  482. delete children[i];
  483. }
  484. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  485. {
  486. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  487. }
  488. void CIntObject::printAtMiddleLoc( 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::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  491. }
  492. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  493. {
  494. blitAt(src, pos.x + x, pos.y + y, dst);
  495. }
  496. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refrsh /*= false*/ )
  497. {
  498. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst, refrsh);
  499. }
  500. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst, bool refresh /*= false*/ )
  501. {
  502. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst, refresh);
  503. }
  504. void CIntObject::disable()
  505. {
  506. if(active)
  507. deactivate();
  508. recActions = DISPOSE;
  509. }
  510. void CIntObject::enable(bool activation)
  511. {
  512. if(!active && activation)
  513. activate();
  514. recActions = 255;
  515. }
  516. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  517. {
  518. return isItIn(&rect, x - pos.x, y - pos.y);
  519. }
  520. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  521. {
  522. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  523. }
  524. void CIntObject::activateWheel()
  525. {
  526. GH.wheelInterested.push_front(this);
  527. active |= WHEEL;
  528. }
  529. void CIntObject::deactivateWheel()
  530. {
  531. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  532. assert(hlp != GH.wheelInterested.end());
  533. GH.wheelInterested.erase(hlp);
  534. active &= ~WHEEL;
  535. }
  536. void CIntObject::wheelScrolled(bool down, bool in)
  537. {
  538. }
  539. void CIntObject::activateDClick()
  540. {
  541. GH.doubleClickInterested.push_front(this);
  542. active |= DOUBLECLICK;
  543. }
  544. void CIntObject::deactivateDClick()
  545. {
  546. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  547. assert(hlp != GH.doubleClickInterested.end());
  548. GH.doubleClickInterested.erase(hlp);
  549. active &= ~DOUBLECLICK;
  550. }
  551. void CIntObject::onDoubleClick()
  552. {
  553. }
  554. const Rect & CIntObject::center( const Rect &r )
  555. {
  556. pos.w = r.w;
  557. pos.h = r.h;
  558. pos.x = screen->w/2 - r.w/2;
  559. pos.y = screen->h/2 - r.h/2;
  560. return pos;
  561. }
  562. const Rect & CIntObject::center()
  563. {
  564. return center(pos);
  565. }
  566. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  567. {
  568. pos.x += p.x;
  569. pos.y += p.y;
  570. if(propagate)
  571. for(size_t i = 0; i < children.size(); i++)
  572. children[i]->moveBy(p, propagate);
  573. }
  574. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  575. {
  576. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  577. }
  578. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  579. {
  580. bg = BG;
  581. freeSurf = Free;
  582. pos.x += x;
  583. pos.y += y;
  584. pos.w = BG->w;
  585. pos.h = BG->h;
  586. }
  587. CPicture::CPicture( const std::string &bmpname, int x, int y )
  588. {
  589. bg = BitmapHandler::loadBitmap(bmpname);
  590. freeSurf = true;;
  591. pos.x += x;
  592. pos.y += y;
  593. if(bg)
  594. {
  595. pos.w = bg->w;
  596. pos.h = bg->h;
  597. }
  598. else
  599. {
  600. pos.w = pos.h = 0;
  601. }
  602. }
  603. CPicture::~CPicture()
  604. {
  605. if(freeSurf)
  606. SDL_FreeSurface(bg);
  607. }
  608. void CPicture::showAll( SDL_Surface * to )
  609. {
  610. if(bg)
  611. blitAt(bg, pos, to);
  612. }
  613. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  614. :myObj(obj)
  615. {
  616. GH.createdObj.push_front(obj);
  617. GH.captureChildren = true;
  618. }
  619. ObjectConstruction::~ObjectConstruction()
  620. {
  621. assert(GH.createdObj.size());
  622. assert(GH.createdObj.front() == myObj);
  623. GH.createdObj.pop_front();
  624. GH.captureChildren = GH.createdObj.size();
  625. }
  626. SetCaptureState::SetCaptureState(bool allow, ui8 actions)
  627. {
  628. previousCapture = GH.captureChildren;
  629. GH.captureChildren = false;
  630. prevActions = GH.defActionsDef;
  631. GH.defActionsDef = actions;
  632. }
  633. SetCaptureState::~SetCaptureState()
  634. {
  635. GH.captureChildren = previousCapture;
  636. GH.defActionsDef = prevActions;
  637. }
  638. void IShowable::redraw()
  639. {
  640. showAll(screenBuf);
  641. if(screenBuf != screen)
  642. showAll(screen);
  643. }
  644. SDLKey arrowToNum( SDLKey key )
  645. {
  646. switch(key)
  647. {
  648. case SDLK_DOWN:
  649. return SDLK_KP2;
  650. case SDLK_UP:
  651. return SDLK_KP8;
  652. case SDLK_LEFT:
  653. return SDLK_KP4;
  654. case SDLK_RIGHT:
  655. return SDLK_KP6;
  656. default:
  657. assert(0);
  658. }
  659. throw std::string("Wrong key!");
  660. }
  661. SDLKey numToDigit( SDLKey key )
  662. {
  663. return SDLKey(key - SDLK_KP0 + SDLK_0);
  664. }
  665. bool isNumKey( SDLKey key, bool number )
  666. {
  667. if(number)
  668. return key >= SDLK_KP0 && key <= SDLK_KP9;
  669. else
  670. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  671. }
  672. bool isArrowKey( SDLKey key )
  673. {
  674. return key >= SDLK_UP && key <= SDLK_LEFT;
  675. }