Container.cpp 11 KB

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