ScrollView.cpp 23 KB

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