Container.cpp 35 KB

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