GUIBase.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 = 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. moveBy(Point(screen->w/2 - r.w/2 - pos.x,
  641. screen->h/2 - r.h/2 - pos.y),
  642. propagate);
  643. return pos;
  644. }
  645. const Rect & CIntObject::center( bool propagate )
  646. {
  647. return center(pos, propagate);
  648. }
  649. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  650. {
  651. pos.x += p.x;
  652. pos.y += p.y;
  653. if(propagate)
  654. for(size_t i = 0; i < children.size(); i++)
  655. children[i]->moveBy(p, propagate);
  656. }
  657. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  658. {
  659. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  660. }
  661. void CIntObject::delChild(CIntObject *child)
  662. {
  663. children -= child;
  664. delete child;
  665. }
  666. void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
  667. {
  668. assert(!vstd::contains(children, child));
  669. assert(child->parent == NULL);
  670. children.push_back(child);
  671. child->parent = this;
  672. if(adjustPosition)
  673. child->pos += pos;
  674. }
  675. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  676. {
  677. assert(vstd::contains(children, child));
  678. assert(child->parent == this);
  679. children -= child;
  680. child->parent = NULL;
  681. if(adjustPosition)
  682. child->pos -= pos;
  683. }
  684. void CIntObject::changeUsedEvents(ui16 what, bool enable, bool adjust /*= true*/)
  685. {
  686. if(enable)
  687. {
  688. used |= what;
  689. if(adjust && active)
  690. activate(what);
  691. }
  692. else
  693. {
  694. used &= ~what;
  695. if(adjust && active)
  696. deactivate(what);
  697. }
  698. }
  699. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  700. {
  701. init();
  702. bg = BG;
  703. freeSurf = Free;
  704. pos.x += x;
  705. pos.y += y;
  706. pos.w = BG->w;
  707. pos.h = BG->h;
  708. }
  709. CPicture::CPicture( const std::string &bmpname, int x, int y )
  710. {
  711. init();
  712. bg = BitmapHandler::loadBitmap(bmpname);
  713. freeSurf = true;;
  714. pos.x += x;
  715. pos.y += y;
  716. if(bg)
  717. {
  718. pos.w = bg->w;
  719. pos.h = bg->h;
  720. }
  721. else
  722. {
  723. pos.w = pos.h = 0;
  724. }
  725. }
  726. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  727. {
  728. init();
  729. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  730. }
  731. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  732. {
  733. init();
  734. createSimpleRect(r, screenFormat, color);
  735. }
  736. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  737. {
  738. srcRect = new Rect(SrcRect);
  739. pos.x += x;
  740. pos.y += y;
  741. bg = BG;
  742. freeSurf = free;
  743. }
  744. CPicture::~CPicture()
  745. {
  746. if(freeSurf)
  747. SDL_FreeSurface(bg);
  748. delete srcRect;
  749. }
  750. void CPicture::init()
  751. {
  752. srcRect = NULL;
  753. }
  754. void CPicture::showAll( SDL_Surface * to )
  755. {
  756. if(bg)
  757. {
  758. if(srcRect)
  759. {
  760. SDL_Rect srcRectCpy = *srcRect;
  761. SDL_Rect dstRect = srcRectCpy;
  762. dstRect.x = pos.x;
  763. dstRect.y = pos.y;
  764. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  765. }
  766. else
  767. blitAt(bg, pos, to);
  768. }
  769. }
  770. void CPicture::convertToScreenBPP()
  771. {
  772. SDL_Surface *hlp = bg;
  773. bg = SDL_ConvertSurface(hlp,screen->format,0);
  774. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  775. SDL_FreeSurface(hlp);
  776. }
  777. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  778. {
  779. pos += r;
  780. pos.w = r.w;
  781. pos.h = r.h;
  782. if(screenFormat)
  783. bg = CSDL_Ext::newSurface(r.w, r.h);
  784. else
  785. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  786. SDL_FillRect(bg, NULL, color);
  787. freeSurf = true;
  788. }
  789. void CPicture::colorizeAndConvert(int player)
  790. {
  791. assert(bg);
  792. assert(bg->format->BitsPerPixel == 8);
  793. graphics->blueToPlayersAdv(bg, player);
  794. convertToScreenBPP();
  795. }
  796. ObjectConstruction::ObjectConstruction( CIntObject *obj )
  797. :myObj(obj)
  798. {
  799. GH.createdObj.push_front(obj);
  800. GH.captureChildren = true;
  801. }
  802. ObjectConstruction::~ObjectConstruction()
  803. {
  804. assert(GH.createdObj.size());
  805. assert(GH.createdObj.front() == myObj);
  806. GH.createdObj.pop_front();
  807. GH.captureChildren = GH.createdObj.size();
  808. }
  809. SetCaptureState::SetCaptureState(bool allow, ui8 actions)
  810. {
  811. previousCapture = GH.captureChildren;
  812. GH.captureChildren = false;
  813. prevActions = GH.defActionsDef;
  814. GH.defActionsDef = actions;
  815. }
  816. SetCaptureState::~SetCaptureState()
  817. {
  818. GH.captureChildren = previousCapture;
  819. GH.defActionsDef = prevActions;
  820. }
  821. void IShowable::redraw()
  822. {
  823. showAll(screenBuf);
  824. if(screenBuf != screen)
  825. showAll(screen);
  826. }
  827. SDLKey arrowToNum( SDLKey key )
  828. {
  829. switch(key)
  830. {
  831. case SDLK_DOWN:
  832. return SDLK_KP2;
  833. case SDLK_UP:
  834. return SDLK_KP8;
  835. case SDLK_LEFT:
  836. return SDLK_KP4;
  837. case SDLK_RIGHT:
  838. return SDLK_KP6;
  839. default:
  840. assert(0);
  841. }
  842. throw std::string("Wrong key!");
  843. }
  844. SDLKey numToDigit( SDLKey key )
  845. {
  846. if(key >= SDLK_KP0 && key <= SDLK_KP9)
  847. return SDLKey(key - SDLK_KP0 + SDLK_0);
  848. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  849. switch(key)
  850. {
  851. REMOVE_KP(PERIOD)
  852. REMOVE_KP(MINUS)
  853. REMOVE_KP(PLUS)
  854. REMOVE_KP(EQUALS)
  855. case SDLK_KP_MULTIPLY:
  856. return SDLK_ASTERISK;
  857. case SDLK_KP_DIVIDE:
  858. return SDLK_SLASH;
  859. case SDLK_KP_ENTER:
  860. return SDLK_RETURN;
  861. default:
  862. tlog3 << "Illegal numkey conversion!" << std::endl;
  863. return SDLK_UNKNOWN;
  864. }
  865. #undef REMOVE_KP
  866. }
  867. bool isNumKey( SDLKey key, bool number )
  868. {
  869. if(number)
  870. return key >= SDLK_KP0 && key <= SDLK_KP9;
  871. else
  872. return key >= SDLK_KP0 && key <= SDLK_KP_EQUALS;
  873. }
  874. bool isArrowKey( SDLKey key )
  875. {
  876. return key >= SDLK_UP && key <= SDLK_LEFT;
  877. }
  878. CIntObject * moveChild(CIntObject *obj, CIntObject *from, CIntObject *to, bool adjustPos)
  879. {
  880. from->removeChild(obj, adjustPos);
  881. to->addChild(obj, adjustPos);
  882. return obj;
  883. }
  884. Rect Rect::createCentered( int w, int h )
  885. {
  886. return Rect(screen->w/2 - w/2, screen->h/2 - h/2, w, h);
  887. }
  888. Rect Rect::around(const Rect &r, int width /*= 1*/) /*creates rect around another */
  889. {
  890. return Rect(r.x - width, r.y - width, r.w + width * 2, r.h + width * 2);
  891. }
  892. Rect Rect::centerIn(const Rect &r)
  893. {
  894. return Rect(r.x + (r.w - w) / 2, r.y + (r.h - h) / 2, w, h);
  895. }