ScrollView.cpp 23 KB

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