CIntObject.cpp 7.5 KB

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