ScrollView.cpp 12 KB

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