ScrollView.cpp 23 KB

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