GUIBase.cpp 24 KB

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