2
0

CIntObject.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. tlog1 << "Warning: IntObject re-activated with mismatching used and active\n";
  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. // tlog4<< "Warning: object already assigned to this parent!\n";
  196. return;
  197. }
  198. if (child->parent_m)
  199. {
  200. // tlog4<< "Warning: object already has parent!\n";
  201. child->parent_m->removeChild(child, adjustPosition);
  202. }
  203. children.push_back(child);
  204. child->parent_m = this;
  205. if(adjustPosition)
  206. child->pos += pos;
  207. if (!active && child->active)
  208. child->deactivate();
  209. if (active && !child->active)
  210. child->activate();
  211. }
  212. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  213. {
  214. if (!child)
  215. return;
  216. assert(vstd::contains(children, child));
  217. assert(child->parent_m == this);
  218. children -= child;
  219. child->parent_m = NULL;
  220. if(adjustPosition)
  221. child->pos -= pos;
  222. }
  223. void CIntObject::drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color)
  224. {
  225. CSDL_Ext::drawBorder(sur, r + pos, color);
  226. }
  227. void CIntObject::redraw()
  228. {
  229. //currently most of calls come from active objects so this check won't affect them
  230. //it should fix glitches when called by inactive elements located below active window
  231. if (active)
  232. {
  233. if (parent_m && (type & REDRAW_PARENT))
  234. {
  235. parent_m->redraw();
  236. }
  237. else
  238. {
  239. showAll(screenBuf);
  240. if(screenBuf != screen)
  241. showAll(screen);
  242. }
  243. }
  244. }
  245. const Rect & CIntObject::center( const Rect &r, bool propagate )
  246. {
  247. pos.w = r.w;
  248. pos.h = r.h;
  249. return center(Point(screen->w/2, screen->h/2), propagate);
  250. }
  251. const Rect & CIntObject::center( bool propagate )
  252. {
  253. return center(pos, propagate);
  254. }
  255. const Rect & CIntObject::center(const Point &p, bool propagate /*= true*/)
  256. {
  257. moveBy(Point(p.x - pos.w/2 - pos.x,
  258. p.y - pos.h/2 - pos.y),
  259. propagate);
  260. return pos;
  261. }
  262. bool CIntObject::captureThisEvent(const SDL_KeyboardEvent & key)
  263. {
  264. return captureAllKeys;
  265. }
  266. void CKeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  267. {
  268. if(vstd::contains(assignedKeys,key.keysym.sym)
  269. || vstd::contains(assignedKeys, CGuiHandler::numToDigit(key.keysym.sym)))
  270. {
  271. bool prev = pressedL;
  272. if(key.state == SDL_PRESSED)
  273. {
  274. pressedL = true;
  275. clickLeft(true, prev);
  276. }
  277. else
  278. {
  279. pressedL = false;
  280. clickLeft(false, prev);
  281. }
  282. }
  283. }