Container.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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::getFocusControl() && (Form::getFocusControl() == this || Form::getFocusControl()->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::getFocusControl() && Form::getFocusControl() != control && Form::getFocusControl()->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* currentFocus = Form::getFocusControl();
  671. Control* current = NULL;
  672. if (currentFocus && currentFocus->isChild(this))
  673. {
  674. if (currentFocus->_parent == this)
  675. {
  676. // Currently focused control is a direct child of us
  677. current = currentFocus;
  678. }
  679. else
  680. {
  681. // Currently focused control is a child of one of our child containers
  682. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  683. {
  684. if (currentFocus->isChild(_controls[i]))
  685. {
  686. current = _controls[i];
  687. break;
  688. }
  689. }
  690. }
  691. }
  692. Control* nextCtrl = NULL;
  693. int nextIndex = direction == NEXT ? INT_MAX : INT_MIN;
  694. bool moveFirst = false;
  695. if (current)
  696. {
  697. // There is a control inside us that currently has focus, so find the next control that
  698. // should receive focus.
  699. int focusableControlCount = 0; // track the number of valid focusable controls in this container
  700. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  701. {
  702. Control* ctrl = _controls[i];
  703. if (!canReceiveFocus(ctrl))
  704. continue;
  705. if ((direction == NEXT && ctrl->_focusIndex > current->_focusIndex && ctrl->_focusIndex < nextIndex) ||
  706. (direction == PREVIOUS && ctrl->_focusIndex < current->_focusIndex && ctrl->_focusIndex > nextIndex))
  707. {
  708. nextCtrl = ctrl;
  709. nextIndex = ctrl->_focusIndex;
  710. }
  711. ++focusableControlCount;
  712. }
  713. if (nextCtrl)
  714. {
  715. if (nextCtrl->isContainer() && static_cast<Container*>(nextCtrl)->moveFocus(direction))
  716. return true;
  717. if (nextCtrl->setFocus())
  718. return true;
  719. }
  720. // Search up into our parent container for a focus move
  721. if (_parent && _parent->moveFocus(direction))
  722. return true;
  723. // We didn't find a control to move to, so we must be the first or last focusable control in our parent.
  724. // Wrap focus to the other side of the container.
  725. if (focusableControlCount > 1)
  726. {
  727. moveFirst = true;
  728. }
  729. }
  730. else
  731. {
  732. moveFirst = true;
  733. }
  734. if (moveFirst)
  735. {
  736. nextIndex = direction == NEXT ? INT_MAX : INT_MIN;
  737. nextCtrl = NULL;
  738. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  739. {
  740. Control* ctrl = _controls[i];
  741. if (!canReceiveFocus(ctrl))
  742. continue;
  743. if ((direction == NEXT && ctrl->_focusIndex < nextIndex) ||
  744. (direction == PREVIOUS && ctrl->_focusIndex > nextIndex))
  745. {
  746. nextCtrl = ctrl;
  747. nextIndex = ctrl->_focusIndex;
  748. }
  749. }
  750. if (nextCtrl)
  751. {
  752. if (nextCtrl->isContainer() && static_cast<Container*>(nextCtrl)->moveFocus(direction))
  753. return true;
  754. if (nextCtrl->setFocus())
  755. return true;
  756. }
  757. }
  758. return false;
  759. }
  760. bool Container::moveFocusDirectional(Direction direction)
  761. {
  762. Control* startControl = Form::getFocusControl();
  763. if (startControl == NULL)
  764. return false;
  765. const Rectangle& startBounds = startControl->_absoluteBounds;
  766. Control* next = NULL;
  767. Vector2 vStart, vNext;
  768. float distance = FLT_MAX;
  769. switch (direction)
  770. {
  771. case UP:
  772. vStart.set(startBounds.x + startBounds.width * 0.5f, startBounds.y);
  773. break;
  774. case DOWN:
  775. vStart.set(startBounds.x + startBounds.width * 0.5f, startBounds.bottom());
  776. break;
  777. case LEFT:
  778. vStart.set(startBounds.x, startBounds.y + startBounds.height * 0.5f);
  779. break;
  780. case RIGHT:
  781. vStart.set(startBounds.right(), startBounds.y + startBounds.height * 0.5f);
  782. break;
  783. }
  784. for (size_t i = 0, count = _controls.size(); i < count; ++i)
  785. {
  786. Control* ctrl = _controls[i];
  787. if (!canReceiveFocus(ctrl))
  788. continue;
  789. const Rectangle& nextBounds = ctrl->getAbsoluteBounds();
  790. switch (direction)
  791. {
  792. case UP:
  793. vNext.set(nextBounds.x + nextBounds.width * 0.5f, nextBounds.bottom());
  794. if (vNext.y > vStart.y)
  795. continue;
  796. break;
  797. case DOWN:
  798. vNext.set(nextBounds.x + nextBounds.width * 0.5f, nextBounds.y);
  799. if (vNext.y < vStart.y)
  800. continue;
  801. break;
  802. case LEFT:
  803. vNext.set(nextBounds.right(), nextBounds.y + nextBounds.height * 0.5f);
  804. if (vNext.x > vStart.x)
  805. continue;
  806. break;
  807. case RIGHT:
  808. vNext.set(nextBounds.x, nextBounds.y + nextBounds.height * 0.5f);
  809. if (vNext.x < vStart.x)
  810. continue;
  811. break;
  812. }
  813. float nextDistance = vStart.distance(vNext);
  814. if (std::fabs(nextDistance) < distance)
  815. {
  816. distance = nextDistance;
  817. next = ctrl;
  818. }
  819. }
  820. if (next)
  821. {
  822. // If this control is a container, try to move focus to the first control within it
  823. if (next->isContainer())
  824. {
  825. if (static_cast<Container*>(next)->moveFocusDirectional(direction))
  826. return true;
  827. }
  828. if (next->setFocus())
  829. return true;
  830. }
  831. else
  832. {
  833. // If no control was found, try searching in our parent container
  834. if (_parent && _parent->moveFocusDirectional(direction))
  835. return true;
  836. }
  837. return false;
  838. }
  839. void Container::startScrolling(float x, float y, bool resetTime)
  840. {
  841. _scrollingVelocity.set(-x, y);
  842. _scrolling = true;
  843. _scrollBarOpacity = 1.0f;
  844. setDirty(DIRTY_BOUNDS);
  845. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  846. {
  847. _scrollBarOpacityClip->stop();
  848. _scrollBarOpacityClip = NULL;
  849. }
  850. if (resetTime)
  851. {
  852. _lastFrameTime = Game::getAbsoluteTime();
  853. }
  854. }
  855. void Container::stopScrolling()
  856. {
  857. _scrollingVelocity.set(0, 0);
  858. _scrolling = false;
  859. setDirty(DIRTY_BOUNDS);
  860. if (_parent)
  861. _parent->stopScrolling();
  862. }
  863. bool Container::isContainer() const
  864. {
  865. return true;
  866. }
  867. Layout::Type Container::getLayoutType(const char* layoutString)
  868. {
  869. if (!layoutString)
  870. {
  871. return Layout::LAYOUT_ABSOLUTE;
  872. }
  873. std::string layoutName(layoutString);
  874. std::transform(layoutName.begin(), layoutName.end(), layoutName.begin(), (int(*)(int))toupper);
  875. if (layoutName == "LAYOUT_ABSOLUTE")
  876. {
  877. return Layout::LAYOUT_ABSOLUTE;
  878. }
  879. else if (layoutName == "LAYOUT_VERTICAL")
  880. {
  881. return Layout::LAYOUT_VERTICAL;
  882. }
  883. else if (layoutName == "LAYOUT_FLOW")
  884. {
  885. return Layout::LAYOUT_FLOW;
  886. }
  887. else
  888. {
  889. // Default.
  890. return Layout::LAYOUT_ABSOLUTE;
  891. }
  892. }
  893. Layout* Container::createLayout(Layout::Type type)
  894. {
  895. switch (type)
  896. {
  897. case Layout::LAYOUT_ABSOLUTE:
  898. return AbsoluteLayout::create();
  899. case Layout::LAYOUT_FLOW:
  900. return FlowLayout::create();
  901. case Layout::LAYOUT_VERTICAL:
  902. return VerticalLayout::create();
  903. default:
  904. return AbsoluteLayout::create();
  905. }
  906. }
  907. void Container::updateScroll()
  908. {
  909. if (_scroll == SCROLL_NONE)
  910. return;
  911. Control::State state = getState();
  912. // Update time.
  913. if (!_lastFrameTime)
  914. {
  915. _lastFrameTime = Game::getAbsoluteTime();
  916. }
  917. double frameTime = Game::getAbsoluteTime();
  918. float elapsedTime = (float)(frameTime - _lastFrameTime);
  919. _lastFrameTime = frameTime;
  920. const Theme::Border& containerBorder = getBorder(state);
  921. const Theme::Padding& containerPadding = getPadding();
  922. // Calculate total width and height.
  923. _totalWidth = _totalHeight = 0.0f;
  924. std::vector<Control*> controls = getControls();
  925. for (size_t i = 0, count = controls.size(); i < count; ++i)
  926. {
  927. Control* control = _controls[i];
  928. const Rectangle& bounds = control->getBounds();
  929. const Theme::Margin& margin = control->getMargin();
  930. float newWidth = bounds.x + bounds.width + margin.right;
  931. if (newWidth > _totalWidth)
  932. {
  933. _totalWidth = newWidth;
  934. }
  935. float newHeight = bounds.y + bounds.height + margin.bottom;
  936. if (newHeight > _totalHeight)
  937. {
  938. _totalHeight = newHeight;
  939. }
  940. }
  941. float vWidth = getImageRegion("verticalScrollBar", state).width;
  942. float hHeight = getImageRegion("horizontalScrollBar", state).height;
  943. float clipWidth = _absoluteBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right - vWidth;
  944. float clipHeight = _absoluteBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom - hHeight;
  945. bool dirty = false;
  946. // Apply and dampen inertia.
  947. if (!_scrollingVelocity.isZero())
  948. {
  949. // Calculate the time passed since last update.
  950. float elapsedSecs = (float)elapsedTime * 0.001f;
  951. _scrollPosition.x += _scrollingVelocity.x * elapsedSecs;
  952. _scrollPosition.y += _scrollingVelocity.y * elapsedSecs;
  953. if (!_scrolling)
  954. {
  955. float dampening = 1.0f - _scrollingFriction * SCROLL_FRICTION_FACTOR * elapsedSecs;
  956. _scrollingVelocity.x *= dampening;
  957. _scrollingVelocity.y *= dampening;
  958. if (fabs(_scrollingVelocity.x) < 100.0f)
  959. _scrollingVelocity.x = 0.0f;
  960. if (fabs(_scrollingVelocity.y) < 100.0f)
  961. _scrollingVelocity.y = 0.0f;
  962. }
  963. dirty = true;
  964. }
  965. // Stop scrolling when the far edge is reached.
  966. if (-_scrollPosition.x > _totalWidth - clipWidth)
  967. {
  968. _scrollPosition.x = -(_totalWidth - clipWidth);
  969. _scrollingVelocity.x = 0;
  970. dirty = true;
  971. }
  972. if (-_scrollPosition.y > _totalHeight - clipHeight)
  973. {
  974. _scrollPosition.y = -(_totalHeight - clipHeight);
  975. _scrollingVelocity.y = 0;
  976. dirty = true;
  977. }
  978. if (_scrollPosition.x > 0)
  979. {
  980. _scrollPosition.x = 0;
  981. _scrollingVelocity.x = 0;
  982. dirty = true;
  983. }
  984. if (_scrollPosition.y > 0)
  985. {
  986. _scrollPosition.y = 0;
  987. _scrollingVelocity.y = 0;
  988. dirty = true;
  989. }
  990. float scrollWidth = 0;
  991. if (clipWidth < _totalWidth)
  992. scrollWidth = (clipWidth / _totalWidth) * clipWidth;
  993. float scrollHeight = 0;
  994. if (clipHeight < _totalHeight)
  995. scrollHeight = (clipHeight / _totalHeight) * clipHeight;
  996. _scrollBarBounds.set(((-_scrollPosition.x) / _totalWidth) * clipWidth,
  997. ((-_scrollPosition.y) / _totalHeight) * clipHeight,
  998. scrollWidth, scrollHeight);
  999. // If scroll velocity is 0 and scrollbars are not always visible, trigger fade-out animation.
  1000. if (!_scrolling && _scrollingVelocity.isZero() && _scrollBarsAutoHide && _scrollBarOpacity == 1.0f)
  1001. {
  1002. float to = 0;
  1003. _scrollBarOpacity = 0.99f;
  1004. if (!_scrollBarOpacityClip)
  1005. {
  1006. Animation* animation = createAnimationFromTo("scrollbar-fade-out", ANIMATE_SCROLLBAR_OPACITY, &_scrollBarOpacity, &to, Curve::QUADRATIC_IN_OUT, SCROLLBAR_FADE_TIME);
  1007. _scrollBarOpacityClip = animation->getClip();
  1008. }
  1009. _scrollBarOpacityClip->play();
  1010. }
  1011. // When scroll position is updated, we need to recompute bounds since children
  1012. // absolute bounds offset will need to be updated.
  1013. if (dirty)
  1014. {
  1015. setDirty(DIRTY_BOUNDS);
  1016. setChildrenDirty(DIRTY_BOUNDS, true);
  1017. }
  1018. }
  1019. void Container::sortControls()
  1020. {
  1021. if (_layout->getType() == Layout::LAYOUT_ABSOLUTE)
  1022. {
  1023. std::sort(_controls.begin(), _controls.end(), &sortControlsByZOrder);
  1024. }
  1025. }
  1026. bool Container::touchEventScroll(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1027. {
  1028. switch (evt)
  1029. {
  1030. case Touch::TOUCH_PRESS:
  1031. if (_contactIndex == INVALID_CONTACT_INDEX)
  1032. {
  1033. bool dirty = !_scrollingVelocity.isZero();
  1034. _contactIndex = (int)contactIndex;
  1035. _scrollingLastX = _scrollingFirstX = _scrollingVeryFirstX = x;
  1036. _scrollingLastY = _scrollingFirstY = _scrollingVeryFirstY = y;
  1037. _scrollingVelocity.set(0, 0);
  1038. _scrolling = true;
  1039. _scrollingStartTimeX = _scrollingStartTimeY = 0;
  1040. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  1041. {
  1042. _scrollBarOpacityClip->stop();
  1043. _scrollBarOpacityClip = NULL;
  1044. }
  1045. _scrollBarOpacity = 1.0f;
  1046. if (dirty)
  1047. setDirty(DIRTY_BOUNDS);
  1048. return false;
  1049. }
  1050. break;
  1051. case Touch::TOUCH_MOVE:
  1052. if (_scrolling && _contactIndex == (int)contactIndex)
  1053. {
  1054. double gameTime = Game::getAbsoluteTime();
  1055. // Calculate the latest movement delta for the next update to use.
  1056. int vx = x - _scrollingLastX;
  1057. int vy = y - _scrollingLastY;
  1058. if (_scrollingMouseVertically)
  1059. {
  1060. float yRatio = _totalHeight / _absoluteBounds.height;
  1061. vy *= yRatio;
  1062. _scrollingVelocity.set(0, -vy);
  1063. _scrollPosition.y -= vy;
  1064. }
  1065. else if (_scrollingMouseHorizontally)
  1066. {
  1067. float xRatio = _totalWidth / _absoluteBounds.width;
  1068. vx *= xRatio;
  1069. _scrollingVelocity.set(-vx, 0);
  1070. _scrollPosition.x -= vx;
  1071. }
  1072. else
  1073. {
  1074. _scrollingVelocity.set(vx, vy);
  1075. _scrollPosition.x += vx;
  1076. _scrollPosition.y += vy;
  1077. }
  1078. _scrollingLastX = x;
  1079. _scrollingLastY = y;
  1080. // If the user changes direction, reset the start time and position.
  1081. bool goingRight = (vx > 0);
  1082. if (goingRight != _scrollingRight)
  1083. {
  1084. _scrollingFirstX = x;
  1085. _scrollingRight = goingRight;
  1086. _scrollingStartTimeX = gameTime;
  1087. }
  1088. bool goingDown = (vy > 0);
  1089. if (goingDown != _scrollingDown)
  1090. {
  1091. _scrollingFirstY = y;
  1092. _scrollingDown = goingDown;
  1093. _scrollingStartTimeY = gameTime;
  1094. }
  1095. if (!_scrollingStartTimeX)
  1096. _scrollingStartTimeX = gameTime;
  1097. if (!_scrollingStartTimeY)
  1098. _scrollingStartTimeY = gameTime;
  1099. _scrollingLastTime = gameTime;
  1100. setDirty(DIRTY_BOUNDS);
  1101. setChildrenDirty(DIRTY_BOUNDS, true);
  1102. return false;
  1103. }
  1104. break;
  1105. case Touch::TOUCH_RELEASE:
  1106. if (_contactIndex == (int) contactIndex)
  1107. {
  1108. _contactIndex = INVALID_CONTACT_INDEX;
  1109. _scrolling = false;
  1110. double gameTime = Game::getAbsoluteTime();
  1111. float timeSinceLastMove = (float)(gameTime - _scrollingLastTime);
  1112. if (timeSinceLastMove > SCROLL_INERTIA_DELAY)
  1113. {
  1114. _scrollingVelocity.set(0, 0);
  1115. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  1116. return false;
  1117. }
  1118. int dx = _scrollingLastX - _scrollingFirstX;
  1119. int dy = _scrollingLastY - _scrollingFirstY;
  1120. float timeTakenX = (float)(gameTime - _scrollingStartTimeX);
  1121. float elapsedSecsX = timeTakenX * 0.001f;
  1122. float timeTakenY = (float)(gameTime - _scrollingStartTimeY);
  1123. float elapsedSecsY = timeTakenY * 0.001f;
  1124. float vx = dx;
  1125. float vy = dy;
  1126. if (elapsedSecsX > 0)
  1127. vx = (float)dx / elapsedSecsX;
  1128. if (elapsedSecsY > 0)
  1129. vy = (float)dy / elapsedSecsY;
  1130. if (_scrollingMouseVertically)
  1131. {
  1132. float yRatio = _totalHeight / _absoluteBounds.height;
  1133. vy *= yRatio;
  1134. _scrollingVelocity.set(0, -vy);
  1135. }
  1136. else if (_scrollingMouseHorizontally)
  1137. {
  1138. float xRatio = _totalWidth / _absoluteBounds.width;
  1139. vx *= xRatio;
  1140. _scrollingVelocity.set(-vx, 0);
  1141. }
  1142. else
  1143. {
  1144. _scrollingVelocity.set(vx, vy);
  1145. }
  1146. _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  1147. setDirty(DIRTY_BOUNDS);
  1148. return false;
  1149. }
  1150. break;
  1151. }
  1152. return false;
  1153. }
  1154. bool Container::mouseEventScroll(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1155. {
  1156. switch (evt)
  1157. {
  1158. case Mouse::MOUSE_PRESS_LEFT_BUTTON:
  1159. {
  1160. bool dirty = false;
  1161. if (_scrollBarVertical)
  1162. {
  1163. float vWidth = _scrollBarVertical->getRegion().width;
  1164. float rightPadding = _absoluteBounds.right() - _viewportBounds.right();
  1165. float topPadding = _viewportBounds.y - _absoluteBounds.y;
  1166. float localVpRight = _bounds.width - rightPadding;
  1167. Rectangle vBounds(
  1168. localVpRight + rightPadding*0.5f - vWidth*0.5f,
  1169. topPadding + _scrollBarBounds.y,
  1170. vWidth, _scrollBarBounds.height);
  1171. if (x >= vBounds.x && x <= vBounds.right())
  1172. {
  1173. // Then we're within the horizontal bounds of the vertical scrollbar.
  1174. // We want to either jump up or down, or drag the scrollbar itself.
  1175. if (y < vBounds.y)
  1176. {
  1177. _scrollPosition.y += _totalHeight / 5.0f;
  1178. dirty = true;
  1179. }
  1180. else if (y > vBounds.bottom())
  1181. {
  1182. _scrollPosition.y -= _totalHeight / 5.0f;
  1183. dirty = true;
  1184. }
  1185. else
  1186. {
  1187. _scrollingMouseVertically = true;
  1188. }
  1189. }
  1190. }
  1191. if (_scrollBarHorizontal)
  1192. {
  1193. float hHeight = _scrollBarHorizontal->getRegion().height;
  1194. float bottomPadding = _absoluteBounds.bottom() - _viewportBounds.bottom();
  1195. float leftPadding = _viewportBounds.x - _absoluteBounds.x;
  1196. float localVpBottom = _bounds.height - bottomPadding;
  1197. Rectangle hBounds(
  1198. leftPadding + _scrollBarBounds.x,
  1199. localVpBottom + bottomPadding*0.5f - hHeight*0.5f,
  1200. _scrollBarBounds.width, hHeight);
  1201. if (y >= hBounds.y && y <= hBounds.bottom())
  1202. {
  1203. // We're within the vertical bounds of the horizontal scrollbar.
  1204. if (x < hBounds.x)
  1205. {
  1206. _scrollPosition.x += _totalWidth / 5.0f;
  1207. dirty = true;
  1208. }
  1209. else if (x > hBounds.x + hBounds.width)
  1210. {
  1211. _scrollPosition.x -= _totalWidth / 5.0f;
  1212. dirty = true;
  1213. }
  1214. else
  1215. {
  1216. _scrollingMouseHorizontally = true;
  1217. }
  1218. }
  1219. }
  1220. if (dirty)
  1221. {
  1222. setDirty(DIRTY_BOUNDS);
  1223. setChildrenDirty(DIRTY_BOUNDS, true);
  1224. }
  1225. return touchEventScroll(Touch::TOUCH_PRESS, x, y, 0);
  1226. }
  1227. case Mouse::MOUSE_MOVE:
  1228. return touchEventScroll(Touch::TOUCH_MOVE, x, y, 0);
  1229. case Mouse::MOUSE_RELEASE_LEFT_BUTTON:
  1230. return touchEventScroll(Touch::TOUCH_RELEASE, x, y, 0);
  1231. case Mouse::MOUSE_WHEEL:
  1232. {
  1233. if (_scrollingVelocity.isZero())
  1234. {
  1235. _lastFrameTime = Game::getAbsoluteTime();
  1236. }
  1237. _scrolling = _scrollingMouseVertically = _scrollingMouseHorizontally = false;
  1238. _scrollingVelocity.y += _scrollWheelSpeed * wheelDelta;
  1239. if (_scrollBarOpacityClip && _scrollBarOpacityClip->isPlaying())
  1240. {
  1241. _scrollBarOpacityClip->stop();
  1242. _scrollBarOpacityClip = NULL;
  1243. }
  1244. _scrollBarOpacity = 1.0f;
  1245. setDirty(DIRTY_BOUNDS);
  1246. return false;
  1247. }
  1248. }
  1249. return false;
  1250. }
  1251. bool Container::inContact()
  1252. {
  1253. for (int i = 0; i < MAX_CONTACT_INDICES; ++i)
  1254. {
  1255. if (_contactIndices[i])
  1256. return true;
  1257. }
  1258. return false;
  1259. }
  1260. Container::Scroll Container::getScroll(const char* scroll)
  1261. {
  1262. if (!scroll)
  1263. return Container::SCROLL_NONE;
  1264. if (strcmp(scroll, "SCROLL_NONE") == 0)
  1265. {
  1266. return Container::SCROLL_NONE;
  1267. }
  1268. else if (strcmp(scroll, "SCROLL_HORIZONTAL") == 0)
  1269. {
  1270. return Container::SCROLL_HORIZONTAL;
  1271. }
  1272. else if (strcmp(scroll, "SCROLL_VERTICAL") == 0)
  1273. {
  1274. return Container::SCROLL_VERTICAL;
  1275. }
  1276. else if (strcmp(scroll, "SCROLL_BOTH") == 0)
  1277. {
  1278. return Container::SCROLL_BOTH;
  1279. }
  1280. else
  1281. {
  1282. GP_ERROR("Failed to get corresponding scroll state for unsupported value '%s'.", scroll);
  1283. }
  1284. return Container::SCROLL_NONE;
  1285. }
  1286. float Container::getScrollingFriction() const
  1287. {
  1288. return _scrollingFriction;
  1289. }
  1290. void Container::setScrollingFriction(float friction)
  1291. {
  1292. _scrollingFriction = friction;
  1293. }
  1294. float Container::getScrollWheelSpeed() const
  1295. {
  1296. return _scrollWheelSpeed;
  1297. }
  1298. void Container::setScrollWheelSpeed(float speed)
  1299. {
  1300. _scrollWheelSpeed = speed;
  1301. }
  1302. static bool sortControlsByZOrder(Control* c1, Control* c2)
  1303. {
  1304. if (c1->getZIndex() < c2->getZIndex())
  1305. return true;
  1306. return false;
  1307. }
  1308. unsigned int Container::getAnimationPropertyComponentCount(int propertyId) const
  1309. {
  1310. switch(propertyId)
  1311. {
  1312. case ANIMATE_SCROLLBAR_OPACITY:
  1313. return 1;
  1314. default:
  1315. return Control::getAnimationPropertyComponentCount(propertyId);
  1316. }
  1317. }
  1318. void Container::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  1319. {
  1320. GP_ASSERT(value);
  1321. switch(propertyId)
  1322. {
  1323. case ANIMATE_SCROLLBAR_OPACITY:
  1324. value->setFloat(0, _scrollBarOpacity);
  1325. break;
  1326. default:
  1327. Control::getAnimationPropertyValue(propertyId, value);
  1328. break;
  1329. }
  1330. }
  1331. void Container::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  1332. {
  1333. GP_ASSERT(value);
  1334. switch(propertyId)
  1335. {
  1336. case ANIMATE_SCROLLBAR_OPACITY:
  1337. _scrollBarOpacity = Curve::lerp(blendWeight, _opacity, value->getFloat(0));
  1338. break;
  1339. default:
  1340. Control::setAnimationPropertyValue(propertyId, value, blendWeight);
  1341. break;
  1342. }
  1343. }
  1344. }