ScrollView.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "BorderImage.h"
  25. #include "InputEvents.h"
  26. #include "ScrollBar.h"
  27. #include "ScrollView.h"
  28. #include "Slider.h"
  29. #include "UI.h"
  30. #include "UIEvents.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. static const float STEP_FACTOR = 300.0f;
  35. extern const char* UI_CATEGORY;
  36. ScrollView::ScrollView(Context* context) :
  37. UIElement(context),
  38. viewPosition_(IntVector2::ZERO),
  39. viewSize_(IntVector2::ZERO),
  40. viewPositionAttr_(IntVector2::ZERO),
  41. touchScrollSpeed_(Vector2::ZERO),
  42. touchScrollSpeedMax_(Vector2::ZERO),
  43. pageStep_(1.0f),
  44. scrollBarsAutoVisible_(true),
  45. ignoreEvents_(false),
  46. resizeContentWidth_(false),
  47. scrollDeceleration_(30.0f),
  48. scrollSnapEpsilon_(M_EPSILON),
  49. scrollTouchDown_(false),
  50. barScrolling_(false)
  51. {
  52. clipChildren_ = true;
  53. SetEnabled(true);
  54. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  55. horizontalScrollBar_ = CreateChild<ScrollBar>("SV_HorizontalScrollBar");
  56. horizontalScrollBar_->SetInternal(true);
  57. horizontalScrollBar_->SetAlignment(HA_LEFT, VA_BOTTOM);
  58. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  59. verticalScrollBar_ = CreateChild<ScrollBar>("SV_VerticalScrollBar");
  60. verticalScrollBar_->SetInternal(true);
  61. verticalScrollBar_->SetAlignment(HA_RIGHT, VA_TOP);
  62. verticalScrollBar_->SetOrientation(O_VERTICAL);
  63. scrollPanel_ = CreateChild<BorderImage>("SV_ScrollPanel");
  64. scrollPanel_->SetInternal(true);
  65. scrollPanel_->SetEnabled(true);
  66. scrollPanel_->SetClipChildren(true);
  67. SubscribeToEvent(horizontalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  68. SubscribeToEvent(horizontalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  69. SubscribeToEvent(verticalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  70. SubscribeToEvent(verticalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  71. SubscribeToEvent(E_TOUCHMOVE, HANDLER(ScrollView, HandleTouchMove));
  72. SubscribeToEvent(E_TOUCHBEGIN, HANDLER(ScrollView, HandleTouchMove));
  73. SubscribeToEvent(E_TOUCHEND, HANDLER(ScrollView, HandleTouchMove));
  74. }
  75. ScrollView::~ScrollView()
  76. {
  77. }
  78. void ScrollView::RegisterObject(Context* context)
  79. {
  80. context->RegisterFactory<ScrollView>(UI_CATEGORY);
  81. COPY_BASE_ATTRIBUTES(ScrollView, UIElement);
  82. UPDATE_ATTRIBUTE_DEFAULT_VALUE(ScrollView, "Clip Children", true);
  83. UPDATE_ATTRIBUTE_DEFAULT_VALUE(ScrollView, "Is Enabled", true);
  84. UPDATE_ATTRIBUTE_DEFAULT_VALUE(ScrollView, "Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  85. REF_ACCESSOR_ATTRIBUTE(ScrollView, VAR_INTVECTOR2, "View Position", GetViewPosition, SetViewPositionAttr, IntVector2, IntVector2::ZERO, AM_FILE);
  86. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Scroll Step", GetScrollStep, SetScrollStep, float, 0.1f, AM_FILE);
  87. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Page Step", GetPageStep, SetPageStep, float, 1.0f, AM_FILE);
  88. ACCESSOR_ATTRIBUTE(ScrollView, VAR_BOOL, "Auto Show/Hide Scrollbars", GetScrollBarsAutoVisible, SetScrollBarsAutoVisible, bool, true, AM_FILE);
  89. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Scroll Deceleration", GetScrollDeceleration, SetScrollDeceleration, float, 30.0f, AM_FILE);
  90. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Scroll Snap Epsilon", GetScrollSnapEpsilon, SetScrollSnapEpsilon, float, 1.0f, AM_FILE);
  91. }
  92. void ScrollView::Update(float timeStep)
  93. {
  94. // Update touch scrolling here if necessary
  95. if (touchScrollSpeed_ == Vector2::ZERO && touchScrollSpeedMax_ == Vector2::ZERO && !barScrolling_)
  96. return;
  97. // Check if we should not scroll:
  98. // - ScrollView is not visible, is not enabled, or doesn't have focus
  99. // - The element being dragged is not a child of the ScrollView, or is one of our scrollbars
  100. if (!IsVisible() || !IsEnabled() || !HasFocus())
  101. {
  102. touchScrollSpeed_ = Vector2::ZERO;
  103. touchScrollSpeedMax_ = Vector2::ZERO;
  104. return;
  105. }
  106. UIElement* dragElement = GetSubsystem<UI>()->GetDragElement();
  107. if (dragElement)
  108. {
  109. UIElement* dragParent = dragElement->GetParent();
  110. bool dragElementIsChild = false;
  111. while (dragParent)
  112. {
  113. if (dragParent == this)
  114. {
  115. dragElementIsChild = true;
  116. break;
  117. }
  118. dragParent = dragParent->GetParent();
  119. }
  120. if (!dragElementIsChild || dragElement == horizontalScrollBar_->GetSlider() || dragElement == verticalScrollBar_->GetSlider())
  121. {
  122. touchScrollSpeed_ = Vector2::ZERO;
  123. touchScrollSpeedMax_ = Vector2::ZERO;
  124. return;
  125. }
  126. }
  127. // Update view position
  128. IntVector2 newPosition = viewPosition_;
  129. newPosition.x_ += (int)touchScrollSpeed_.x_;
  130. newPosition.y_ += (int)touchScrollSpeed_.y_;
  131. SetViewPosition(newPosition);
  132. // Smooth deceleration
  133. ScrollSmooth(timeStep);
  134. }
  135. void ScrollView::ApplyAttributes()
  136. {
  137. UIElement::ApplyAttributes();
  138. // Set the scrollbar orientations again and perform size update now that the style is known
  139. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  140. verticalScrollBar_->SetOrientation(O_VERTICAL);
  141. // If the scroll panel has a child, it should be the content element, which has some special handling
  142. if (scrollPanel_->GetNumChildren())
  143. SetContentElement(scrollPanel_->GetChild(0));
  144. OnResize();
  145. // Reapply view position with proper content element and size
  146. SetViewPosition(viewPositionAttr_);
  147. }
  148. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  149. {
  150. if (delta > 0)
  151. verticalScrollBar_->StepBack();
  152. if (delta < 0)
  153. verticalScrollBar_->StepForward();
  154. }
  155. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  156. {
  157. switch (key)
  158. {
  159. case KEY_LEFT:
  160. if (horizontalScrollBar_->IsVisible())
  161. {
  162. if (qualifiers & QUAL_CTRL)
  163. horizontalScrollBar_->SetValue(0.0f);
  164. else
  165. horizontalScrollBar_->StepBack();
  166. }
  167. break;
  168. case KEY_RIGHT:
  169. if (horizontalScrollBar_->IsVisible())
  170. {
  171. if (qualifiers & QUAL_CTRL)
  172. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  173. else
  174. horizontalScrollBar_->StepForward();
  175. }
  176. break;
  177. case KEY_HOME:
  178. qualifiers |= QUAL_CTRL;
  179. // Fallthru
  180. case KEY_UP:
  181. if (verticalScrollBar_->IsVisible())
  182. {
  183. if (qualifiers & QUAL_CTRL)
  184. verticalScrollBar_->SetValue(0.0f);
  185. else
  186. verticalScrollBar_->StepBack();
  187. }
  188. break;
  189. case KEY_END:
  190. qualifiers |= QUAL_CTRL;
  191. // Fallthru
  192. case KEY_DOWN:
  193. if (verticalScrollBar_->IsVisible())
  194. {
  195. if (qualifiers & QUAL_CTRL)
  196. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  197. else
  198. verticalScrollBar_->StepForward();
  199. }
  200. break;
  201. case KEY_PAGEUP:
  202. if (verticalScrollBar_->IsVisible())
  203. verticalScrollBar_->ChangeValue(-pageStep_);
  204. break;
  205. case KEY_PAGEDOWN:
  206. if (verticalScrollBar_->IsVisible())
  207. verticalScrollBar_->ChangeValue(pageStep_);
  208. break;
  209. }
  210. }
  211. void ScrollView::OnResize()
  212. {
  213. UpdatePanelSize();
  214. UpdateViewSize();
  215. // If scrollbar autovisibility is enabled, check whether scrollbars should be visible.
  216. // This may force another update of the panel size
  217. if (scrollBarsAutoVisible_)
  218. {
  219. ignoreEvents_ = true;
  220. horizontalScrollBar_->SetVisible(horizontalScrollBar_->GetRange() > M_EPSILON);
  221. verticalScrollBar_->SetVisible(verticalScrollBar_->GetRange() > M_EPSILON);
  222. ignoreEvents_ = false;
  223. UpdatePanelSize();
  224. }
  225. }
  226. void ScrollView::SetContentElement(UIElement* element)
  227. {
  228. if (element == contentElement_)
  229. return;
  230. if (contentElement_)
  231. {
  232. scrollPanel_->RemoveChild(contentElement_);
  233. UnsubscribeFromEvent(contentElement_, E_RESIZED);
  234. }
  235. contentElement_ = element;
  236. if (contentElement_)
  237. {
  238. scrollPanel_->AddChild(contentElement_);
  239. SubscribeToEvent(contentElement_, E_RESIZED, HANDLER(ScrollView, HandleElementResized));
  240. }
  241. OnResize();
  242. }
  243. void ScrollView::SetViewPosition(const IntVector2& position)
  244. {
  245. UpdateView(position);
  246. UpdateScrollBars();
  247. }
  248. void ScrollView::SetViewPosition(int x, int y)
  249. {
  250. SetViewPosition(IntVector2(x, y));
  251. }
  252. void ScrollView::SetScrollBarsVisible(bool horizontal, bool vertical)
  253. {
  254. scrollBarsAutoVisible_ = false;
  255. horizontalScrollBar_->SetVisible(horizontal);
  256. verticalScrollBar_->SetVisible(vertical);
  257. }
  258. void ScrollView::SetScrollBarsAutoVisible(bool enable)
  259. {
  260. if (enable != scrollBarsAutoVisible_)
  261. {
  262. scrollBarsAutoVisible_ = enable;
  263. // Check whether scrollbars should be visible now
  264. if (enable)
  265. OnResize();
  266. else
  267. {
  268. horizontalScrollBar_->SetVisible(true);
  269. verticalScrollBar_->SetVisible(true);
  270. }
  271. }
  272. }
  273. void ScrollView::SetScrollStep(float step)
  274. {
  275. horizontalScrollBar_->SetScrollStep(step);
  276. verticalScrollBar_->SetScrollStep(step);
  277. }
  278. void ScrollView::SetPageStep(float step)
  279. {
  280. pageStep_ = Max(step, 0.0f);
  281. }
  282. float ScrollView::GetScrollStep() const
  283. {
  284. return horizontalScrollBar_->GetScrollStep();
  285. }
  286. void ScrollView::SetViewPositionAttr(const IntVector2& value)
  287. {
  288. viewPositionAttr_ = value;
  289. SetViewPosition(value);
  290. }
  291. bool ScrollView::FilterImplicitAttributes(XMLElement& dest) const
  292. {
  293. if (!UIElement::FilterImplicitAttributes(dest))
  294. return false;
  295. XMLElement childElem = dest.GetChild("element");
  296. if (!FilterScrollBarImplicitAttributes(childElem, "SV_HorizontalScrollBar"))
  297. return false;
  298. if (!RemoveChildXML(childElem, "Vert Alignment", "Bottom"))
  299. return false;
  300. childElem = childElem.GetNext("element");
  301. if (!FilterScrollBarImplicitAttributes(childElem, "SV_VerticalScrollBar"))
  302. return false;
  303. if (!RemoveChildXML(childElem, "Horiz Alignment", "Right"))
  304. return false;
  305. childElem = childElem.GetNext("element");
  306. if (!childElem)
  307. return false;
  308. if (!RemoveChildXML(childElem, "Name", "SV_ScrollPanel"))
  309. return false;
  310. if (!RemoveChildXML(childElem, "Is Enabled", "true"))
  311. return false;
  312. if (!RemoveChildXML(childElem, "Clip Children", "true"))
  313. return false;
  314. if (!RemoveChildXML(childElem, "Size"))
  315. return false;
  316. return true;
  317. }
  318. bool ScrollView::FilterScrollBarImplicitAttributes(XMLElement& dest, const String& name) const
  319. {
  320. if (!dest)
  321. return false;
  322. if (!RemoveChildXML(dest, "Name", name))
  323. return false;
  324. if (!RemoveChildXML(dest, "Orientation"))
  325. return false;
  326. if (!RemoveChildXML(dest, "Range"))
  327. return false;
  328. if (!RemoveChildXML(dest, "Step Factor"))
  329. return false;
  330. if (scrollBarsAutoVisible_)
  331. {
  332. if (!RemoveChildXML(dest, "Is Visible"))
  333. return false;
  334. }
  335. return true;
  336. }
  337. void ScrollView::UpdatePanelSize()
  338. {
  339. // Ignore events in case content element resizes itself along with the panel
  340. // (content element resize triggers our OnResize(), so it could lead to infinite recursion)
  341. ignoreEvents_ = true;
  342. IntVector2 panelSize = GetSize();
  343. if (verticalScrollBar_->IsVisible())
  344. panelSize.x_ -= verticalScrollBar_->GetWidth();
  345. if (horizontalScrollBar_->IsVisible())
  346. panelSize.y_ -= horizontalScrollBar_->GetHeight();
  347. scrollPanel_->SetSize(panelSize);
  348. horizontalScrollBar_->SetWidth(scrollPanel_->GetWidth());
  349. verticalScrollBar_->SetHeight(scrollPanel_->GetHeight());
  350. if (resizeContentWidth_ && contentElement_)
  351. {
  352. IntRect panelBorder = scrollPanel_->GetClipBorder();
  353. contentElement_->SetWidth(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  354. UpdateViewSize();
  355. }
  356. ignoreEvents_ = false;
  357. }
  358. void ScrollView::UpdateViewSize()
  359. {
  360. IntVector2 size(IntVector2::ZERO);
  361. if (contentElement_)
  362. size = contentElement_->GetSize();
  363. IntRect panelBorder = scrollPanel_->GetClipBorder();
  364. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  365. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight() - panelBorder.top_ - panelBorder.bottom_);
  366. UpdateView(viewPosition_);
  367. UpdateScrollBars();
  368. }
  369. void ScrollView::UpdateScrollBars()
  370. {
  371. ignoreEvents_ = true;
  372. IntVector2 size = scrollPanel_->GetSize();
  373. IntRect panelBorder = scrollPanel_->GetClipBorder();
  374. size.x_ -= panelBorder.left_ + panelBorder.right_;
  375. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  376. if (size.x_ > 0 && viewSize_.x_ > 0)
  377. {
  378. float range = (float)viewSize_.x_ / (float)size.x_ - 1.0f;
  379. horizontalScrollBar_->SetRange(range);
  380. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  381. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  382. }
  383. if (size.y_ > 0 && viewSize_.y_ > 0)
  384. {
  385. float range = (float)viewSize_.y_ / (float)size.y_ - 1.0f;
  386. verticalScrollBar_->SetRange(range);
  387. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  388. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  389. }
  390. ignoreEvents_ = false;
  391. }
  392. void ScrollView::UpdateView(const IntVector2& position)
  393. {
  394. IntVector2 oldPosition = viewPosition_;
  395. IntRect panelBorder = scrollPanel_->GetClipBorder();
  396. IntVector2 panelSize(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_, scrollPanel_->GetHeight() -
  397. panelBorder.top_ - panelBorder.bottom_);
  398. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - panelSize.x_);
  399. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - panelSize.y_);
  400. scrollPanel_->SetChildOffset(IntVector2(-viewPosition_.x_ + panelBorder.left_, -viewPosition_.y_ + panelBorder.top_));
  401. if (viewPosition_ != oldPosition)
  402. {
  403. using namespace ViewChanged;
  404. VariantMap& eventData = GetEventDataMap();
  405. eventData[P_ELEMENT] = this;
  406. eventData[P_X] = viewPosition_.x_;
  407. eventData[P_Y] = viewPosition_.y_;
  408. SendEvent(E_VIEWCHANGED, eventData);
  409. }
  410. }
  411. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  412. {
  413. if (!ignoreEvents_)
  414. {
  415. IntVector2 size = scrollPanel_->GetSize();
  416. IntRect panelBorder = scrollPanel_->GetClipBorder();
  417. size.x_ -= panelBorder.left_ + panelBorder.right_;
  418. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  419. UpdateView(IntVector2(
  420. (int)(horizontalScrollBar_->GetValue() * (float)size.x_),
  421. (int)(verticalScrollBar_->GetValue() * (float)size.y_)
  422. ));
  423. }
  424. }
  425. void ScrollView::HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData)
  426. {
  427. // Need to recalculate panel size when scrollbar visibility changes
  428. if (!ignoreEvents_)
  429. OnResize();
  430. }
  431. void ScrollView::HandleElementResized(StringHash eventType, VariantMap& eventData)
  432. {
  433. if (!ignoreEvents_)
  434. OnResize();
  435. }
  436. void ScrollView::HandleTouchMove(StringHash eventType, VariantMap& eventData)
  437. {
  438. using namespace TouchMove;
  439. if (eventType == E_TOUCHMOVE && !barScrolling_)
  440. {
  441. scrollTouchDown_ = true;
  442. // Take new scrolling speed if it's faster than the current accumulated value
  443. float dX = (float)-eventData[P_DX].GetInt();
  444. float dY = (float)-eventData[P_DY].GetInt();
  445. if (Abs(dX) > Abs(touchScrollSpeed_.x_))
  446. touchScrollSpeed_.x_ = dX;
  447. if (Abs(dY) > Abs(touchScrollSpeed_.y_))
  448. touchScrollSpeed_.y_ = dY;
  449. touchScrollSpeedMax_.x_ = dX;
  450. touchScrollSpeedMax_.y_ = dY;
  451. }
  452. else if (eventType == E_TOUCHBEGIN)
  453. {
  454. int X = eventData[P_X].GetInt();
  455. int Y = eventData[P_Y].GetInt();
  456. IntVector2 pos = IntVector2(X, Y);
  457. // Prevent conflict between touch scroll and scrollbar scroll
  458. if (horizontalScrollBar_->IsVisible() && horizontalScrollBar_->IsInsideCombined(pos, true))
  459. barScrolling_ = true;
  460. if (verticalScrollBar_->IsVisible() && verticalScrollBar_->IsInsideCombined(pos, true))
  461. barScrolling_ = true;
  462. // Stop the smooth scrolling
  463. touchScrollSpeed_ = Vector2::ZERO;
  464. touchScrollSpeedMax_ = Vector2::ZERO;
  465. }
  466. else if (eventType == E_TOUCHEND)
  467. {
  468. // 'Flick' action
  469. barScrolling_ = false;
  470. scrollTouchDown_ = false;
  471. if (Abs(touchScrollSpeedMax_.x_) > scrollSnapEpsilon_ )
  472. touchScrollSpeed_.x_ = touchScrollSpeedMax_.x_;
  473. else
  474. touchScrollSpeed_.x_ = 0;
  475. if (Abs(touchScrollSpeedMax_.y_) > scrollSnapEpsilon_ )
  476. touchScrollSpeed_.y_ = touchScrollSpeedMax_.y_;
  477. else
  478. touchScrollSpeed_.y_ = 0;
  479. touchScrollSpeedMax_ = Vector2::ZERO;
  480. }
  481. }
  482. void ScrollView::ScrollSmooth(float timeStep)
  483. {
  484. // Decay the momentum
  485. if (touchScrollSpeedMax_.x_ >= scrollSnapEpsilon_)
  486. {
  487. touchScrollSpeedMax_.x_ -= scrollDeceleration_ * timeStep;
  488. touchScrollSpeedMax_.x_ = touchScrollSpeedMax_.x_ > 0 ? touchScrollSpeedMax_.x_ : 0;
  489. }
  490. else if (touchScrollSpeedMax_.x_ <= -scrollSnapEpsilon_)
  491. {
  492. touchScrollSpeedMax_.x_ += scrollDeceleration_ * timeStep;
  493. touchScrollSpeedMax_.x_ = touchScrollSpeedMax_.x_ < 0 ? touchScrollSpeedMax_.x_ : 0;
  494. }
  495. else
  496. touchScrollSpeedMax_.x_ = 0;
  497. if (touchScrollSpeedMax_.y_ >= scrollSnapEpsilon_)
  498. {
  499. touchScrollSpeedMax_.y_ -= scrollDeceleration_ * timeStep;
  500. touchScrollSpeedMax_.y_ = touchScrollSpeedMax_.y_ > 0 ? touchScrollSpeedMax_.y_ : 0;
  501. }
  502. else if (touchScrollSpeedMax_.y_ <= -scrollSnapEpsilon_)
  503. {
  504. touchScrollSpeedMax_.y_ += scrollDeceleration_ * timeStep;
  505. touchScrollSpeedMax_.y_ = touchScrollSpeedMax_.y_ < 0 ? touchScrollSpeedMax_.y_ : 0;
  506. }
  507. else
  508. touchScrollSpeedMax_.y_ = 0;
  509. // Control vs flick
  510. if (scrollTouchDown_)
  511. {
  512. // Finger is held down: control = instant stop
  513. touchScrollSpeed_ = Vector2::ZERO;
  514. }
  515. else
  516. {
  517. // Finger is released: flick = smooth deceleration
  518. if (touchScrollSpeed_.x_ >= scrollSnapEpsilon_)
  519. {
  520. touchScrollSpeed_.x_ -= scrollDeceleration_ * timeStep;
  521. if (touchScrollSpeed_.x_ < 0)
  522. {
  523. touchScrollSpeed_.x_ = 0;
  524. }
  525. if (horizontalScrollBar_->GetValue() >= horizontalScrollBar_->GetRange() - M_EPSILON)
  526. {
  527. // Stop movement when we reach end of scroll
  528. touchScrollSpeed_.x_ = 0;
  529. }
  530. }
  531. else if (touchScrollSpeed_.x_ < -scrollSnapEpsilon_)
  532. {
  533. touchScrollSpeed_.x_ += scrollDeceleration_ * timeStep;
  534. if (touchScrollSpeed_.x_ > 0)
  535. {
  536. touchScrollSpeed_.x_ = 0;
  537. }
  538. if (horizontalScrollBar_->GetValue() <= M_EPSILON)
  539. {
  540. // Stop movement when we reach end of scroll
  541. touchScrollSpeed_.x_ = 0;
  542. }
  543. }
  544. else
  545. touchScrollSpeed_.x_ = 0;
  546. if (touchScrollSpeed_.y_ >= scrollSnapEpsilon_)
  547. {
  548. touchScrollSpeed_.y_ -= scrollDeceleration_ * timeStep;
  549. if (touchScrollSpeed_.y_ < 0)
  550. {
  551. touchScrollSpeed_.y_ = 0;
  552. }
  553. if (verticalScrollBar_->GetValue() >= verticalScrollBar_->GetRange() - M_EPSILON)
  554. {
  555. // Stop movement when we reach end of scroll
  556. touchScrollSpeed_.y_ = 0;
  557. }
  558. }
  559. else if (touchScrollSpeed_.y_ < -scrollSnapEpsilon_)
  560. {
  561. touchScrollSpeed_.y_ += scrollDeceleration_ * timeStep;
  562. if (touchScrollSpeed_.y_ > 0)
  563. {
  564. touchScrollSpeed_.y_ = 0;
  565. }
  566. if (verticalScrollBar_->GetValue() <= M_EPSILON)
  567. {
  568. // Stop movement when we reach end of scroll
  569. touchScrollSpeed_.y_ = 0;
  570. }
  571. }
  572. else
  573. touchScrollSpeed_.y_ = 0;
  574. }
  575. }
  576. }