2
0

CIntObject.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. activate();
  169. recActions = 255;
  170. }
  171. void CIntObject::setEnabled(bool on)
  172. {
  173. if (on)
  174. enable();
  175. else
  176. disable();
  177. }
  178. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  179. {
  180. Point newPos = pos.topLeft();
  181. vstd::amax(newPos.x, borderWidth);
  182. vstd::amax(newPos.y, borderWidth);
  183. vstd::amin(newPos.x, GH.screenDimensions().x - borderWidth - pos.w);
  184. vstd::amin(newPos.y, GH.screenDimensions().y - borderWidth - pos.h);
  185. if (newPos != pos.topLeft())
  186. moveTo(newPos, propagate);
  187. }
  188. void CIntObject::moveBy(const Point & p, bool propagate)
  189. {
  190. pos.x += p.x;
  191. pos.y += p.y;
  192. if(propagate)
  193. for(auto & elem : children)
  194. elem->moveBy(p, propagate);
  195. }
  196. void CIntObject::moveTo(const Point & p, bool propagate)
  197. {
  198. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  199. }
  200. void CIntObject::addChild(CIntObject * child, bool adjustPosition)
  201. {
  202. if (vstd::contains(children, child))
  203. {
  204. return;
  205. }
  206. if (child->parent_m)
  207. {
  208. child->parent_m->removeChild(child, adjustPosition);
  209. }
  210. children.push_back(child);
  211. child->parent_m = this;
  212. if(adjustPosition)
  213. child->pos += pos.topLeft();
  214. if (!active && child->active)
  215. child->deactivate();
  216. if (active && !child->active)
  217. child->activate();
  218. }
  219. void CIntObject::removeChild(CIntObject * child, bool adjustPosition)
  220. {
  221. if (!child)
  222. return;
  223. if(!vstd::contains(children, child))
  224. throw std::runtime_error("Wrong child object");
  225. if(child->parent_m != this)
  226. throw std::runtime_error("Wrong child object");
  227. children -= child;
  228. child->parent_m = nullptr;
  229. if(adjustPosition)
  230. child->pos -= pos.topLeft();
  231. }
  232. void CIntObject::redraw()
  233. {
  234. //currently most of calls come from active objects so this check won't affect them
  235. //it should fix glitches when called by inactive elements located below active window
  236. if (active)
  237. {
  238. if (parent_m && (type & REDRAW_PARENT))
  239. {
  240. parent_m->redraw();
  241. }
  242. else
  243. {
  244. showAll(screenBuf);
  245. if(screenBuf != screen)
  246. showAll(screen);
  247. }
  248. }
  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.topInt().get() != this)
  303. logGlobal->error("Only top interface must be closed");
  304. GH.popInts(1);
  305. }
  306. IStatusBar::~IStatusBar()
  307. {}