CIntObject.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "../renderSDL/SDL_Extensions.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(SDL_Surface * 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(SDL_Surface * 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::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, const SDL_Color & kolor, SDL_Surface * dst)
  86. {
  87. graphics->fonts[font]->renderTextCenter(dst, text, kolor, pos.topLeft() + p);
  88. }
  89. void CIntObject::printAtMiddleWBLoc( const std::string & text, const Point &p, EFonts font, int charpr, const SDL_Color & kolor, SDL_Surface * dst)
  90. {
  91. graphics->fonts[font]->renderTextLinesCenter(dst, CMessage::breakText(text, charpr, font), kolor, pos.topLeft() + p);
  92. }
  93. void CIntObject::addUsedEvents(ui16 newActions)
  94. {
  95. if (isActive())
  96. activateEvents(~used & newActions);
  97. used |= newActions;
  98. }
  99. void CIntObject::removeUsedEvents(ui16 newActions)
  100. {
  101. if (isActive())
  102. deactivateEvents(used & newActions);
  103. used &= ~newActions;
  104. }
  105. void CIntObject::disable()
  106. {
  107. if(isActive())
  108. deactivate();
  109. recActions = DISPOSE;
  110. }
  111. void CIntObject::enable()
  112. {
  113. if(!isActive() && (!parent_m || parent_m->isActive()))
  114. {
  115. activate();
  116. redraw();
  117. }
  118. recActions = 255;
  119. }
  120. void CIntObject::setEnabled(bool on)
  121. {
  122. if (on)
  123. enable();
  124. else
  125. disable();
  126. }
  127. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  128. {
  129. Point newPos = pos.topLeft();
  130. vstd::amax(newPos.x, borderWidth);
  131. vstd::amax(newPos.y, borderWidth);
  132. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  133. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  134. if (newPos != pos.topLeft())
  135. moveTo(newPos, propagate);
  136. }
  137. void CIntObject::moveBy(const Point & p, bool propagate)
  138. {
  139. pos.x += p.x;
  140. pos.y += p.y;
  141. if(propagate)
  142. for(auto & elem : children)
  143. elem->moveBy(p, propagate);
  144. }
  145. void CIntObject::moveTo(const Point & p, bool propagate)
  146. {
  147. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  148. }
  149. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  150. {
  151. if (vstd::contains(children, child))
  152. {
  153. return;
  154. }
  155. if (child->parent_m)
  156. {
  157. child->parent_m->removeChild(child, adjustPosition);
  158. }
  159. children.push_back(child);
  160. child->parent_m = this;
  161. if(adjustPosition)
  162. child->moveBy(pos.topLeft(), adjustPosition);
  163. if (!isActive() && child->isActive())
  164. child->deactivate();
  165. if (isActive()&& !child->isActive())
  166. child->activate();
  167. }
  168. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  169. {
  170. if (!child)
  171. return;
  172. if(!vstd::contains(children, child))
  173. throw std::runtime_error("Wrong child object");
  174. if(child->parent_m != this)
  175. throw std::runtime_error("Wrong child object");
  176. children -= child;
  177. child->parent_m = nullptr;
  178. if(adjustPosition)
  179. child->pos -= pos.topLeft();
  180. }
  181. void CIntObject::redraw()
  182. {
  183. //currently most of calls come from active objects so this check won't affect them
  184. //it should fix glitches when called by inactive elements located below active window
  185. if (isActive())
  186. {
  187. if (parent_m && (type & REDRAW_PARENT))
  188. {
  189. parent_m->redraw();
  190. }
  191. else
  192. {
  193. showAll(screenBuf);
  194. if(screenBuf != screen)
  195. showAll(screen);
  196. }
  197. }
  198. }
  199. bool CIntObject::isInside(const Point & position)
  200. {
  201. return pos.isInside(position);
  202. }
  203. void CIntObject::onScreenResize()
  204. {
  205. center(pos, true);
  206. }
  207. const Rect & CIntObject::center( const Rect &r, bool propagate )
  208. {
  209. pos.w = r.w;
  210. pos.h = r.h;
  211. return center(Point(GH.screenDimensions().x/2, GH.screenDimensions().y/2), propagate);
  212. }
  213. const Rect & CIntObject::center( bool propagate )
  214. {
  215. return center(pos, propagate);
  216. }
  217. const Rect & CIntObject::center(const Point & p, bool propagate)
  218. {
  219. moveBy(Point(p.x - pos.w/2 - pos.x,
  220. p.y - pos.h/2 - pos.y),
  221. propagate);
  222. return pos;
  223. }
  224. bool CIntObject::captureThisKey(EShortcut key)
  225. {
  226. return captureAllKeys;
  227. }
  228. CKeyShortcut::CKeyShortcut()
  229. : assignedKey(EShortcut::NONE)
  230. , shortcutPressed(false)
  231. {}
  232. CKeyShortcut::CKeyShortcut(EShortcut key)
  233. : assignedKey(key)
  234. {
  235. }
  236. void CKeyShortcut::keyPressed(EShortcut key)
  237. {
  238. if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed)
  239. {
  240. shortcutPressed = true;
  241. clickLeft(true, false);
  242. }
  243. }
  244. void CKeyShortcut::keyReleased(EShortcut key)
  245. {
  246. if( assignedKey == key && assignedKey != EShortcut::NONE && shortcutPressed)
  247. {
  248. shortcutPressed = false;
  249. clickLeft(false, true);
  250. }
  251. }
  252. WindowBase::WindowBase(int used_, Point pos_)
  253. : CIntObject(used_, pos_)
  254. {
  255. }
  256. void WindowBase::close()
  257. {
  258. if(!GH.windows().isTopWindow(this))
  259. logGlobal->error("Only top interface must be closed");
  260. GH.windows().popWindows(1);
  261. }