CIntObject.cpp 8.1 KB

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