CIntObject.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * CIntObject.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CIntObject.h"
  12. #include "CGuiHandler.h"
  13. #include "../renderSDL/SDL_Extensions.h"
  14. #include "../windows/CMessage.h"
  15. #include "../CMT.h"
  16. #include <SDL_pixels.h>
  17. IShowActivatable::IShowActivatable()
  18. {
  19. type = 0;
  20. }
  21. CIntObject::CIntObject(int used_, Point pos_):
  22. parent_m(nullptr),
  23. active_m(0),
  24. parent(parent_m),
  25. active(active_m)
  26. {
  27. hovered = captureAllKeys = strongInterest = false;
  28. toNextTick = timerDelay = 0;
  29. used = used_;
  30. recActions = defActions = GH.defActionsDef;
  31. pos.x = pos_.x;
  32. pos.y = pos_.y;
  33. pos.w = 0;
  34. pos.h = 0;
  35. if(GH.captureChildren)
  36. GH.createdObj.front()->addChild(this, true);
  37. }
  38. CIntObject::~CIntObject()
  39. {
  40. if(active_m)
  41. deactivate();
  42. while(!children.empty())
  43. {
  44. if((defActions & DISPOSE) && (children.front()->recActions & DISPOSE))
  45. delete children.front();
  46. else
  47. removeChild(children.front());
  48. }
  49. if(parent_m)
  50. parent_m->removeChild(this);
  51. }
  52. void CIntObject::setTimer(int msToTrigger)
  53. {
  54. if (!(active & TIME))
  55. activate(TIME);
  56. toNextTick = timerDelay = msToTrigger;
  57. used |= TIME;
  58. }
  59. void CIntObject::onTimer(int timePassed)
  60. {
  61. toNextTick -= timePassed;
  62. if (toNextTick < 0)
  63. {
  64. toNextTick += timerDelay;
  65. tick();
  66. }
  67. }
  68. void CIntObject::show(SDL_Surface * to)
  69. {
  70. if(defActions & UPDATE)
  71. for(auto & elem : children)
  72. if(elem->recActions & UPDATE)
  73. elem->show(to);
  74. }
  75. void CIntObject::showAll(SDL_Surface * to)
  76. {
  77. if(defActions & SHOWALL)
  78. {
  79. for(auto & elem : children)
  80. if(elem->recActions & SHOWALL)
  81. elem->showAll(to);
  82. }
  83. }
  84. void CIntObject::activate()
  85. {
  86. if (active_m)
  87. {
  88. if ((used | GENERAL) == active_m)
  89. return;
  90. else
  91. {
  92. logGlobal->warn("Warning: IntObject re-activated with mismatching used and active");
  93. deactivate(); //FIXME: better to avoid such possibility at all
  94. }
  95. }
  96. active_m |= GENERAL;
  97. activate(used);
  98. if(defActions & ACTIVATE)
  99. for(auto & elem : children)
  100. if(elem->recActions & ACTIVATE)
  101. elem->activate();
  102. }
  103. void CIntObject::activate(ui16 what)
  104. {
  105. GH.handleElementActivate(this, what);
  106. }
  107. void CIntObject::deactivate()
  108. {
  109. if (!active_m)
  110. return;
  111. active_m &= ~ GENERAL;
  112. deactivate(active_m);
  113. assert(!active_m);
  114. if(defActions & DEACTIVATE)
  115. for(auto & elem : children)
  116. if(elem->recActions & DEACTIVATE)
  117. elem->deactivate();
  118. }
  119. void CIntObject::deactivate(ui16 what)
  120. {
  121. GH.handleElementDeActivate(this, what);
  122. }
  123. void CIntObject::click(MouseButton btn, tribool down, bool previousState)
  124. {
  125. switch(btn)
  126. {
  127. default:
  128. case MouseButton::LEFT:
  129. clickLeft(down, previousState);
  130. break;
  131. case MouseButton::MIDDLE:
  132. clickMiddle(down, previousState);
  133. break;
  134. case MouseButton::RIGHT:
  135. clickRight(down, previousState);
  136. break;
  137. }
  138. }
  139. void CIntObject::printAtLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  140. {
  141. graphics->fonts[font]->renderTextLeft(dst, text, kolor, Point(pos.x + x, pos.y + y));
  142. }
  143. void CIntObject::printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  144. {
  145. printAtMiddleLoc(text, Point(x,y), font, kolor, dst);
  146. }
  147. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  148. {
  149. graphics->fonts[font]->renderTextCenter(dst, text, kolor, pos.topLeft() + p);
  150. }
  151. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  152. {
  153. CSDL_Ext::blitAt(src, pos.x + x, pos.y + y, dst);
  154. }
  155. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  156. {
  157. blitAtLoc(src, p.x, p.y, dst);
  158. }
  159. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  160. {
  161. graphics->fonts[font]->renderTextLinesCenter(dst, CMessage::breakText(text, charpr, font), kolor, Point(pos.x + x, pos.y + y));
  162. }
  163. void CIntObject::addUsedEvents(ui16 newActions)
  164. {
  165. if (active_m)
  166. activate(~used & newActions);
  167. used |= newActions;
  168. }
  169. void CIntObject::removeUsedEvents(ui16 newActions)
  170. {
  171. if (active_m)
  172. deactivate(used & newActions);
  173. used &= ~newActions;
  174. }
  175. void CIntObject::disable()
  176. {
  177. if(active)
  178. deactivate();
  179. recActions = DISPOSE;
  180. }
  181. void CIntObject::enable()
  182. {
  183. if(!active_m && (!parent_m || parent_m->active))
  184. activate();
  185. recActions = 255;
  186. }
  187. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  188. {
  189. Point newPos = pos.topLeft();
  190. vstd::amax(newPos.x, borderWidth);
  191. vstd::amax(newPos.y, borderWidth);
  192. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  193. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  194. if (newPos != pos.topLeft())
  195. moveTo(newPos, propagate);
  196. }
  197. void CIntObject::moveBy(const Point & p, bool propagate)
  198. {
  199. pos.x += p.x;
  200. pos.y += p.y;
  201. if(propagate)
  202. for(auto & elem : children)
  203. elem->moveBy(p, propagate);
  204. }
  205. void CIntObject::moveTo(const Point & p, bool propagate)
  206. {
  207. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  208. }
  209. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  210. {
  211. if (vstd::contains(children, child))
  212. {
  213. return;
  214. }
  215. if (child->parent_m)
  216. {
  217. child->parent_m->removeChild(child, adjustPosition);
  218. }
  219. children.push_back(child);
  220. child->parent_m = this;
  221. if(adjustPosition)
  222. child->pos += pos.topLeft();
  223. if (!active && child->active)
  224. child->deactivate();
  225. if (active && !child->active)
  226. child->activate();
  227. }
  228. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  229. {
  230. if (!child)
  231. return;
  232. if(!vstd::contains(children, child))
  233. throw std::runtime_error("Wrong child object");
  234. if(child->parent_m != this)
  235. throw std::runtime_error("Wrong child object");
  236. children -= child;
  237. child->parent_m = nullptr;
  238. if(adjustPosition)
  239. child->pos -= pos.topLeft();
  240. }
  241. void CIntObject::redraw()
  242. {
  243. //currently most of calls come from active objects so this check won't affect them
  244. //it should fix glitches when called by inactive elements located below active window
  245. if (active)
  246. {
  247. if (parent_m && (type & REDRAW_PARENT))
  248. {
  249. parent_m->redraw();
  250. }
  251. else
  252. {
  253. showAll(screenBuf);
  254. if(screenBuf != screen)
  255. showAll(screen);
  256. }
  257. }
  258. }
  259. const Rect & CIntObject::center( const Rect &r, bool propagate )
  260. {
  261. pos.w = r.w;
  262. pos.h = r.h;
  263. return center(Point(GH.screenDimensions().x/2, GH.screenDimensions().y/2), propagate);
  264. }
  265. const Rect & CIntObject::center( bool propagate )
  266. {
  267. return center(pos, propagate);
  268. }
  269. const Rect & CIntObject::center(const Point & p, bool propagate)
  270. {
  271. moveBy(Point(p.x - pos.w/2 - pos.x,
  272. p.y - pos.h/2 - pos.y),
  273. propagate);
  274. return pos;
  275. }
  276. bool CIntObject::captureThisKey(const SDL_Keycode & key)
  277. {
  278. return captureAllKeys;
  279. }
  280. CKeyShortcut::CKeyShortcut()
  281. {}
  282. CKeyShortcut::CKeyShortcut(int key)
  283. {
  284. if (key != SDLK_UNKNOWN)
  285. assignedKeys.insert(key);
  286. }
  287. CKeyShortcut::CKeyShortcut(std::set<int> Keys)
  288. :assignedKeys(Keys)
  289. {}
  290. void CKeyShortcut::keyPressed(const SDL_Keycode & key)
  291. {
  292. if(vstd::contains(assignedKeys,key)
  293. || vstd::contains(assignedKeys, CGuiHandler::numToDigit(key)))
  294. {
  295. bool prev = mouseState(MouseButton::LEFT);
  296. updateMouseState(MouseButton::LEFT, true);
  297. clickLeft(true, prev);
  298. }
  299. }
  300. void CKeyShortcut::keyReleased(const SDL_Keycode & key)
  301. {
  302. if(vstd::contains(assignedKeys,key)
  303. || vstd::contains(assignedKeys, CGuiHandler::numToDigit(key)))
  304. {
  305. bool prev = mouseState(MouseButton::LEFT);
  306. updateMouseState(MouseButton::LEFT, false);
  307. clickLeft(false, prev);
  308. }
  309. }
  310. WindowBase::WindowBase(int used_, Point pos_)
  311. : CIntObject(used_, pos_)
  312. {
  313. }
  314. void WindowBase::close()
  315. {
  316. if(GH.topInt().get() != this)
  317. logGlobal->error("Only top interface must be closed");
  318. GH.popInts(1);
  319. }
  320. IStatusBar::~IStatusBar()
  321. {}