Container.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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 "JoystickControl.h"
  14. #include "ImageControl.h"
  15. #include "Form.h"
  16. #include "Game.h"
  17. #include "ControlFactory.h"
  18. namespace gameplay
  19. {
  20. // If the user stops scrolling for this amount of time (in millis) before touch/click release, don't apply inertia.
  21. static const long SCROLL_INERTIA_DELAY = 100L;
  22. // Factor to multiply friction by before applying to velocity.
  23. static const float SCROLL_FRICTION_FACTOR = 5.0f;
  24. // Distance that must be scrolled before isScrolling() will return true, used e.g. to cancel button-click events.
  25. static const float SCROLL_THRESHOLD = 10.0f;
  26. // Number of milliseconds to fade auto-hide scrollbars out for
  27. static const long SCROLLBAR_FADE_TIME = 1500L;
  28. // If the DPad or joystick is held down, this is the initial delay in milliseconds between focus change events.
  29. static const float FOCUS_CHANGE_REPEAT_DELAY = 300.0f;
  30. /**
  31. * Sort function for use with _controls.sort(), based on Z-Order.
  32. *
  33. * @param c1 The first control
  34. * @param c2 The second control
  35. * return true if the first controls z index is less than the second.
  36. */
  37. static bool sortControlsByZOrder(Control* c1, Control* c2);
  38. void Container::clearContacts()
  39. {
  40. for (int i = 0; i < MAX_CONTACT_INDICES; ++i)
  41. _contactIndices[i] = false;
  42. }
  43. Container::Container()
  44. : _layout(NULL), _activeControl(NULL), _scrollBarTopCap(NULL), _scrollBarVertical(NULL), _scrollBarBottomCap(NULL),
  45. _scrollBarLeftCap(NULL), _scrollBarHorizontal(NULL), _scrollBarRightCap(NULL),
  46. _scroll(SCROLL_NONE), _scrollBarBounds(Rectangle::empty()), _scrollPosition(Vector2::zero()),
  47. _scrollBarsAutoHide(false), _scrollBarOpacity(1.0f), _scrolling(false),
  48. _scrollingVeryFirstX(0), _scrollingVeryFirstY(0), _scrollingFirstX(0), _scrollingFirstY(0), _scrollingLastX(0), _scrollingLastY(0),
  49. _scrollingStartTimeX(0), _scrollingStartTimeY(0), _scrollingLastTime(0),
  50. _scrollingVelocity(Vector2::zero()), _scrollingFriction(1.0f), _scrollWheelSpeed(400.0f),
  51. _scrollingRight(false), _scrollingDown(false),
  52. _scrollingMouseVertically(false), _scrollingMouseHorizontally(false),
  53. _scrollBarOpacityClip(NULL), _zIndexDefault(0),
  54. _selectButtonDown(false), _lastFrameTime(0), _totalWidth(0), _totalHeight(0),
  55. _initializedWithScroll(false), _scrollWheelRequiresFocus(false)
  56. {
  57. clearContacts();
  58. }
  59. Container::~Container()
  60. {
  61. std::vector<Control*>::iterator it;
  62. for (it = _controls.begin(); it < _controls.end(); it++)
  63. {
  64. SAFE_RELEASE((*it));
  65. }
  66. SAFE_RELEASE(_layout);
  67. }
  68. Container* Container::create(const char* id, Theme::Style* style, Layout::Type layout)
  69. {
  70. Container* container = new Container();
  71. container->_id = id ? id : "";
  72. container->_layout = createLayout(layout);
  73. container->initialize("Container", style, NULL);
  74. return container;
  75. }
  76. Control* Container::create(Theme::Style* style, Properties* properties)
  77. {
  78. Container* container = new Container();
  79. container->initialize("Container", style, properties);
  80. return container;
  81. }
  82. void Container::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  83. {
  84. Control::initialize(typeName, style, properties);
  85. if (properties)
  86. {
  87. // Parse layout
  88. Properties* layoutNS = properties->getNamespace("layout", true, false);
  89. if (layoutNS)
  90. {
  91. _layout = createLayout(getLayoutType(layoutNS->getString("type")));
  92. switch (_layout->getType())
  93. {
  94. case Layout::LAYOUT_FLOW:
  95. static_cast<FlowLayout*>(_layout)->setSpacing(layoutNS->getInt("horizontalSpacing"), layoutNS->getInt("verticalSpacing"));
  96. break;
  97. case Layout::LAYOUT_VERTICAL:
  98. static_cast<VerticalLayout*>(_layout)->setSpacing(layoutNS->getInt("spacing"));
  99. break;
  100. }
  101. }
  102. else
  103. {
  104. _layout = createLayout(getLayoutType(properties->getString("layout")));
  105. }
  106. setScroll(getScroll(properties->getString("scroll")));
  107. _scrollBarsAutoHide = properties->getBool("scrollBarsAutoHide");
  108. if (_scrollBarsAutoHide)
  109. {
  110. _scrollBarOpacity = 0.0f;
  111. }
  112. _scrollWheelRequiresFocus = properties->getBool("scrollWheelRequiresFocus");
  113. if (properties->exists("scrollingFriction"))
  114. _scrollingFriction = properties->getFloat("scrollingFriction");
  115. if (properties->exists("scrollWheelSpeed"))
  116. _scrollWheelSpeed = properties->getFloat("scrollWheelSpeed");
  117. addControls(properties);
  118. const char* activeControl = properties->getString("activeControl");
  119. if (activeControl)
  120. {
  121. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  122. {
  123. if (_controls[i]->_id == activeControl)
  124. {
  125. _activeControl = _controls[i];
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. // Create a default layout if one does not yet exist
  132. if (_layout == NULL)
  133. _layout = createLayout(Layout::LAYOUT_ABSOLUTE);
  134. }
  135. void Container::addControls(Properties* properties)
  136. {
  137. GP_ASSERT(properties);
  138. // Add all the controls to this container.
  139. Properties* controlSpace = properties->getNextNamespace();
  140. while (controlSpace != NULL)
  141. {
  142. const char* controlName = controlSpace->getNamespace();
  143. // Pass our own style into the creation of the child control.
  144. // The child control's style will be looked up using the passed in style's theme.
  145. Control* control = ControlFactory::getInstance()->createControl(controlName, _style, controlSpace);
  146. // Add the new control to the form.
  147. if (control)
  148. {
  149. addControl(control);
  150. control->release();
  151. }
  152. // Get the next control.
  153. controlSpace = properties->getNextNamespace();
  154. }
  155. // Sort controls by Z-Order.
  156. sortControls();
  157. }
  158. Layout* Container::getLayout()
  159. {
  160. return _layout;
  161. }
  162. void Container::setLayout(Layout::Type type)
  163. {
  164. if (_layout == NULL || _layout->getType() != type)
  165. {
  166. SAFE_RELEASE(_layout);
  167. _layout = createLayout(type);
  168. setDirty(Control::DIRTY_BOUNDS);
  169. }
  170. }
  171. unsigned int Container::addControl(Control* control)
  172. {
  173. GP_ASSERT(control);
  174. // Remove the control from its current parent
  175. if (control->_parent && control->_parent != this)
  176. {
  177. control->_parent->removeControl(control);
  178. }
  179. if (control->getZIndex() == -1)
  180. {
  181. control->setZIndex(_zIndexDefault++);
  182. }
  183. if (control->getFocusIndex() == -1)
  184. {
  185. // Find the current largest focus index
  186. int maxFocusIndex = 0;
  187. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  188. {
  189. if (_controls[i]->_focusIndex > maxFocusIndex)
  190. maxFocusIndex = _controls[i]->_focusIndex;
  191. }
  192. control->setFocusIndex(maxFocusIndex + 1);
  193. }
  194. if (control->_parent != this)
  195. {
  196. _controls.push_back(control);
  197. control->addRef();
  198. control->_parent = this;
  199. sortControls();
  200. return (unsigned int)(_controls.size() - 1);
  201. }
  202. else
  203. {
  204. // Control is already in this container.
  205. // Do nothing but determine and return control's index.
  206. const size_t size = _controls.size();
  207. for (size_t i = 0; i < size; ++i)
  208. {
  209. Control* c = _controls[i];
  210. if (c == control)
  211. {
  212. return (unsigned int)i;
  213. }
  214. }
  215. // Should never reach this.
  216. GP_ASSERT(false);
  217. return 0;
  218. }
  219. }
  220. void Container::insertControl(Control* control, unsigned int index)
  221. {
  222. GP_ASSERT(control);
  223. if (control->_parent && control->_parent != this)
  224. {
  225. control->_parent->removeControl(control);
  226. }
  227. if (control->_parent != this)
  228. {
  229. std::vector<Control*>::iterator it = _controls.begin() + index;
  230. _controls.insert(it, control);
  231. control->addRef();
  232. control->_parent = this;
  233. }
  234. }
  235. void Container::removeControl(unsigned int index)
  236. {
  237. GP_ASSERT(index < _controls.size());
  238. std::vector<Control*>::iterator it = _controls.begin() + index;
  239. Control* control = *it;
  240. _controls.erase(it);
  241. control->_parent = NULL;
  242. if (_activeControl == control)
  243. _activeControl = NULL;
  244. Form::verifyRemovedControlState(control);
  245. SAFE_RELEASE(control);
  246. }
  247. void Container::removeControl(const char* id)
  248. {
  249. GP_ASSERT(id);
  250. for (size_t i = 0, size = _controls.size(); i < size; ++i)
  251. {
  252. Control* c = _controls[i];
  253. if (strcmp(id, c->getId()) == 0)
  254. {
  255. removeControl((unsigned int)i);
  256. return;
  257. }
  258. }
  259. }
  260. void Container::removeControl(Control* control)
  261. {
  262. GP_ASSERT(control);
  263. for (size_t i = 0, size = _controls.size(); i < size; ++i)
  264. {
  265. Control* c = _controls[i];
  266. if (c == control)
  267. {
  268. removeControl((unsigned int)i);
  269. return;
  270. }
  271. }
  272. }
  273. Control* Container::getControl(unsigned int index) const
  274. {
  275. GP_ASSERT(index < _controls.size());
  276. return _controls[index];
  277. }
  278. Control* Container::getControl(const char* id) const
  279. {
  280. GP_ASSERT(id);
  281. std::vector<Control*>::const_iterator it;
  282. for (it = _controls.begin(); it < _controls.end(); it++)
  283. {
  284. Control* c = *it;
  285. GP_ASSERT(c);
  286. if (strcmp(id, c->getId()) == 0)
  287. {
  288. return c;
  289. }
  290. else if (c->isContainer())
  291. {
  292. Control* cc = ((Container*)c)->getControl(id);
  293. if (cc)
  294. {
  295. return cc;
  296. }
  297. }
  298. }
  299. return NULL;
  300. }
  301. unsigned int Container::getControlCount() const
  302. {
  303. return (unsigned int)_controls.size();
  304. }
  305. const std::vector<Control*>& Container::getControls() const
  306. {
  307. return _controls;
  308. }
  309. bool Container::isForm() const
  310. {
  311. return false;
  312. }
  313. void Container::setScroll(Scroll scroll)
  314. {
  315. if (scroll != _scroll)
  316. {
  317. _scroll = scroll;
  318. if (_scroll == SCROLL_NONE)
  319. {
  320. _scrollPosition.set(0, 0);
  321. }
  322. else
  323. {
  324. // Scrollable containers can be focused (to allow scrolling)
  325. _canFocus = true;
  326. }
  327. setDirty(DIRTY_BOUNDS | DIRTY_STATE);
  328. }
  329. }
  330. Container::Scroll Container::getScroll() const
  331. {
  332. return _scroll;
  333. }
  334. void Container::setScrollBarsAutoHide(bool autoHide)
  335. {
  336. if (autoHide != _scrollBarsAutoHide)
  337. {
  338. _scrollBarsAutoHide = autoHide;
  339. setDirty(DIRTY_BOUNDS | DIRTY_STATE);
  340. }
  341. }
  342. bool Container::isScrollBarsAutoHide() const
  343. {
  344. return _scrollBarsAutoHide;
  345. }
  346. bool Container::isScrolling() const
  347. {
  348. if (_scrolling &&
  349. (abs(_scrollingLastX - _scrollingVeryFirstX) > SCROLL_THRESHOLD ||
  350. abs(_scrollingLastY - _scrollingVeryFirstY) > SCROLL_THRESHOLD))
  351. {
  352. return true;
  353. }
  354. if (_parent && _parent->isScrolling())
  355. return true;
  356. return false;
  357. }
  358. const Vector2& Container::getScrollPosition() const
  359. {
  360. return _scrollPosition;
  361. }
  362. void Container::setScrollPosition(const Vector2& scrollPosition)
  363. {
  364. _scrollPosition = scrollPosition;
  365. }
  366. Animation* Container::getAnimation(const char* id) const
  367. {
  368. std::vector<Control*>::const_iterator itr = _controls.begin();
  369. std::vector<Control*>::const_iterator end = _controls.end();
  370. Control* control = NULL;
  371. for (; itr != end; itr++)
  372. {
  373. control = *itr;
  374. GP_ASSERT(control);
  375. Animation* animation = control->getAnimation(id);
  376. if (animation)
  377. return animation;
  378. if (control->isContainer())
  379. {
  380. animation = ((Container*)control)->getAnimation(id);
  381. if (animation)
  382. return animation;
  383. }
  384. }
  385. return NULL;
  386. }
  387. const char* Container::getType() const
  388. {
  389. return "container";
  390. }
  391. bool Container::getScrollWheelRequiresFocus() const
  392. {
  393. return _scrollWheelRequiresFocus;
  394. }
  395. void Container::setScrollWheelRequiresFocus(bool required)
  396. {
  397. _scrollWheelRequiresFocus = required;
  398. }
  399. bool Container::setFocus()
  400. {
  401. // If this container (or one of its children) already has focus, do nothing
  402. if (Form::_focusControl && (Form::_focusControl == this || Form::_focusControl->isChild(this)))
  403. return true;
  404. // First try to set focus to our active control
  405. if (_activeControl)
  406. {
  407. if (_activeControl->setFocus())
  408. return true;
  409. }
  410. // Try to set focus to one of our children
  411. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  412. {
  413. if (_controls[i]->setFocus())
  414. return true;
  415. }
  416. // Lastly, try to set focus to ourself if none of our children will accept it
  417. return Control::setFocus();
  418. }
  419. Control* Container::getActiveControl() const
  420. {
  421. return _activeControl;
  422. }
  423. void Container::setActiveControl(Control* control)
  424. {
  425. if (std::find(_controls.begin(), _controls.end(), control) != _controls.end())
  426. {
  427. _activeControl = control;
  428. // If a control within this container currently has focus, switch focus to the new active control
  429. if (Form::_focusControl && Form::_focusControl != control && Form::_focusControl->isChild(this))
  430. Form::setFocusControl(control);
  431. }
  432. }
  433. void Container::setChildrenDirty(int bits, bool recursive)
  434. {
  435. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  436. {
  437. Control* ctrl = _controls[i];
  438. ctrl->setDirty(bits);
  439. if (recursive && ctrl->isContainer())
  440. static_cast<Container*>(ctrl)->setChildrenDirty(bits, true);
  441. }
  442. }
  443. void Container::update(float elapsedTime)
  444. {
  445. Control::update(elapsedTime);
  446. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  447. _controls[i]->update(elapsedTime);
  448. }
  449. void Container::updateState(State state)
  450. {
  451. Control::updateState(state);
  452. // Get scrollbar images and diminish clipping bounds to make room for scrollbars.
  453. if ((_scroll & SCROLL_HORIZONTAL) == SCROLL_HORIZONTAL)
  454. {
  455. _scrollBarLeftCap = getImage("scrollBarLeftCap", state);
  456. _scrollBarHorizontal = getImage("horizontalScrollBar", state);
  457. _scrollBarRightCap = getImage("scrollBarRightCap", state);
  458. }
  459. if ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL)
  460. {
  461. _scrollBarTopCap = getImage("scrollBarTopCap", state);
  462. _scrollBarVertical = getImage("verticalScrollBar", state);
  463. _scrollBarBottomCap = getImage("scrollBarBottomCap", state);
  464. }
  465. }
  466. void Container::updateBounds()
  467. {
  468. // Compute total bounds of container
  469. Control::updateBounds();
  470. // Handle automatically sizing based on our children
  471. if (_autoSize != AUTO_SIZE_NONE)
  472. {
  473. if (_autoSize & AUTO_SIZE_WIDTH)
  474. {
  475. // Size ourself to tightly fit the width of our children
  476. float width = 0;
  477. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  478. {
  479. Control* ctrl = _controls[i];
  480. if (!ctrl->isXPercentage() && !ctrl->isWidthPercentage())
  481. {
  482. float w = ctrl->getX() + ctrl->getWidth();
  483. if (width < w)
  484. width = w;
  485. }
  486. }
  487. width += getBorder(NORMAL).left + getBorder(NORMAL).right + getPadding().left + getPadding().right;
  488. setWidthInternal(width);
  489. }
  490. if (_autoSize & AUTO_SIZE_HEIGHT)
  491. {
  492. // Size ourself to tightly fit the height of our children
  493. float height = 0;
  494. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  495. {
  496. Control* ctrl = _controls[i];
  497. if (!ctrl->isYPercentage() && !ctrl->isHeightPercentage())
  498. {
  499. float h = ctrl->getY() + ctrl->getHeight();
  500. if (height < h)
  501. height = h;
  502. }
  503. }
  504. height += getBorder(NORMAL).top + getBorder(NORMAL).bottom + getPadding().top + getPadding().bottom;
  505. setHeightInternal(height);
  506. }
  507. }
  508. // Update layout to position children correctly within us
  509. GP_ASSERT(_layout);
  510. _layout->update(this);
  511. }
  512. void Container::updateAbsoluteBounds(const Vector2& offset)
  513. {
  514. Control::updateAbsoluteBounds(offset);
  515. // Get scrollbar images and diminish clipping bounds to make room for scrollbars.
  516. if ((_scroll & SCROLL_HORIZONTAL) == SCROLL_HORIZONTAL)
  517. {
  518. GP_ASSERT(_scrollBarLeftCap && _scrollBarHorizontal && _scrollBarRightCap);
  519. _viewportBounds.height -= _scrollBarHorizontal->getRegion().height;
  520. _viewportClipBounds.height -= _scrollBarHorizontal->getRegion().height;
  521. }
  522. if ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL)
  523. {
  524. GP_ASSERT(_scrollBarTopCap && _scrollBarVertical && _scrollBarBottomCap);
  525. _viewportBounds.width -= _scrollBarVertical->getRegion().width;
  526. _viewportClipBounds.width -= _scrollBarVertical->getRegion().width;
  527. }
  528. // Update scroll position and scrollbars after updating absolute bounds since
  529. // computation relies on up-to-date absolute bounds information.
  530. updateScroll();
  531. }
  532. bool Container::updateChildBounds()
  533. {
  534. bool result = false;
  535. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  536. {
  537. Control* ctrl = _controls[i];
  538. GP_ASSERT(ctrl);
  539. if (ctrl->isVisible())
  540. {
  541. bool changed = ctrl->updateBoundsInternal(_scrollPosition);
  542. // If the child bounds have changed, dirty our bounds and all of our
  543. // parent bounds so that our layout and/or bounds are recomputed.
  544. if (changed)
  545. {
  546. Control* parent = this;
  547. while (parent && parent->_autoSize != AUTO_SIZE_NONE)
  548. {
  549. parent->setDirty(DIRTY_BOUNDS);
  550. parent = parent->_parent;
  551. }
  552. }
  553. result = result || changed;
  554. }
  555. }
  556. return result;
  557. }
  558. unsigned int Container::draw(Form* form, const Rectangle& clip)
  559. {
  560. if (!_visible)
  561. return 0;
  562. // Draw container skin
  563. unsigned int drawCalls = Control::draw(form, clip);
  564. // Draw child controls
  565. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  566. {
  567. Control* control = _controls[i];
  568. if (control && control->_absoluteClipBounds.intersects(_absoluteClipBounds))
  569. {
  570. drawCalls += control->draw(form, _viewportClipBounds);
  571. }
  572. }
  573. // Draw scrollbars
  574. if (_scroll != SCROLL_NONE && (_scrollBarOpacity > 0.0f))
  575. {
  576. // Draw scroll bars.
  577. Rectangle clipRegion(_absoluteClipBounds);
  578. SpriteBatch* batch = _style->getTheme()->getSpriteBatch();
  579. startBatch(form, batch);
  580. if (_scrollBarBounds.height > 0 && ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL))
  581. {
  582. const Rectangle& topRegion = _scrollBarTopCap->getRegion();
  583. const Theme::UVs& topUVs = _scrollBarTopCap->getUVs();
  584. Vector4 topColor = _scrollBarTopCap->getColor();
  585. topColor.w *= _scrollBarOpacity * _opacity;
  586. const Rectangle& verticalRegion = _scrollBarVertical->getRegion();
  587. const Theme::UVs& verticalUVs = _scrollBarVertical->getUVs();
  588. Vector4 verticalColor = _scrollBarVertical->getColor();
  589. verticalColor.w *= _scrollBarOpacity * _opacity;
  590. const Rectangle& bottomRegion = _scrollBarBottomCap->getRegion();
  591. const Theme::UVs& bottomUVs = _scrollBarBottomCap->getUVs();
  592. Vector4 bottomColor = _scrollBarBottomCap->getColor();
  593. bottomColor.w *= _scrollBarOpacity * _opacity;
  594. clipRegion.width += verticalRegion.width;
  595. Rectangle bounds(_viewportBounds.right() + (_absoluteBounds.right() - _viewportBounds.right())*0.5f - topRegion.width*0.5f, _viewportBounds.y + _scrollBarBounds.y, topRegion.width, topRegion.height);
  596. batch->draw(bounds.x, bounds.y, bounds.width, bounds.height, topUVs.u1, topUVs.v1, topUVs.u2, topUVs.v2, topColor, clipRegion);
  597. bounds.y += topRegion.height;
  598. bounds.height = _scrollBarBounds.height - topRegion.height - bottomRegion.height;
  599. batch->draw(bounds.x, bounds.y, bounds.width, bounds.height, verticalUVs.u1, verticalUVs.v1, verticalUVs.u2, verticalUVs.v2, verticalColor, clipRegion);
  600. bounds.y += bounds.height;
  601. bounds.height = bottomRegion.height;
  602. batch->draw(bounds.x, bounds.y, bounds.width, bounds.height, bottomUVs.u1, bottomUVs.v1, bottomUVs.u2, bottomUVs.v2, bottomColor, clipRegion);
  603. drawCalls += 3;
  604. }
  605. if (_scrollBarBounds.width > 0 && ((_scroll & SCROLL_HORIZONTAL) == SCROLL_HORIZONTAL))
  606. {
  607. const Rectangle& leftRegion = _scrollBarLeftCap->getRegion();
  608. const Theme::UVs& leftUVs = _scrollBarLeftCap->getUVs();
  609. Vector4 leftColor = _scrollBarLeftCap->getColor();
  610. leftColor.w *= _scrollBarOpacity * _opacity;
  611. const Rectangle& horizontalRegion = _scrollBarHorizontal->getRegion();
  612. const Theme::UVs& horizontalUVs = _scrollBarHorizontal->getUVs();
  613. Vector4 horizontalColor = _scrollBarHorizontal->getColor();
  614. horizontalColor.w *= _scrollBarOpacity * _opacity;
  615. const Rectangle& rightRegion = _scrollBarRightCap->getRegion();
  616. const Theme::UVs& rightUVs = _scrollBarRightCap->getUVs();
  617. Vector4 rightColor = _scrollBarRightCap->getColor();
  618. rightColor.w *= _scrollBarOpacity * _opacity;
  619. clipRegion.height += horizontalRegion.height;
  620. Rectangle bounds(_viewportBounds.x + _scrollBarBounds.x, _viewportBounds.bottom() + (_absoluteBounds.bottom() - _viewportBounds.bottom())*0.5f - leftRegion.height*0.5f, leftRegion.width, leftRegion.height);
  621. batch->draw(bounds.x, bounds.y, bounds.width, bounds.height, leftUVs.u1, leftUVs.v1, leftUVs.u2, leftUVs.v2, leftColor, clipRegion);
  622. bounds.x += leftRegion.width;
  623. bounds.width = _scrollBarBounds.width - leftRegion.width - rightRegion.width;
  624. batch->draw(bounds.x, bounds.y, bounds.width, bounds.height, horizontalUVs.u1, horizontalUVs.v1, horizontalUVs.u2, horizontalUVs.v2, horizontalColor, clipRegion);
  625. bounds.x += bounds.width;
  626. bounds.width = rightRegion.width;
  627. batch->draw(bounds.x, bounds.y, bounds.width, bounds.height, rightUVs.u1, rightUVs.v1, rightUVs.u2, rightUVs.v2, rightColor, clipRegion);
  628. drawCalls += 3;
  629. }
  630. finishBatch(form, batch);
  631. }
  632. return drawCalls;
  633. }
  634. static bool canReceiveFocus(Control* control)
  635. {
  636. if (control->getFocusIndex() < 0 || !(control->isEnabled() && control->isVisible()))
  637. return false;
  638. if (control->canFocus())
  639. return true;
  640. if (control->isContainer())
  641. {
  642. Container* container = static_cast<Container*>(control);
  643. for (unsigned int i = 0, count = (unsigned int)container->getControlCount(); i < count; ++i)
  644. {
  645. if (canReceiveFocus(container->getControl(i)))
  646. return true;
  647. }
  648. }
  649. return false;
  650. }
  651. bool Container::moveFocus(Direction direction)
  652. {
  653. switch (direction)
  654. {
  655. case NEXT:
  656. case PREVIOUS:
  657. return moveFocusNextPrevious(direction);
  658. case UP:
  659. case DOWN:
  660. case LEFT:
  661. case RIGHT:
  662. return moveFocusDirectional(direction);
  663. default:
  664. return false;
  665. }
  666. }
  667. bool Container::moveFocusNextPrevious(Direction direction)
  668. {
  669. // Get the current control that has focus (either directly or indirectly) within this container
  670. Control* current = NULL;
  671. if (Form::_focusControl && Form::_focusControl->isChild(this))
  672. {
  673. if (Form::_focusControl->_parent == this)
  674. {
  675. // Currently focused control is a direct child of us
  676. current = Form::_focusControl;
  677. }
  678. else
  679. {
  680. // Currently focused control is a child of one of our child containers
  681. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  682. {
  683. if (Form::_focusControl->isChild(_controls[i]))
  684. {
  685. current = _controls[i];
  686. break;
  687. }
  688. }
  689. }
  690. }
  691. Control* nextCtrl = NULL;
  692. int nextIndex = direction == NEXT ? INT_MAX : INT_MIN;
  693. bool moveFirst = false;
  694. if (current)
  695. {
  696. // There is a control inside us that currently has focus, so find the next control that
  697. // should receive focus.
  698. int focusableControlCount = 0; // track the number of valid focusable controls in this container
  699. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  700. {
  701. Control* ctrl = _controls[i];
  702. if (!canReceiveFocus(ctrl))
  703. continue;
  704. if ((direction == NEXT && ctrl->_focusIndex > current->_focusIndex && ctrl->_focusIndex < nextIndex) ||
  705. (direction == PREVIOUS && ctrl->_focusIndex < current->_focusIndex && ctrl->_focusIndex > nextIndex))
  706. {
  707. nextCtrl = ctrl;
  708. nextIndex = ctrl->_focusIndex;
  709. }
  710. ++focusableControlCount;
  711. }
  712. if (nextCtrl)
  713. {
  714. if (nextCtrl->isContainer() && static_cast<Container*>(nextCtrl)->moveFocus(direction))
  715. return true;
  716. if (nextCtrl->setFocus())
  717. return true;
  718. }
  719. // Search up into our parent container for a focus move
  720. if (_parent && _parent->moveFocus(direction))
  721. return true;
  722. // We didn't find a control to move to, so we must be the first or last focusable control in our parent.
  723. // Wrap focus to the other side of the container.
  724. if (focusableControlCount > 1)
  725. {
  726. moveFirst = true;
  727. }
  728. }
  729. else
  730. {
  731. moveFirst = true;
  732. }
  733. if (moveFirst)
  734. {
  735. nextIndex = direction == NEXT ? INT_MAX : INT_MIN;
  736. nextCtrl = NULL;
  737. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  738. {
  739. Control* ctrl = _controls[i];
  740. if (!canReceiveFocus(ctrl))
  741. continue;
  742. if ((direction == NEXT && ctrl->_focusIndex < nextIndex) ||
  743. (direction == PREVIOUS && ctrl->_focusIndex > nextIndex))
  744. {
  745. nextCtrl = ctrl;
  746. nextIndex = ctrl->_focusIndex;
  747. }
  748. }
  749. if (nextCtrl)
  750. {
  751. if (nextCtrl->isContainer() && static_cast<Container*>(nextCtrl)->moveFocus(direction))
  752. return true;
  753. if (nextCtrl->setFocus())
  754. return true;
  755. }
  756. }
  757. return false;
  758. }
  759. bool Container::moveFocusDirectional(Direction direction)
  760. {
  761. Control* startControl = Form::_focusControl;
  762. if (startControl == NULL)
  763. return false;
  764. const Rectangle& startBounds = startControl->_absoluteBounds;
  765. Control* next = NULL;
  766. Vector2 vStart, vNext;
  767. float distance = FLT_MAX;
  768. switch (direction)
  769. {
  770. case UP:
  771. vStart.set(startBounds.x + startBounds.width * 0.5f, startBounds.y);
  772. break;
  773. case DOWN:
  774. vStart.set(startBounds.x + startBounds.width * 0.5f, startBounds.bottom());
  775. break;
  776. case LEFT:
  777. vStart.set(startBounds.x, startBounds.y + startBounds.height * 0.5f);
  778. break;
  779. case RIGHT:
  780. vStart.set(startBounds.right(), startBounds.y + startBounds.height * 0.5f);
  781. break;
  782. }
  783. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  784. {
  785. Control* ctrl = _controls[i];
  786. if (!canReceiveFocus(ctrl))
  787. continue;
  788. const Rectangle& nextBounds = ctrl->getAbsoluteBounds();
  789. switch (direction)
  790. {
  791. case UP:
  792. vNext.set(nextBounds.x + nextBounds.width * 0.5f, nextBounds.bottom());
  793. if (vNext.y > vStart.y)
  794. continue;
  795. break;
  796. case DOWN:
  797. vNext.set(nextBounds.x + nextBounds.width * 0.5f, nextBounds.y);
  798. if (vNext.y < vStart.y)
  799. continue;
  800. break;
  801. case LEFT:
  802. vNext.set(nextBounds.right(), nextBounds.y + nextBounds.height * 0.5f);
  803. if (vNext.x > vStart.x)
  804. continue;
  805. break;
  806. case RIGHT:
  807. vNext.set(nextBounds.x, nextBounds.y + nextBounds.height * 0.5f);
  808. if (vNext.x < vStart.x)
  809. continue;
  810. break;
  811. }
  812. float nextDistance = vStart.distance(vNext);
  813. if (std::fabs(nextDistance) < distance)
  814. {
  815. distance = nextDistance;
  816. next = ctrl;
  817. }
  818. }
  819. if (next)
  820. {
  821. // If this control is a container, try to move focus to the first control within it
  822. if (next->isContainer())
  823. {
  824. if (static_cast<Container*>(next)->moveFocusDirectional(direction))
  825. return true;
  826. }
  827. if (next->setFocus())
  828. return true;
  829. }
  830. else
  831. {
  832. // If no control was found, try searching in our parent container
  833. if (_parent && _parent->moveFocusDirectional(direction))
  834. return true;
  835. }
  836. return false;
  837. }
  838. void Container::startScrolling(float x, float y, bool resetTime)
  839. {
  840. _scrollingVelocity.set(-x, y);
  841. _scrolling = true;
  842. _scrollBarOpacity = 1.0f;
  843. setDirty(DIRTY_BOUNDS);
  844. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  845. {
  846. _scrollBarOpacityClip->stop();
  847. _scrollBarOpacityClip = NULL;
  848. }
  849. if (resetTime)
  850. {
  851. _lastFrameTime = Game::getAbsoluteTime();
  852. }
  853. }
  854. void Container::stopScrolling()
  855. {
  856. _scrollingVelocity.set(0, 0);
  857. _scrolling = false;
  858. setDirty(DIRTY_BOUNDS);
  859. if (_parent)
  860. _parent->stopScrolling();
  861. }
  862. bool Container::isContainer() const
  863. {
  864. return true;
  865. }
  866. Layout::Type Container::getLayoutType(const char* layoutString)
  867. {
  868. if (!layoutString)
  869. {
  870. return Layout::LAYOUT_ABSOLUTE;
  871. }
  872. std::string layoutName(layoutString);
  873. std::transform(layoutName.begin(), layoutName.end(), layoutName.begin(), (int(*)(int))toupper);
  874. if (layoutName == "LAYOUT_ABSOLUTE")
  875. {
  876. return Layout::LAYOUT_ABSOLUTE;
  877. }
  878. else if (layoutName == "LAYOUT_VERTICAL")
  879. {
  880. return Layout::LAYOUT_VERTICAL;
  881. }
  882. else if (layoutName == "LAYOUT_FLOW")
  883. {
  884. return Layout::LAYOUT_FLOW;
  885. }
  886. else
  887. {
  888. // Default.
  889. return Layout::LAYOUT_ABSOLUTE;
  890. }
  891. }
  892. Layout* Container::createLayout(Layout::Type type)
  893. {
  894. switch (type)
  895. {
  896. case Layout::LAYOUT_ABSOLUTE:
  897. return AbsoluteLayout::create();
  898. case Layout::LAYOUT_FLOW:
  899. return FlowLayout::create();
  900. case Layout::LAYOUT_VERTICAL:
  901. return VerticalLayout::create();
  902. default:
  903. return AbsoluteLayout::create();
  904. }
  905. }
  906. void Container::updateScroll()
  907. {
  908. if (_scroll == SCROLL_NONE)
  909. return;
  910. Control::State state = getState();
  911. // Update time.
  912. if (!_lastFrameTime)
  913. {
  914. _lastFrameTime = Game::getAbsoluteTime();
  915. }
  916. double frameTime = Game::getAbsoluteTime();
  917. float elapsedTime = (float)(frameTime - _lastFrameTime);
  918. _lastFrameTime = frameTime;
  919. const Theme::Border& containerBorder = getBorder(state);
  920. const Theme::Padding& containerPadding = getPadding();
  921. // Calculate total width and height.
  922. _totalWidth = _totalHeight = 0.0f;
  923. std::vector<Control*> controls = getControls();
  924. for (size_t i = 0, count = controls.size(); i < count; ++i)
  925. {
  926. Control* control = _controls[i];
  927. const Rectangle& bounds = control->getBounds();
  928. const Theme::Margin& margin = control->getMargin();
  929. float newWidth = bounds.x + bounds.width + margin.right;
  930. if (newWidth > _totalWidth)
  931. {
  932. _totalWidth = newWidth;
  933. }
  934. float newHeight = bounds.y + bounds.height + margin.bottom;
  935. if (newHeight > _totalHeight)
  936. {
  937. _totalHeight = newHeight;
  938. }
  939. }
  940. float vWidth = getImageRegion("verticalScrollBar", state).width;
  941. float hHeight = getImageRegion("horizontalScrollBar", state).height;
  942. float clipWidth = _absoluteBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right - vWidth;
  943. float clipHeight = _absoluteBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom - hHeight;
  944. bool dirty = false;
  945. // Apply and dampen inertia.
  946. if (!_scrollingVelocity.isZero())
  947. {
  948. // Calculate the time passed since last update.
  949. float elapsedSecs = (float)elapsedTime * 0.001f;
  950. _scrollPosition.x += _scrollingVelocity.x * elapsedSecs;
  951. _scrollPosition.y += _scrollingVelocity.y * elapsedSecs;
  952. if (!_scrolling)
  953. {
  954. float dampening = 1.0f - _scrollingFriction * SCROLL_FRICTION_FACTOR * elapsedSecs;
  955. _scrollingVelocity.x *= dampening;
  956. _scrollingVelocity.y *= dampening;
  957. if (fabs(_scrollingVelocity.x) < 100.0f)
  958. _scrollingVelocity.x = 0.0f;
  959. if (fabs(_scrollingVelocity.y) < 100.0f)
  960. _scrollingVelocity.y = 0.0f;
  961. }
  962. dirty = true;
  963. }
  964. // Stop scrolling when the far edge is reached.
  965. if (-_scrollPosition.x > _totalWidth - clipWidth)
  966. {
  967. _scrollPosition.x = -(_totalWidth - clipWidth);
  968. _scrollingVelocity.x = 0;
  969. dirty = true;
  970. }
  971. if (-_scrollPosition.y > _totalHeight - clipHeight)
  972. {
  973. _scrollPosition.y = -(_totalHeight - clipHeight);
  974. _scrollingVelocity.y = 0;
  975. dirty = true;
  976. }
  977. if (_scrollPosition.x > 0)
  978. {
  979. _scrollPosition.x = 0;
  980. _scrollingVelocity.x = 0;
  981. dirty = true;
  982. }
  983. if (_scrollPosition.y > 0)
  984. {
  985. _scrollPosition.y = 0;
  986. _scrollingVelocity.y = 0;
  987. dirty = true;
  988. }
  989. float scrollWidth = 0;
  990. if (clipWidth < _totalWidth)
  991. scrollWidth = (clipWidth / _totalWidth) * clipWidth;
  992. float scrollHeight = 0;
  993. if (clipHeight < _totalHeight)
  994. scrollHeight = (clipHeight / _totalHeight) * clipHeight;
  995. _scrollBarBounds.set(((-_scrollPosition.x) / _totalWidth) * clipWidth,
  996. ((-_scrollPosition.y) / _totalHeight) * clipHeight,
  997. scrollWidth, scrollHeight);
  998. // If scroll velocity is 0 and scrollbars are not always visible, trigger fade-out animation.
  999. if (!_scrolling && _scrollingVelocity.isZero() && _scrollBarsAutoHide && _scrollBarOpacity == 1.0f)
  1000. {
  1001. float to = 0;
  1002. _scrollBarOpacity = 0.99f;
  1003. if (!_scrollBarOpacityClip)
  1004. {
  1005. Animation* animation = createAnimationFromTo("scrollbar-fade-out", ANIMATE_SCROLLBAR_OPACITY, &_scrollBarOpacity, &to, Curve::QUADRATIC_IN_OUT, SCROLLBAR_FADE_TIME);
  1006. _scrollBarOpacityClip = animation->getClip();
  1007. }
  1008. _scrollBarOpacityClip->play();
  1009. }
  1010. // When scroll position is updated, we need to recompute bounds since children
  1011. // absolute bounds offset will need to be updated.
  1012. if (dirty)
  1013. {
  1014. setDirty(DIRTY_BOUNDS);
  1015. setChildrenDirty(DIRTY_BOUNDS, true);
  1016. }
  1017. }
  1018. void Container::sortControls()
  1019. {
  1020. if (_layout->getType() == Layout::LAYOUT_ABSOLUTE)
  1021. {
  1022. std::sort(_controls.begin(), _controls.end(), &sortControlsByZOrder);
  1023. }
  1024. }
  1025. bool Container::touchEventScroll(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1026. {
  1027. switch (evt)
  1028. {
  1029. case Touch::TOUCH_PRESS:
  1030. if (_contactIndex == INVALID_CONTACT_INDEX)
  1031. {
  1032. bool dirty = !_scrollingVelocity.isZero();
  1033. _contactIndex = (int)contactIndex;
  1034. _scrollingLastX = _scrollingFirstX = _scrollingVeryFirstX = x;
  1035. _scrollingLastY = _scrollingFirstY = _scrollingVeryFirstY = y;
  1036. _scrollingVelocity.set(0, 0);
  1037. _scrolling = true;
  1038. _scrollingStartTimeX = _scrollingStartTimeY = 0;
  1039. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  1040. {
  1041. _scrollBarOpacityClip->stop();
  1042. _scrollBarOpacityClip = NULL;
  1043. }
  1044. _scrollBarOpacity = 1.0f;
  1045. if (dirty)
  1046. setDirty(DIRTY_BOUNDS);
  1047. return false;
  1048. }
  1049. break;
  1050. case Touch::TOUCH_MOVE:
  1051. if (_scrolling && _contactIndex == (int)contactIndex)
  1052. {
  1053. double gameTime = Game::getAbsoluteTime();
  1054. // Calculate the latest movement delta for the next update to use.
  1055. int vx = x - _scrollingLastX;
  1056. int vy = y - _scrollingLastY;
  1057. if (_scrollingMouseVertically)
  1058. {
  1059. float yRatio = _totalHeight / _absoluteBounds.height;
  1060. vy *= yRatio;
  1061. _scrollingVelocity.set(0, -vy);
  1062. _scrollPosition.y -= vy;
  1063. }
  1064. else if (_scrollingMouseHorizontally)
  1065. {
  1066. float xRatio = _totalWidth / _absoluteBounds.width;
  1067. vx *= xRatio;
  1068. _scrollingVelocity.set(-vx, 0);
  1069. _scrollPosition.x -= vx;
  1070. }
  1071. else
  1072. {
  1073. _scrollingVelocity.set(vx, vy);
  1074. _scrollPosition.x += vx;
  1075. _scrollPosition.y += vy;
  1076. }
  1077. _scrollingLastX = x;
  1078. _scrollingLastY = y;
  1079. // If the user changes direction, reset the start time and position.
  1080. bool goingRight = (vx > 0);
  1081. if (goingRight != _scrollingRight)
  1082. {
  1083. _scrollingFirstX = x;
  1084. _scrollingRight = goingRight;
  1085. _scrollingStartTimeX = gameTime;
  1086. }
  1087. bool goingDown = (vy > 0);
  1088. if (goingDown != _scrollingDown)
  1089. {
  1090. _scrollingFirstY = y;
  1091. _scrollingDown = goingDown;
  1092. _scrollingStartTimeY = gameTime;
  1093. }
  1094. if (!_scrollingStartTimeX)
  1095. _scrollingStartTimeX = gameTime;
  1096. if (!_scrollingStartTimeY)
  1097. _scrollingStartTimeY = gameTime;
  1098. _scrollingLastTime = gameTime;
  1099. setDirty(DIRTY_BOUNDS);
  1100. setChildrenDirty(DIRTY_BOUNDS, true);
  1101. return false;
  1102. }
  1103. break;
  1104. case Touch::TOUCH_RELEASE:
  1105. if (_contactIndex == (int) contactIndex)
  1106. {
  1107. _contactIndex = INVALID_CONTACT_INDEX;
  1108. _scrolling = false;
  1109. double gameTime = Game::getAbsoluteTime();
  1110. float timeSinceLastMove = (float)(gameTime - _scrollingLastTime);
  1111. if (timeSinceLastMove > SCROLL_INERTIA_DELAY)
  1112. {
  1113. _scrollingVelocity.set(0, 0);
  1114. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  1115. return false;
  1116. }
  1117. int dx = _scrollingLastX - _scrollingFirstX;
  1118. int dy = _scrollingLastY - _scrollingFirstY;
  1119. float timeTakenX = (float)(gameTime - _scrollingStartTimeX);
  1120. float elapsedSecsX = timeTakenX * 0.001f;
  1121. float timeTakenY = (float)(gameTime - _scrollingStartTimeY);
  1122. float elapsedSecsY = timeTakenY * 0.001f;
  1123. float vx = dx;
  1124. float vy = dy;
  1125. if (elapsedSecsX > 0)
  1126. vx = (float)dx / elapsedSecsX;
  1127. if (elapsedSecsY > 0)
  1128. vy = (float)dy / elapsedSecsY;
  1129. if (_scrollingMouseVertically)
  1130. {
  1131. float yRatio = _totalHeight / _absoluteBounds.height;
  1132. vy *= yRatio;
  1133. _scrollingVelocity.set(0, -vy);
  1134. }
  1135. else if (_scrollingMouseHorizontally)
  1136. {
  1137. float xRatio = _totalWidth / _absoluteBounds.width;
  1138. vx *= xRatio;
  1139. _scrollingVelocity.set(-vx, 0);
  1140. }
  1141. else
  1142. {
  1143. _scrollingVelocity.set(vx, vy);
  1144. }
  1145. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  1146. setDirty(DIRTY_BOUNDS);
  1147. return false;
  1148. }
  1149. break;
  1150. }
  1151. return false;
  1152. }
  1153. bool Container::mouseEventScroll(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1154. {
  1155. switch (evt)
  1156. {
  1157. case Mouse::MOUSE_PRESS_LEFT_BUTTON:
  1158. {
  1159. bool dirty = false;
  1160. if (_scrollBarVertical)
  1161. {
  1162. float vWidth = _scrollBarVertical->getRegion().width;
  1163. float rightPadding = _absoluteBounds.right() - _viewportBounds.right();
  1164. float topPadding = _viewportBounds.y - _absoluteBounds.y;
  1165. float localVpRight = _bounds.width - rightPadding;
  1166. Rectangle vBounds(
  1167. localVpRight + rightPadding*0.5f - vWidth*0.5f,
  1168. topPadding + _scrollBarBounds.y,
  1169. vWidth, _scrollBarBounds.height);
  1170. if (x >= vBounds.x && x <= vBounds.right())
  1171. {
  1172. // Then we're within the horizontal bounds of the vertical scrollbar.
  1173. // We want to either jump up or down, or drag the scrollbar itself.
  1174. if (y < vBounds.y)
  1175. {
  1176. _scrollPosition.y += _totalHeight / 5.0f;
  1177. dirty = true;
  1178. }
  1179. else if (y > vBounds.bottom())
  1180. {
  1181. _scrollPosition.y -= _totalHeight / 5.0f;
  1182. dirty = true;
  1183. }
  1184. else
  1185. {
  1186. _scrollingMouseVertically = true;
  1187. }
  1188. }
  1189. }
  1190. if (_scrollBarHorizontal)
  1191. {
  1192. float hHeight = _scrollBarHorizontal->getRegion().height;
  1193. float bottomPadding = _absoluteBounds.bottom() - _viewportBounds.bottom();
  1194. float leftPadding = _viewportBounds.x - _absoluteBounds.x;
  1195. float localVpBottom = _bounds.height - bottomPadding;
  1196. Rectangle hBounds(
  1197. leftPadding + _scrollBarBounds.x,
  1198. localVpBottom + bottomPadding*0.5f - hHeight*0.5f,
  1199. _scrollBarBounds.width, hHeight);
  1200. if (y >= hBounds.y && y <= hBounds.bottom())
  1201. {
  1202. // We're within the vertical bounds of the horizontal scrollbar.
  1203. if (x < hBounds.x)
  1204. {
  1205. _scrollPosition.x += _totalWidth / 5.0f;
  1206. dirty = true;
  1207. }
  1208. else if (x > hBounds.x + hBounds.width)
  1209. {
  1210. _scrollPosition.x -= _totalWidth / 5.0f;
  1211. dirty = true;
  1212. }
  1213. else
  1214. {
  1215. _scrollingMouseHorizontally = true;
  1216. }
  1217. }
  1218. }
  1219. if (dirty)
  1220. {
  1221. setDirty(DIRTY_BOUNDS);
  1222. setChildrenDirty(DIRTY_BOUNDS, true);
  1223. }
  1224. return touchEventScroll(Touch::TOUCH_PRESS, x, y, 0);
  1225. }
  1226. case Mouse::MOUSE_MOVE:
  1227. return touchEventScroll(Touch::TOUCH_MOVE, x, y, 0);
  1228. case Mouse::MOUSE_RELEASE_LEFT_BUTTON:
  1229. return touchEventScroll(Touch::TOUCH_RELEASE, x, y, 0);
  1230. case Mouse::MOUSE_WHEEL:
  1231. {
  1232. if (_scrollingVelocity.isZero())
  1233. {
  1234. _lastFrameTime = Game::getAbsoluteTime();
  1235. }
  1236. _scrolling = _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  1237. _scrollingVelocity.y += _scrollWheelSpeed * wheelDelta;
  1238. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  1239. {
  1240. _scrollBarOpacityClip->stop();
  1241. _scrollBarOpacityClip = NULL;
  1242. }
  1243. _scrollBarOpacity = 1.0f;
  1244. setDirty(DIRTY_BOUNDS);
  1245. return false;
  1246. }
  1247. }
  1248. return false;
  1249. }
  1250. bool Container::inContact()
  1251. {
  1252. for (int i = 0; i < MAX_CONTACT_INDICES; ++i)
  1253. {
  1254. if (_contactIndices[i])
  1255. return true;
  1256. }
  1257. return false;
  1258. }
  1259. Container::Scroll Container::getScroll(const char* scroll)
  1260. {
  1261. if (!scroll)
  1262. return Container::SCROLL_NONE;
  1263. if (strcmp(scroll, "SCROLL_NONE") == 0)
  1264. {
  1265. return Container::SCROLL_NONE;
  1266. }
  1267. else if (strcmp(scroll, "SCROLL_HORIZONTAL") == 0)
  1268. {
  1269. return Container::SCROLL_HORIZONTAL;
  1270. }
  1271. else if (strcmp(scroll, "SCROLL_VERTICAL") == 0)
  1272. {
  1273. return Container::SCROLL_VERTICAL;
  1274. }
  1275. else if (strcmp(scroll, "SCROLL_BOTH") == 0)
  1276. {
  1277. return Container::SCROLL_BOTH;
  1278. }
  1279. else
  1280. {
  1281. GP_ERROR("Failed to get corresponding scroll state for unsupported value '%s'.", scroll);
  1282. }
  1283. return Container::SCROLL_NONE;
  1284. }
  1285. float Container::getScrollingFriction() const
  1286. {
  1287. return _scrollingFriction;
  1288. }
  1289. void Container::setScrollingFriction(float friction)
  1290. {
  1291. _scrollingFriction = friction;
  1292. }
  1293. float Container::getScrollWheelSpeed() const
  1294. {
  1295. return _scrollWheelSpeed;
  1296. }
  1297. void Container::setScrollWheelSpeed(float speed)
  1298. {
  1299. _scrollWheelSpeed = speed;
  1300. }
  1301. static bool sortControlsByZOrder(Control* c1, Control* c2)
  1302. {
  1303. if (c1->getZIndex() < c2->getZIndex())
  1304. return true;
  1305. return false;
  1306. }
  1307. unsigned int Container::getAnimationPropertyComponentCount(int propertyId) const
  1308. {
  1309. switch(propertyId)
  1310. {
  1311. case ANIMATE_SCROLLBAR_OPACITY:
  1312. return 1;
  1313. default:
  1314. return Control::getAnimationPropertyComponentCount(propertyId);
  1315. }
  1316. }
  1317. void Container::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  1318. {
  1319. GP_ASSERT(value);
  1320. switch(propertyId)
  1321. {
  1322. case ANIMATE_SCROLLBAR_OPACITY:
  1323. value->setFloat(0, _scrollBarOpacity);
  1324. break;
  1325. default:
  1326. Control::getAnimationPropertyValue(propertyId, value);
  1327. break;
  1328. }
  1329. }
  1330. void Container::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  1331. {
  1332. GP_ASSERT(value);
  1333. switch(propertyId)
  1334. {
  1335. case ANIMATE_SCROLLBAR_OPACITY:
  1336. _scrollBarOpacity = Curve::lerp(blendWeight, _opacity, value->getFloat(0));
  1337. break;
  1338. default:
  1339. Control::setAnimationPropertyValue(propertyId, value, blendWeight);
  1340. break;
  1341. }
  1342. }
  1343. }