CIntObject.cpp 7.8 KB

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