CIntObject.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "WindowHandler.h"
  14. #include "EventDispatcher.h"
  15. #include "Shortcut.h"
  16. #include "../render/Canvas.h"
  17. #include "../windows/CMessage.h"
  18. #include "../CMT.h"
  19. CIntObject::CIntObject(int used_, Point pos_):
  20. parent_m(nullptr),
  21. parent(parent_m),
  22. redrawParent(false),
  23. inputEnabled(true),
  24. used(used_),
  25. recActions(ALL_ACTIONS),
  26. pos(pos_, Point())
  27. {
  28. if(GH.captureChildren)
  29. GH.createdObj.front()->addChild(this, true);
  30. }
  31. CIntObject::~CIntObject()
  32. {
  33. if(isActive())
  34. deactivate();
  35. while(!children.empty())
  36. removeChild(children.front());
  37. if(parent_m)
  38. parent_m->removeChild(this);
  39. }
  40. void CIntObject::show(Canvas & to)
  41. {
  42. for(auto & elem : children)
  43. if(elem->recActions & UPDATE)
  44. elem->show(to);
  45. }
  46. void CIntObject::showAll(Canvas & to)
  47. {
  48. for(auto & elem : children)
  49. if(elem->recActions & SHOWALL)
  50. elem->showAll(to);
  51. }
  52. void CIntObject::activate()
  53. {
  54. if (isActive())
  55. return;
  56. if (inputEnabled)
  57. activateEvents(used | GENERAL);
  58. else
  59. activateEvents(GENERAL);
  60. assert(isActive());
  61. for(auto & elem : children)
  62. if(elem->recActions & ACTIVATE)
  63. elem->activate();
  64. }
  65. void CIntObject::deactivate()
  66. {
  67. if (!isActive())
  68. return;
  69. deactivateEvents(used | GENERAL);
  70. assert(!isActive());
  71. for(auto & elem : children)
  72. if(elem->recActions & DEACTIVATE)
  73. elem->deactivate();
  74. }
  75. void CIntObject::addUsedEvents(ui16 newActions)
  76. {
  77. if (isActive() && inputEnabled)
  78. activateEvents(~used & newActions);
  79. used |= newActions;
  80. }
  81. void CIntObject::removeUsedEvents(ui16 newActions)
  82. {
  83. if (isActive())
  84. deactivateEvents(used & newActions);
  85. used &= ~newActions;
  86. }
  87. void CIntObject::disable()
  88. {
  89. if(isActive())
  90. deactivate();
  91. recActions = NO_ACTIONS;
  92. }
  93. void CIntObject::enable()
  94. {
  95. if(!isActive() && (!parent_m || parent_m->isActive()))
  96. {
  97. activate();
  98. redraw();
  99. }
  100. recActions = ALL_ACTIONS;
  101. }
  102. void CIntObject::setEnabled(bool on)
  103. {
  104. if (on)
  105. enable();
  106. else
  107. disable();
  108. }
  109. void CIntObject::setInputEnabled(bool on)
  110. {
  111. if (inputEnabled == on)
  112. return;
  113. inputEnabled = on;
  114. if (isActive())
  115. {
  116. assert((used & GENERAL) == 0);
  117. if (on)
  118. activateEvents(used);
  119. else
  120. deactivateEvents(used);
  121. }
  122. for(auto & elem : children)
  123. elem->setInputEnabled(on);
  124. }
  125. void CIntObject::setRedrawParent(bool on)
  126. {
  127. redrawParent = on;
  128. }
  129. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  130. {
  131. fitToRect(Rect(Point(0, 0), GH.screenDimensions()), borderWidth, propagate);
  132. }
  133. void CIntObject::fitToRect(Rect rect, int borderWidth, bool propagate)
  134. {
  135. Point newPos = pos.topLeft();
  136. vstd::amax(newPos.x, rect.x + borderWidth);
  137. vstd::amax(newPos.y, rect.y + borderWidth);
  138. vstd::amin(newPos.x, rect.x + rect.w - borderWidth - pos.w);
  139. vstd::amin(newPos.y, rect.y + rect.h - borderWidth - pos.h);
  140. if (newPos != pos.topLeft())
  141. moveTo(newPos, propagate);
  142. }
  143. void CIntObject::moveBy(const Point & p, bool propagate)
  144. {
  145. pos.x += p.x;
  146. pos.y += p.y;
  147. if(propagate)
  148. for(auto & elem : children)
  149. elem->moveBy(p, propagate);
  150. }
  151. void CIntObject::moveTo(const Point & p, bool propagate)
  152. {
  153. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  154. }
  155. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  156. {
  157. if (vstd::contains(children, child))
  158. {
  159. return;
  160. }
  161. if (child->parent_m)
  162. {
  163. child->parent_m->removeChild(child, adjustPosition);
  164. }
  165. children.push_back(child);
  166. child->parent_m = this;
  167. if(adjustPosition)
  168. child->moveBy(pos.topLeft(), adjustPosition);
  169. if (inputEnabled != child->inputEnabled)
  170. child->setInputEnabled(inputEnabled);
  171. if (!isActive() && child->isActive())
  172. child->deactivate();
  173. if (isActive()&& !child->isActive())
  174. child->activate();
  175. }
  176. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  177. {
  178. if (!child)
  179. return;
  180. if(!vstd::contains(children, child))
  181. throw std::runtime_error("Wrong child object");
  182. if(child->parent_m != this)
  183. throw std::runtime_error("Wrong child object");
  184. children -= child;
  185. child->parent_m = nullptr;
  186. if(adjustPosition)
  187. child->pos -= pos.topLeft();
  188. }
  189. void CIntObject::redraw()
  190. {
  191. //currently most of calls come from active objects so this check won't affect them
  192. //it should fix glitches when called by inactive elements located below active window
  193. if (isActive())
  194. {
  195. if (parent_m && redrawParent)
  196. {
  197. parent_m->redraw();
  198. }
  199. else
  200. {
  201. Canvas buffer = Canvas::createFromSurface(screenBuf, CanvasScalingPolicy::AUTO);
  202. showAll(buffer);
  203. if(screenBuf != screen)
  204. {
  205. Canvas screenBuffer = Canvas::createFromSurface(screen, CanvasScalingPolicy::AUTO);
  206. showAll(screenBuffer);
  207. }
  208. }
  209. }
  210. }
  211. void CIntObject::moveChildForeground(const CIntObject * childToMove)
  212. {
  213. for(auto child = children.begin(); child != children.end(); child++)
  214. if(*child == childToMove && child != children.end())
  215. {
  216. std::rotate(child, child + 1, children.end());
  217. }
  218. }
  219. bool CIntObject::receiveEvent(const Point & position, int eventType) const
  220. {
  221. return pos.isInside(position);
  222. }
  223. const Rect & CIntObject::getPosition() const
  224. {
  225. return pos;
  226. }
  227. void CIntObject::onScreenResize()
  228. {
  229. center(pos, true);
  230. }
  231. bool CIntObject::isPopupWindow() const
  232. {
  233. return false;
  234. }
  235. const Rect & CIntObject::center( const Rect &r, bool propagate )
  236. {
  237. pos.w = r.w;
  238. pos.h = r.h;
  239. return center(Point(GH.screenDimensions().x/2, GH.screenDimensions().y/2), propagate);
  240. }
  241. const Rect & CIntObject::center( bool propagate )
  242. {
  243. return center(pos, propagate);
  244. }
  245. const Rect & CIntObject::center(const Point & p, bool propagate)
  246. {
  247. moveBy(Point(p.x - pos.w/2 - pos.x,
  248. p.y - pos.h/2 - pos.y),
  249. propagate);
  250. return pos;
  251. }
  252. bool CIntObject::captureThisKey(EShortcut key)
  253. {
  254. return false;
  255. }
  256. CKeyShortcut::CKeyShortcut()
  257. : assignedKey(EShortcut::NONE)
  258. , shortcutPressed(false)
  259. {}
  260. CKeyShortcut::CKeyShortcut(EShortcut key)
  261. : assignedKey(key)
  262. , shortcutPressed(false)
  263. {
  264. }
  265. void CKeyShortcut::keyPressed(EShortcut key)
  266. {
  267. if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed)
  268. {
  269. shortcutPressed = true;
  270. clickPressed(GH.getCursorPosition());
  271. }
  272. }
  273. void CKeyShortcut::keyReleased(EShortcut key)
  274. {
  275. if( assignedKey == key && assignedKey != EShortcut::NONE && shortcutPressed)
  276. {
  277. shortcutPressed = false;
  278. clickReleased(GH.getCursorPosition());
  279. }
  280. }
  281. WindowBase::WindowBase(int used_, Point pos_)
  282. : CIntObject(used_, pos_)
  283. {
  284. }
  285. void WindowBase::close()
  286. {
  287. if(!GH.windows().isTopWindow(this))
  288. {
  289. auto topWindow = GH.windows().topWindow<IShowActivatable>().get();
  290. throw std::runtime_error(std::string("Only top interface can be closed! Top window is ") + typeid(*this).name() + " but attempted to close " + typeid(*topWindow).name());
  291. }
  292. GH.windows().popWindows(1);
  293. }