Container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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->initialize(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. Animation* Container::getAnimation(const char* id) const
  180. {
  181. std::vector<Control*>::const_iterator itr = _controls.begin();
  182. std::vector<Control*>::const_iterator end = _controls.end();
  183. Control* control = NULL;
  184. for (; itr != end; itr++)
  185. {
  186. control = *itr;
  187. Animation* animation = control->getAnimation(id);
  188. if (animation)
  189. return animation;
  190. if (control->isContainer())
  191. {
  192. animation = ((Container*)control)->getAnimation(id);
  193. if (animation)
  194. return animation;
  195. }
  196. }
  197. return NULL;
  198. }
  199. void Container::update(const Rectangle& clip)
  200. {
  201. // Update this container's viewport.
  202. Control::update(clip);
  203. _layout->update(this);
  204. }
  205. void Container::drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip)
  206. {
  207. // First draw our own border.
  208. Control::drawBorder(spriteBatch, clip);
  209. // Now call drawBorder on all controls within this container.
  210. std::vector<Control*>::const_iterator it;
  211. for (it = _controls.begin(); it < _controls.end(); it++)
  212. {
  213. Control* control = *it;
  214. control->drawBorder(spriteBatch, _clip);
  215. }
  216. }
  217. void Container::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  218. {
  219. std::vector<Control*>::const_iterator it;
  220. for (it = _controls.begin(); it < _controls.end(); it++)
  221. {
  222. Control* control = *it;
  223. control->drawImages(spriteBatch, _clip);
  224. }
  225. _dirty = false;
  226. }
  227. void Container::drawText(const Rectangle& clip)
  228. {
  229. std::vector<Control*>::const_iterator it;
  230. for (it = _controls.begin(); it < _controls.end(); it++)
  231. {
  232. Control* control = *it;
  233. control->drawText(_clip);
  234. }
  235. _dirty = false;
  236. }
  237. bool Container::isDirty()
  238. {
  239. if (_dirty)
  240. {
  241. return true;
  242. }
  243. else
  244. {
  245. std::vector<Control*>::const_iterator it;
  246. for (it = _controls.begin(); it < _controls.end(); it++)
  247. {
  248. if ((*it)->isDirty())
  249. {
  250. return true;
  251. }
  252. }
  253. }
  254. return false;
  255. }
  256. bool Container::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  257. {
  258. if (!isEnabled())
  259. {
  260. return false;
  261. }
  262. bool eventConsumed = false;
  263. const Theme::Border& border = getBorder(_state);
  264. const Theme::Padding& padding = getPadding();
  265. float xPos = border.left + padding.left;
  266. float yPos = border.top + padding.top;
  267. std::vector<Control*>::const_iterator it;
  268. for (it = _controls.begin(); it < _controls.end(); it++)
  269. {
  270. Control* control = *it;
  271. if (!control->isEnabled())
  272. {
  273. continue;
  274. }
  275. const Rectangle& bounds = control->getClipBounds();
  276. if (control->getState() != Control::NORMAL ||
  277. (evt == Touch::TOUCH_PRESS &&
  278. x >= xPos + bounds.x &&
  279. x <= xPos + bounds.x + bounds.width &&
  280. y >= yPos + bounds.y &&
  281. y <= yPos + bounds.y + bounds.height))
  282. {
  283. // Pass on the event's clip relative to the control.
  284. eventConsumed |= control->touchEvent(evt, x - xPos - bounds.x, y - yPos - bounds.y, contactIndex);
  285. }
  286. }
  287. if (!isEnabled())
  288. {
  289. return (_consumeTouchEvents | eventConsumed);
  290. }
  291. switch (evt)
  292. {
  293. case Touch::TOUCH_PRESS:
  294. setState(Control::FOCUS);
  295. break;
  296. case Touch::TOUCH_RELEASE:
  297. setState(Control::NORMAL);
  298. break;
  299. }
  300. return (_consumeTouchEvents | eventConsumed);
  301. }
  302. void Container::keyEvent(Keyboard::KeyEvent evt, int key)
  303. {
  304. std::vector<Control*>::const_iterator it;
  305. for (it = _controls.begin(); it < _controls.end(); it++)
  306. {
  307. Control* control = *it;
  308. if (!control->isEnabled())
  309. {
  310. continue;
  311. }
  312. if (control->isContainer() || control->getState() == Control::FOCUS)
  313. {
  314. control->keyEvent(evt, key);
  315. }
  316. }
  317. }
  318. bool Container::isContainer()
  319. {
  320. return true;
  321. }
  322. Layout::Type Container::getLayoutType(const char* layoutString)
  323. {
  324. if (!layoutString)
  325. {
  326. return Layout::LAYOUT_ABSOLUTE;
  327. }
  328. std::string layoutName(layoutString);
  329. std::transform(layoutName.begin(), layoutName.end(), layoutName.begin(), (int(*)(int))toupper);
  330. if (layoutName == "LAYOUT_ABSOLUTE")
  331. {
  332. return Layout::LAYOUT_ABSOLUTE;
  333. }
  334. else if (layoutName == "LAYOUT_VERTICAL")
  335. {
  336. return Layout::LAYOUT_VERTICAL;
  337. }
  338. else if (layoutName == "LAYOUT_FLOW")
  339. {
  340. return Layout::LAYOUT_FLOW;
  341. }
  342. else
  343. {
  344. // Default.
  345. return Layout::LAYOUT_ABSOLUTE;
  346. }
  347. }
  348. }