Container.cpp 47 KB

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