Container.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. _scrollingLastX = _scrollingFirstX = x;
  630. _scrollingLastY = _scrollingFirstY = y;
  631. _scrollingVelocity.set(0, 0);
  632. _scrolling = true;
  633. _scrollingStartTimeX = _scrollingStartTimeY = 0;
  634. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  635. {
  636. _scrollBarOpacityClip->stop();
  637. _scrollBarOpacityClip = NULL;
  638. }
  639. _scrollBarOpacity = 1.0f;
  640. return _consumeInputEvents;
  641. case Touch::TOUCH_MOVE:
  642. if (_scrolling)
  643. {
  644. double gameTime = Game::getAbsoluteTime();
  645. // Calculate the latest movement delta for the next update to use.
  646. int vx = x - _scrollingLastX;
  647. int vy = y - _scrollingLastY;
  648. if (_scrollingMouseVertically)
  649. {
  650. float yRatio = _totalHeight / _absoluteBounds.height;
  651. vy *= yRatio;
  652. _scrollingVelocity.set(0, -vy);
  653. _scrollPosition.y -= vy;
  654. }
  655. else if (_scrollingMouseHorizontally)
  656. {
  657. float xRatio = _totalWidth / _absoluteBounds.width;
  658. vx *= xRatio;
  659. _scrollingVelocity.set(-vx, 0);
  660. _scrollPosition.x -= vx;
  661. }
  662. else
  663. {
  664. _scrollingVelocity.set(vx, vy);
  665. _scrollPosition.x += vx;
  666. _scrollPosition.y += vy;
  667. }
  668. _scrollingLastX = x;
  669. _scrollingLastY = y;
  670. // If the user changes direction, reset the start time and position.
  671. bool goingRight = (vx > 0);
  672. if (goingRight != _scrollingRight)
  673. {
  674. _scrollingFirstX = x;
  675. _scrollingRight = goingRight;
  676. _scrollingStartTimeX = gameTime;
  677. }
  678. bool goingDown = (vy > 0);
  679. if (goingDown != _scrollingDown)
  680. {
  681. _scrollingFirstY = y;
  682. _scrollingDown = goingDown;
  683. _scrollingStartTimeY = gameTime;
  684. }
  685. if (!_scrollingStartTimeX)
  686. _scrollingStartTimeX = gameTime;
  687. if (!_scrollingStartTimeY)
  688. _scrollingStartTimeY = gameTime;
  689. _scrollingLastTime = gameTime;
  690. return _consumeInputEvents;
  691. }
  692. break;
  693. case Touch::TOUCH_RELEASE:
  694. _scrolling = false;
  695. double gameTime = Game::getAbsoluteTime();
  696. float timeSinceLastMove = (float)(gameTime - _scrollingLastTime);
  697. if (timeSinceLastMove > SCROLL_INERTIA_DELAY)
  698. {
  699. _scrollingVelocity.set(0, 0);
  700. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  701. return _consumeInputEvents;
  702. }
  703. int dx = _scrollingLastX - _scrollingFirstX;
  704. int dy = _scrollingLastY - _scrollingFirstY;
  705. float timeTakenX = (float)(gameTime - _scrollingStartTimeX);
  706. float elapsedSecsX = timeTakenX * 0.001f;
  707. float timeTakenY = (float)(gameTime - _scrollingStartTimeY);
  708. float elapsedSecsY = timeTakenY * 0.001f;
  709. float vx = dx;
  710. float vy = dy;
  711. if (elapsedSecsX > 0)
  712. vx = (float)dx / elapsedSecsX;
  713. if (elapsedSecsY > 0)
  714. vy = (float)dy / elapsedSecsY;
  715. if (_scrollingMouseVertically)
  716. {
  717. float yRatio = _totalHeight / _absoluteBounds.height;
  718. vy *= yRatio;
  719. _scrollingVelocity.set(0, -vy);
  720. }
  721. else if (_scrollingMouseHorizontally)
  722. {
  723. float xRatio = _totalWidth / _absoluteBounds.width;
  724. vx *= xRatio;
  725. _scrollingVelocity.set(-vx, 0);
  726. }
  727. else
  728. {
  729. _scrollingVelocity.set(vx, vy);
  730. }
  731. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  732. return _consumeInputEvents;
  733. }
  734. return false;
  735. }
  736. bool Container::mouseEventScroll(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  737. {
  738. switch(evt)
  739. {
  740. case Mouse::MOUSE_PRESS_LEFT_BUTTON:
  741. {
  742. if (_scrollBarVertical)
  743. {
  744. float vWidth = _scrollBarVertical->getRegion().width;
  745. Rectangle vBounds(_viewportBounds.x + _viewportBounds.width - vWidth,
  746. _scrollBarBounds.y,
  747. vWidth, _scrollBarBounds.height);
  748. if (x + _viewportBounds.x >= vBounds.x &&
  749. x + _viewportBounds.x <= vBounds.x + vBounds.width)
  750. {
  751. // Then we're within the horizontal bounds of the verticle scrollbar.
  752. // We want to either jump up or down, or drag the scrollbar itself.
  753. if (y < vBounds.y)
  754. {
  755. _scrollPosition.y += _totalHeight / 5.0f;
  756. }
  757. else if (y > vBounds.y + vBounds.height)
  758. {
  759. _scrollPosition.y -= _totalHeight / 5.0f;
  760. }
  761. else
  762. {
  763. _scrollingMouseVertically = true;
  764. }
  765. }
  766. }
  767. if (_scrollBarHorizontal)
  768. {
  769. float hHeight = _scrollBarHorizontal->getRegion().height;
  770. Rectangle hBounds(_scrollBarBounds.x,
  771. _viewportBounds.y + _viewportBounds.height - hHeight,
  772. _scrollBarBounds.width, hHeight);
  773. if (y + _viewportBounds.y >= hBounds.y &&
  774. y + _viewportBounds.y <= hBounds.y + hBounds.height)
  775. {
  776. // We're within the vertical bounds of the horizontal scrollbar.
  777. if (x < hBounds.x)
  778. _scrollPosition.x += _totalWidth / 5.0f;
  779. else if (x > hBounds.x + hBounds.width)
  780. _scrollPosition.x -= _totalWidth / 5.0f;
  781. else
  782. _scrollingMouseHorizontally = true;
  783. }
  784. }
  785. return touchEventScroll(Touch::TOUCH_PRESS, x, y, 0);
  786. }
  787. case Mouse::MOUSE_MOVE:
  788. return touchEventScroll(Touch::TOUCH_MOVE, x, y, 0);
  789. case Mouse::MOUSE_RELEASE_LEFT_BUTTON:
  790. return touchEventScroll(Touch::TOUCH_RELEASE, x, y, 0);
  791. case Mouse::MOUSE_WHEEL:
  792. _scrollPosition.y += (_totalHeight / 10.0f) * wheelDelta;
  793. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  794. {
  795. _scrollBarOpacityClip->stop();
  796. _scrollBarOpacityClip = NULL;
  797. }
  798. _scrollBarOpacity = 1.0f;
  799. return _consumeInputEvents;
  800. }
  801. return false;
  802. }
  803. bool Container::pointerEvent(bool mouse, char evt, int x, int y, int data)
  804. {
  805. if (!isEnabled())
  806. {
  807. return false;
  808. }
  809. bool eventConsumed = false;
  810. const Theme::Border& border = getBorder(_state);
  811. const Theme::Padding& padding = getPadding();
  812. float xPos = border.left + padding.left;
  813. float yPos = border.top + padding.top;
  814. Vector2* offset = NULL;
  815. if (_scroll != SCROLL_NONE)
  816. {
  817. offset = &_scrollPosition;
  818. }
  819. std::vector<Control*>::const_iterator it;
  820. for (it = _controls.begin(); it < _controls.end(); it++)
  821. {
  822. Control* control = *it;
  823. GP_ASSERT(control);
  824. if (!control->isEnabled())
  825. {
  826. continue;
  827. }
  828. const Rectangle& bounds = control->getBounds();
  829. float boundsX = bounds.x;
  830. float boundsY = bounds.y;
  831. if (offset)
  832. {
  833. boundsX += offset->x;
  834. boundsY += offset->y;
  835. }
  836. Control::State currentState = control->getState();
  837. if ((control->isContainer() && currentState == Control::FOCUS) ||
  838. (currentState != Control::NORMAL && control->_contactIndex == data) ||
  839. ((evt == Touch::TOUCH_PRESS ||
  840. evt == Mouse::MOUSE_PRESS_LEFT_BUTTON ||
  841. evt == Mouse::MOUSE_PRESS_MIDDLE_BUTTON ||
  842. evt == Mouse::MOUSE_PRESS_RIGHT_BUTTON ||
  843. evt == Mouse::MOUSE_WHEEL) &&
  844. x >= xPos + boundsX &&
  845. x <= xPos + boundsX + bounds.width &&
  846. y >= yPos + boundsY &&
  847. y <= yPos + boundsY + bounds.height))
  848. {
  849. // Pass on the event's clip relative to the control.
  850. if (mouse)
  851. eventConsumed |= control->mouseEvent((Mouse::MouseEvent)evt, x - xPos - boundsX, y - yPos - boundsY, data);
  852. else
  853. eventConsumed |= control->touchEvent((Touch::TouchEvent)evt, x - xPos - boundsX, y - yPos - boundsY, (unsigned int)data);
  854. }
  855. }
  856. if (!isEnabled())
  857. {
  858. return (_consumeInputEvents | eventConsumed);
  859. }
  860. switch (evt)
  861. {
  862. case Touch::TOUCH_PRESS:
  863. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  864. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  865. {
  866. setState(Control::FOCUS);
  867. if (eventConsumed)
  868. _contactIndices++;
  869. }
  870. else if (_contactIndices == 0)
  871. {
  872. setState(Control::NORMAL);
  873. return false;
  874. }
  875. break;
  876. case Touch::TOUCH_RELEASE:
  877. {
  878. if (eventConsumed)
  879. {
  880. if (_contactIndices > 0)
  881. _contactIndices--;
  882. }
  883. }
  884. break;
  885. }
  886. if (!eventConsumed && _scroll != SCROLL_NONE && getState() == Control::FOCUS)
  887. {
  888. if ((mouse && mouseEventScroll((Mouse::MouseEvent)evt, x - xPos, y - yPos, data)) ||
  889. (!mouse && touchEventScroll((Touch::TouchEvent)evt, x - xPos, y - yPos, (unsigned int)data)))
  890. {
  891. _dirty = true;
  892. eventConsumed = true;
  893. }
  894. }
  895. return (_consumeInputEvents | eventConsumed);
  896. }
  897. Container::Scroll Container::getScroll(const char* scroll)
  898. {
  899. if (!scroll)
  900. return Container::SCROLL_NONE;
  901. if (strcmp(scroll, "SCROLL_NONE") == 0)
  902. {
  903. return Container::SCROLL_NONE;
  904. }
  905. else if (strcmp(scroll, "SCROLL_HORIZONTAL") == 0)
  906. {
  907. return Container::SCROLL_HORIZONTAL;
  908. }
  909. else if (strcmp(scroll, "SCROLL_VERTICAL") == 0)
  910. {
  911. return Container::SCROLL_VERTICAL;
  912. }
  913. else if (strcmp(scroll, "SCROLL_BOTH") == 0)
  914. {
  915. return Container::SCROLL_BOTH;
  916. }
  917. else
  918. {
  919. GP_ERROR("Failed to get corresponding scroll state for unsupported value '%s'.", scroll);
  920. }
  921. return Container::SCROLL_NONE;
  922. }
  923. static bool sortControlsByZOrder(Control* c1, Control* c2)
  924. {
  925. if (c1->getZIndex() < c2->getZIndex())
  926. return true;
  927. return false;
  928. }
  929. unsigned int Container::getAnimationPropertyComponentCount(int propertyId) const
  930. {
  931. switch(propertyId)
  932. {
  933. case ANIMATE_OPACITY:
  934. return 1;
  935. default:
  936. return Control::getAnimationPropertyComponentCount(propertyId);
  937. }
  938. }
  939. void Container::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  940. {
  941. GP_ASSERT(value);
  942. switch(propertyId)
  943. {
  944. case ANIMATE_OPACITY:
  945. value->setFloat(0, _scrollBarOpacity);
  946. break;
  947. default:
  948. Control::getAnimationPropertyValue(propertyId, value);
  949. break;
  950. }
  951. }
  952. void Container::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  953. {
  954. GP_ASSERT(value);
  955. switch(propertyId)
  956. {
  957. case ANIMATE_OPACITY:
  958. _scrollBarOpacity = Curve::lerp(blendWeight, _opacity, value->getFloat(0));
  959. _dirty = true;
  960. break;
  961. default:
  962. Control::setAnimationPropertyValue(propertyId, value, blendWeight);
  963. break;
  964. }
  965. }
  966. }