ScrollView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "BorderImage.h"
  26. #include "InputEvents.h"
  27. #include "ScrollBar.h"
  28. #include "ScrollView.h"
  29. #include "UIEvents.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. static const float STEP_FACTOR = 300.0f;
  34. OBJECTTYPESTATIC(ScrollView);
  35. ScrollView::ScrollView(Context* context) :
  36. UIElement(context),
  37. viewPosition_(IntVector2::ZERO),
  38. viewSize_(IntVector2::ZERO),
  39. viewPositionAttr_(IntVector2::ZERO),
  40. pageStep_(1.0f)
  41. {
  42. clipChildren_ = true;
  43. active_ = true;
  44. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  45. horizontalScrollBar_ = CreateChild<ScrollBar>();
  46. horizontalScrollBar_->SetInternal(true);
  47. horizontalScrollBar_->SetAlignment(HA_LEFT, VA_BOTTOM);
  48. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  49. verticalScrollBar_ = CreateChild<ScrollBar>();
  50. verticalScrollBar_->SetInternal(true);
  51. verticalScrollBar_->SetAlignment(HA_RIGHT, VA_TOP);
  52. verticalScrollBar_->SetOrientation(O_VERTICAL);
  53. scrollPanel_ = CreateChild<BorderImage>();
  54. scrollPanel_->SetInternal(true);
  55. scrollPanel_->SetActive(true);
  56. scrollPanel_->SetClipChildren(true);
  57. SubscribeToEvent(horizontalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  58. SubscribeToEvent(horizontalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  59. SubscribeToEvent(verticalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  60. SubscribeToEvent(verticalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  61. }
  62. ScrollView::~ScrollView()
  63. {
  64. }
  65. void ScrollView::RegisterObject(Context* context)
  66. {
  67. context->RegisterFactory<ScrollView>();
  68. REF_ACCESSOR_ATTRIBUTE(ScrollView, VAR_INTVECTOR2, "View Position", GetViewPosition, SetViewPositionAttr, IntVector2, IntVector2::ZERO, AM_FILE);
  69. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Scroll Step", GetScrollStep, SetScrollStep, float, 0.1f, AM_FILE);
  70. ACCESSOR_ATTRIBUTE(ScrollView, VAR_FLOAT, "Page Step", GetPageStep, SetPageStep, float, 1.0f, AM_FILE);
  71. COPY_BASE_ATTRIBUTES(ScrollView, UIElement);
  72. }
  73. void ScrollView::ApplyAttributes()
  74. {
  75. UIElement::ApplyAttributes();
  76. // Set the scrollbar orientations again and perform size update now that the style is known
  77. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  78. verticalScrollBar_->SetOrientation(O_VERTICAL);
  79. // If the scroll panel has a child, it should be the content element, which has some special handling
  80. if (scrollPanel_->GetNumChildren())
  81. SetContentElement(scrollPanel_->GetChild(0));
  82. OnResize();
  83. // Reapply view position with proper content element and size
  84. SetViewPosition(viewPositionAttr_);
  85. }
  86. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  87. {
  88. if (delta > 0)
  89. verticalScrollBar_->StepBack();
  90. if (delta < 0)
  91. verticalScrollBar_->StepForward();
  92. }
  93. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  94. {
  95. switch (key)
  96. {
  97. case KEY_LEFT:
  98. if (horizontalScrollBar_->IsVisible())
  99. {
  100. if (qualifiers & QUAL_CTRL)
  101. horizontalScrollBar_->SetValue(0.0f);
  102. else
  103. horizontalScrollBar_->StepBack();
  104. }
  105. break;
  106. case KEY_RIGHT:
  107. if (horizontalScrollBar_->IsVisible())
  108. {
  109. if (qualifiers & QUAL_CTRL)
  110. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  111. else
  112. horizontalScrollBar_->StepForward();
  113. }
  114. break;
  115. case KEY_UP:
  116. if (verticalScrollBar_->IsVisible())
  117. {
  118. if (qualifiers & QUAL_CTRL)
  119. verticalScrollBar_->SetValue(0.0f);
  120. else
  121. verticalScrollBar_->StepBack();
  122. }
  123. break;
  124. case KEY_DOWN:
  125. if (verticalScrollBar_->IsVisible())
  126. {
  127. if (qualifiers & QUAL_CTRL)
  128. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  129. else
  130. verticalScrollBar_->StepForward();
  131. }
  132. break;
  133. case KEY_PAGEUP:
  134. if (verticalScrollBar_->IsVisible())
  135. verticalScrollBar_->ChangeValue(-pageStep_);
  136. break;
  137. case KEY_PAGEDOWN:
  138. if (verticalScrollBar_->IsVisible())
  139. verticalScrollBar_->ChangeValue(pageStep_);
  140. break;
  141. case KEY_HOME:
  142. if (verticalScrollBar_->IsVisible())
  143. verticalScrollBar_->SetValue(0.0f);
  144. break;
  145. case KEY_END:
  146. if (verticalScrollBar_->IsVisible())
  147. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  148. break;
  149. }
  150. }
  151. void ScrollView::OnResize()
  152. {
  153. IntVector2 panelSize = GetSize();
  154. if (verticalScrollBar_->IsVisible())
  155. panelSize.x_ -= verticalScrollBar_->GetWidth();
  156. if (horizontalScrollBar_->IsVisible())
  157. panelSize.y_ -= horizontalScrollBar_->GetHeight();
  158. scrollPanel_->SetSize(panelSize);
  159. horizontalScrollBar_->SetWidth(scrollPanel_->GetWidth());
  160. verticalScrollBar_->SetHeight(scrollPanel_->GetHeight());
  161. UpdateViewSize();
  162. }
  163. void ScrollView::SetContentElement(UIElement* element)
  164. {
  165. if (element == contentElement_)
  166. return;
  167. if (contentElement_)
  168. {
  169. scrollPanel_->RemoveChild(contentElement_);
  170. UnsubscribeFromEvent(contentElement_, E_RESIZED);
  171. }
  172. contentElement_ = element;
  173. if (contentElement_)
  174. {
  175. scrollPanel_->AddChild(contentElement_);
  176. SubscribeToEvent(contentElement_, E_RESIZED, HANDLER(ScrollView, HandleElementResized));
  177. }
  178. UpdateViewSize();
  179. }
  180. void ScrollView::SetViewPosition(const IntVector2& position)
  181. {
  182. UpdateView(position);
  183. UpdateScrollBars();
  184. }
  185. void ScrollView::SetViewPosition(int x, int y)
  186. {
  187. SetViewPosition(IntVector2(x, y));
  188. }
  189. void ScrollView::SetScrollBarsVisible(bool horizontal, bool vertical)
  190. {
  191. horizontalScrollBar_->SetVisible(horizontal);
  192. verticalScrollBar_->SetVisible(vertical);
  193. }
  194. void ScrollView::SetScrollStep(float step)
  195. {
  196. horizontalScrollBar_->SetScrollStep(step);
  197. verticalScrollBar_->SetScrollStep(step);
  198. }
  199. void ScrollView::SetPageStep(float step)
  200. {
  201. pageStep_ = Max(step, 0.0f);
  202. }
  203. float ScrollView::GetScrollStep() const
  204. {
  205. return horizontalScrollBar_->GetScrollStep();
  206. }
  207. void ScrollView::SetViewPositionAttr(const IntVector2& value)
  208. {
  209. viewPositionAttr_ = value;
  210. SetViewPosition(value);
  211. }
  212. void ScrollView::UpdateViewSize()
  213. {
  214. IntVector2 size(IntVector2::ZERO);
  215. if (contentElement_)
  216. size = contentElement_->GetSize();
  217. IntRect panelBorder = scrollPanel_->GetClipBorder();
  218. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  219. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight() - panelBorder.top_ - panelBorder.bottom_);
  220. UpdateView(viewPosition_);
  221. UpdateScrollBars();
  222. }
  223. void ScrollView::UpdateScrollBars()
  224. {
  225. ignoreEvents_ = true;
  226. IntVector2 size = scrollPanel_->GetSize();
  227. IntRect panelBorder = scrollPanel_->GetClipBorder();
  228. size.x_ -= panelBorder.left_ + panelBorder.right_;
  229. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  230. if (horizontalScrollBar_ && size.x_ > 0 && viewSize_.x_ > 0)
  231. {
  232. horizontalScrollBar_->SetRange((float)viewSize_.x_ / (float)size.x_ - 1.0f);
  233. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  234. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  235. }
  236. if (verticalScrollBar_ && size.y_ > 0 && viewSize_.y_ > 0)
  237. {
  238. verticalScrollBar_->SetRange((float)viewSize_.y_ / (float)size.y_ - 1.0f);
  239. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  240. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  241. }
  242. ignoreEvents_ = false;
  243. }
  244. void ScrollView::UpdateView(const IntVector2& position)
  245. {
  246. IntVector2 oldPosition = viewPosition_;
  247. IntRect panelBorder = scrollPanel_->GetClipBorder();
  248. IntVector2 panelSize(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_, scrollPanel_->GetHeight() -
  249. panelBorder.top_ - panelBorder.bottom_);
  250. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - panelSize.x_);
  251. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - panelSize.y_);
  252. scrollPanel_->SetChildOffset(IntVector2(-viewPosition_.x_ + panelBorder.left_, -viewPosition_.y_ + panelBorder.top_));
  253. if (viewPosition_ != oldPosition)
  254. {
  255. using namespace ViewChanged;
  256. VariantMap eventData;
  257. eventData[P_ELEMENT] = (void*)this;
  258. eventData[P_X] = viewPosition_.x_;
  259. eventData[P_Y] = viewPosition_.y_;
  260. SendEvent(E_VIEWCHANGED, eventData);
  261. }
  262. }
  263. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  264. {
  265. IntVector2 size = scrollPanel_->GetSize();
  266. IntRect panelBorder = scrollPanel_->GetClipBorder();
  267. size.x_ -= panelBorder.left_ + panelBorder.right_;
  268. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  269. if (!ignoreEvents_)
  270. {
  271. UpdateView(IntVector2(
  272. (int)(horizontalScrollBar_->GetValue() * (float)size.x_),
  273. (int)(verticalScrollBar_->GetValue() * (float)size.y_)
  274. ));
  275. }
  276. }
  277. void ScrollView::HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData)
  278. {
  279. // Need to recalculate panel size when scrollbar visibility changes
  280. OnResize();
  281. }
  282. void ScrollView::HandleElementResized(StringHash eventType, VariantMap& eventData)
  283. {
  284. UpdateViewSize();
  285. }
  286. }