CIntObject.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. while(!children.empty())
  37. {
  38. if((defActions & DISPOSE) && (children.front()->recActions & DISPOSE))
  39. delete children.front();
  40. else
  41. removeChild(children.front());
  42. }
  43. if(parent_m)
  44. parent_m->removeChild(this);
  45. }
  46. void CIntObject::show(Canvas & to)
  47. {
  48. if(defActions & UPDATE)
  49. for(auto & elem : children)
  50. if(elem->recActions & UPDATE)
  51. elem->show(to);
  52. }
  53. void CIntObject::showAll(Canvas & to)
  54. {
  55. if(defActions & SHOWALL)
  56. {
  57. for(auto & elem : children)
  58. if(elem->recActions & SHOWALL)
  59. elem->showAll(to);
  60. }
  61. }
  62. void CIntObject::activate()
  63. {
  64. if (isActive())
  65. return;
  66. if (inputEnabled)
  67. activateEvents(used | GENERAL);
  68. else
  69. activateEvents(GENERAL);
  70. assert(isActive());
  71. if(defActions & ACTIVATE)
  72. for(auto & elem : children)
  73. if(elem->recActions & ACTIVATE)
  74. elem->activate();
  75. }
  76. void CIntObject::deactivate()
  77. {
  78. if (!isActive())
  79. return;
  80. deactivateEvents(used | GENERAL);
  81. assert(!isActive());
  82. if(defActions & DEACTIVATE)
  83. for(auto & elem : children)
  84. if(elem->recActions & DEACTIVATE)
  85. elem->deactivate();
  86. }
  87. void CIntObject::addUsedEvents(ui16 newActions)
  88. {
  89. if (isActive() && inputEnabled)
  90. activateEvents(~used & newActions);
  91. used |= newActions;
  92. }
  93. void CIntObject::removeUsedEvents(ui16 newActions)
  94. {
  95. if (isActive())
  96. deactivateEvents(used & newActions);
  97. used &= ~newActions;
  98. }
  99. void CIntObject::disable()
  100. {
  101. if(isActive())
  102. deactivate();
  103. recActions = DISPOSE;
  104. }
  105. void CIntObject::enable()
  106. {
  107. if(!isActive() && (!parent_m || parent_m->isActive()))
  108. {
  109. activate();
  110. redraw();
  111. }
  112. recActions = 255;
  113. }
  114. void CIntObject::setEnabled(bool on)
  115. {
  116. if (on)
  117. enable();
  118. else
  119. disable();
  120. }
  121. void CIntObject::setInputEnabled(bool on)
  122. {
  123. if (inputEnabled == on)
  124. return;
  125. inputEnabled = on;
  126. if (isActive())
  127. {
  128. assert((used & GENERAL) == 0);
  129. if (on)
  130. activateEvents(used);
  131. else
  132. deactivateEvents(used);
  133. }
  134. for(auto & elem : children)
  135. elem->setInputEnabled(on);
  136. }
  137. void CIntObject::setRedrawParent(bool on)
  138. {
  139. redrawParent = on;
  140. }
  141. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  142. {
  143. Point newPos = pos.topLeft();
  144. vstd::amax(newPos.x, borderWidth);
  145. vstd::amax(newPos.y, borderWidth);
  146. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  147. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  148. if (newPos != pos.topLeft())
  149. moveTo(newPos, propagate);
  150. }
  151. void CIntObject::moveBy(const Point & p, bool propagate)
  152. {
  153. pos.x += p.x;
  154. pos.y += p.y;
  155. if(propagate)
  156. for(auto & elem : children)
  157. elem->moveBy(p, propagate);
  158. }
  159. void CIntObject::moveTo(const Point & p, bool propagate)
  160. {
  161. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  162. }
  163. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  164. {
  165. if (vstd::contains(children, child))
  166. {
  167. return;
  168. }
  169. if (child->parent_m)
  170. {
  171. child->parent_m->removeChild(child, adjustPosition);
  172. }
  173. children.push_back(child);
  174. child->parent_m = this;
  175. if(adjustPosition)
  176. child->moveBy(pos.topLeft(), adjustPosition);
  177. if (inputEnabled != child->inputEnabled)
  178. child->setInputEnabled(inputEnabled);
  179. if (!isActive() && child->isActive())
  180. child->deactivate();
  181. if (isActive()&& !child->isActive())
  182. child->activate();
  183. }
  184. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  185. {
  186. if (!child)
  187. return;
  188. if(!vstd::contains(children, child))
  189. throw std::runtime_error("Wrong child object");
  190. if(child->parent_m != this)
  191. throw std::runtime_error("Wrong child object");
  192. children -= child;
  193. child->parent_m = nullptr;
  194. if(adjustPosition)
  195. child->pos -= pos.topLeft();
  196. }
  197. void CIntObject::redraw()
  198. {
  199. //currently most of calls come from active objects so this check won't affect them
  200. //it should fix glitches when called by inactive elements located below active window
  201. if (isActive())
  202. {
  203. if (parent_m && redrawParent)
  204. {
  205. parent_m->redraw();
  206. }
  207. else
  208. {
  209. Canvas buffer = Canvas::createFromSurface(screenBuf);
  210. showAll(buffer);
  211. if(screenBuf != screen)
  212. {
  213. Canvas screenBuffer = Canvas::createFromSurface(screen);
  214. showAll(screenBuffer);
  215. }
  216. }
  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. logGlobal->error("Only top interface must be closed");
  289. GH.windows().popWindows(1);
  290. }