GUIBase.cpp 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. if (conf.cc.fullscreen)
  320. CCS->curh->centerCursor();
  321. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  322. while(!terminate)
  323. {
  324. if(curInt)
  325. curInt->update(); // calls a update and drawing process of the loaded game interface object at the moment
  326. mainFPSmng->framerateDelay(); // holds a constant FPS
  327. }
  328. } HANDLE_EXCEPTION
  329. }
  330. CGuiHandler::CGuiHandler()
  331. :lastClick(-500, -500)
  332. {
  333. curInt = NULL;
  334. current = NULL;
  335. terminate = false;
  336. statusbar = NULL;
  337. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  338. mainFPSmng = new FPSManager(48);
  339. mainFPSmng->init(); // resets internal clock, needed for FPS manager
  340. }
  341. CGuiHandler::~CGuiHandler()
  342. {
  343. delete mainFPSmng;
  344. }
  345. void CGuiHandler::breakEventHandling()
  346. {
  347. current = NULL;
  348. }
  349. void CGuiHandler::drawFPSCounter()
  350. {
  351. const static SDL_Color yellow = {255, 255, 0, 0};
  352. static SDL_Rect overlay = { 0, 0, 64, 32};
  353. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  354. SDL_FillRect(screen, &overlay, black);
  355. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  356. CSDL_Ext::printAt(fps, 10, 10, FONT_BIG, yellow, screen);
  357. }
  358. void CIntObject::activateLClick()
  359. {
  360. GH.lclickable.push_front(this);
  361. active |= LCLICK;
  362. }
  363. void CIntObject::deactivateLClick()
  364. {
  365. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  366. assert(hlp != GH.lclickable.end());
  367. GH.lclickable.erase(hlp);
  368. active &= ~LCLICK;
  369. }
  370. void CIntObject::clickLeft(tribool down, bool previousState)
  371. {
  372. }
  373. void CIntObject::activateRClick()
  374. {
  375. GH.rclickable.push_front(this);
  376. active |= RCLICK;
  377. }
  378. void CIntObject::deactivateRClick()
  379. {
  380. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  381. assert(hlp != GH.rclickable.end());
  382. GH.rclickable.erase(hlp);
  383. active &= ~RCLICK;
  384. }
  385. void CIntObject::clickRight(tribool down, bool previousState)
  386. {
  387. }
  388. void CIntObject::activateHover()
  389. {
  390. GH.hoverable.push_front(this);
  391. active |= HOVER;
  392. }
  393. void CIntObject::deactivateHover()
  394. {
  395. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  396. assert(hlp != GH.hoverable.end());
  397. GH.hoverable.erase(hlp);
  398. active &= ~HOVER;
  399. }
  400. void CIntObject::hover( bool on )
  401. {
  402. }
  403. void CIntObject::activateKeys()
  404. {
  405. GH.keyinterested.push_front(this);
  406. active |= KEYBOARD;
  407. }
  408. void CIntObject::deactivateKeys()
  409. {
  410. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  411. assert(hlp != GH.keyinterested.end());
  412. GH.keyinterested.erase(hlp);
  413. active &= ~KEYBOARD;
  414. }
  415. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  416. {
  417. }
  418. void CIntObject::activateMouseMove()
  419. {
  420. GH.motioninterested.push_front(this);
  421. active |= MOVE;
  422. }
  423. void CIntObject::deactivateMouseMove()
  424. {
  425. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  426. assert(hlp != GH.motioninterested.end());
  427. GH.motioninterested.erase(hlp);
  428. active &= ~MOVE;
  429. }
  430. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  431. {
  432. }
  433. void CIntObject::activateTimer()
  434. {
  435. GH.timeinterested.push_back(this);
  436. active |= TIME;
  437. }
  438. void CIntObject::deactivateTimer()
  439. {
  440. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  441. assert(hlp != GH.timeinterested.end());
  442. GH.timeinterested.erase(hlp);
  443. active &= ~TIME;
  444. }
  445. void CIntObject::tick()
  446. {
  447. }
  448. CIntObject::CIntObject()
  449. {
  450. pressedL = pressedR = hovered = captureAllKeys = strongInterest = false;
  451. toNextTick = active = used = 0;
  452. recActions = defActions = GH.defActionsDef;
  453. pos.x = 0;
  454. pos.y = 0;
  455. pos.w = 0;
  456. pos.h = 0;
  457. if(GH.captureChildren)
  458. {
  459. assert(GH.createdObj.size());
  460. parent = GH.createdObj.front();
  461. parent->children.push_back(this);
  462. if(parent->defActions & SHARE_POS)
  463. {
  464. pos.x = parent->pos.x;
  465. pos.y = parent->pos.y;
  466. }
  467. }
  468. else
  469. parent = NULL;
  470. }
  471. void CIntObject::show( SDL_Surface * to )
  472. {
  473. if(defActions & UPDATE)
  474. for(size_t i = 0; i < children.size(); i++)
  475. if(children[i]->recActions & UPDATE)
  476. children[i]->show(to);
  477. }
  478. void CIntObject::showAll( SDL_Surface * to )
  479. {
  480. if(defActions & SHOWALL)
  481. {
  482. for(size_t i = 0; i < children.size(); i++)
  483. if(children[i]->recActions & SHOWALL)
  484. children[i]->showAll(to);
  485. }
  486. else
  487. show(to);
  488. }
  489. void CIntObject::activate()
  490. {
  491. assert(!active);
  492. active |= GENERAL;
  493. activate(used);
  494. if(defActions & ACTIVATE)
  495. for(size_t i = 0; i < children.size(); i++)
  496. if(children[i]->recActions & ACTIVATE)
  497. children[i]->activate();
  498. }
  499. void CIntObject::activate(ui16 what)
  500. {
  501. if(what & LCLICK)
  502. activateLClick();
  503. if(what & RCLICK)
  504. activateRClick();
  505. if(what & HOVER)
  506. activateHover();
  507. if(what & MOVE)
  508. activateMouseMove();
  509. if(what & KEYBOARD)
  510. activateKeys();
  511. if(what & TIME)
  512. activateTimer();
  513. if(what & WHEEL)
  514. activateWheel();
  515. if(what & DOUBLECLICK)
  516. activateDClick();
  517. }
  518. void CIntObject::deactivate()
  519. {
  520. assert(active);
  521. active &= ~ GENERAL;
  522. deactivate(used);
  523. assert(!active);
  524. if(defActions & DEACTIVATE)
  525. for(size_t i = 0; i < children.size(); i++)
  526. if(children[i]->recActions & DEACTIVATE)
  527. children[i]->deactivate();
  528. }
  529. void CIntObject::deactivate(ui16 what)
  530. {
  531. if(what & LCLICK)
  532. deactivateLClick();
  533. if(what & RCLICK)
  534. deactivateRClick();
  535. if(what & HOVER)
  536. deactivateHover();
  537. if(what & MOVE)
  538. deactivateMouseMove();
  539. if(what & KEYBOARD)
  540. deactivateKeys();
  541. if(what & TIME) // TIME is special
  542. deactivateTimer();
  543. if(what & WHEEL)
  544. deactivateWheel();
  545. if(what & DOUBLECLICK)
  546. deactivateDClick();
  547. }
  548. CIntObject::~CIntObject()
  549. {
  550. assert(!active); //do not delete active obj
  551. if(defActions & DISPOSE)
  552. for(size_t i = 0; i < children.size(); i++)
  553. if(children[i]->recActions & DISPOSE)
  554. delete children[i];
  555. if(parent && GH.createdObj.size()) //temporary object destroyed
  556. parent->children -= this;
  557. }
  558. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  559. {
  560. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst);
  561. }
  562. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  563. {
  564. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst);
  565. }
  566. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  567. {
  568. printAtMiddleLoc(text, p.x, p.y, font, kolor, dst);
  569. }
  570. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  571. {
  572. blitAt(src, pos.x + x, pos.y + y, dst);
  573. }
  574. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  575. {
  576. blitAtLoc(src, p.x, p.y, dst);
  577. }
  578. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  579. {
  580. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst);
  581. }
  582. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst )
  583. {
  584. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst);
  585. }
  586. void CIntObject::disable()
  587. {
  588. if(active)
  589. deactivate();
  590. recActions = DISPOSE;
  591. }
  592. void CIntObject::enable(bool activation)
  593. {
  594. if(!active && activation)
  595. activate();
  596. recActions = 255;
  597. }
  598. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  599. {
  600. return isItIn(&rect, x - pos.x, y - pos.y);
  601. }
  602. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  603. {
  604. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  605. }
  606. void CIntObject::activateWheel()
  607. {
  608. GH.wheelInterested.push_front(this);
  609. active |= WHEEL;
  610. }
  611. void CIntObject::deactivateWheel()
  612. {
  613. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  614. assert(hlp != GH.wheelInterested.end());
  615. GH.wheelInterested.erase(hlp);
  616. active &= ~WHEEL;
  617. }
  618. void CIntObject::wheelScrolled(bool down, bool in)
  619. {
  620. }
  621. void CIntObject::activateDClick()
  622. {
  623. GH.doubleClickInterested.push_front(this);
  624. active |= DOUBLECLICK;
  625. }
  626. void CIntObject::deactivateDClick()
  627. {
  628. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  629. assert(hlp != GH.doubleClickInterested.end());
  630. GH.doubleClickInterested.erase(hlp);
  631. active &= ~DOUBLECLICK;
  632. }
  633. void CIntObject::onDoubleClick()
  634. {
  635. }
  636. const Rect & CIntObject::center( const Rect &r, bool propagate )
  637. {
  638. pos.w = r.w;
  639. pos.h = r.h;
  640. return center(Point(screen->w/2, screen->h/2), propagate);
  641. }
  642. const Rect & CIntObject::center( bool propagate )
  643. {
  644. return center(pos, propagate);
  645. }
  646. const Rect & CIntObject::center(const Point &p, bool propagate /*= true*/)
  647. {
  648. moveBy(Point(p.x - pos.w/2 - pos.x,
  649. p.y - pos.h/2 - pos.y),
  650. propagate);
  651. return pos;
  652. }
  653. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  654. {
  655. Point newPos = pos.topLeft();
  656. amax(newPos.x, borderWidth);
  657. amax(newPos.y, borderWidth);
  658. amin(newPos.x, screen->w - borderWidth - pos.w);
  659. amin(newPos.y, screen->h - borderWidth - pos.h);
  660. if (newPos != pos.topLeft())
  661. moveTo(newPos, propagate);
  662. }
  663. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  664. {
  665. pos.x += p.x;
  666. pos.y += p.y;
  667. if(propagate)
  668. for(size_t i = 0; i < children.size(); i++)
  669. children[i]->moveBy(p, propagate);
  670. }
  671. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  672. {
  673. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  674. }
  675. void CIntObject::delChild(CIntObject *child)
  676. {
  677. children -= child;
  678. delete child;
  679. }
  680. void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
  681. {
  682. assert(!vstd::contains(children, child));
  683. assert(child->parent == NULL);
  684. children.push_back(child);
  685. child->parent = this;
  686. if(adjustPosition)
  687. child->pos += pos;
  688. }
  689. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  690. {
  691. assert(vstd::contains(children, child));
  692. assert(child->parent == this);
  693. children -= child;
  694. child->parent = NULL;
  695. if(adjustPosition)
  696. child->pos -= pos;
  697. }
  698. void CIntObject::changeUsedEvents(ui16 what, bool enable, bool adjust /*= true*/)
  699. {
  700. if(enable)
  701. {
  702. used |= what;
  703. if(adjust && active)
  704. activate(what);
  705. }
  706. else
  707. {
  708. used &= ~what;
  709. if(adjust && active)
  710. deactivate(what);
  711. }
  712. }
  713. void CIntObject::drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color)
  714. {
  715. CSDL_Ext::drawBorder(sur, r + pos, color);
  716. }
  717. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  718. {
  719. init();
  720. bg = BG;
  721. freeSurf = Free;
  722. pos.x += x;
  723. pos.y += y;
  724. pos.w = BG->w;
  725. pos.h = BG->h;
  726. }
  727. CPicture::CPicture( const std::string &bmpname, int x, int y )
  728. {
  729. init();
  730. bg = BitmapHandler::loadBitmap(bmpname);
  731. freeSurf = true;;
  732. pos.x += x;
  733. pos.y += y;
  734. if(bg)
  735. {
  736. pos.w = bg->w;
  737. pos.h = bg->h;
  738. }
  739. else
  740. {
  741. pos.w = pos.h = 0;
  742. }
  743. }
  744. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  745. {
  746. init();
  747. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  748. }
  749. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  750. {
  751. init();
  752. createSimpleRect(r, screenFormat, color);
  753. }
  754. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  755. {
  756. needRefresh = false;
  757. srcRect = new Rect(SrcRect);
  758. pos.x += x;
  759. pos.y += y;
  760. pos.w = srcRect->w;
  761. pos.h = srcRect->h;
  762. bg = BG;
  763. freeSurf = free;
  764. }
  765. CPicture::~CPicture()
  766. {
  767. if(freeSurf)
  768. SDL_FreeSurface(bg);
  769. delete srcRect;
  770. }
  771. void CPicture::init()
  772. {
  773. needRefresh = false;
  774. srcRect = NULL;
  775. }
  776. void CPicture::show( SDL_Surface * to )
  777. {
  778. if (needRefresh)
  779. showAll(to);
  780. }
  781. void CPicture::showAll( SDL_Surface * to )
  782. {
  783. if(bg)
  784. {
  785. if(srcRect)
  786. {
  787. SDL_Rect srcRectCpy = *srcRect;
  788. SDL_Rect dstRect = srcRectCpy;
  789. dstRect.x = pos.x;
  790. dstRect.y = pos.y;
  791. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  792. }
  793. else
  794. blitAt(bg, pos, to);
  795. }
  796. }
  797. void CPicture::convertToScreenBPP()
  798. {
  799. SDL_Surface *hlp = bg;
  800. bg = SDL_ConvertSurface(hlp,screen->format,0);
  801. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  802. SDL_FreeSurface(hlp);
  803. }
  804. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  805. {
  806. pos += r;
  807. pos.w = r.w;
  808. pos.h = r.h;
  809. if(screenFormat)
  810. bg = CSDL_Ext::newSurface(r.w, r.h);
  811. else
  812. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  813. SDL_FillRect(bg, NULL, color);
  814. freeSurf = true;
  815. }
  816. void CPicture::colorizeAndConvert(int player)
  817. {
  818. assert(bg);
  819. colorize(player);
  820. convertToScreenBPP();
  821. }
  822. void CPicture::colorize(int player)
  823. {
  824. assert(bg);
  825. assert(bg->format->BitsPerPixel == 8);
  826. graphics->blueToPlayersAdv(bg, player);
  827. }
  828. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  829. :myObj(obj)
  830. {
  831. GH.createdObj.push_front(obj);
  832. GH.captureChildren = true;
  833. }
  834. ObjectConstruction::~ObjectConstruction()
  835. {
  836. assert(GH.createdObj.size());
  837. assert(GH.createdObj.front() == myObj);
  838. GH.createdObj.pop_front();
  839. GH.captureChildren = GH.createdObj.size();
  840. }
  841. SetCaptureState::SetCaptureState(bool allow, ui8 actions)
  842. {
  843. previousCapture = GH.captureChildren;
  844. GH.captureChildren = false;
  845. prevActions = GH.defActionsDef;
  846. GH.defActionsDef = actions;
  847. }
  848. SetCaptureState::~SetCaptureState()
  849. {
  850. GH.captureChildren = previousCapture;
  851. GH.defActionsDef = prevActions;
  852. }
  853. void CIntObject::redraw()
  854. {
  855. if (parent && (type & REDRAW_PARENT))
  856. {
  857. parent->redraw();
  858. }
  859. else
  860. {
  861. showAll(screenBuf);
  862. if(screenBuf != screen)
  863. showAll(screen);
  864. }
  865. }
  866. SDLKey arrowToNum( SDLKey key )
  867. {
  868. switch(key)
  869. {
  870. case SDLK_DOWN:
  871. return SDLK_KP2;
  872. case SDLK_UP:
  873. return SDLK_KP8;
  874. case SDLK_LEFT:
  875. return SDLK_KP4;
  876. case SDLK_RIGHT:
  877. return SDLK_KP6;
  878. default:
  879. assert(0);
  880. }
  881. throw std::string("Wrong key!");
  882. }
  883. SDLKey numToDigit( SDLKey key )
  884. {
  885. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  886. return SDLKey(key - SDLK_KP0 + SDLK_0);
  887. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  888. switch(key)
  889. {
  890. REMOVE_KP(PERIOD)
  891. REMOVE_KP(MINUS)
  892. REMOVE_KP(PLUS)
  893. REMOVE_KP(EQUALS)
  894. case SDLK_KP_MULTIPLY:
  895. return SDLK_ASTERISK;
  896. case SDLK_KP_DIVIDE:
  897. return SDLK_SLASH;
  898. case SDLK_KP_ENTER:
  899. return SDLK_RETURN;
  900. default:
  901. tlog3 << "Illegal numkey conversion!" << std::endl;
  902. return SDLK_UNKNOWN;
  903. }
  904. #undef REMOVE_KP
  905. }
  906. bool isNumKey( SDLKey key, bool number )
  907. {
  908. if(number)
  909. return key >= SDLK_KP0 && key <= SDLK_KP9;
  910. else
  911. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  912. }
  913. bool isArrowKey( SDLKey key )
  914. {
  915. return key >= SDLK_UP && key <= SDLK_LEFT;
  916. }
  917. CIntObject * moveChild(CIntObject *obj, CIntObject *from, CIntObject *to, bool adjustPos)
  918. {
  919. from->removeChild(obj, adjustPos);
  920. to->addChild(obj, adjustPos);
  921. return obj;
  922. }
  923. Rect Rect::createCentered( int w, int h )
  924. {
  925. return Rect(screen->w/2 - w/2, screen->h/2 - h/2, w, h);
  926. }
  927. Rect Rect::around(const Rect &r, int width /*= 1*/) /*creates rect around another */
  928. {
  929. return Rect(r.x - width, r.y - width, r.w + width * 2, r.h + width * 2);
  930. }
  931. Rect Rect::centerIn(const Rect &r)
  932. {
  933. return Rect(r.x + (r.w - w) / 2, r.y + (r.h - h) / 2, w, h);
  934. }