ScrollView.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_(IntVector2::ZERO),
  42. pageStep_(1.0f),
  43. scrollBarsAutoVisible_(true),
  44. ignoreEvents_(false),
  45. resizeContentWidth_(false)
  46. {
  47. clipChildren_ = true;
  48. enabled_ = true;
  49. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  50. horizontalScrollBar_ = CreateChild<ScrollBar>("SV_HorizontalScrollBar");
  51. horizontalScrollBar_->SetInternal(true);
  52. horizontalScrollBar_->SetAlignment(HA_LEFT, VA_BOTTOM);
  53. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  54. verticalScrollBar_ = CreateChild<ScrollBar>("SV_VerticalScrollBar");
  55. verticalScrollBar_->SetInternal(true);
  56. verticalScrollBar_->SetAlignment(HA_RIGHT, VA_TOP);
  57. verticalScrollBar_->SetOrientation(O_VERTICAL);
  58. scrollPanel_ = CreateChild<BorderImage>("SV_ScrollPanel");
  59. scrollPanel_->SetInternal(true);
  60. scrollPanel_->SetEnabled(true);
  61. scrollPanel_->SetClipChildren(true);
  62. SubscribeToEvent(horizontalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  63. SubscribeToEvent(horizontalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  64. SubscribeToEvent(verticalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  65. SubscribeToEvent(verticalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  66. SubscribeToEvent(E_TOUCHMOVE, HANDLER(ScrollView, HandleTouchMove));
  67. }
  68. ScrollView::~ScrollView()
  69. {
  70. }
  71. void ScrollView::RegisterObject(Context* context)
  72. {
  73. context->RegisterFactory<ScrollView>(UI_CATEGORY);
  74. COPY_BASE_ATTRIBUTES(ScrollView, UIElement);
  75. UPDATE_ATTRIBUTE_DEFAULT_VALUE(ScrollView, "Clip Children", true);
  76. UPDATE_ATTRIBUTE_DEFAULT_VALUE(ScrollView, "Is Enabled", true);
  77. UPDATE_ATTRIBUTE_DEFAULT_VALUE(ScrollView, "Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  78. REF_ACCESSOR_ATTRIBUTE(ScrollView, VAR_INTVECTOR2, "View Position", GetViewPosition, SetViewPositionAttr, IntVector2, IntVector2::ZERO, AM_FILE);
  79. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Scroll Step", GetScrollStep, SetScrollStep, float, 0.1f, AM_FILE);
  80. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Page Step", GetPageStep, SetPageStep, float, 1.0f, AM_FILE);
  81. ACCESSOR_ATTRIBUTE(ScrollView, VAR_BOOL, "Auto Show/Hide Scrollbars", GetScrollBarsAutoVisible, SetScrollBarsAutoVisible, bool, true, AM_FILE);
  82. }
  83. void ScrollView::Update(float timeStep)
  84. {
  85. // Update touch scrolling here if necessary
  86. if (touchScrollSpeed_ == IntVector2::ZERO)
  87. return;
  88. // Check if we should not scroll:
  89. // - ScrollView is not visible, is not enabled, or doesn't have focus
  90. // - The element being dragged is not a child of the ScrollView, or is one of our scrollbars
  91. if (!IsVisible() || !IsEnabled() || !HasFocus())
  92. {
  93. touchScrollSpeed_ = IntVector2::ZERO;
  94. return;
  95. }
  96. UIElement* dragElement = GetSubsystem<UI>()->GetDragElement();
  97. if (dragElement)
  98. {
  99. UIElement* dragParent = dragElement->GetParent();
  100. bool dragElementIsChild = false;
  101. while (dragParent)
  102. {
  103. if (dragParent == this)
  104. {
  105. dragElementIsChild = true;
  106. break;
  107. }
  108. dragParent = dragParent->GetParent();
  109. }
  110. if (!dragElementIsChild || dragElement == horizontalScrollBar_->GetSlider() || dragElement == verticalScrollBar_->GetSlider())
  111. {
  112. touchScrollSpeed_ = IntVector2::ZERO;
  113. return;
  114. }
  115. }
  116. // Update view position, reset speed accumulation for next frame
  117. IntVector2 newPosition = viewPosition_;
  118. newPosition.x_ += touchScrollSpeed_.x_;
  119. newPosition.y_ += touchScrollSpeed_.y_;
  120. SetViewPosition(newPosition);
  121. /// \todo Could have smooth deceleration
  122. touchScrollSpeed_ = IntVector2::ZERO;
  123. }
  124. void ScrollView::ApplyAttributes()
  125. {
  126. UIElement::ApplyAttributes();
  127. // Set the scrollbar orientations again and perform size update now that the style is known
  128. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  129. verticalScrollBar_->SetOrientation(O_VERTICAL);
  130. // If the scroll panel has a child, it should be the content element, which has some special handling
  131. if (scrollPanel_->GetNumChildren())
  132. SetContentElement(scrollPanel_->GetChild(0));
  133. OnResize();
  134. // Reapply view position with proper content element and size
  135. SetViewPosition(viewPositionAttr_);
  136. }
  137. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  138. {
  139. if (delta > 0)
  140. verticalScrollBar_->StepBack();
  141. if (delta < 0)
  142. verticalScrollBar_->StepForward();
  143. }
  144. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  145. {
  146. switch (key)
  147. {
  148. case KEY_LEFT:
  149. if (horizontalScrollBar_->IsVisible())
  150. {
  151. if (qualifiers & QUAL_CTRL)
  152. horizontalScrollBar_->SetValue(0.0f);
  153. else
  154. horizontalScrollBar_->StepBack();
  155. }
  156. break;
  157. case KEY_RIGHT:
  158. if (horizontalScrollBar_->IsVisible())
  159. {
  160. if (qualifiers & QUAL_CTRL)
  161. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  162. else
  163. horizontalScrollBar_->StepForward();
  164. }
  165. break;
  166. case KEY_HOME:
  167. qualifiers |= QUAL_CTRL;
  168. // Fallthru
  169. case KEY_UP:
  170. if (verticalScrollBar_->IsVisible())
  171. {
  172. if (qualifiers & QUAL_CTRL)
  173. verticalScrollBar_->SetValue(0.0f);
  174. else
  175. verticalScrollBar_->StepBack();
  176. }
  177. break;
  178. case KEY_END:
  179. qualifiers |= QUAL_CTRL;
  180. // Fallthru
  181. case KEY_DOWN:
  182. if (verticalScrollBar_->IsVisible())
  183. {
  184. if (qualifiers & QUAL_CTRL)
  185. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  186. else
  187. verticalScrollBar_->StepForward();
  188. }
  189. break;
  190. case KEY_PAGEUP:
  191. if (verticalScrollBar_->IsVisible())
  192. verticalScrollBar_->ChangeValue(-pageStep_);
  193. break;
  194. case KEY_PAGEDOWN:
  195. if (verticalScrollBar_->IsVisible())
  196. verticalScrollBar_->ChangeValue(pageStep_);
  197. break;
  198. }
  199. }
  200. void ScrollView::OnResize()
  201. {
  202. UpdatePanelSize();
  203. UpdateViewSize();
  204. // If scrollbar autovisibility is enabled, check whether scrollbars should be visible.
  205. // This may force another update of the panel size
  206. if (scrollBarsAutoVisible_)
  207. {
  208. ignoreEvents_ = true;
  209. horizontalScrollBar_->SetVisible(horizontalScrollBar_->GetRange() > M_EPSILON);
  210. verticalScrollBar_->SetVisible(verticalScrollBar_->GetRange() > M_EPSILON);
  211. ignoreEvents_ = false;
  212. UpdatePanelSize();
  213. }
  214. }
  215. void ScrollView::SetContentElement(UIElement* element)
  216. {
  217. if (element == contentElement_)
  218. return;
  219. if (contentElement_)
  220. {
  221. scrollPanel_->RemoveChild(contentElement_);
  222. UnsubscribeFromEvent(contentElement_, E_RESIZED);
  223. }
  224. contentElement_ = element;
  225. if (contentElement_)
  226. {
  227. scrollPanel_->AddChild(contentElement_);
  228. SubscribeToEvent(contentElement_, E_RESIZED, HANDLER(ScrollView, HandleElementResized));
  229. }
  230. OnResize();
  231. }
  232. void ScrollView::SetViewPosition(const IntVector2& position)
  233. {
  234. UpdateView(position);
  235. UpdateScrollBars();
  236. }
  237. void ScrollView::SetViewPosition(int x, int y)
  238. {
  239. SetViewPosition(IntVector2(x, y));
  240. }
  241. void ScrollView::SetScrollBarsVisible(bool horizontal, bool vertical)
  242. {
  243. scrollBarsAutoVisible_ = false;
  244. horizontalScrollBar_->SetVisible(horizontal);
  245. verticalScrollBar_->SetVisible(vertical);
  246. }
  247. void ScrollView::SetScrollBarsAutoVisible(bool enable)
  248. {
  249. if (enable != scrollBarsAutoVisible_)
  250. {
  251. scrollBarsAutoVisible_ = enable;
  252. // Check whether scrollbars should be visible now
  253. if (enable)
  254. OnResize();
  255. else
  256. {
  257. horizontalScrollBar_->SetVisible(true);
  258. verticalScrollBar_->SetVisible(true);
  259. }
  260. }
  261. }
  262. void ScrollView::SetScrollStep(float step)
  263. {
  264. horizontalScrollBar_->SetScrollStep(step);
  265. verticalScrollBar_->SetScrollStep(step);
  266. }
  267. void ScrollView::SetPageStep(float step)
  268. {
  269. pageStep_ = Max(step, 0.0f);
  270. }
  271. float ScrollView::GetScrollStep() const
  272. {
  273. return horizontalScrollBar_->GetScrollStep();
  274. }
  275. void ScrollView::SetViewPositionAttr(const IntVector2& value)
  276. {
  277. viewPositionAttr_ = value;
  278. SetViewPosition(value);
  279. }
  280. bool ScrollView::FilterImplicitAttributes(XMLElement& dest) const
  281. {
  282. if (!UIElement::FilterImplicitAttributes(dest))
  283. return false;
  284. XMLElement childElem = dest.GetChild("element");
  285. if (!FilterScrollBarImplicitAttributes(childElem, "SV_HorizontalScrollBar"))
  286. return false;
  287. if (!RemoveChildXML(childElem, "Vert Alignment", "Bottom"))
  288. return false;
  289. childElem = childElem.GetNext("element");
  290. if (!FilterScrollBarImplicitAttributes(childElem, "SV_VerticalScrollBar"))
  291. return false;
  292. if (!RemoveChildXML(childElem, "Horiz Alignment", "Right"))
  293. return false;
  294. childElem = childElem.GetNext("element");
  295. if (!childElem)
  296. return false;
  297. if (!RemoveChildXML(childElem, "Name", "SV_ScrollPanel"))
  298. return false;
  299. if (!RemoveChildXML(childElem, "Is Enabled", "true"))
  300. return false;
  301. if (!RemoveChildXML(childElem, "Clip Children", "true"))
  302. return false;
  303. if (!RemoveChildXML(childElem, "Size"))
  304. return false;
  305. return true;
  306. }
  307. bool ScrollView::FilterScrollBarImplicitAttributes(XMLElement& dest, const String& name) const
  308. {
  309. if (!dest)
  310. return false;
  311. if (!RemoveChildXML(dest, "Name", name))
  312. return false;
  313. if (!RemoveChildXML(dest, "Orientation"))
  314. return false;
  315. if (!RemoveChildXML(dest, "Range"))
  316. return false;
  317. if (!RemoveChildXML(dest, "Step Factor"))
  318. return false;
  319. if (scrollBarsAutoVisible_)
  320. {
  321. if (!RemoveChildXML(dest, "Is Visible"))
  322. return false;
  323. }
  324. return true;
  325. }
  326. void ScrollView::UpdatePanelSize()
  327. {
  328. // Ignore events in case content element resizes itself along with the panel
  329. // (content element resize triggers our OnResize(), so it could lead to infinite recursion)
  330. ignoreEvents_ = true;
  331. IntVector2 panelSize = GetSize();
  332. if (verticalScrollBar_->IsVisible())
  333. panelSize.x_ -= verticalScrollBar_->GetWidth();
  334. if (horizontalScrollBar_->IsVisible())
  335. panelSize.y_ -= horizontalScrollBar_->GetHeight();
  336. scrollPanel_->SetSize(panelSize);
  337. horizontalScrollBar_->SetWidth(scrollPanel_->GetWidth());
  338. verticalScrollBar_->SetHeight(scrollPanel_->GetHeight());
  339. if (resizeContentWidth_ && contentElement_)
  340. {
  341. IntRect panelBorder = scrollPanel_->GetClipBorder();
  342. contentElement_->SetWidth(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  343. UpdateViewSize();
  344. }
  345. ignoreEvents_ = false;
  346. }
  347. void ScrollView::UpdateViewSize()
  348. {
  349. IntVector2 size(IntVector2::ZERO);
  350. if (contentElement_)
  351. size = contentElement_->GetSize();
  352. IntRect panelBorder = scrollPanel_->GetClipBorder();
  353. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  354. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight() - panelBorder.top_ - panelBorder.bottom_);
  355. UpdateView(viewPosition_);
  356. UpdateScrollBars();
  357. }
  358. void ScrollView::UpdateScrollBars()
  359. {
  360. ignoreEvents_ = true;
  361. IntVector2 size = scrollPanel_->GetSize();
  362. IntRect panelBorder = scrollPanel_->GetClipBorder();
  363. size.x_ -= panelBorder.left_ + panelBorder.right_;
  364. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  365. if (size.x_ > 0 && viewSize_.x_ > 0)
  366. {
  367. float range = (float)viewSize_.x_ / (float)size.x_ - 1.0f;
  368. horizontalScrollBar_->SetRange(range);
  369. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  370. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  371. }
  372. if (size.y_ > 0 && viewSize_.y_ > 0)
  373. {
  374. float range = (float)viewSize_.y_ / (float)size.y_ - 1.0f;
  375. verticalScrollBar_->SetRange(range);
  376. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  377. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  378. }
  379. ignoreEvents_ = false;
  380. }
  381. void ScrollView::UpdateView(const IntVector2& position)
  382. {
  383. IntVector2 oldPosition = viewPosition_;
  384. IntRect panelBorder = scrollPanel_->GetClipBorder();
  385. IntVector2 panelSize(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_, scrollPanel_->GetHeight() -
  386. panelBorder.top_ - panelBorder.bottom_);
  387. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - panelSize.x_);
  388. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - panelSize.y_);
  389. scrollPanel_->SetChildOffset(IntVector2(-viewPosition_.x_ + panelBorder.left_, -viewPosition_.y_ + panelBorder.top_));
  390. if (viewPosition_ != oldPosition)
  391. {
  392. using namespace ViewChanged;
  393. VariantMap& eventData = GetEventDataMap();
  394. eventData[P_ELEMENT] = this;
  395. eventData[P_X] = viewPosition_.x_;
  396. eventData[P_Y] = viewPosition_.y_;
  397. SendEvent(E_VIEWCHANGED, eventData);
  398. }
  399. }
  400. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  401. {
  402. if (!ignoreEvents_)
  403. {
  404. IntVector2 size = scrollPanel_->GetSize();
  405. IntRect panelBorder = scrollPanel_->GetClipBorder();
  406. size.x_ -= panelBorder.left_ + panelBorder.right_;
  407. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  408. UpdateView(IntVector2(
  409. (int)(horizontalScrollBar_->GetValue() * (float)size.x_),
  410. (int)(verticalScrollBar_->GetValue() * (float)size.y_)
  411. ));
  412. }
  413. }
  414. void ScrollView::HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData)
  415. {
  416. // Need to recalculate panel size when scrollbar visibility changes
  417. if (!ignoreEvents_)
  418. OnResize();
  419. }
  420. void ScrollView::HandleElementResized(StringHash eventType, VariantMap& eventData)
  421. {
  422. if (!ignoreEvents_)
  423. OnResize();
  424. }
  425. void ScrollView::HandleTouchMove(StringHash eventType, VariantMap& eventData)
  426. {
  427. using namespace TouchMove;
  428. // Take new scrolling speed if it's faster than the current accumulated value
  429. int dX = -eventData[P_DX].GetInt();
  430. int dY = -eventData[P_DY].GetInt();
  431. if (Abs(dX) > Abs(touchScrollSpeed_.x_))
  432. touchScrollSpeed_.x_ = dX;
  433. if (Abs(dY) > Abs(touchScrollSpeed_.y_))
  434. touchScrollSpeed_.y_ = dY;
  435. }
  436. }