CIntObject.cpp 7.7 KB

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