CIntObject.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "Shortcut.h"
  15. #include "../render/Canvas.h"
  16. #include "../windows/CMessage.h"
  17. #include "../CMT.h"
  18. CIntObject::CIntObject(int used_, Point pos_):
  19. parent_m(nullptr),
  20. parent(parent_m),
  21. type(0)
  22. {
  23. captureAllKeys = false;
  24. used = used_;
  25. recActions = defActions = GH.defActionsDef;
  26. pos.x = pos_.x;
  27. pos.y = pos_.y;
  28. pos.w = 0;
  29. pos.h = 0;
  30. if(GH.captureChildren)
  31. GH.createdObj.front()->addChild(this, true);
  32. }
  33. CIntObject::~CIntObject()
  34. {
  35. if(isActive())
  36. deactivate();
  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. activateEvents(used | GENERAL);
  68. assert(isActive());
  69. if(defActions & ACTIVATE)
  70. for(auto & elem : children)
  71. if(elem->recActions & ACTIVATE)
  72. elem->activate();
  73. }
  74. void CIntObject::deactivate()
  75. {
  76. if (!isActive())
  77. return;
  78. deactivateEvents(ALL);
  79. assert(!isActive());
  80. if(defActions & DEACTIVATE)
  81. for(auto & elem : children)
  82. if(elem->recActions & DEACTIVATE)
  83. elem->deactivate();
  84. }
  85. void CIntObject::addUsedEvents(ui16 newActions)
  86. {
  87. if (isActive())
  88. activateEvents(~used & newActions);
  89. used |= newActions;
  90. }
  91. void CIntObject::removeUsedEvents(ui16 newActions)
  92. {
  93. if (isActive())
  94. deactivateEvents(used & newActions);
  95. used &= ~newActions;
  96. }
  97. void CIntObject::disable()
  98. {
  99. if(isActive())
  100. deactivate();
  101. recActions = DISPOSE;
  102. }
  103. void CIntObject::enable()
  104. {
  105. if(!isActive() && (!parent_m || parent_m->isActive()))
  106. {
  107. activate();
  108. redraw();
  109. }
  110. recActions = 255;
  111. }
  112. void CIntObject::setEnabled(bool on)
  113. {
  114. if (on)
  115. enable();
  116. else
  117. disable();
  118. }
  119. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  120. {
  121. Point newPos = pos.topLeft();
  122. vstd::amax(newPos.x, borderWidth);
  123. vstd::amax(newPos.y, borderWidth);
  124. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  125. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  126. if (newPos != pos.topLeft())
  127. moveTo(newPos, propagate);
  128. }
  129. void CIntObject::moveBy(const Point & p, bool propagate)
  130. {
  131. pos.x += p.x;
  132. pos.y += p.y;
  133. if(propagate)
  134. for(auto & elem : children)
  135. elem->moveBy(p, propagate);
  136. }
  137. void CIntObject::moveTo(const Point & p, bool propagate)
  138. {
  139. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  140. }
  141. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  142. {
  143. if (vstd::contains(children, child))
  144. {
  145. return;
  146. }
  147. if (child->parent_m)
  148. {
  149. child->parent_m->removeChild(child, adjustPosition);
  150. }
  151. children.push_back(child);
  152. child->parent_m = this;
  153. if(adjustPosition)
  154. child->moveBy(pos.topLeft(), adjustPosition);
  155. if (!isActive() && child->isActive())
  156. child->deactivate();
  157. if (isActive()&& !child->isActive())
  158. child->activate();
  159. }
  160. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  161. {
  162. if (!child)
  163. return;
  164. if(!vstd::contains(children, child))
  165. throw std::runtime_error("Wrong child object");
  166. if(child->parent_m != this)
  167. throw std::runtime_error("Wrong child object");
  168. children -= child;
  169. child->parent_m = nullptr;
  170. if(adjustPosition)
  171. child->pos -= pos.topLeft();
  172. }
  173. void CIntObject::redraw()
  174. {
  175. //currently most of calls come from active objects so this check won't affect them
  176. //it should fix glitches when called by inactive elements located below active window
  177. if (isActive())
  178. {
  179. if (parent_m && (type & REDRAW_PARENT))
  180. {
  181. parent_m->redraw();
  182. }
  183. else
  184. {
  185. Canvas buffer = Canvas::createFromSurface(screenBuf);
  186. showAll(buffer);
  187. if(screenBuf != screen)
  188. {
  189. Canvas screenBuffer = Canvas::createFromSurface(screen);
  190. showAll(screenBuffer);
  191. }
  192. }
  193. }
  194. }
  195. bool CIntObject::receiveEvent(const Point & position, int eventType) const
  196. {
  197. return pos.isInside(position);
  198. }
  199. void CIntObject::onScreenResize()
  200. {
  201. center(pos, true);
  202. }
  203. const Rect & CIntObject::center( const Rect &r, bool propagate )
  204. {
  205. pos.w = r.w;
  206. pos.h = r.h;
  207. return center(Point(GH.screenDimensions().x/2, GH.screenDimensions().y/2), propagate);
  208. }
  209. const Rect & CIntObject::center( bool propagate )
  210. {
  211. return center(pos, propagate);
  212. }
  213. const Rect & CIntObject::center(const Point & p, bool propagate)
  214. {
  215. moveBy(Point(p.x - pos.w/2 - pos.x,
  216. p.y - pos.h/2 - pos.y),
  217. propagate);
  218. return pos;
  219. }
  220. bool CIntObject::captureThisKey(EShortcut key)
  221. {
  222. return captureAllKeys;
  223. }
  224. CKeyShortcut::CKeyShortcut()
  225. : assignedKey(EShortcut::NONE)
  226. , shortcutPressed(false)
  227. {}
  228. CKeyShortcut::CKeyShortcut(EShortcut key)
  229. : assignedKey(key)
  230. {
  231. }
  232. void CKeyShortcut::keyPressed(EShortcut key)
  233. {
  234. if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed)
  235. {
  236. shortcutPressed = true;
  237. clickLeft(true, false);
  238. }
  239. }
  240. void CKeyShortcut::keyReleased(EShortcut key)
  241. {
  242. if( assignedKey == key && assignedKey != EShortcut::NONE && shortcutPressed)
  243. {
  244. shortcutPressed = false;
  245. clickLeft(false, true);
  246. }
  247. }
  248. WindowBase::WindowBase(int used_, Point pos_)
  249. : CIntObject(used_, pos_)
  250. {
  251. }
  252. void WindowBase::close()
  253. {
  254. if(!GH.windows().isTopWindow(this))
  255. logGlobal->error("Only top interface must be closed");
  256. GH.windows().popWindows(1);
  257. }