CIntObject.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "StdInc.h"
  2. #include "CIntObject.h"
  3. #include "CGuiHandler.h"
  4. #include "../SDL_Extensions.h"
  5. void CIntObject::activateLClick()
  6. {
  7. GH.lclickable.push_front(this);
  8. active |= LCLICK;
  9. }
  10. void CIntObject::deactivateLClick()
  11. {
  12. std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
  13. assert(hlp != GH.lclickable.end());
  14. GH.lclickable.erase(hlp);
  15. active &= ~LCLICK;
  16. }
  17. void CIntObject::clickLeft(tribool down, bool previousState)
  18. {
  19. }
  20. void CIntObject::activateRClick()
  21. {
  22. GH.rclickable.push_front(this);
  23. active |= RCLICK;
  24. }
  25. void CIntObject::deactivateRClick()
  26. {
  27. std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
  28. assert(hlp != GH.rclickable.end());
  29. GH.rclickable.erase(hlp);
  30. active &= ~RCLICK;
  31. }
  32. void CIntObject::clickRight(tribool down, bool previousState)
  33. {
  34. }
  35. void CIntObject::activateHover()
  36. {
  37. GH.hoverable.push_front(this);
  38. active |= HOVER;
  39. }
  40. void CIntObject::deactivateHover()
  41. {
  42. std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
  43. assert(hlp != GH.hoverable.end());
  44. GH.hoverable.erase(hlp);
  45. active &= ~HOVER;
  46. }
  47. void CIntObject::hover( bool on )
  48. {
  49. }
  50. void CIntObject::activateKeys()
  51. {
  52. GH.keyinterested.push_front(this);
  53. active |= KEYBOARD;
  54. }
  55. void CIntObject::deactivateKeys()
  56. {
  57. std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
  58. assert(hlp != GH.keyinterested.end());
  59. GH.keyinterested.erase(hlp);
  60. active &= ~KEYBOARD;
  61. }
  62. void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
  63. {
  64. }
  65. void CIntObject::activateMouseMove()
  66. {
  67. GH.motioninterested.push_front(this);
  68. active |= MOVE;
  69. }
  70. void CIntObject::deactivateMouseMove()
  71. {
  72. std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
  73. assert(hlp != GH.motioninterested.end());
  74. GH.motioninterested.erase(hlp);
  75. active &= ~MOVE;
  76. }
  77. void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  78. {
  79. }
  80. void CIntObject::activateTimer()
  81. {
  82. GH.timeinterested.push_back(this);
  83. active |= TIME;
  84. }
  85. void CIntObject::deactivateTimer()
  86. {
  87. std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
  88. assert(hlp != GH.timeinterested.end());
  89. GH.timeinterested.erase(hlp);
  90. active &= ~TIME;
  91. }
  92. void CIntObject::tick()
  93. {
  94. }
  95. CIntObject::CIntObject()
  96. {
  97. pressedL = pressedR = hovered = captureAllKeys = strongInterest = false;
  98. toNextTick = active = used = 0;
  99. recActions = defActions = GH.defActionsDef;
  100. pos.x = 0;
  101. pos.y = 0;
  102. pos.w = 0;
  103. pos.h = 0;
  104. if(GH.captureChildren)
  105. {
  106. assert(GH.createdObj.size());
  107. parent = GH.createdObj.front();
  108. parent->children.push_back(this);
  109. if(parent->defActions & SHARE_POS)
  110. {
  111. pos.x = parent->pos.x;
  112. pos.y = parent->pos.y;
  113. }
  114. }
  115. else
  116. parent = NULL;
  117. }
  118. void CIntObject::show( SDL_Surface * to )
  119. {
  120. if(defActions & UPDATE)
  121. for(size_t i = 0; i < children.size(); i++)
  122. if(children[i]->recActions & UPDATE)
  123. children[i]->show(to);
  124. }
  125. void CIntObject::showAll( SDL_Surface * to )
  126. {
  127. if(defActions & SHOWALL)
  128. {
  129. for(size_t i = 0; i < children.size(); i++)
  130. if(children[i]->recActions & SHOWALL)
  131. children[i]->showAll(to);
  132. }
  133. else
  134. show(to);
  135. }
  136. void CIntObject::activate()
  137. {
  138. assert(!active);
  139. active |= GENERAL;
  140. activate(used);
  141. if(defActions & ACTIVATE)
  142. for(size_t i = 0; i < children.size(); i++)
  143. if(children[i]->recActions & ACTIVATE)
  144. children[i]->activate();
  145. }
  146. void CIntObject::activate(ui16 what)
  147. {
  148. if(what & LCLICK)
  149. activateLClick();
  150. if(what & RCLICK)
  151. activateRClick();
  152. if(what & HOVER)
  153. activateHover();
  154. if(what & MOVE)
  155. activateMouseMove();
  156. if(what & KEYBOARD)
  157. activateKeys();
  158. if(what & TIME)
  159. activateTimer();
  160. if(what & WHEEL)
  161. activateWheel();
  162. if(what & DOUBLECLICK)
  163. activateDClick();
  164. }
  165. void CIntObject::deactivate()
  166. {
  167. assert(active);
  168. active &= ~ GENERAL;
  169. deactivate(used);
  170. assert(!active);
  171. if(defActions & DEACTIVATE)
  172. for(size_t i = 0; i < children.size(); i++)
  173. if(children[i]->recActions & DEACTIVATE)
  174. children[i]->deactivate();
  175. }
  176. void CIntObject::deactivate(ui16 what)
  177. {
  178. if(what & LCLICK)
  179. deactivateLClick();
  180. if(what & RCLICK)
  181. deactivateRClick();
  182. if(what & HOVER)
  183. deactivateHover();
  184. if(what & MOVE)
  185. deactivateMouseMove();
  186. if(what & KEYBOARD)
  187. deactivateKeys();
  188. if(what & TIME) // TIME is special
  189. deactivateTimer();
  190. if(what & WHEEL)
  191. deactivateWheel();
  192. if(what & DOUBLECLICK)
  193. deactivateDClick();
  194. }
  195. CIntObject::~CIntObject()
  196. {
  197. assert(!active); //do not delete active obj
  198. if(defActions & DISPOSE)
  199. for(size_t i = 0; i < children.size(); i++)
  200. if(children[i]->recActions & DISPOSE)
  201. delete children[i];
  202. if(parent && GH.createdObj.size()) //temporary object destroyed
  203. parent->children -= this;
  204. }
  205. void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  206. {
  207. CSDL_Ext::printAt(text, pos.x + x, pos.y + y, font, kolor, dst);
  208. }
  209. void CIntObject::printAtMiddleLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  210. {
  211. CSDL_Ext::printAtMiddle(text, pos.x + x, pos.y + y, font, kolor, dst);
  212. }
  213. void CIntObject::printAtMiddleLoc(const std::string & text, const SPoint &p, EFonts font, SDL_Color kolor, SDL_Surface * dst)
  214. {
  215. printAtMiddleLoc(text, p.x, p.y, font, kolor, dst);
  216. }
  217. void CIntObject::blitAtLoc( SDL_Surface * src, int x, int y, SDL_Surface * dst )
  218. {
  219. blitAt(src, pos.x + x, pos.y + y, dst);
  220. }
  221. void CIntObject::blitAtLoc(SDL_Surface * src, const SPoint &p, SDL_Surface * dst)
  222. {
  223. blitAtLoc(src, p.x, p.y, dst);
  224. }
  225. void CIntObject::printAtMiddleWBLoc( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  226. {
  227. CSDL_Ext::printAtMiddleWB(text, pos.x + x, pos.y + y, font, charpr, kolor, dst);
  228. }
  229. void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor, SDL_Surface * dst )
  230. {
  231. CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst);
  232. }
  233. void CIntObject::disable()
  234. {
  235. if(active)
  236. deactivate();
  237. recActions = DISPOSE;
  238. }
  239. void CIntObject::enable(bool activation)
  240. {
  241. if(!active && activation)
  242. activate();
  243. recActions = 255;
  244. }
  245. bool CIntObject::isItInLoc( const SDL_Rect &rect, int x, int y )
  246. {
  247. return isItIn(&rect, x - pos.x, y - pos.y);
  248. }
  249. bool CIntObject::isItInLoc( const SDL_Rect &rect, const SPoint &p )
  250. {
  251. return isItIn(&rect, p.x - pos.x, p.y - pos.y);
  252. }
  253. void CIntObject::activateWheel()
  254. {
  255. GH.wheelInterested.push_front(this);
  256. active |= WHEEL;
  257. }
  258. void CIntObject::deactivateWheel()
  259. {
  260. std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
  261. assert(hlp != GH.wheelInterested.end());
  262. GH.wheelInterested.erase(hlp);
  263. active &= ~WHEEL;
  264. }
  265. void CIntObject::wheelScrolled(bool down, bool in)
  266. {
  267. }
  268. void CIntObject::activateDClick()
  269. {
  270. GH.doubleClickInterested.push_front(this);
  271. active |= DOUBLECLICK;
  272. }
  273. void CIntObject::deactivateDClick()
  274. {
  275. std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
  276. assert(hlp != GH.doubleClickInterested.end());
  277. GH.doubleClickInterested.erase(hlp);
  278. active &= ~DOUBLECLICK;
  279. }
  280. void CIntObject::onDoubleClick()
  281. {
  282. }
  283. void CIntObject::fitToScreen(int borderWidth, bool propagate)
  284. {
  285. SPoint newPos = pos.topLeft();
  286. vstd::amax(newPos.x, borderWidth);
  287. vstd::amax(newPos.y, borderWidth);
  288. vstd::amin(newPos.x, screen->w - borderWidth - pos.w);
  289. vstd::amin(newPos.y, screen->h - borderWidth - pos.h);
  290. if (newPos != pos.topLeft())
  291. moveTo(newPos, propagate);
  292. }
  293. void CIntObject::moveBy( const SPoint &p, bool propagate /*= true*/ )
  294. {
  295. pos.x += p.x;
  296. pos.y += p.y;
  297. if(propagate)
  298. for(size_t i = 0; i < children.size(); i++)
  299. children[i]->moveBy(p, propagate);
  300. }
  301. void CIntObject::moveTo( const SPoint &p, bool propagate /*= true*/ )
  302. {
  303. moveBy(SPoint(p.x - pos.x, p.y - pos.y), propagate);
  304. }
  305. void CIntObject::delChild(CIntObject *child)
  306. {
  307. children -= child;
  308. delete child;
  309. }
  310. void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
  311. {
  312. assert(!vstd::contains(children, child));
  313. assert(child->parent == NULL);
  314. children.push_back(child);
  315. child->parent = this;
  316. if(adjustPosition)
  317. child->pos += pos;
  318. }
  319. void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
  320. {
  321. assert(vstd::contains(children, child));
  322. assert(child->parent == this);
  323. children -= child;
  324. child->parent = NULL;
  325. if(adjustPosition)
  326. child->pos -= pos;
  327. }
  328. void CIntObject::changeUsedEvents(ui16 what, bool enable, bool adjust /*= true*/)
  329. {
  330. if(enable)
  331. {
  332. used |= what;
  333. if(adjust && active)
  334. activate(what);
  335. }
  336. else
  337. {
  338. used &= ~what;
  339. if(adjust && active)
  340. deactivate(what);
  341. }
  342. }
  343. void CIntObject::drawBorderLoc(SDL_Surface * sur, const SRect &r, const int3 &color)
  344. {
  345. CSDL_Ext::drawBorder(sur, r + pos, color);
  346. }
  347. void CIntObject::redraw()
  348. {
  349. if (parent && (type & REDRAW_PARENT))
  350. {
  351. parent->redraw();
  352. }
  353. else
  354. {
  355. showAll(screenBuf);
  356. if(screenBuf != screen)
  357. showAll(screen);
  358. }
  359. }
  360. const SRect & CIntObject::center( const SRect &r, bool propagate )
  361. {
  362. pos.w = r.w;
  363. pos.h = r.h;
  364. return center(SPoint(screen->w/2, screen->h/2), propagate);
  365. }
  366. const SRect & CIntObject::center( bool propagate )
  367. {
  368. return center(pos, propagate);
  369. }
  370. const SRect & CIntObject::center(const SPoint &p, bool propagate /*= true*/)
  371. {
  372. moveBy(SPoint(p.x - pos.w/2 - pos.x,
  373. p.y - pos.h/2 - pos.y),
  374. propagate);
  375. return pos;
  376. }