CIntObject.cpp 8.2 KB

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