GUIBase.cpp 19 KB

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