ScrollView.cpp 23 KB

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