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