GUIBase.cpp 20 KB

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