ScrollView.cpp 14 KB

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