CIntObject.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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::printAtRightLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::WHITE*/, SDL_Surface * dst/*=screen*/ )
  123. {
  124. graphics->fonts[font]->renderTextRight(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/*=Colors::WHITE*/, SDL_Surface * dst/*=screen*/ )
  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. 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::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst )
  147. {
  148. graphics->fonts[font]->renderTextRight(dst, text, kolor, Point(pos.x + x, pos.y + y));
  149. }
  150. void CIntObject::addUsedEvents(ui16 newActions)
  151. {
  152. if (active_m)
  153. activate(~used & newActions);
  154. used |= newActions;
  155. }
  156. void CIntObject::removeUsedEvents(ui16 newActions)
  157. {
  158. if (active_m)
  159. deactivate(used & newActions);
  160. used &= ~newActions;
  161. }
  162. void CIntObject::disable()
  163. {
  164. if(active)
  165. deactivate();
  166. recActions = DISPOSE;
  167. }
  168. void CIntObject::enable()
  169. {
  170. if(!active_m && parent_m->active)
  171. activate();
  172. recActions = 255;
  173. }
  174. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  175. {
  176. return isItIn(&rect, x - pos.x, y - pos.y);
  177. }
  178. bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
  179. {
  180. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  181. }
  182. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  183. {
  184. Point newPos = pos.topLeft();
  185. vstd::amax(newPos.x, borderWidth);
  186. vstd::amax(newPos.y, borderWidth);
  187. vstd::amin(newPos.x, screen->w - borderWidth - pos.w);
  188. vstd::amin(newPos.y, screen->h - borderWidth - pos.h);
  189. if (newPos != pos.topLeft())
  190. moveTo(newPos, propagate);
  191. }
  192. void CIntObject::moveBy( const Point &p, bool propagate /*= true*/ )
  193. {
  194. pos.x += p.x;
  195. pos.y += p.y;
  196. if(propagate)
  197. for(auto & elem : children)
  198. elem->moveBy(p, propagate);
  199. }
  200. void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
  201. {
  202. moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
  203. }
  204. void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
  205. {
  206. if (vstd::contains(children, child))
  207. {
  208. return;
  209. }
  210. if (child->parent_m)
  211. {
  212. child->parent_m->removeChild(child, adjustPosition);
  213. }
  214. children.push_back(child);
  215. child->parent_m = this;
  216. if(adjustPosition)
  217. child->pos += pos;
  218. if (!active && child->active)
  219. child->deactivate();
  220. if (active && !child->active)
  221. child->activate();
  222. }
  223. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  224. {
  225. if (!child)
  226. return;
  227. assert(vstd::contains(children, child));
  228. assert(child->parent_m == this);
  229. children -= child;
  230. child->parent_m = nullptr;
  231. if(adjustPosition)
  232. child->pos -= pos;
  233. }
  234. void CIntObject::drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color)
  235. {
  236. CSDL_Ext::drawBorder(sur, r + pos, color);
  237. }
  238. void CIntObject::redraw()
  239. {
  240. //currently most of calls come from active objects so this check won't affect them
  241. //it should fix glitches when called by inactive elements located below active window
  242. if (active)
  243. {
  244. if (parent_m && (type & REDRAW_PARENT))
  245. {
  246. parent_m->redraw();
  247. }
  248. else
  249. {
  250. showAll(screenBuf);
  251. if(screenBuf != screen)
  252. showAll(screen);
  253. }
  254. }
  255. }
  256. const Rect & CIntObject::center( const Rect &r, bool propagate )
  257. {
  258. pos.w = r.w;
  259. pos.h = r.h;
  260. return center(Point(screen->w/2, screen->h/2), propagate);
  261. }
  262. const Rect & CIntObject::center( bool propagate )
  263. {
  264. return center(pos, propagate);
  265. }
  266. const Rect & CIntObject::center(const Point &p, bool propagate /*= true*/)
  267. {
  268. moveBy(Point(p.x - pos.w/2 - pos.x,
  269. p.y - pos.h/2 - pos.y),
  270. propagate);
  271. return pos;
  272. }
  273. bool CIntObject::captureThisEvent(const SDL_KeyboardEvent & key)
  274. {
  275. return captureAllKeys;
  276. }
  277. CKeyShortcut::CKeyShortcut()
  278. {}
  279. CKeyShortcut::CKeyShortcut(int key)
  280. {
  281. if (key != SDLK_UNKNOWN)
  282. assignedKeys.insert(key);
  283. }
  284. CKeyShortcut::CKeyShortcut(std::set<int> Keys)
  285. :assignedKeys(Keys)
  286. {}
  287. void CKeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  288. {
  289. if(vstd::contains(assignedKeys,key.keysym.sym)
  290. || vstd::contains(assignedKeys, CGuiHandler::numToDigit(key.keysym.sym)))
  291. {
  292. bool prev = pressedL;
  293. if(key.state == SDL_PRESSED)
  294. {
  295. pressedL = true;
  296. clickLeft(true, prev);
  297. }
  298. else
  299. {
  300. pressedL = false;
  301. clickLeft(false, prev);
  302. }
  303. }
  304. }