CIntObject.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  136. {
  137. CSDL_Ext::blitAt(src, pos.x + x, pos.y + y, dst);
  138. }
  139. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  140. {
  141. blitAtLoc(src, p.x, p.y, dst);
  142. }
  143. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  144. {
  145. graphics->fonts[font]->renderTextLinesCenter(dst, CMessage::breakText(text, charpr, font), kolor, Point(pos.x + x, pos.y + y));
  146. }
  147. void CIntObject::addUsedEvents(ui16 newActions)
  148. {
  149. if (active_m)
  150. activate(~used & newActions);
  151. used |= newActions;
  152. }
  153. void CIntObject::removeUsedEvents(ui16 newActions)
  154. {
  155. if (active_m)
  156. deactivate(used & newActions);
  157. used &= ~newActions;
  158. }
  159. void CIntObject::disable()
  160. {
  161. if(active)
  162. deactivate();
  163. recActions = DISPOSE;
  164. }
  165. void CIntObject::enable()
  166. {
  167. if(!active_m && (!parent_m || parent_m->active))
  168. {
  169. activate();
  170. redraw();
  171. }
  172. recActions = 255;
  173. }
  174. void CIntObject::setEnabled(bool on)
  175. {
  176. if (on)
  177. enable();
  178. else
  179. disable();
  180. }
  181. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  182. {
  183. Point newPos = pos.topLeft();
  184. vstd::amax(newPos.x, borderWidth);
  185. vstd::amax(newPos.y, borderWidth);
  186. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  187. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  188. if (newPos != pos.topLeft())
  189. moveTo(newPos, propagate);
  190. }
  191. void CIntObject::moveBy(const Point & p, bool propagate)
  192. {
  193. pos.x += p.x;
  194. pos.y += p.y;
  195. if(propagate)
  196. for(auto & elem : children)
  197. elem->moveBy(p, propagate);
  198. }
  199. void CIntObject::moveTo(const Point & p, bool propagate)
  200. {
  201. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  202. }
  203. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  204. {
  205. if (vstd::contains(children, child))
  206. {
  207. return;
  208. }
  209. if (child->parent_m)
  210. {
  211. child->parent_m->removeChild(child, adjustPosition);
  212. }
  213. children.push_back(child);
  214. child->parent_m = this;
  215. if(adjustPosition)
  216. child->moveBy(pos.topLeft(), adjustPosition);
  217. if (!active && child->active)
  218. child->deactivate();
  219. if (active && !child->active)
  220. child->activate();
  221. }
  222. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  223. {
  224. if (!child)
  225. return;
  226. if(!vstd::contains(children, child))
  227. throw std::runtime_error("Wrong child object");
  228. if(child->parent_m != this)
  229. throw std::runtime_error("Wrong child object");
  230. children -= child;
  231. child->parent_m = nullptr;
  232. if(adjustPosition)
  233. child->pos -= pos.topLeft();
  234. }
  235. void CIntObject::redraw()
  236. {
  237. //currently most of calls come from active objects so this check won't affect them
  238. //it should fix glitches when called by inactive elements located below active window
  239. if (active)
  240. {
  241. if (parent_m && (type & REDRAW_PARENT))
  242. {
  243. parent_m->redraw();
  244. }
  245. else
  246. {
  247. showAll(screenBuf);
  248. if(screenBuf != screen)
  249. showAll(screen);
  250. }
  251. }
  252. }
  253. void CIntObject::onScreenResize()
  254. {
  255. center(pos, true);
  256. }
  257. const Rect & CIntObject::center( const Rect &r, bool propagate )
  258. {
  259. pos.w = r.w;
  260. pos.h = r.h;
  261. return center(Point(GH.screenDimensions().x/2, GH.screenDimensions().y/2), propagate);
  262. }
  263. const Rect & CIntObject::center( bool propagate )
  264. {
  265. return center(pos, propagate);
  266. }
  267. const Rect & CIntObject::center(const Point & p, bool propagate)
  268. {
  269. moveBy(Point(p.x - pos.w/2 - pos.x,
  270. p.y - pos.h/2 - pos.y),
  271. propagate);
  272. return pos;
  273. }
  274. bool CIntObject::captureThisKey(EShortcut key)
  275. {
  276. return captureAllKeys;
  277. }
  278. CKeyShortcut::CKeyShortcut()
  279. : assignedKey(EShortcut::NONE)
  280. {}
  281. CKeyShortcut::CKeyShortcut(EShortcut key)
  282. : assignedKey(key)
  283. {
  284. }
  285. void CKeyShortcut::keyPressed(EShortcut key)
  286. {
  287. if( assignedKey == key && assignedKey != EShortcut::NONE)
  288. {
  289. bool prev = mouseState(MouseButton::LEFT);
  290. updateMouseState(MouseButton::LEFT, true);
  291. clickLeft(true, prev);
  292. }
  293. }
  294. void CKeyShortcut::keyReleased(EShortcut key)
  295. {
  296. if( assignedKey == key && assignedKey != EShortcut::NONE)
  297. {
  298. bool prev = mouseState(MouseButton::LEFT);
  299. updateMouseState(MouseButton::LEFT, false);
  300. clickLeft(false, prev);
  301. }
  302. }
  303. WindowBase::WindowBase(int used_, Point pos_)
  304. : CIntObject(used_, pos_)
  305. {
  306. }
  307. void WindowBase::close()
  308. {
  309. if(GH.topInt().get() != this)
  310. logGlobal->error("Only top interface must be closed");
  311. GH.popInts(1);
  312. }
  313. IStatusBar::~IStatusBar()
  314. {}