2
0

CIntObject.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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::printAtMiddleLoc(const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  142. {
  143. printAtMiddleLoc(text, Point(x,y), font, kolor, dst);
  144. }
  145. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  146. {
  147. graphics->fonts[font]->renderTextCenter(dst, text, kolor, pos.topLeft() + p);
  148. }
  149. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  150. {
  151. blitAt(src, pos.x + x, pos.y + y, dst);
  152. }
  153. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  154. {
  155. blitAtLoc(src, p.x, p.y, dst);
  156. }
  157. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  158. {
  159. graphics->fonts[font]->renderTextLinesCenter(dst, CMessage::breakText(text, charpr, font), kolor, Point(pos.x + x, pos.y + y));
  160. }
  161. void CIntObject::addUsedEvents(ui16 newActions)
  162. {
  163. if (active_m)
  164. activate(~used & newActions);
  165. used |= newActions;
  166. }
  167. void CIntObject::removeUsedEvents(ui16 newActions)
  168. {
  169. if (active_m)
  170. deactivate(used & newActions);
  171. used &= ~newActions;
  172. }
  173. void CIntObject::disable()
  174. {
  175. if(active)
  176. deactivate();
  177. recActions = DISPOSE;
  178. }
  179. void CIntObject::enable()
  180. {
  181. if(!active_m && (!parent_m || parent_m->active))
  182. activate();
  183. recActions = 255;
  184. }
  185. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  186. {
  187. return isItIn(&rect, x - pos.x, y - pos.y);
  188. }
  189. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  190. {
  191. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  192. }
  193. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  194. {
  195. Point newPos = pos.topLeft();
  196. vstd::amax(newPos.x, borderWidth);
  197. vstd::amax(newPos.y, borderWidth);
  198. vstd::amin(newPos.x, screen->w - borderWidth - pos.w);
  199. vstd::amin(newPos.y, screen->h - borderWidth - pos.h);
  200. if (newPos != pos.topLeft())
  201. moveTo(newPos, propagate);
  202. }
  203. void CIntObject::moveBy(const Point & p, bool propagate)
  204. {
  205. pos.x += p.x;
  206. pos.y += p.y;
  207. if(propagate)
  208. for(auto & elem : children)
  209. elem->moveBy(p, propagate);
  210. }
  211. void CIntObject::moveTo(const Point & p, bool propagate)
  212. {
  213. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  214. }
  215. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  216. {
  217. if (vstd::contains(children, child))
  218. {
  219. return;
  220. }
  221. if (child->parent_m)
  222. {
  223. child->parent_m->removeChild(child, adjustPosition);
  224. }
  225. children.push_back(child);
  226. child->parent_m = this;
  227. if(adjustPosition)
  228. child->pos += pos.topLeft();
  229. if (!active && child->active)
  230. child->deactivate();
  231. if (active && !child->active)
  232. child->activate();
  233. }
  234. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  235. {
  236. if (!child)
  237. return;
  238. if(!vstd::contains(children, child))
  239. throw std::runtime_error("Wrong child object");
  240. if(child->parent_m != this)
  241. throw std::runtime_error("Wrong child object");
  242. children -= child;
  243. child->parent_m = nullptr;
  244. if(adjustPosition)
  245. child->pos -= pos.topLeft();
  246. }
  247. void CIntObject::redraw()
  248. {
  249. //currently most of calls come from active objects so this check won't affect them
  250. //it should fix glitches when called by inactive elements located below active window
  251. if (active)
  252. {
  253. if (parent_m && (type & REDRAW_PARENT))
  254. {
  255. parent_m->redraw();
  256. }
  257. else
  258. {
  259. showAll(screenBuf);
  260. if(screenBuf != screen)
  261. showAll(screen);
  262. }
  263. }
  264. }
  265. const Rect & CIntObject::center( const Rect &r, bool propagate )
  266. {
  267. pos.w = r.w;
  268. pos.h = r.h;
  269. return center(Point(screen->w/2, screen->h/2), propagate);
  270. }
  271. const Rect & CIntObject::center( bool propagate )
  272. {
  273. return center(pos, propagate);
  274. }
  275. const Rect & CIntObject::center(const Point & p, bool propagate)
  276. {
  277. moveBy(Point(p.x - pos.w/2 - pos.x,
  278. p.y - pos.h/2 - pos.y),
  279. propagate);
  280. return pos;
  281. }
  282. bool CIntObject::captureThisEvent(const SDL_KeyboardEvent & key)
  283. {
  284. return captureAllKeys;
  285. }
  286. CKeyShortcut::CKeyShortcut()
  287. {}
  288. CKeyShortcut::CKeyShortcut(int key)
  289. {
  290. if (key != SDLK_UNKNOWN)
  291. assignedKeys.insert(key);
  292. }
  293. CKeyShortcut::CKeyShortcut(std::set<int> Keys)
  294. :assignedKeys(Keys)
  295. {}
  296. void CKeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  297. {
  298. if(vstd::contains(assignedKeys,key.keysym.sym)
  299. || vstd::contains(assignedKeys, CGuiHandler::numToDigit(key.keysym.sym)))
  300. {
  301. bool prev = mouseState(EIntObjMouseBtnType::LEFT);
  302. updateMouseState(EIntObjMouseBtnType::LEFT, key.state == SDL_PRESSED);
  303. clickLeft(key.state == SDL_PRESSED, prev);
  304. }
  305. }
  306. WindowBase::WindowBase(int used_, Point pos_)
  307. : CIntObject(used_, pos_)
  308. {
  309. }
  310. void WindowBase::close()
  311. {
  312. if(GH.topInt().get() != this)
  313. logGlobal->error("Only top interface must be closed");
  314. GH.popInts(1);
  315. }
  316. IStatusBar::~IStatusBar()
  317. {}