ScrollView.cpp 21 KB

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