ScrollView.cpp 23 KB

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