Container.cpp 11 KB

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