Container.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. #include "Joystick.h"
  14. #include "Game.h"
  15. namespace gameplay
  16. {
  17. // If the user stops scrolling for this amount of time (in millis) before touch/click release, don't apply inertia.
  18. static const long SCROLL_INERTIA_DELAY = 100L;
  19. // Factor to multiply friction by before applying to velocity.
  20. static const float SCROLL_FRICTION_FACTOR = 5.0f;
  21. /**
  22. * Sort function for use with _controls.sort(), based on Z-Order.
  23. *
  24. * @param c1 The first control
  25. * @param c2 The second control
  26. * return true if the first controls z index is less than the second.
  27. */
  28. static bool sortControlsByZOrder(Control* c1, Control* c2);
  29. Container::Container()
  30. : _layout(NULL), _scrollBarTopCap(NULL), _scrollBarVertical(NULL), _scrollBarBottomCap(NULL),
  31. _scrollBarLeftCap(NULL), _scrollBarHorizontal(NULL), _scrollBarRightCap(NULL),
  32. _scroll(SCROLL_NONE), _scrollBarBounds(Rectangle::empty()), _scrollPosition(Vector2::zero()),
  33. _scrollBarsAutoHide(false), _scrollBarOpacity(1.0f), _scrolling(false),
  34. _scrollingFirstX(0), _scrollingFirstY(0), _scrollingLastX(0), _scrollingLastY(0),
  35. _scrollingStartTimeX(0), _scrollingStartTimeY(0), _scrollingLastTime(0),
  36. _scrollingVelocity(Vector2::zero()), _scrollingFriction(1.0f),
  37. _scrollingRight(false), _scrollingDown(false),
  38. _scrollingMouseVertically(false), _scrollingMouseHorizontally(false),
  39. _scrollBarOpacityClip(NULL), _zIndexDefault(0), _focusIndexDefault(0), _focusIndexMax(0), _totalWidth(0), _totalHeight(0),
  40. _contactIndices(0), _initializedWithScroll(false)
  41. {
  42. }
  43. Container::~Container()
  44. {
  45. std::vector<Control*>::iterator it;
  46. for (it = _controls.begin(); it < _controls.end(); it++)
  47. {
  48. SAFE_RELEASE((*it));
  49. }
  50. SAFE_RELEASE(_layout);
  51. }
  52. Container* Container::create(const char* id, Theme::Style* style, Layout::Type layoutType)
  53. {
  54. GP_ASSERT(style);
  55. Container* container = Container::create(layoutType);
  56. if (id)
  57. container->_id = id;
  58. container->_style = style;
  59. return container;
  60. }
  61. Container* Container::create(Layout::Type type)
  62. {
  63. Layout* layout = NULL;
  64. switch (type)
  65. {
  66. case Layout::LAYOUT_ABSOLUTE:
  67. layout = AbsoluteLayout::create();
  68. break;
  69. case Layout::LAYOUT_FLOW:
  70. layout = FlowLayout::create();
  71. break;
  72. case Layout::LAYOUT_VERTICAL:
  73. layout = VerticalLayout::create();
  74. break;
  75. }
  76. Container* container = new Container();
  77. container->_layout = layout;
  78. return container;
  79. }
  80. Container* Container::create(Theme::Style* style, Properties* properties, Theme* theme)
  81. {
  82. GP_ASSERT(properties);
  83. const char* layoutString = properties->getString("layout");
  84. Container* container = Container::create(getLayoutType(layoutString));
  85. container->initialize(style, properties);
  86. container->_scroll = getScroll(properties->getString("scroll"));
  87. container->_scrollBarsAutoHide = properties->getBool("scrollBarsAutoHide");
  88. if (container->_scrollBarsAutoHide)
  89. {
  90. container->_scrollBarOpacity = 0.0f;
  91. }
  92. container->addControls(theme, properties);
  93. container->_layout->update(container, container->_scrollPosition);
  94. return container;
  95. }
  96. void Container::addControls(Theme* theme, Properties* properties)
  97. {
  98. GP_ASSERT(theme);
  99. GP_ASSERT(properties);
  100. // Add all the controls to this container.
  101. Properties* controlSpace = properties->getNextNamespace();
  102. while (controlSpace != NULL)
  103. {
  104. Control* control = NULL;
  105. const char* controlStyleName = controlSpace->getString("style");
  106. Theme::Style* controlStyle = NULL;
  107. if (controlStyleName)
  108. {
  109. controlStyle = theme->getStyle(controlStyleName);
  110. }
  111. else
  112. {
  113. controlStyle = theme->getEmptyStyle();
  114. }
  115. std::string controlName(controlSpace->getNamespace());
  116. std::transform(controlName.begin(), controlName.end(), controlName.begin(), (int(*)(int))toupper);
  117. if (controlName == "LABEL")
  118. {
  119. control = Label::create(controlStyle, controlSpace);
  120. }
  121. else if (controlName == "BUTTON")
  122. {
  123. control = Button::create(controlStyle, controlSpace);
  124. }
  125. else if (controlName == "CHECKBOX")
  126. {
  127. control = CheckBox::create(controlStyle, controlSpace);
  128. }
  129. else if (controlName == "RADIOBUTTON")
  130. {
  131. control = RadioButton::create(controlStyle, controlSpace);
  132. }
  133. else if (controlName == "CONTAINER")
  134. {
  135. control = Container::create(controlStyle, controlSpace, theme);
  136. }
  137. else if (controlName == "SLIDER")
  138. {
  139. control = Slider::create(controlStyle, controlSpace);
  140. }
  141. else if (controlName == "TEXTBOX")
  142. {
  143. control = TextBox::create(controlStyle, controlSpace);
  144. }
  145. else if (controlName == "JOYSTICK")
  146. {
  147. control = Joystick::create(controlStyle, controlSpace);
  148. }
  149. else
  150. {
  151. GP_ERROR("Failed to create control; unrecognized control name '%s'.", controlName.c_str());
  152. }
  153. // Add the new control to the form.
  154. if (control)
  155. {
  156. addControl(control);
  157. control->release();
  158. if (control->getZIndex() == -1)
  159. {
  160. control->setZIndex(_zIndexDefault++);
  161. }
  162. if (control->getFocusIndex() == -1)
  163. {
  164. control->setFocusIndex(_focusIndexDefault++);
  165. }
  166. int focusIndex = control->getFocusIndex();
  167. if (focusIndex > _focusIndexMax)
  168. _focusIndexMax = focusIndex;
  169. }
  170. // Get the next control.
  171. controlSpace = properties->getNextNamespace();
  172. }
  173. // Sort controls by Z-Order.
  174. std::sort(_controls.begin(), _controls.end(), &sortControlsByZOrder);
  175. }
  176. Layout* Container::getLayout()
  177. {
  178. return _layout;
  179. }
  180. unsigned int Container::addControl(Control* control)
  181. {
  182. GP_ASSERT(control);
  183. if (control->_parent && control->_parent != this)
  184. {
  185. control->_parent->removeControl(control);
  186. }
  187. if (control->_parent != this)
  188. {
  189. _controls.push_back(control);
  190. control->addRef();
  191. control->_parent = this;
  192. return _controls.size() - 1;
  193. }
  194. else
  195. {
  196. // Control is already in this container.
  197. // Do nothing but determine and return control's index.
  198. const size_t size = _controls.size();
  199. for (size_t i = 0; i < size; ++i)
  200. {
  201. Control* c = _controls[i];
  202. if (c == control)
  203. {
  204. return i;
  205. }
  206. }
  207. // Should never reach this.
  208. GP_ASSERT(false);
  209. return 0;
  210. }
  211. }
  212. void Container::insertControl(Control* control, unsigned int index)
  213. {
  214. GP_ASSERT(control);
  215. if (control->_parent && control->_parent != this)
  216. {
  217. control->_parent->removeControl(control);
  218. }
  219. if (control->_parent != this)
  220. {
  221. std::vector<Control*>::iterator it = _controls.begin() + index;
  222. _controls.insert(it, control);
  223. control->addRef();
  224. control->_parent = this;
  225. }
  226. }
  227. void Container::removeControl(unsigned int index)
  228. {
  229. GP_ASSERT(index < _controls.size());
  230. std::vector<Control*>::iterator it = _controls.begin() + index;
  231. _controls.erase(it);
  232. Control* control = *it;
  233. control->_parent = NULL;
  234. SAFE_RELEASE(control);
  235. }
  236. void Container::removeControl(const char* id)
  237. {
  238. GP_ASSERT(id);
  239. std::vector<Control*>::iterator it;
  240. for (it = _controls.begin(); it < _controls.end(); it++)
  241. {
  242. Control* c = *it;
  243. if (strcmp(id, c->getId()) == 0)
  244. {
  245. SAFE_RELEASE(c);
  246. _controls.erase(it);
  247. return;
  248. }
  249. }
  250. }
  251. void Container::removeControl(Control* control)
  252. {
  253. GP_ASSERT(control);
  254. std::vector<Control*>::iterator it;
  255. for (it = _controls.begin(); it < _controls.end(); it++)
  256. {
  257. if (*it == control)
  258. {
  259. SAFE_RELEASE(control);
  260. _controls.erase(it);
  261. return;
  262. }
  263. }
  264. }
  265. Control* Container::getControl(unsigned int index) const
  266. {
  267. std::vector<Control*>::const_iterator it = _controls.begin() + index;
  268. return *it;
  269. }
  270. Control* Container::getControl(const char* id) const
  271. {
  272. GP_ASSERT(id);
  273. std::vector<Control*>::const_iterator it;
  274. for (it = _controls.begin(); it < _controls.end(); it++)
  275. {
  276. Control* c = *it;
  277. GP_ASSERT(c);
  278. if (strcmp(id, c->getId()) == 0)
  279. {
  280. return c;
  281. }
  282. else if (c->isContainer())
  283. {
  284. Control* cc = ((Container*)c)->getControl(id);
  285. if (cc)
  286. {
  287. return cc;
  288. }
  289. }
  290. }
  291. return NULL;
  292. }
  293. const std::vector<Control*>& Container::getControls() const
  294. {
  295. return _controls;
  296. }
  297. void Container::setScroll(Scroll scroll)
  298. {
  299. if (scroll != _scroll)
  300. {
  301. _scroll = scroll;
  302. _dirty = true;
  303. }
  304. }
  305. Container::Scroll Container::getScroll() const
  306. {
  307. return _scroll;
  308. }
  309. void Container::setScrollBarsAutoHide(bool autoHide)
  310. {
  311. if (autoHide != _scrollBarsAutoHide)
  312. {
  313. _scrollBarsAutoHide = autoHide;
  314. _dirty = true;
  315. }
  316. }
  317. bool Container::isScrollBarsAutoHide() const
  318. {
  319. return _scrollBarsAutoHide;
  320. }
  321. Animation* Container::getAnimation(const char* id) const
  322. {
  323. std::vector<Control*>::const_iterator itr = _controls.begin();
  324. std::vector<Control*>::const_iterator end = _controls.end();
  325. Control* control = NULL;
  326. for (; itr != end; itr++)
  327. {
  328. control = *itr;
  329. GP_ASSERT(control);
  330. Animation* animation = control->getAnimation(id);
  331. if (animation)
  332. return animation;
  333. if (control->isContainer())
  334. {
  335. animation = ((Container*)control)->getAnimation(id);
  336. if (animation)
  337. return animation;
  338. }
  339. }
  340. return NULL;
  341. }
  342. const char* Container::getType() const
  343. {
  344. return "container";
  345. }
  346. void Container::update(const Control* container, const Vector2& offset)
  347. {
  348. // Update this container's viewport.
  349. Control::update(container, offset);
  350. // Get scrollbar images and diminish clipping bounds to make room for scrollbars.
  351. if ((_scroll & SCROLL_HORIZONTAL) == SCROLL_HORIZONTAL)
  352. {
  353. _scrollBarLeftCap = getImage("scrollBarLeftCap", _state);
  354. _scrollBarHorizontal = getImage("horizontalScrollBar", _state);
  355. _scrollBarRightCap = getImage("scrollBarRightCap", _state);
  356. GP_ASSERT(_scrollBarLeftCap && _scrollBarHorizontal && _scrollBarRightCap);
  357. _viewportClipBounds.height -= _scrollBarHorizontal->getRegion().height;
  358. }
  359. if ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL)
  360. {
  361. _scrollBarTopCap = getImage("scrollBarTopCap", _state);
  362. _scrollBarVertical = getImage("verticalScrollBar", _state);
  363. _scrollBarBottomCap = getImage("scrollBarBottomCap", _state);
  364. GP_ASSERT(_scrollBarTopCap && _scrollBarVertical && _scrollBarBottomCap);
  365. _viewportClipBounds.width -= _scrollBarVertical->getRegion().width;
  366. }
  367. // Sort controls by Z-Order.
  368. std::sort(_controls.begin(), _controls.end(), &sortControlsByZOrder);
  369. GP_ASSERT(_layout);
  370. if (_scroll != SCROLL_NONE)
  371. updateScroll();
  372. else
  373. _layout->update(this, Vector2::zero());
  374. }
  375. void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsClear, bool cleared, float targetHeight)
  376. {
  377. if (needsClear)
  378. {
  379. GL_ASSERT( glEnable(GL_SCISSOR_TEST) );
  380. float clearY = targetHeight - _clearBounds.y - _clearBounds.height;
  381. GL_ASSERT( glScissor(_clearBounds.x, clearY, _clearBounds.width, _clearBounds.height) );
  382. Game::getInstance()->clear(Game::CLEAR_COLOR, Vector4::zero(), 1.0f, 0);
  383. GL_ASSERT( glDisable(GL_SCISSOR_TEST) );
  384. needsClear = false;
  385. cleared = true;
  386. }
  387. else if (!cleared)
  388. {
  389. needsClear = true;
  390. }
  391. spriteBatch->start();
  392. Control::drawBorder(spriteBatch, clip);
  393. spriteBatch->finish();
  394. std::vector<Control*>::const_iterator it;
  395. Rectangle boundsUnion = Rectangle::empty();
  396. for (it = _controls.begin(); it < _controls.end(); it++)
  397. {
  398. Control* control = *it;
  399. GP_ASSERT(control);
  400. if (!needsClear || control->isDirty() || control->_clearBounds.intersects(boundsUnion))
  401. {
  402. control->draw(spriteBatch, _viewportClipBounds, needsClear, cleared, targetHeight);
  403. Rectangle::combine(control->_clearBounds, boundsUnion, &boundsUnion);
  404. }
  405. }
  406. if (_scroll != SCROLL_NONE && (_scrollBarOpacity > 0.0f))
  407. {
  408. // Draw scroll bars.
  409. Rectangle clipRegion(_viewportClipBounds);
  410. spriteBatch->start();
  411. if (_scrollBarBounds.height > 0 &&
  412. ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL))
  413. {
  414. const Rectangle& topRegion = _scrollBarTopCap->getRegion();
  415. const Theme::UVs& topUVs = _scrollBarTopCap->getUVs();
  416. Vector4 topColor = _scrollBarTopCap->getColor();
  417. topColor.w *= _scrollBarOpacity * _opacity;
  418. const Rectangle& verticalRegion = _scrollBarVertical->getRegion();
  419. const Theme::UVs& verticalUVs = _scrollBarVertical->getUVs();
  420. Vector4 verticalColor = _scrollBarVertical->getColor();
  421. verticalColor.w *= _scrollBarOpacity * _opacity;
  422. const Rectangle& bottomRegion = _scrollBarBottomCap->getRegion();
  423. const Theme::UVs& bottomUVs = _scrollBarBottomCap->getUVs();
  424. Vector4 bottomColor = _scrollBarBottomCap->getColor();
  425. bottomColor.w *= _scrollBarOpacity * _opacity;
  426. clipRegion.width += verticalRegion.width;
  427. Rectangle bounds(_viewportBounds.x + _viewportBounds.width - verticalRegion.width, _viewportBounds.y + _scrollBarBounds.y, topRegion.width, topRegion.height);
  428. spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, topUVs.u1, topUVs.v1, topUVs.u2, topUVs.v2, topColor, clipRegion);
  429. bounds.y += topRegion.height;
  430. bounds.height = _scrollBarBounds.height - topRegion.height - bottomRegion.height;
  431. spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, verticalUVs.u1, verticalUVs.v1, verticalUVs.u2, verticalUVs.v2, verticalColor, clipRegion);
  432. bounds.y += bounds.height;
  433. bounds.height = bottomRegion.height;
  434. spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, bottomUVs.u1, bottomUVs.v1, bottomUVs.u2, bottomUVs.v2, bottomColor, clipRegion);
  435. }
  436. if (_scrollBarBounds.width > 0 &&
  437. ((_scroll & SCROLL_HORIZONTAL) == SCROLL_HORIZONTAL))
  438. {
  439. const Rectangle& leftRegion = _scrollBarLeftCap->getRegion();
  440. const Theme::UVs& leftUVs = _scrollBarLeftCap->getUVs();
  441. Vector4 leftColor = _scrollBarLeftCap->getColor();
  442. leftColor.w *= _scrollBarOpacity * _opacity;
  443. const Rectangle& horizontalRegion = _scrollBarHorizontal->getRegion();
  444. const Theme::UVs& horizontalUVs = _scrollBarHorizontal->getUVs();
  445. Vector4 horizontalColor = _scrollBarHorizontal->getColor();
  446. horizontalColor.w *= _scrollBarOpacity * _opacity;
  447. const Rectangle& rightRegion = _scrollBarRightCap->getRegion();
  448. const Theme::UVs& rightUVs = _scrollBarRightCap->getUVs();
  449. Vector4 rightColor = _scrollBarRightCap->getColor();
  450. rightColor.w *= _scrollBarOpacity * _opacity;
  451. clipRegion.height += horizontalRegion.height;
  452. Rectangle bounds(_viewportBounds.x + _scrollBarBounds.x, _viewportBounds.y + _viewportBounds.height - horizontalRegion.height, leftRegion.width, leftRegion.height);
  453. spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, leftUVs.u1, leftUVs.v1, leftUVs.u2, leftUVs.v2, leftColor, clipRegion);
  454. bounds.x += leftRegion.width;
  455. bounds.width = _scrollBarBounds.width - leftRegion.width - rightRegion.width;
  456. spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, horizontalUVs.u1, horizontalUVs.v1, horizontalUVs.u2, horizontalUVs.v2, horizontalColor, clipRegion);
  457. bounds.x += bounds.width;
  458. bounds.width = rightRegion.width;
  459. spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, rightUVs.u1, rightUVs.v1, rightUVs.u2, rightUVs.v2, rightColor, clipRegion);
  460. }
  461. spriteBatch->finish();
  462. if (_scrollingVelocity.isZero())
  463. {
  464. _dirty = false;
  465. }
  466. }
  467. else
  468. {
  469. _dirty = false;
  470. }
  471. }
  472. bool Container::isDirty()
  473. {
  474. if (_dirty)
  475. {
  476. return true;
  477. }
  478. else
  479. {
  480. std::vector<Control*>::const_iterator it;
  481. for (it = _controls.begin(); it < _controls.end(); it++)
  482. {
  483. GP_ASSERT(*it);
  484. if ((*it)->isDirty())
  485. {
  486. return true;
  487. }
  488. }
  489. }
  490. return false;
  491. }
  492. bool Container::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  493. {
  494. return pointerEvent(false, evt, x, y, (int)contactIndex);
  495. }
  496. bool Container::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  497. {
  498. return pointerEvent(true, evt, x, y, wheelDelta);
  499. }
  500. bool Container::keyEvent(Keyboard::KeyEvent evt, int key)
  501. {
  502. std::vector<Control*>::const_iterator it;
  503. for (it = _controls.begin(); it < _controls.end(); it++)
  504. {
  505. Control* control = *it;
  506. GP_ASSERT(control);
  507. if (!control->isEnabled())
  508. {
  509. continue;
  510. }
  511. if (control->getState() == Control::FOCUS)
  512. {
  513. if (control->keyEvent(evt, key))
  514. {
  515. return _consumeInputEvents;
  516. }
  517. else if (evt == Keyboard::KEY_CHAR && key == Keyboard::KEY_TAB)
  518. {
  519. // Shift focus to next control.
  520. int focusIndex = control->getFocusIndex() + 1; // Index to search for.
  521. if (focusIndex > _focusIndexMax)
  522. {
  523. focusIndex = 0;
  524. }
  525. control->setState(Control::NORMAL);
  526. std::vector<Control*>::const_iterator itt;
  527. for (itt = _controls.begin(); itt < _controls.end(); itt++)
  528. {
  529. Control* nextControl = *itt;
  530. if (nextControl->getFocusIndex() == focusIndex)
  531. {
  532. nextControl->setState(Control::FOCUS);
  533. return _consumeInputEvents;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. return false;
  540. }
  541. bool Container::isContainer() const
  542. {
  543. return true;
  544. }
  545. Layout::Type Container::getLayoutType(const char* layoutString)
  546. {
  547. if (!layoutString)
  548. {
  549. return Layout::LAYOUT_ABSOLUTE;
  550. }
  551. std::string layoutName(layoutString);
  552. std::transform(layoutName.begin(), layoutName.end(), layoutName.begin(), (int(*)(int))toupper);
  553. if (layoutName == "LAYOUT_ABSOLUTE")
  554. {
  555. return Layout::LAYOUT_ABSOLUTE;
  556. }
  557. else if (layoutName == "LAYOUT_VERTICAL")
  558. {
  559. return Layout::LAYOUT_VERTICAL;
  560. }
  561. else if (layoutName == "LAYOUT_FLOW")
  562. {
  563. return Layout::LAYOUT_FLOW;
  564. }
  565. else
  566. {
  567. // Default.
  568. return Layout::LAYOUT_ABSOLUTE;
  569. }
  570. }
  571. void Container::updateScroll()
  572. {
  573. if (!_initializedWithScroll)
  574. {
  575. _initializedWithScroll = true;
  576. _layout->update(this, _scrollPosition);
  577. }
  578. // Update Time.
  579. static double lastFrameTime = Game::getGameTime();
  580. double frameTime = Game::getGameTime();
  581. float elapsedTime = (float)(frameTime - lastFrameTime);
  582. lastFrameTime = frameTime;
  583. const Theme::Border& containerBorder = getBorder(_state);
  584. const Theme::Padding& containerPadding = getPadding();
  585. // Calculate total width and height.
  586. std::vector<Control*> controls = getControls();
  587. unsigned int controlsCount = controls.size();
  588. for (unsigned int i = 0; i < controlsCount; i++)
  589. {
  590. Control* control = controls.at(i);
  591. const Rectangle& bounds = control->getBounds();
  592. const Theme::Margin& margin = control->getMargin();
  593. float newWidth = bounds.x + bounds.width;
  594. if (newWidth > _totalWidth)
  595. {
  596. _totalWidth = newWidth;
  597. }
  598. float newHeight = bounds.y + bounds.height;
  599. if (newHeight > _totalHeight)
  600. {
  601. _totalHeight = newHeight;
  602. }
  603. }
  604. float vWidth = getImageRegion("verticalScrollBar", _state).width;
  605. float hHeight = getImageRegion("horizontalScrollBar", _state).height;
  606. float clipWidth = _bounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right - vWidth;
  607. float clipHeight = _bounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom - hHeight;
  608. // Apply and dampen inertia.
  609. if (!_scrolling && !_scrollingVelocity.isZero())
  610. {
  611. // Calculate the time passed since last update.
  612. float elapsedSecs = (float)elapsedTime * 0.001f;
  613. _scrollPosition.x += _scrollingVelocity.x * elapsedSecs;
  614. _scrollPosition.y += _scrollingVelocity.y * elapsedSecs;
  615. float dampening = 1.0f - _scrollingFriction * SCROLL_FRICTION_FACTOR * elapsedSecs;
  616. _scrollingVelocity.x *= dampening;
  617. _scrollingVelocity.y *= dampening;
  618. if (fabs(_scrollingVelocity.x) < 100.0f)
  619. _scrollingVelocity.x = 0.0f;
  620. if (fabs(_scrollingVelocity.y) < 100.0f)
  621. _scrollingVelocity.y = 0.0f;
  622. }
  623. // Stop scrolling when the far edge is reached.
  624. if (-_scrollPosition.x > _totalWidth - clipWidth)
  625. {
  626. _scrollPosition.x = -(_totalWidth - clipWidth);
  627. _scrollingVelocity.x = 0;
  628. }
  629. if (-_scrollPosition.y > _totalHeight - clipHeight)
  630. {
  631. _scrollPosition.y = -(_totalHeight - clipHeight);
  632. _scrollingVelocity.y = 0;
  633. }
  634. if (_scrollPosition.x > 0)
  635. {
  636. _scrollPosition.x = 0;
  637. _scrollingVelocity.x = 0;
  638. }
  639. if (_scrollPosition.y > 0)
  640. {
  641. _scrollPosition.y = 0;
  642. _scrollingVelocity.y = 0;
  643. }
  644. float scrollWidth = 0;
  645. if (clipWidth < _totalWidth)
  646. scrollWidth = (clipWidth / _totalWidth) * clipWidth;
  647. float scrollHeight = 0;
  648. if (clipHeight < _totalHeight)
  649. scrollHeight = (clipHeight / _totalHeight) * clipHeight;
  650. _scrollBarBounds.set(((-_scrollPosition.x) / _totalWidth) * clipWidth,
  651. ((-_scrollPosition.y) / _totalHeight) * clipHeight,
  652. scrollWidth, scrollHeight);
  653. // If scroll velocity is 0 and scrollbars are not always visible, trigger fade-out animation.
  654. if (!_scrolling && _scrollingVelocity.isZero() && _scrollBarsAutoHide && _scrollBarOpacity == 1.0f)
  655. {
  656. float to = 0;
  657. _scrollBarOpacity = 0.99f;
  658. if (!_scrollBarOpacityClip)
  659. {
  660. Animation* animation = createAnimationFromTo("scrollbar-fade-out", ANIMATE_OPACITY, &_scrollBarOpacity, &to, Curve::QUADRATIC_IN_OUT, 500L);
  661. _scrollBarOpacityClip = animation->getClip();
  662. }
  663. _scrollBarOpacityClip->play();
  664. }
  665. // Position controls within scroll area.
  666. _layout->update(this, _scrollPosition);
  667. }
  668. bool Container::touchEventScroll(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  669. {
  670. switch(evt)
  671. {
  672. case Touch::TOUCH_PRESS:
  673. if (_contactIndex == INVALID_CONTACT_INDEX)
  674. {
  675. _contactIndex = (int) contactIndex;
  676. _contactIndices++;
  677. _scrollingLastX = _scrollingFirstX = x;
  678. _scrollingLastY = _scrollingFirstY = y;
  679. _scrollingVelocity.set(0, 0);
  680. _scrolling = true;
  681. _scrollingStartTimeX = _scrollingStartTimeY = 0;
  682. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  683. {
  684. _scrollBarOpacityClip->stop();
  685. _scrollBarOpacityClip = NULL;
  686. }
  687. _scrollBarOpacity = 1.0f;
  688. _dirty = true;
  689. return _consumeInputEvents;
  690. }
  691. break;
  692. case Touch::TOUCH_MOVE:
  693. if (_scrolling && _contactIndex == (int) contactIndex)
  694. {
  695. double gameTime = Game::getAbsoluteTime();
  696. // Calculate the latest movement delta for the next update to use.
  697. int vx = x - _scrollingLastX;
  698. int vy = y - _scrollingLastY;
  699. if (_scrollingMouseVertically)
  700. {
  701. float yRatio = _totalHeight / _absoluteBounds.height;
  702. vy *= yRatio;
  703. _scrollingVelocity.set(0, -vy);
  704. _scrollPosition.y -= vy;
  705. }
  706. else if (_scrollingMouseHorizontally)
  707. {
  708. float xRatio = _totalWidth / _absoluteBounds.width;
  709. vx *= xRatio;
  710. _scrollingVelocity.set(-vx, 0);
  711. _scrollPosition.x -= vx;
  712. }
  713. else
  714. {
  715. _scrollingVelocity.set(vx, vy);
  716. _scrollPosition.x += vx;
  717. _scrollPosition.y += vy;
  718. }
  719. _scrollingLastX = x;
  720. _scrollingLastY = y;
  721. // If the user changes direction, reset the start time and position.
  722. bool goingRight = (vx > 0);
  723. if (goingRight != _scrollingRight)
  724. {
  725. _scrollingFirstX = x;
  726. _scrollingRight = goingRight;
  727. _scrollingStartTimeX = gameTime;
  728. }
  729. bool goingDown = (vy > 0);
  730. if (goingDown != _scrollingDown)
  731. {
  732. _scrollingFirstY = y;
  733. _scrollingDown = goingDown;
  734. _scrollingStartTimeY = gameTime;
  735. }
  736. if (!_scrollingStartTimeX)
  737. _scrollingStartTimeX = gameTime;
  738. if (!_scrollingStartTimeY)
  739. _scrollingStartTimeY = gameTime;
  740. _scrollingLastTime = gameTime;
  741. _dirty = true;
  742. return _consumeInputEvents;
  743. }
  744. break;
  745. case Touch::TOUCH_RELEASE:
  746. if (_contactIndex == (int) contactIndex)
  747. {
  748. _contactIndex = INVALID_CONTACT_INDEX;
  749. _contactIndices--;
  750. _scrolling = false;
  751. double gameTime = Game::getAbsoluteTime();
  752. float timeSinceLastMove = (float)(gameTime - _scrollingLastTime);
  753. if (timeSinceLastMove > SCROLL_INERTIA_DELAY)
  754. {
  755. _scrollingVelocity.set(0, 0);
  756. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  757. _dirty = true;
  758. return _consumeInputEvents;
  759. }
  760. int dx = _scrollingLastX - _scrollingFirstX;
  761. int dy = _scrollingLastY - _scrollingFirstY;
  762. float timeTakenX = (float)(gameTime - _scrollingStartTimeX);
  763. float elapsedSecsX = timeTakenX * 0.001f;
  764. float timeTakenY = (float)(gameTime - _scrollingStartTimeY);
  765. float elapsedSecsY = timeTakenY * 0.001f;
  766. float vx = dx;
  767. float vy = dy;
  768. if (elapsedSecsX > 0)
  769. vx = (float)dx / elapsedSecsX;
  770. if (elapsedSecsY > 0)
  771. vy = (float)dy / elapsedSecsY;
  772. if (_scrollingMouseVertically)
  773. {
  774. float yRatio = _totalHeight / _absoluteBounds.height;
  775. vy *= yRatio;
  776. _scrollingVelocity.set(0, -vy);
  777. }
  778. else if (_scrollingMouseHorizontally)
  779. {
  780. float xRatio = _totalWidth / _absoluteBounds.width;
  781. vx *= xRatio;
  782. _scrollingVelocity.set(-vx, 0);
  783. }
  784. else
  785. {
  786. _scrollingVelocity.set(vx, vy);
  787. }
  788. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  789. _dirty = true;
  790. return _consumeInputEvents;
  791. }
  792. break;
  793. }
  794. return false;
  795. }
  796. bool Container::mouseEventScroll(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  797. {
  798. switch(evt)
  799. {
  800. case Mouse::MOUSE_PRESS_LEFT_BUTTON:
  801. {
  802. if (_scrollBarVertical)
  803. {
  804. float vWidth = _scrollBarVertical->getRegion().width;
  805. Rectangle vBounds(_viewportBounds.x + _viewportBounds.width - vWidth,
  806. _scrollBarBounds.y,
  807. vWidth, _scrollBarBounds.height);
  808. if (x + _viewportBounds.x >= vBounds.x &&
  809. x + _viewportBounds.x <= vBounds.x + vBounds.width)
  810. {
  811. // Then we're within the horizontal bounds of the verticle scrollbar.
  812. // We want to either jump up or down, or drag the scrollbar itself.
  813. if (y < vBounds.y)
  814. {
  815. _scrollPosition.y += _totalHeight / 5.0f;
  816. }
  817. else if (y > vBounds.y + vBounds.height)
  818. {
  819. _scrollPosition.y -= _totalHeight / 5.0f;
  820. }
  821. else
  822. {
  823. _scrollingMouseVertically = true;
  824. }
  825. }
  826. }
  827. if (_scrollBarHorizontal)
  828. {
  829. float hHeight = _scrollBarHorizontal->getRegion().height;
  830. Rectangle hBounds(_scrollBarBounds.x,
  831. _viewportBounds.y + _viewportBounds.height - hHeight,
  832. _scrollBarBounds.width, hHeight);
  833. if (y + _viewportBounds.y >= hBounds.y &&
  834. y + _viewportBounds.y <= hBounds.y + hBounds.height)
  835. {
  836. // We're within the vertical bounds of the horizontal scrollbar.
  837. if (x < hBounds.x)
  838. _scrollPosition.x += _totalWidth / 5.0f;
  839. else if (x > hBounds.x + hBounds.width)
  840. _scrollPosition.x -= _totalWidth / 5.0f;
  841. else
  842. _scrollingMouseHorizontally = true;
  843. }
  844. }
  845. return touchEventScroll(Touch::TOUCH_PRESS, x, y, 0);
  846. }
  847. case Mouse::MOUSE_MOVE:
  848. return touchEventScroll(Touch::TOUCH_MOVE, x, y, 0);
  849. case Mouse::MOUSE_RELEASE_LEFT_BUTTON:
  850. return touchEventScroll(Touch::TOUCH_RELEASE, x, y, 0);
  851. case Mouse::MOUSE_WHEEL:
  852. _scrollPosition.y += (_totalHeight / 10.0f) * wheelDelta;
  853. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  854. {
  855. _scrollBarOpacityClip->stop();
  856. _scrollBarOpacityClip = NULL;
  857. }
  858. _scrollBarOpacity = 1.0f;
  859. _dirty = true;
  860. return _consumeInputEvents;
  861. }
  862. return false;
  863. }
  864. bool Container::pointerEvent(bool mouse, char evt, int x, int y, int data)
  865. {
  866. if (!isEnabled())
  867. {
  868. return false;
  869. }
  870. bool eventConsumed = false;
  871. const Theme::Border& border = getBorder(_state);
  872. const Theme::Padding& padding = getPadding();
  873. float xPos = border.left + padding.left;
  874. float yPos = border.top + padding.top;
  875. Vector2* offset = NULL;
  876. if (_scroll != SCROLL_NONE)
  877. {
  878. offset = &_scrollPosition;
  879. }
  880. std::vector<Control*>::const_iterator it;
  881. for (it = _controls.begin(); it < _controls.end(); it++)
  882. {
  883. Control* control = *it;
  884. GP_ASSERT(control);
  885. if (!control->isEnabled())
  886. {
  887. continue;
  888. }
  889. const Rectangle& bounds = control->getBounds();
  890. float boundsX = bounds.x;
  891. float boundsY = bounds.y;
  892. if (offset)
  893. {
  894. boundsX += offset->x;
  895. boundsY += offset->y;
  896. }
  897. Control::State currentState = control->getState();
  898. if ((control->isContainer() && currentState == Control::FOCUS) ||
  899. (currentState != Control::NORMAL) ||
  900. ((evt == Touch::TOUCH_PRESS ||
  901. evt == Mouse::MOUSE_PRESS_LEFT_BUTTON ||
  902. evt == Mouse::MOUSE_PRESS_MIDDLE_BUTTON ||
  903. evt == Mouse::MOUSE_PRESS_RIGHT_BUTTON) &&
  904. x >= xPos + boundsX &&
  905. x <= xPos + boundsX + bounds.width &&
  906. y >= yPos + boundsY &&
  907. y <= yPos + boundsY + bounds.height))
  908. {
  909. // Pass on the event's clip relative to the control.
  910. if (mouse)
  911. eventConsumed |= control->mouseEvent((Mouse::MouseEvent)evt, x - xPos - boundsX, y - yPos - boundsY, data);
  912. else
  913. eventConsumed |= control->touchEvent((Touch::TouchEvent)evt, x - xPos - boundsX, y - yPos - boundsY, (unsigned int)data);
  914. }
  915. }
  916. if (!isEnabled())
  917. {
  918. return (_consumeInputEvents | eventConsumed);
  919. }
  920. bool withinClipBounds = false;
  921. switch (evt)
  922. {
  923. case Touch::TOUCH_PRESS:
  924. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  925. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  926. {
  927. setState(Control::FOCUS);
  928. withinClipBounds = true;
  929. if (eventConsumed)
  930. _contactIndices++;
  931. }
  932. else if (_contactIndices == 0)
  933. {
  934. setState(Control::NORMAL);
  935. _contactIndex = INVALID_CONTACT_INDEX;
  936. return false;
  937. }
  938. break;
  939. case Touch::TOUCH_RELEASE:
  940. {
  941. if (eventConsumed)
  942. {
  943. if (_contactIndices > 0)
  944. _contactIndices--;
  945. }
  946. }
  947. break;
  948. }
  949. if (!eventConsumed && _scroll != SCROLL_NONE && getState() == Control::FOCUS && (evt != Touch::TOUCH_PRESS || withinClipBounds))
  950. {
  951. if ((mouse && mouseEventScroll((Mouse::MouseEvent)evt, x - xPos, y - yPos, data)) ||
  952. (!mouse && touchEventScroll((Touch::TouchEvent)evt, x - xPos, y - yPos, (unsigned int)data)))
  953. {
  954. eventConsumed = true;
  955. }
  956. }
  957. return (_consumeInputEvents | eventConsumed);
  958. }
  959. Container::Scroll Container::getScroll(const char* scroll)
  960. {
  961. if (!scroll)
  962. return Container::SCROLL_NONE;
  963. if (strcmp(scroll, "SCROLL_NONE") == 0)
  964. {
  965. return Container::SCROLL_NONE;
  966. }
  967. else if (strcmp(scroll, "SCROLL_HORIZONTAL") == 0)
  968. {
  969. return Container::SCROLL_HORIZONTAL;
  970. }
  971. else if (strcmp(scroll, "SCROLL_VERTICAL") == 0)
  972. {
  973. return Container::SCROLL_VERTICAL;
  974. }
  975. else if (strcmp(scroll, "SCROLL_BOTH") == 0)
  976. {
  977. return Container::SCROLL_BOTH;
  978. }
  979. else
  980. {
  981. GP_ERROR("Failed to get corresponding scroll state for unsupported value '%s'.", scroll);
  982. }
  983. return Container::SCROLL_NONE;
  984. }
  985. static bool sortControlsByZOrder(Control* c1, Control* c2)
  986. {
  987. if (c1->getZIndex() < c2->getZIndex())
  988. return true;
  989. return false;
  990. }
  991. unsigned int Container::getAnimationPropertyComponentCount(int propertyId) const
  992. {
  993. switch(propertyId)
  994. {
  995. case ANIMATE_OPACITY:
  996. return 1;
  997. default:
  998. return Control::getAnimationPropertyComponentCount(propertyId);
  999. }
  1000. }
  1001. void Container::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  1002. {
  1003. GP_ASSERT(value);
  1004. switch(propertyId)
  1005. {
  1006. case ANIMATE_OPACITY:
  1007. value->setFloat(0, _scrollBarOpacity);
  1008. break;
  1009. default:
  1010. Control::getAnimationPropertyValue(propertyId, value);
  1011. break;
  1012. }
  1013. }
  1014. void Container::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  1015. {
  1016. GP_ASSERT(value);
  1017. switch(propertyId)
  1018. {
  1019. case ANIMATE_OPACITY:
  1020. _scrollBarOpacity = Curve::lerp(blendWeight, _opacity, value->getFloat(0));
  1021. _dirty = true;
  1022. break;
  1023. default:
  1024. Control::setAnimationPropertyValue(propertyId, value, blendWeight);
  1025. break;
  1026. }
  1027. }
  1028. }