CIntObject.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "GameEngine.h"
  13. #include "WindowHandler.h"
  14. #include "EventDispatcher.h"
  15. #include "Shortcut.h"
  16. #include "../render/Canvas.h"
  17. #include "../render/IScreenHandler.h"
  18. #include "../windows/CMessage.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(ENGINE->captureChildren)
  29. ENGINE->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. bool CIntObject::isDisabled() const
  103. {
  104. return recActions == NO_ACTIONS;
  105. }
  106. void CIntObject::setEnabled(bool on)
  107. {
  108. if (on)
  109. enable();
  110. else
  111. disable();
  112. }
  113. void CIntObject::setInputEnabled(bool on)
  114. {
  115. if (inputEnabled == on)
  116. return;
  117. inputEnabled = on;
  118. if (isActive())
  119. {
  120. assert((used & GENERAL) == 0);
  121. if (on)
  122. activateEvents(used);
  123. else
  124. deactivateEvents(used);
  125. }
  126. for(auto & elem : children)
  127. elem->setInputEnabled(on);
  128. }
  129. void CIntObject::setRedrawParent(bool on)
  130. {
  131. redrawParent = on;
  132. }
  133. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  134. {
  135. fitToRect(Rect(Point(0, 0), ENGINE->screenDimensions()), borderWidth, propagate);
  136. }
  137. void CIntObject::fitToRect(Rect rect, int borderWidth, bool propagate)
  138. {
  139. Point newPos = pos.topLeft();
  140. vstd::amax(newPos.x, rect.x + borderWidth);
  141. vstd::amax(newPos.y, rect.y + borderWidth);
  142. vstd::amin(newPos.x, rect.x + rect.w - borderWidth - pos.w);
  143. vstd::amin(newPos.y, rect.y + rect.h - borderWidth - pos.h);
  144. if (newPos != pos.topLeft())
  145. moveTo(newPos, propagate);
  146. }
  147. void CIntObject::moveBy(const Point & p, bool propagate)
  148. {
  149. pos.x += p.x;
  150. pos.y += p.y;
  151. if(propagate)
  152. for(auto & elem : children)
  153. elem->moveBy(p, propagate);
  154. }
  155. void CIntObject::moveTo(const Point & p, bool propagate)
  156. {
  157. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  158. }
  159. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  160. {
  161. if (vstd::contains(children, child))
  162. {
  163. return;
  164. }
  165. if (child->parent_m)
  166. {
  167. child->parent_m->removeChild(child, adjustPosition);
  168. }
  169. children.push_back(child);
  170. child->parent_m = this;
  171. if(adjustPosition)
  172. child->moveBy(pos.topLeft(), adjustPosition);
  173. if (inputEnabled != child->inputEnabled)
  174. child->setInputEnabled(inputEnabled);
  175. if (!isActive() && child->isActive())
  176. child->deactivate();
  177. if (isActive()&& !child->isActive())
  178. child->activate();
  179. }
  180. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  181. {
  182. if (!child)
  183. return;
  184. if(!vstd::contains(children, child))
  185. throw std::runtime_error("Wrong child object");
  186. if(child->parent_m != this)
  187. throw std::runtime_error("Wrong child object");
  188. children -= child;
  189. child->parent_m = nullptr;
  190. if(adjustPosition)
  191. child->pos -= pos.topLeft();
  192. }
  193. void CIntObject::redraw()
  194. {
  195. //currently most of calls come from active objects so this check won't affect them
  196. //it should fix glitches when called by inactive elements located below active window
  197. if (isActive())
  198. {
  199. if (parent_m && redrawParent)
  200. {
  201. parent_m->redraw();
  202. }
  203. else
  204. {
  205. Canvas buffer = ENGINE->screenHandler().getScreenCanvas();
  206. showAll(buffer);
  207. }
  208. }
  209. }
  210. void CIntObject::moveChildForeground(const CIntObject * childToMove)
  211. {
  212. for(auto child = children.begin(); child != children.end(); child++)
  213. if(*child == childToMove && child != children.end())
  214. {
  215. std::rotate(child, child + 1, children.end());
  216. }
  217. }
  218. bool CIntObject::receiveEvent(const Point & position, int eventType) const
  219. {
  220. return pos.isInside(position);
  221. }
  222. const Rect & CIntObject::getPosition() const
  223. {
  224. return pos;
  225. }
  226. void CIntObject::onScreenResize()
  227. {
  228. center(pos, true);
  229. }
  230. bool CIntObject::isPopupWindow() const
  231. {
  232. return false;
  233. }
  234. const Rect & CIntObject::center( const Rect &r, bool propagate )
  235. {
  236. pos.w = r.w;
  237. pos.h = r.h;
  238. return center(Point(ENGINE->screenDimensions().x/2, ENGINE->screenDimensions().y/2), propagate);
  239. }
  240. const Rect & CIntObject::center( bool propagate )
  241. {
  242. return center(pos, propagate);
  243. }
  244. const Rect & CIntObject::center(const Point & p, bool propagate)
  245. {
  246. moveBy(Point(p.x - pos.w/2 - pos.x,
  247. p.y - pos.h/2 - pos.y),
  248. propagate);
  249. return pos;
  250. }
  251. bool CIntObject::captureThisKey(EShortcut key)
  252. {
  253. return false;
  254. }
  255. CKeyShortcut::CKeyShortcut()
  256. : assignedKey(EShortcut::NONE)
  257. , shortcutPressed(false)
  258. {}
  259. CKeyShortcut::CKeyShortcut(EShortcut key)
  260. : assignedKey(key)
  261. , shortcutPressed(false)
  262. {
  263. }
  264. void CKeyShortcut::keyPressed(EShortcut key)
  265. {
  266. if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed)
  267. {
  268. shortcutPressed = true;
  269. clickPressed(ENGINE->getCursorPosition());
  270. }
  271. }
  272. void CKeyShortcut::keyReleased(EShortcut key)
  273. {
  274. if( assignedKey == key && assignedKey != EShortcut::NONE && shortcutPressed)
  275. {
  276. shortcutPressed = false;
  277. clickReleased(ENGINE->getCursorPosition());
  278. }
  279. }
  280. WindowBase::WindowBase(int used_, Point pos_)
  281. : CIntObject(used_, pos_)
  282. {
  283. }
  284. void WindowBase::close()
  285. {
  286. if(!ENGINE->windows().isTopWindow(this))
  287. {
  288. auto topWindow = ENGINE->windows().topWindow<IShowActivatable>().get();
  289. throw std::runtime_error(std::string("Only top interface can be closed! Top window is ") + typeid(*topWindow).name() + " but attempted to close " + typeid(*this).name());
  290. }
  291. ENGINE->windows().popWindows(1);
  292. }