CIntObject.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "StdInc.h"
  2. #include "CIntObject.h"
  3. #include "CGuiHandler.h"
  4. #include "SDL_Extensions.h"
  5. #include "../CMessage.h"
  6. void ILockedUpdatable::runLocked(std::function<void(IUpdateable*)> cb)
  7. {
  8. boost::unique_lock<boost::recursive_mutex> lock(updateGuard);
  9. cb(this);
  10. }
  11. CIntObject::CIntObject(int used_, Point pos_):
  12. parent_m(nullptr),
  13. active_m(0),
  14. parent(parent_m),
  15. active(active_m)
  16. {
  17. pressedL = pressedR = hovered = captureAllKeys = strongInterest = false;
  18. toNextTick = timerDelay = 0;
  19. used = used_;
  20. recActions = defActions = GH.defActionsDef;
  21. pos.x = pos_.x;
  22. pos.y = pos_.y;
  23. pos.w = 0;
  24. pos.h = 0;
  25. if(GH.captureChildren)
  26. GH.createdObj.front()->addChild(this, true);
  27. }
  28. void CIntObject::setTimer(int msToTrigger)
  29. {
  30. if (!(active & TIME))
  31. activate(TIME);
  32. toNextTick = timerDelay = msToTrigger;
  33. used |= TIME;
  34. }
  35. void CIntObject::onTimer(int timePassed)
  36. {
  37. toNextTick -= timePassed;
  38. if (toNextTick < 0)
  39. {
  40. toNextTick += timerDelay;
  41. tick();
  42. }
  43. }
  44. void CIntObject::show(SDL_Surface * to)
  45. {
  46. if(defActions & UPDATE)
  47. for(auto & elem : children)
  48. if(elem->recActions & UPDATE)
  49. elem->show(to);
  50. }
  51. void CIntObject::showAll(SDL_Surface * to)
  52. {
  53. if(defActions & SHOWALL)
  54. {
  55. for(auto & elem : children)
  56. if(elem->recActions & SHOWALL)
  57. elem->showAll(to);
  58. }
  59. }
  60. void CIntObject::activate()
  61. {
  62. if (active_m)
  63. {
  64. if ((used | GENERAL) == active_m)
  65. return;
  66. else
  67. {
  68. logGlobal->warnStream() << "Warning: IntObject re-activated with mismatching used and active";
  69. deactivate(); //FIXME: better to avoid such possibility at all
  70. }
  71. }
  72. active_m |= GENERAL;
  73. activate(used);
  74. if(defActions & ACTIVATE)
  75. for(auto & elem : children)
  76. if(elem->recActions & ACTIVATE)
  77. elem->activate();
  78. }
  79. void CIntObject::activate(ui16 what)
  80. {
  81. GH.handleElementActivate(this, what);
  82. }
  83. void CIntObject::deactivate()
  84. {
  85. if (!active_m)
  86. return;
  87. active_m &= ~ GENERAL;
  88. deactivate(active_m);
  89. assert(!active_m);
  90. if(defActions & DEACTIVATE)
  91. for(auto & elem : children)
  92. if(elem->recActions & DEACTIVATE)
  93. elem->deactivate();
  94. }
  95. void CIntObject::deactivate(ui16 what)
  96. {
  97. GH.handleElementDeActivate(this, what);
  98. }
  99. CIntObject::~CIntObject()
  100. {
  101. if (active_m)
  102. deactivate();
  103. if(defActions & DISPOSE)
  104. {
  105. while (!children.empty())
  106. if(children.front()->recActions & DISPOSE)
  107. delete children.front();
  108. else
  109. removeChild(children.front());
  110. }
  111. if(parent_m)
  112. parent_m->removeChild(this);
  113. }
  114. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::WHITE*/, SDL_Surface * dst/*=screen*/ )
  115. {
  116. graphics->fonts[font]->renderTextLeft(dst, text, kolor, Point(pos.x + x, pos.y + y));
  117. }
  118. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::WHITE*/, SDL_Surface * dst/*=screen*/ )
  119. {
  120. printAtMiddleLoc(text, Point(x,y), font, kolor, dst);
  121. }
  122. void CIntObject::printAtMiddleLoc(const std::string & text, const Point &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  123. {
  124. graphics->fonts[font]->renderTextCenter(dst, text, kolor, pos.topLeft() + p);
  125. }
  126. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  127. {
  128. blitAt(src, pos.x + x, pos.y + y, dst);
  129. }
  130. void CIntObject::blitAtLoc(SDL_Surface * src, const Point &p, SDL_Surface * dst)
  131. {
  132. blitAtLoc(src, p.x, p.y, dst);
  133. }
  134. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  135. {
  136. graphics->fonts[font]->renderTextLinesCenter(dst, CMessage::breakText(text, charpr, font), kolor, Point(pos.x + x, pos.y + y));
  137. }
  138. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst )
  139. {
  140. graphics->fonts[font]->renderTextRight(dst, text, kolor, Point(pos.x + x, pos.y + y));
  141. }
  142. void CIntObject::addUsedEvents(ui16 newActions)
  143. {
  144. if (active_m)
  145. activate(~used & newActions);
  146. used |= newActions;
  147. }
  148. void CIntObject::removeUsedEvents(ui16 newActions)
  149. {
  150. if (active_m)
  151. deactivate(used & newActions);
  152. used &= ~newActions;
  153. }
  154. void CIntObject::disable()
  155. {
  156. if(active)
  157. deactivate();
  158. recActions = DISPOSE;
  159. }
  160. void CIntObject::enable()
  161. {
  162. if(!active_m && parent_m->active)
  163. activate();
  164. recActions = 255;
  165. }
  166. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  167. {
  168. return isItIn(&rect, x - pos.x, y - pos.y);
  169. }
  170. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  171. {
  172. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  173. }
  174. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  175. {
  176. Point newPos = pos.topLeft();
  177. vstd::amax(newPos.x, borderWidth);
  178. vstd::amax(newPos.y, borderWidth);
  179. vstd::amin(newPos.x, screen->w - borderWidth - pos.w);
  180. vstd::amin(newPos.y, screen->h - borderWidth - pos.h);
  181. if (newPos != pos.topLeft())
  182. moveTo(newPos, propagate);
  183. }
  184. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  185. {
  186. pos.x += p.x;
  187. pos.y += p.y;
  188. if(propagate)
  189. for(auto & elem : children)
  190. elem->moveBy(p, propagate);
  191. }
  192. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  193. {
  194. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  195. }
  196. void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
  197. {
  198. if (vstd::contains(children, child))
  199. {
  200. return;
  201. }
  202. if (child->parent_m)
  203. {
  204. child->parent_m->removeChild(child, adjustPosition);
  205. }
  206. children.push_back(child);
  207. child->parent_m = this;
  208. if(adjustPosition)
  209. child->pos += pos;
  210. if (!active && child->active)
  211. child->deactivate();
  212. if (active && !child->active)
  213. child->activate();
  214. }
  215. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  216. {
  217. if (!child)
  218. return;
  219. assert(vstd::contains(children, child));
  220. assert(child->parent_m == this);
  221. children -= child;
  222. child->parent_m = nullptr;
  223. if(adjustPosition)
  224. child->pos -= pos;
  225. }
  226. void CIntObject::drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color)
  227. {
  228. CSDL_Ext::drawBorder(sur, r + pos, color);
  229. }
  230. void CIntObject::redraw()
  231. {
  232. //currently most of calls come from active objects so this check won't affect them
  233. //it should fix glitches when called by inactive elements located below active window
  234. if (active)
  235. {
  236. if (parent_m && (type & REDRAW_PARENT))
  237. {
  238. parent_m->redraw();
  239. }
  240. else
  241. {
  242. showAll(screenBuf);
  243. if(screenBuf != screen)
  244. showAll(screen);
  245. }
  246. }
  247. }
  248. const Rect & CIntObject::center( const Rect &r, bool propagate )
  249. {
  250. pos.w = r.w;
  251. pos.h = r.h;
  252. return center(Point(screen->w/2, screen->h/2), propagate);
  253. }
  254. const Rect & CIntObject::center( bool propagate )
  255. {
  256. return center(pos, propagate);
  257. }
  258. const Rect & CIntObject::center(const Point &p, bool propagate /*= true*/)
  259. {
  260. moveBy(Point(p.x - pos.w/2 - pos.x,
  261. p.y - pos.h/2 - pos.y),
  262. propagate);
  263. return pos;
  264. }
  265. bool CIntObject::captureThisEvent(const SDL_KeyboardEvent & key)
  266. {
  267. return captureAllKeys;
  268. }
  269. void CKeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  270. {
  271. if(vstd::contains(assignedKeys,key.keysym.sym)
  272. || vstd::contains(assignedKeys, CGuiHandler::numToDigit(key.keysym.sym)))
  273. {
  274. bool prev = pressedL;
  275. if(key.state == SDL_PRESSED)
  276. {
  277. pressedL = true;
  278. clickLeft(true, prev);
  279. }
  280. else
  281. {
  282. pressedL = false;
  283. clickLeft(false, prev);
  284. }
  285. }
  286. }