CIntObject.cpp 7.4 KB

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