Container.cpp 35 KB

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