2
0

ScrollView.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. if (GetSubsystem<UI>()->IsDragging())
  107. {
  108. Vector<UIElement*> dragElements = GetSubsystem<UI>()->GetDragElements();
  109. for (unsigned i = 0; i< dragElements.Size(); i++)
  110. {
  111. UIElement* dragElement = dragElements[i];
  112. int dragButtons = dragElement->GetDragButtonCombo();
  113. if (dragButtons != MOUSEB_LEFT)
  114. continue;
  115. UIElement* dragParent = dragElement->GetParent();
  116. bool dragElementIsChild = false;
  117. while (dragParent)
  118. {
  119. if (dragParent == this)
  120. {
  121. dragElementIsChild = true;
  122. break;
  123. }
  124. dragParent = dragParent->GetParent();
  125. }
  126. if (!dragElementIsChild || dragElement == horizontalScrollBar_->GetSlider() || dragElement == verticalScrollBar_->GetSlider())
  127. {
  128. touchScrollSpeed_ = Vector2::ZERO;
  129. touchScrollSpeedMax_ = Vector2::ZERO;
  130. return;
  131. }
  132. }
  133. }
  134. // Update view position
  135. IntVector2 newPosition = viewPosition_;
  136. newPosition.x_ += (int)touchScrollSpeed_.x_;
  137. newPosition.y_ += (int)touchScrollSpeed_.y_;
  138. SetViewPosition(newPosition);
  139. // Smooth deceleration
  140. ScrollSmooth(timeStep);
  141. }
  142. void ScrollView::ApplyAttributes()
  143. {
  144. UIElement::ApplyAttributes();
  145. // Set the scrollbar orientations again and perform size update now that the style is known
  146. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  147. verticalScrollBar_->SetOrientation(O_VERTICAL);
  148. // If the scroll panel has a child, it should be the content element, which has some special handling
  149. if (scrollPanel_->GetNumChildren())
  150. SetContentElement(scrollPanel_->GetChild(0));
  151. OnResize();
  152. // Reapply view position with proper content element and size
  153. SetViewPosition(viewPositionAttr_);
  154. }
  155. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  156. {
  157. if (delta > 0)
  158. verticalScrollBar_->StepBack();
  159. if (delta < 0)
  160. verticalScrollBar_->StepForward();
  161. }
  162. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  163. {
  164. switch (key)
  165. {
  166. case KEY_LEFT:
  167. if (horizontalScrollBar_->IsVisible())
  168. {
  169. if (qualifiers & QUAL_CTRL)
  170. horizontalScrollBar_->SetValue(0.0f);
  171. else
  172. horizontalScrollBar_->StepBack();
  173. }
  174. break;
  175. case KEY_RIGHT:
  176. if (horizontalScrollBar_->IsVisible())
  177. {
  178. if (qualifiers & QUAL_CTRL)
  179. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  180. else
  181. horizontalScrollBar_->StepForward();
  182. }
  183. break;
  184. case KEY_HOME:
  185. qualifiers |= QUAL_CTRL;
  186. // Fallthru
  187. case KEY_UP:
  188. if (verticalScrollBar_->IsVisible())
  189. {
  190. if (qualifiers & QUAL_CTRL)
  191. verticalScrollBar_->SetValue(0.0f);
  192. else
  193. verticalScrollBar_->StepBack();
  194. }
  195. break;
  196. case KEY_END:
  197. qualifiers |= QUAL_CTRL;
  198. // Fallthru
  199. case KEY_DOWN:
  200. if (verticalScrollBar_->IsVisible())
  201. {
  202. if (qualifiers & QUAL_CTRL)
  203. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  204. else
  205. verticalScrollBar_->StepForward();
  206. }
  207. break;
  208. case KEY_PAGEUP:
  209. if (verticalScrollBar_->IsVisible())
  210. verticalScrollBar_->ChangeValue(-pageStep_);
  211. break;
  212. case KEY_PAGEDOWN:
  213. if (verticalScrollBar_->IsVisible())
  214. verticalScrollBar_->ChangeValue(pageStep_);
  215. break;
  216. }
  217. }
  218. void ScrollView::OnResize()
  219. {
  220. UpdatePanelSize();
  221. UpdateViewSize();
  222. // If scrollbar autovisibility is enabled, check whether scrollbars should be visible.
  223. // This may force another update of the panel size
  224. if (scrollBarsAutoVisible_)
  225. {
  226. ignoreEvents_ = true;
  227. horizontalScrollBar_->SetVisible(horizontalScrollBar_->GetRange() > M_EPSILON);
  228. verticalScrollBar_->SetVisible(verticalScrollBar_->GetRange() > M_EPSILON);
  229. ignoreEvents_ = false;
  230. UpdatePanelSize();
  231. }
  232. }
  233. void ScrollView::SetContentElement(UIElement* element)
  234. {
  235. if (element == contentElement_)
  236. return;
  237. if (contentElement_)
  238. {
  239. scrollPanel_->RemoveChild(contentElement_);
  240. UnsubscribeFromEvent(contentElement_, E_RESIZED);
  241. }
  242. contentElement_ = element;
  243. if (contentElement_)
  244. {
  245. scrollPanel_->AddChild(contentElement_);
  246. SubscribeToEvent(contentElement_, E_RESIZED, HANDLER(ScrollView, HandleElementResized));
  247. }
  248. OnResize();
  249. }
  250. void ScrollView::SetViewPosition(const IntVector2& position)
  251. {
  252. UpdateView(position);
  253. UpdateScrollBars();
  254. }
  255. void ScrollView::SetViewPosition(int x, int y)
  256. {
  257. SetViewPosition(IntVector2(x, y));
  258. }
  259. void ScrollView::SetScrollBarsVisible(bool horizontal, bool vertical)
  260. {
  261. scrollBarsAutoVisible_ = false;
  262. horizontalScrollBar_->SetVisible(horizontal);
  263. verticalScrollBar_->SetVisible(vertical);
  264. }
  265. void ScrollView::SetScrollBarsAutoVisible(bool enable)
  266. {
  267. if (enable != scrollBarsAutoVisible_)
  268. {
  269. scrollBarsAutoVisible_ = enable;
  270. // Check whether scrollbars should be visible now
  271. if (enable)
  272. OnResize();
  273. else
  274. {
  275. horizontalScrollBar_->SetVisible(true);
  276. verticalScrollBar_->SetVisible(true);
  277. }
  278. }
  279. }
  280. void ScrollView::SetScrollStep(float step)
  281. {
  282. horizontalScrollBar_->SetScrollStep(step);
  283. verticalScrollBar_->SetScrollStep(step);
  284. }
  285. void ScrollView::SetPageStep(float step)
  286. {
  287. pageStep_ = Max(step, 0.0f);
  288. }
  289. float ScrollView::GetScrollStep() const
  290. {
  291. return horizontalScrollBar_->GetScrollStep();
  292. }
  293. void ScrollView::SetViewPositionAttr(const IntVector2& value)
  294. {
  295. viewPositionAttr_ = value;
  296. SetViewPosition(value);
  297. }
  298. bool ScrollView::FilterImplicitAttributes(XMLElement& dest) const
  299. {
  300. if (!UIElement::FilterImplicitAttributes(dest))
  301. return false;
  302. XMLElement childElem = dest.GetChild("element");
  303. if (!FilterScrollBarImplicitAttributes(childElem, "SV_HorizontalScrollBar"))
  304. return false;
  305. if (!RemoveChildXML(childElem, "Vert Alignment", "Bottom"))
  306. return false;
  307. childElem = childElem.GetNext("element");
  308. if (!FilterScrollBarImplicitAttributes(childElem, "SV_VerticalScrollBar"))
  309. return false;
  310. if (!RemoveChildXML(childElem, "Horiz Alignment", "Right"))
  311. return false;
  312. childElem = childElem.GetNext("element");
  313. if (!childElem)
  314. return false;
  315. if (!RemoveChildXML(childElem, "Name", "SV_ScrollPanel"))
  316. return false;
  317. if (!RemoveChildXML(childElem, "Is Enabled", "true"))
  318. return false;
  319. if (!RemoveChildXML(childElem, "Clip Children", "true"))
  320. return false;
  321. if (!RemoveChildXML(childElem, "Size"))
  322. return false;
  323. return true;
  324. }
  325. bool ScrollView::FilterScrollBarImplicitAttributes(XMLElement& dest, const String& name) const
  326. {
  327. if (!dest)
  328. return false;
  329. if (!RemoveChildXML(dest, "Name", name))
  330. return false;
  331. if (!RemoveChildXML(dest, "Orientation"))
  332. return false;
  333. if (!RemoveChildXML(dest, "Range"))
  334. return false;
  335. if (!RemoveChildXML(dest, "Step Factor"))
  336. return false;
  337. if (scrollBarsAutoVisible_)
  338. {
  339. if (!RemoveChildXML(dest, "Is Visible"))
  340. return false;
  341. }
  342. return true;
  343. }
  344. void ScrollView::UpdatePanelSize()
  345. {
  346. // Ignore events in case content element resizes itself along with the panel
  347. // (content element resize triggers our OnResize(), so it could lead to infinite recursion)
  348. ignoreEvents_ = true;
  349. IntVector2 panelSize = GetSize();
  350. if (verticalScrollBar_->IsVisible())
  351. panelSize.x_ -= verticalScrollBar_->GetWidth();
  352. if (horizontalScrollBar_->IsVisible())
  353. panelSize.y_ -= horizontalScrollBar_->GetHeight();
  354. scrollPanel_->SetSize(panelSize);
  355. horizontalScrollBar_->SetWidth(scrollPanel_->GetWidth());
  356. verticalScrollBar_->SetHeight(scrollPanel_->GetHeight());
  357. if (resizeContentWidth_ && contentElement_)
  358. {
  359. IntRect panelBorder = scrollPanel_->GetClipBorder();
  360. contentElement_->SetWidth(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  361. UpdateViewSize();
  362. }
  363. ignoreEvents_ = false;
  364. }
  365. void ScrollView::UpdateViewSize()
  366. {
  367. IntVector2 size(IntVector2::ZERO);
  368. if (contentElement_)
  369. size = contentElement_->GetSize();
  370. IntRect panelBorder = scrollPanel_->GetClipBorder();
  371. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  372. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight() - panelBorder.top_ - panelBorder.bottom_);
  373. UpdateView(viewPosition_);
  374. UpdateScrollBars();
  375. }
  376. void ScrollView::UpdateScrollBars()
  377. {
  378. ignoreEvents_ = true;
  379. IntVector2 size = scrollPanel_->GetSize();
  380. IntRect panelBorder = scrollPanel_->GetClipBorder();
  381. size.x_ -= panelBorder.left_ + panelBorder.right_;
  382. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  383. if (size.x_ > 0 && viewSize_.x_ > 0)
  384. {
  385. float range = (float)viewSize_.x_ / (float)size.x_ - 1.0f;
  386. horizontalScrollBar_->SetRange(range);
  387. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  388. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  389. }
  390. if (size.y_ > 0 && viewSize_.y_ > 0)
  391. {
  392. float range = (float)viewSize_.y_ / (float)size.y_ - 1.0f;
  393. verticalScrollBar_->SetRange(range);
  394. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  395. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  396. }
  397. ignoreEvents_ = false;
  398. }
  399. void ScrollView::UpdateView(const IntVector2& position)
  400. {
  401. IntVector2 oldPosition = viewPosition_;
  402. IntRect panelBorder = scrollPanel_->GetClipBorder();
  403. IntVector2 panelSize(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_, scrollPanel_->GetHeight() -
  404. panelBorder.top_ - panelBorder.bottom_);
  405. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - panelSize.x_);
  406. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - panelSize.y_);
  407. scrollPanel_->SetChildOffset(IntVector2(-viewPosition_.x_ + panelBorder.left_, -viewPosition_.y_ + panelBorder.top_));
  408. if (viewPosition_ != oldPosition)
  409. {
  410. using namespace ViewChanged;
  411. VariantMap& eventData = GetEventDataMap();
  412. eventData[P_ELEMENT] = this;
  413. eventData[P_X] = viewPosition_.x_;
  414. eventData[P_Y] = viewPosition_.y_;
  415. SendEvent(E_VIEWCHANGED, eventData);
  416. }
  417. }
  418. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  419. {
  420. if (!ignoreEvents_)
  421. {
  422. IntVector2 size = scrollPanel_->GetSize();
  423. IntRect panelBorder = scrollPanel_->GetClipBorder();
  424. size.x_ -= panelBorder.left_ + panelBorder.right_;
  425. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  426. UpdateView(IntVector2(
  427. (int)(horizontalScrollBar_->GetValue() * (float)size.x_),
  428. (int)(verticalScrollBar_->GetValue() * (float)size.y_)
  429. ));
  430. }
  431. }
  432. void ScrollView::HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData)
  433. {
  434. // Need to recalculate panel size when scrollbar visibility changes
  435. if (!ignoreEvents_)
  436. OnResize();
  437. }
  438. void ScrollView::HandleElementResized(StringHash eventType, VariantMap& eventData)
  439. {
  440. if (!ignoreEvents_)
  441. OnResize();
  442. }
  443. void ScrollView::HandleTouchMove(StringHash eventType, VariantMap& eventData)
  444. {
  445. using namespace TouchMove;
  446. if (eventType == E_TOUCHMOVE && !barScrolling_)
  447. {
  448. scrollTouchDown_ = true;
  449. // Take new scrolling speed if it's faster than the current accumulated value
  450. float dX = (float)-eventData[P_DX].GetInt();
  451. float dY = (float)-eventData[P_DY].GetInt();
  452. if (Abs(dX) > Abs(touchScrollSpeed_.x_))
  453. touchScrollSpeed_.x_ = dX;
  454. if (Abs(dY) > Abs(touchScrollSpeed_.y_))
  455. touchScrollSpeed_.y_ = dY;
  456. touchScrollSpeedMax_.x_ = dX;
  457. touchScrollSpeedMax_.y_ = dY;
  458. }
  459. else if (eventType == E_TOUCHBEGIN)
  460. {
  461. int X = eventData[P_X].GetInt();
  462. int Y = eventData[P_Y].GetInt();
  463. IntVector2 pos = IntVector2(X, Y);
  464. // Prevent conflict between touch scroll and scrollbar scroll
  465. if (horizontalScrollBar_->IsVisible() && horizontalScrollBar_->IsInsideCombined(pos, true))
  466. barScrolling_ = true;
  467. if (verticalScrollBar_->IsVisible() && verticalScrollBar_->IsInsideCombined(pos, true))
  468. barScrolling_ = true;
  469. // Stop the smooth scrolling
  470. touchScrollSpeed_ = Vector2::ZERO;
  471. touchScrollSpeedMax_ = Vector2::ZERO;
  472. }
  473. else if (eventType == E_TOUCHEND)
  474. {
  475. // 'Flick' action
  476. barScrolling_ = false;
  477. scrollTouchDown_ = false;
  478. if (Abs(touchScrollSpeedMax_.x_) > scrollSnapEpsilon_ )
  479. touchScrollSpeed_.x_ = touchScrollSpeedMax_.x_;
  480. else
  481. touchScrollSpeed_.x_ = 0;
  482. if (Abs(touchScrollSpeedMax_.y_) > scrollSnapEpsilon_ )
  483. touchScrollSpeed_.y_ = touchScrollSpeedMax_.y_;
  484. else
  485. touchScrollSpeed_.y_ = 0;
  486. touchScrollSpeedMax_ = Vector2::ZERO;
  487. }
  488. }
  489. void ScrollView::ScrollSmooth(float timeStep)
  490. {
  491. // Decay the momentum
  492. if (touchScrollSpeedMax_.x_ >= scrollSnapEpsilon_)
  493. {
  494. touchScrollSpeedMax_.x_ -= scrollDeceleration_ * timeStep;
  495. touchScrollSpeedMax_.x_ = touchScrollSpeedMax_.x_ > 0 ? touchScrollSpeedMax_.x_ : 0;
  496. }
  497. else if (touchScrollSpeedMax_.x_ <= -scrollSnapEpsilon_)
  498. {
  499. touchScrollSpeedMax_.x_ += scrollDeceleration_ * timeStep;
  500. touchScrollSpeedMax_.x_ = touchScrollSpeedMax_.x_ < 0 ? touchScrollSpeedMax_.x_ : 0;
  501. }
  502. else
  503. touchScrollSpeedMax_.x_ = 0;
  504. if (touchScrollSpeedMax_.y_ >= scrollSnapEpsilon_)
  505. {
  506. touchScrollSpeedMax_.y_ -= scrollDeceleration_ * timeStep;
  507. touchScrollSpeedMax_.y_ = touchScrollSpeedMax_.y_ > 0 ? touchScrollSpeedMax_.y_ : 0;
  508. }
  509. else if (touchScrollSpeedMax_.y_ <= -scrollSnapEpsilon_)
  510. {
  511. touchScrollSpeedMax_.y_ += scrollDeceleration_ * timeStep;
  512. touchScrollSpeedMax_.y_ = touchScrollSpeedMax_.y_ < 0 ? touchScrollSpeedMax_.y_ : 0;
  513. }
  514. else
  515. touchScrollSpeedMax_.y_ = 0;
  516. // Control vs flick
  517. if (scrollTouchDown_)
  518. {
  519. // Finger is held down: control = instant stop
  520. touchScrollSpeed_ = Vector2::ZERO;
  521. }
  522. else
  523. {
  524. // Finger is released: flick = smooth deceleration
  525. if (touchScrollSpeed_.x_ >= scrollSnapEpsilon_)
  526. {
  527. touchScrollSpeed_.x_ -= scrollDeceleration_ * timeStep;
  528. if (touchScrollSpeed_.x_ < 0)
  529. {
  530. touchScrollSpeed_.x_ = 0;
  531. }
  532. if (horizontalScrollBar_->GetValue() >= horizontalScrollBar_->GetRange() - M_EPSILON)
  533. {
  534. // Stop movement when we reach end of scroll
  535. touchScrollSpeed_.x_ = 0;
  536. }
  537. }
  538. else if (touchScrollSpeed_.x_ < -scrollSnapEpsilon_)
  539. {
  540. touchScrollSpeed_.x_ += scrollDeceleration_ * timeStep;
  541. if (touchScrollSpeed_.x_ > 0)
  542. {
  543. touchScrollSpeed_.x_ = 0;
  544. }
  545. if (horizontalScrollBar_->GetValue() <= M_EPSILON)
  546. {
  547. // Stop movement when we reach end of scroll
  548. touchScrollSpeed_.x_ = 0;
  549. }
  550. }
  551. else
  552. touchScrollSpeed_.x_ = 0;
  553. if (touchScrollSpeed_.y_ >= scrollSnapEpsilon_)
  554. {
  555. touchScrollSpeed_.y_ -= scrollDeceleration_ * timeStep;
  556. if (touchScrollSpeed_.y_ < 0)
  557. {
  558. touchScrollSpeed_.y_ = 0;
  559. }
  560. if (verticalScrollBar_->GetValue() >= verticalScrollBar_->GetRange() - M_EPSILON)
  561. {
  562. // Stop movement when we reach end of scroll
  563. touchScrollSpeed_.y_ = 0;
  564. }
  565. }
  566. else if (touchScrollSpeed_.y_ < -scrollSnapEpsilon_)
  567. {
  568. touchScrollSpeed_.y_ += scrollDeceleration_ * timeStep;
  569. if (touchScrollSpeed_.y_ > 0)
  570. {
  571. touchScrollSpeed_.y_ = 0;
  572. }
  573. if (verticalScrollBar_->GetValue() <= M_EPSILON)
  574. {
  575. // Stop movement when we reach end of scroll
  576. touchScrollSpeed_.y_ = 0;
  577. }
  578. }
  579. else
  580. touchScrollSpeed_.y_ = 0;
  581. }
  582. }
  583. }