CIntObject.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 "InterfaceEventDispatcher.h"
  14. #include "WindowHandler.h"
  15. #include "Shortcut.h"
  16. #include "../renderSDL/SDL_Extensions.h"
  17. #include "../windows/CMessage.h"
  18. #include "../CMT.h"
  19. #include <SDL_pixels.h>
  20. AEventsReceiver::AEventsReceiver()
  21. : activeState(0)
  22. , hoveredState(false)
  23. , strongInterestState(false)
  24. {
  25. }
  26. bool AEventsReceiver::isHovered() const
  27. {
  28. return hoveredState;
  29. }
  30. bool AEventsReceiver::isActive() const
  31. {
  32. return activeState;
  33. }
  34. bool AEventsReceiver::isActive(int flags) const
  35. {
  36. return activeState & flags;
  37. }
  38. bool AEventsReceiver::isMouseButtonPressed(MouseButton btn) const
  39. {
  40. return currentMouseState.count(btn) ? currentMouseState.at(btn) : false;
  41. }
  42. void AEventsReceiver::setMoveEventStrongInterest(bool on)
  43. {
  44. strongInterestState = on;
  45. }
  46. void AEventsReceiver::activateEvents(ui16 what)
  47. {
  48. assert((what & GENERAL) || (activeState & GENERAL));
  49. activeState |= GENERAL;
  50. GH.eventDispatcher().handleElementActivate(this, what);
  51. }
  52. void AEventsReceiver::deactivateEvents(ui16 what)
  53. {
  54. if (what & GENERAL)
  55. activeState &= ~GENERAL;
  56. GH.eventDispatcher().handleElementDeActivate(this, what & activeState);
  57. }
  58. void AEventsReceiver::click(MouseButton btn, tribool down, bool previousState)
  59. {
  60. switch(btn)
  61. {
  62. default:
  63. case MouseButton::LEFT:
  64. clickLeft(down, previousState);
  65. break;
  66. case MouseButton::MIDDLE:
  67. clickMiddle(down, previousState);
  68. break;
  69. case MouseButton::RIGHT:
  70. clickRight(down, previousState);
  71. break;
  72. }
  73. }
  74. CIntObject::CIntObject(int used_, Point pos_):
  75. parent_m(nullptr),
  76. parent(parent_m),
  77. type(0)
  78. {
  79. captureAllKeys = false;
  80. used = used_;
  81. recActions = defActions = GH.defActionsDef;
  82. pos.x = pos_.x;
  83. pos.y = pos_.y;
  84. pos.w = 0;
  85. pos.h = 0;
  86. if(GH.captureChildren)
  87. GH.createdObj.front()->addChild(this, true);
  88. }
  89. CIntObject::~CIntObject()
  90. {
  91. if(isActive())
  92. deactivate();
  93. while(!children.empty())
  94. {
  95. if((defActions & DISPOSE) && (children.front()->recActions & DISPOSE))
  96. delete children.front();
  97. else
  98. removeChild(children.front());
  99. }
  100. if(parent_m)
  101. parent_m->removeChild(this);
  102. }
  103. void CIntObject::show(SDL_Surface * to)
  104. {
  105. if(defActions & UPDATE)
  106. for(auto & elem : children)
  107. if(elem->recActions & UPDATE)
  108. elem->show(to);
  109. }
  110. void CIntObject::showAll(SDL_Surface * to)
  111. {
  112. if(defActions & SHOWALL)
  113. {
  114. for(auto & elem : children)
  115. if(elem->recActions & SHOWALL)
  116. elem->showAll(to);
  117. }
  118. }
  119. void CIntObject::activate()
  120. {
  121. if (isActive())
  122. return;
  123. activateEvents(used | GENERAL);
  124. assert(isActive());
  125. if(defActions & ACTIVATE)
  126. for(auto & elem : children)
  127. if(elem->recActions & ACTIVATE)
  128. elem->activate();
  129. }
  130. void CIntObject::deactivate()
  131. {
  132. if (!isActive())
  133. return;
  134. deactivateEvents(ALL);
  135. assert(!isActive());
  136. if(defActions & DEACTIVATE)
  137. for(auto & elem : children)
  138. if(elem->recActions & DEACTIVATE)
  139. elem->deactivate();
  140. }
  141. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  142. {
  143. graphics->fonts[font]->renderTextCenter(dst, text, kolor, pos.topLeft() + p);
  144. }
  145. void CIntObject::printAtMiddleWBLoc( const std::string & text, const Point &p, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  146. {
  147. graphics->fonts[font]->renderTextLinesCenter(dst, CMessage::breakText(text, charpr, font), kolor, pos.topLeft() + p);
  148. }
  149. void CIntObject::addUsedEvents(ui16 newActions)
  150. {
  151. if (isActive())
  152. activateEvents(~used & newActions);
  153. used |= newActions;
  154. }
  155. void CIntObject::removeUsedEvents(ui16 newActions)
  156. {
  157. if (isActive())
  158. deactivateEvents(used & newActions);
  159. used &= ~newActions;
  160. }
  161. void CIntObject::disable()
  162. {
  163. if(isActive())
  164. deactivate();
  165. recActions = DISPOSE;
  166. }
  167. void CIntObject::enable()
  168. {
  169. if(!isActive() && (!parent_m || parent_m->isActive()))
  170. {
  171. activate();
  172. redraw();
  173. }
  174. recActions = 255;
  175. }
  176. void CIntObject::setEnabled(bool on)
  177. {
  178. if (on)
  179. enable();
  180. else
  181. disable();
  182. }
  183. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  184. {
  185. Point newPos = pos.topLeft();
  186. vstd::amax(newPos.x, borderWidth);
  187. vstd::amax(newPos.y, borderWidth);
  188. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  189. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  190. if (newPos != pos.topLeft())
  191. moveTo(newPos, propagate);
  192. }
  193. void CIntObject::moveBy(const Point & p, bool propagate)
  194. {
  195. pos.x += p.x;
  196. pos.y += p.y;
  197. if(propagate)
  198. for(auto & elem : children)
  199. elem->moveBy(p, propagate);
  200. }
  201. void CIntObject::moveTo(const Point & p, bool propagate)
  202. {
  203. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  204. }
  205. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  206. {
  207. if (vstd::contains(children, child))
  208. {
  209. return;
  210. }
  211. if (child->parent_m)
  212. {
  213. child->parent_m->removeChild(child, adjustPosition);
  214. }
  215. children.push_back(child);
  216. child->parent_m = this;
  217. if(adjustPosition)
  218. child->moveBy(pos.topLeft(), adjustPosition);
  219. if (!isActive() && child->isActive())
  220. child->deactivate();
  221. if (isActive()&& !child->isActive())
  222. child->activate();
  223. }
  224. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  225. {
  226. if (!child)
  227. return;
  228. if(!vstd::contains(children, child))
  229. throw std::runtime_error("Wrong child object");
  230. if(child->parent_m != this)
  231. throw std::runtime_error("Wrong child object");
  232. children -= child;
  233. child->parent_m = nullptr;
  234. if(adjustPosition)
  235. child->pos -= pos.topLeft();
  236. }
  237. void CIntObject::redraw()
  238. {
  239. //currently most of calls come from active objects so this check won't affect them
  240. //it should fix glitches when called by inactive elements located below active window
  241. if (isActive())
  242. {
  243. if (parent_m && (type & REDRAW_PARENT))
  244. {
  245. parent_m->redraw();
  246. }
  247. else
  248. {
  249. showAll(screenBuf);
  250. if(screenBuf != screen)
  251. showAll(screen);
  252. }
  253. }
  254. }
  255. bool CIntObject::isInside(const Point & position)
  256. {
  257. return pos.isInside(position);
  258. }
  259. void CIntObject::onScreenResize()
  260. {
  261. center(pos, true);
  262. }
  263. const Rect & CIntObject::center( const Rect &r, bool propagate )
  264. {
  265. pos.w = r.w;
  266. pos.h = r.h;
  267. return center(Point(GH.screenDimensions().x/2, GH.screenDimensions().y/2), propagate);
  268. }
  269. const Rect & CIntObject::center( bool propagate )
  270. {
  271. return center(pos, propagate);
  272. }
  273. const Rect & CIntObject::center(const Point & p, bool propagate)
  274. {
  275. moveBy(Point(p.x - pos.w/2 - pos.x,
  276. p.y - pos.h/2 - pos.y),
  277. propagate);
  278. return pos;
  279. }
  280. bool CIntObject::captureThisKey(EShortcut key)
  281. {
  282. return captureAllKeys;
  283. }
  284. CKeyShortcut::CKeyShortcut()
  285. : assignedKey(EShortcut::NONE)
  286. , shortcutPressed(false)
  287. {}
  288. CKeyShortcut::CKeyShortcut(EShortcut key)
  289. : assignedKey(key)
  290. {
  291. }
  292. void CKeyShortcut::keyPressed(EShortcut key)
  293. {
  294. if( assignedKey == key && assignedKey != EShortcut::NONE && !shortcutPressed)
  295. {
  296. shortcutPressed = true;
  297. clickLeft(true, false);
  298. }
  299. }
  300. void CKeyShortcut::keyReleased(EShortcut key)
  301. {
  302. if( assignedKey == key && assignedKey != EShortcut::NONE && shortcutPressed)
  303. {
  304. shortcutPressed = false;
  305. clickLeft(false, true);
  306. }
  307. }
  308. WindowBase::WindowBase(int used_, Point pos_)
  309. : CIntObject(used_, pos_)
  310. {
  311. }
  312. void WindowBase::close()
  313. {
  314. if(!GH.windows().isTopWindow(this))
  315. logGlobal->error("Only top interface must be closed");
  316. GH.windows().popWindows(1);
  317. }
  318. IStatusBar::~IStatusBar()
  319. {}