CIntObject.cpp 7.5 KB

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