CIntObject.cpp 6.6 KB

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