CIntObject.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. Point newPos = pos.topLeft();
  132. vstd::amax(newPos.x, borderWidth);
  133. vstd::amax(newPos.y, borderWidth);
  134. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  135. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  136. if (newPos != pos.topLeft())
  137. moveTo(newPos, propagate);
  138. }
  139. void CIntObject::moveBy(const Point & p, bool propagate)
  140. {
  141. pos.x += p.x;
  142. pos.y += p.y;
  143. if(propagate)
  144. for(auto & elem : children)
  145. elem->moveBy(p, propagate);
  146. }
  147. void CIntObject::moveTo(const Point & p, bool propagate)
  148. {
  149. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  150. }
  151. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  152. {
  153. if (vstd::contains(children, child))
  154. {
  155. return;
  156. }
  157. if (child->parent_m)
  158. {
  159. child->parent_m->removeChild(child, adjustPosition);
  160. }
  161. children.push_back(child);
  162. child->parent_m = this;
  163. if(adjustPosition)
  164. child->moveBy(pos.topLeft(), adjustPosition);
  165. if (inputEnabled != child->inputEnabled)
  166. child->setInputEnabled(inputEnabled);
  167. if (!isActive() && child->isActive())
  168. child->deactivate();
  169. if (isActive()&& !child->isActive())
  170. child->activate();
  171. }
  172. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  173. {
  174. if (!child)
  175. return;
  176. if(!vstd::contains(children, child))
  177. throw std::runtime_error("Wrong child object");
  178. if(child->parent_m != this)
  179. throw std::runtime_error("Wrong child object");
  180. children -= child;
  181. child->parent_m = nullptr;
  182. if(adjustPosition)
  183. child->pos -= pos.topLeft();
  184. }
  185. void CIntObject::redraw()
  186. {
  187. //currently most of calls come from active objects so this check won't affect them
  188. //it should fix glitches when called by inactive elements located below active window
  189. if (isActive())
  190. {
  191. if (parent_m && redrawParent)
  192. {
  193. parent_m->redraw();
  194. }
  195. else
  196. {
  197. Canvas buffer = Canvas::createFromSurface(screenBuf);
  198. showAll(buffer);
  199. if(screenBuf != screen)
  200. {
  201. Canvas screenBuffer = Canvas::createFromSurface(screen);
  202. showAll(screenBuffer);
  203. }
  204. }
  205. }
  206. }
  207. void CIntObject::moveChildForeground(const CIntObject * childToMove)
  208. {
  209. for(auto child = children.begin(); child != children.end(); child++)
  210. if(*child == childToMove && child != children.end())
  211. {
  212. std::rotate(child, child + 1, children.end());
  213. }
  214. }
  215. bool CIntObject::receiveEvent(const Point & position, int eventType) const
  216. {
  217. return pos.isInside(position);
  218. }
  219. const Rect & CIntObject::getPosition() const
  220. {
  221. return pos;
  222. }
  223. void CIntObject::onScreenResize()
  224. {
  225. center(pos, true);
  226. }
  227. bool CIntObject::isPopupWindow() const
  228. {
  229. return false;
  230. }
  231. const Rect & CIntObject::center( const Rect &r, bool propagate )
  232. {
  233. pos.w = r.w;
  234. pos.h = r.h;
  235. return center(Point(GH.screenDimensions().x/2, GH.screenDimensions().y/2), propagate);
  236. }
  237. const Rect & CIntObject::center( bool propagate )
  238. {
  239. return center(pos, propagate);
  240. }
  241. const Rect & CIntObject::center(const Point & p, bool propagate)
  242. {
  243. moveBy(Point(p.x - pos.w/2 - pos.x,
  244. p.y - pos.h/2 - pos.y),
  245. propagate);
  246. return pos;
  247. }
  248. bool CIntObject::captureThisKey(EShortcut key)
  249. {
  250. return false;
  251. }
  252. CKeyShortcut::CKeyShortcut()
  253. : assignedKey(EShortcut::NONE)
  254. , shortcutPressed(false)
  255. {}
  256. CKeyShortcut::CKeyShortcut(EShortcut key)
  257. : assignedKey(key)
  258. , shortcutPressed(false)
  259. {
  260. }
  261. void CKeyShortcut::keyPressed(EShortcut key)
  262. {
  263. if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed)
  264. {
  265. shortcutPressed = true;
  266. clickPressed(GH.getCursorPosition());
  267. }
  268. }
  269. void CKeyShortcut::keyReleased(EShortcut key)
  270. {
  271. if( assignedKey == key && assignedKey != EShortcut::NONE && shortcutPressed)
  272. {
  273. shortcutPressed = false;
  274. clickReleased(GH.getCursorPosition());
  275. }
  276. }
  277. WindowBase::WindowBase(int used_, Point pos_)
  278. : CIntObject(used_, pos_)
  279. {
  280. }
  281. void WindowBase::close()
  282. {
  283. if(!GH.windows().isTopWindow(this))
  284. logGlobal->error("Only top interface must be closed");
  285. GH.windows().popWindows(1);
  286. }