2
0

CIntObject.cpp 7.8 KB

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