CIntObject.cpp 8.2 KB

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