GUIBase.cpp 22 KB

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