CIntObject.cpp 8.3 KB

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