GUIBase.cpp 24 KB

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