GUIBase.cpp 24 KB

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