ScrollView.cpp 24 KB

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