Container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "Base.h"
  2. #include "Container.h"
  3. #include "Layout.h"
  4. #include "AbsoluteLayout.h"
  5. #include "VerticalLayout.h"
  6. #include "Label.h"
  7. #include "Button.h"
  8. #include "CheckBox.h"
  9. #include "RadioButton.h"
  10. #include "Slider.h"
  11. #include "TextBox.h"
  12. namespace gameplay
  13. {
  14. Container::Container() : _layout(NULL)
  15. {
  16. }
  17. Container::Container(const Container& copy)
  18. {
  19. }
  20. Container::~Container()
  21. {
  22. std::vector<Control*>::iterator it;
  23. for (it = _controls.begin(); it < _controls.end(); it++)
  24. {
  25. SAFE_RELEASE((*it));
  26. }
  27. SAFE_RELEASE(_layout);
  28. }
  29. Container* Container::create(Layout::Type type)
  30. {
  31. Layout* layout = NULL;
  32. switch(type)
  33. {
  34. case Layout::LAYOUT_ABSOLUTE:
  35. layout = AbsoluteLayout::create();
  36. break;
  37. case Layout::LAYOUT_FLOW:
  38. break;
  39. case Layout::LAYOUT_VERTICAL:
  40. layout = VerticalLayout::create();
  41. break;
  42. }
  43. Container* container = new Container();
  44. container->_layout = layout;
  45. return container;
  46. }
  47. Container* Container::create(Theme::Style* style, Properties* properties, Theme* theme)
  48. {
  49. const char* layoutString = properties->getString("layout");
  50. Container* container = Container::create(getLayoutType(layoutString));
  51. container->init(style, properties);
  52. container->addControls(theme, properties);
  53. return container;
  54. }
  55. void Container::addControls(Theme* theme, Properties* properties)
  56. {
  57. // Add all the controls to this container.
  58. Properties* controlSpace = properties->getNextNamespace();
  59. while (controlSpace != NULL)
  60. {
  61. Control* control = NULL;
  62. const char* controlStyleName = controlSpace->getString("style");
  63. Theme::Style* controlStyle = NULL;
  64. if (controlStyleName)
  65. {
  66. controlStyle = theme->getStyle(controlStyleName);
  67. }
  68. std::string controlName(controlSpace->getNamespace());
  69. std::transform(controlName.begin(), controlName.end(), controlName.begin(), (int(*)(int))toupper);
  70. if (controlName == "LABEL")
  71. {
  72. control = Label::create(controlStyle, controlSpace);
  73. }
  74. else if (controlName == "BUTTON")
  75. {
  76. control = Button::create(controlStyle, controlSpace);
  77. }
  78. else if (controlName == "CHECKBOX")
  79. {
  80. control = CheckBox::create(controlStyle, controlSpace);
  81. }
  82. else if (controlName == "RADIOBUTTON")
  83. {
  84. control = RadioButton::create(controlStyle, controlSpace);
  85. }
  86. else if (controlName == "CONTAINER")
  87. {
  88. control = Container::create(controlStyle, controlSpace, theme);
  89. }
  90. else if (controlName == "SLIDER")
  91. {
  92. control = Slider::create(controlStyle, controlSpace);
  93. }
  94. else if (controlName == "TEXTBOX")
  95. {
  96. control = TextBox::create(controlStyle, controlSpace);
  97. }
  98. // Add the new control to the form.
  99. if (control)
  100. {
  101. addControl(control);
  102. }
  103. // Get the next control.
  104. controlSpace = properties->getNextNamespace();
  105. }
  106. }
  107. Layout* Container::getLayout()
  108. {
  109. return _layout;
  110. }
  111. unsigned int Container::addControl(Control* control)
  112. {
  113. _controls.push_back(control);
  114. return _controls.size() - 1;
  115. }
  116. void Container::insertControl(Control* control, unsigned int index)
  117. {
  118. std::vector<Control*>::iterator it = _controls.begin() + index;
  119. _controls.insert(it, control);
  120. }
  121. void Container::removeControl(unsigned int index)
  122. {
  123. std::vector<Control*>::iterator it = _controls.begin() + index;
  124. _controls.erase(it);
  125. }
  126. void Container::removeControl(const char* id)
  127. {
  128. std::vector<Control*>::iterator it;
  129. for (it = _controls.begin(); it < _controls.end(); it++)
  130. {
  131. Control* c = *it;
  132. if (strcmp(id, c->getID()) == 0)
  133. {
  134. _controls.erase(it);
  135. }
  136. }
  137. }
  138. void Container::removeControl(Control* control)
  139. {
  140. std::vector<Control*>::iterator it;
  141. for (it = _controls.begin(); it < _controls.end(); it++)
  142. {
  143. if (*it == control)
  144. {
  145. _controls.erase(it);
  146. }
  147. }
  148. }
  149. Control* Container::getControl(unsigned int index) const
  150. {
  151. std::vector<Control*>::const_iterator it = _controls.begin() + index;
  152. return *it;
  153. }
  154. Control* Container::getControl(const char* id) const
  155. {
  156. std::vector<Control*>::const_iterator it;
  157. for (it = _controls.begin(); it < _controls.end(); it++)
  158. {
  159. Control* c = *it;
  160. if (strcmp(id, c->getID()) == 0)
  161. {
  162. return c;
  163. }
  164. else if (c->isContainer())
  165. {
  166. Control* cc = ((Container*)c)->getControl(id);
  167. if (cc)
  168. {
  169. return cc;
  170. }
  171. }
  172. }
  173. return NULL;
  174. }
  175. std::vector<Control*> Container::getControls() const
  176. {
  177. return _controls;
  178. }
  179. void Container::update(const Rectangle& clip)
  180. {
  181. // Update this container's viewport.
  182. Control::update(clip);
  183. if (isDirty())
  184. {
  185. _layout->update(this);
  186. }
  187. }
  188. void Container::drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip)
  189. {
  190. // First draw our own border.
  191. Control::drawBorder(spriteBatch, clip);
  192. // Now call drawBorder on all controls within this container.
  193. //Vector2 pos(clip.x + _position.x, clip.y + _position.y);
  194. //const Rectangle newClip(clip.x + _position.x, clip.y + _position.y, _size.x, _size.y);
  195. std::vector<Control*>::const_iterator it;
  196. for (it = _controls.begin(); it < _controls.end(); it++)
  197. {
  198. Control* control = *it;
  199. control->drawBorder(spriteBatch, _clip);
  200. }
  201. }
  202. void Container::drawSprites(SpriteBatch* spriteBatch, const Rectangle& clip)
  203. {
  204. //const Rectangle newClip(clip.x + _position.x, clip.y + _position.y, _size.x, _size.y);
  205. std::vector<Control*>::const_iterator it;
  206. for (it = _controls.begin(); it < _controls.end(); it++)
  207. {
  208. Control* control = *it;
  209. control->drawSprites(spriteBatch, _clip);
  210. }
  211. _dirty = false;
  212. }
  213. void Container::drawText(const Rectangle& clip)
  214. {
  215. //const Rectangle newClip(clip.x + _position.x, clip.y + _position.y, _size.x, _size.y);
  216. std::vector<Control*>::const_iterator it;
  217. for (it = _controls.begin(); it < _controls.end(); it++)
  218. {
  219. Control* control = *it;
  220. control->drawText(_clip);
  221. }
  222. _dirty = false;
  223. }
  224. bool Container::isDirty()
  225. {
  226. if (_dirty)
  227. {
  228. return true;
  229. }
  230. else
  231. {
  232. std::vector<Control*>::const_iterator it;
  233. for (it = _controls.begin(); it < _controls.end(); it++)
  234. {
  235. if ((*it)->isDirty())
  236. {
  237. return true;
  238. }
  239. }
  240. }
  241. return false;
  242. }
  243. bool Container::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  244. {
  245. if (!isEnabled())
  246. {
  247. return false;
  248. }
  249. bool eventConsumed = false;
  250. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  251. Theme::Border border;
  252. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  253. if (containerRegion)
  254. {
  255. border = overlay->getContainerRegion()->getBorder();
  256. }
  257. Theme::Padding padding = _style->getPadding();
  258. float xPos = border.left + padding.left;
  259. float yPos = border.top + padding.top;
  260. std::vector<Control*>::const_iterator it;
  261. for (it = _controls.begin(); it < _controls.end(); it++)
  262. {
  263. Control* control = *it;
  264. if (!control->isEnabled())
  265. {
  266. continue;
  267. }
  268. const Rectangle& bounds = control->getBounds();
  269. if (control->getState() != Control::NORMAL ||
  270. (evt == Touch::TOUCH_PRESS &&
  271. x >= xPos + bounds.x &&
  272. x <= xPos + bounds.x + bounds.width &&
  273. y >= yPos + bounds.y &&
  274. y <= yPos + bounds.y + bounds.height))
  275. {
  276. // Pass on the event's clip relative to the control.
  277. eventConsumed |= control->touchEvent(evt, x - xPos - bounds.x, y - yPos - bounds.y, contactIndex);
  278. }
  279. }
  280. if (!isEnabled())
  281. {
  282. return (_consumeTouchEvents | eventConsumed);
  283. }
  284. switch (evt)
  285. {
  286. case Touch::TOUCH_PRESS:
  287. setState(Control::FOCUS);
  288. break;
  289. case Touch::TOUCH_RELEASE:
  290. setState(Control::NORMAL);
  291. break;
  292. }
  293. return (_consumeTouchEvents | eventConsumed);
  294. }
  295. void Container::keyEvent(Keyboard::KeyEvent evt, int key)
  296. {
  297. std::vector<Control*>::const_iterator it;
  298. for (it = _controls.begin(); it < _controls.end(); it++)
  299. {
  300. Control* control = *it;
  301. if (!control->isEnabled())
  302. {
  303. continue;
  304. }
  305. if (control->isContainer() || control->getState() == Control::FOCUS)
  306. {
  307. control->keyEvent(evt, key);
  308. }
  309. }
  310. }
  311. bool Container::isContainer()
  312. {
  313. return true;
  314. }
  315. Layout::Type Container::getLayoutType(const char* layoutString)
  316. {
  317. if (!layoutString)
  318. {
  319. return Layout::LAYOUT_ABSOLUTE;
  320. }
  321. std::string layoutName(layoutString);
  322. std::transform(layoutName.begin(), layoutName.end(), layoutName.begin(), (int(*)(int))toupper);
  323. if (layoutName == "LAYOUT_ABSOLUTE")
  324. {
  325. return Layout::LAYOUT_ABSOLUTE;
  326. }
  327. else if (layoutName == "LAYOUT_VERTICAL")
  328. {
  329. return Layout::LAYOUT_VERTICAL;
  330. }
  331. else if (layoutName == "LAYOUT_FLOW")
  332. {
  333. return Layout::LAYOUT_FLOW;
  334. }
  335. else
  336. {
  337. // Default.
  338. return Layout::LAYOUT_ABSOLUTE;
  339. }
  340. }
  341. }