GUIBase.cpp 23 KB

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