ScrollView.cpp 11 KB

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