ScrollView.cpp 23 KB

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