CIntObject.cpp 7.7 KB

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