GUIBase.cpp 23 KB

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