Container.cpp 11 KB

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