ScrollView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. 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, SetViewPosition, 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. }
  83. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  84. {
  85. if (delta > 0)
  86. verticalScrollBar_->StepBack();
  87. if (delta < 0)
  88. verticalScrollBar_->StepForward();
  89. }
  90. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  91. {
  92. switch (key)
  93. {
  94. case KEY_LEFT:
  95. if (horizontalScrollBar_->IsVisible())
  96. {
  97. if (qualifiers & QUAL_CTRL)
  98. horizontalScrollBar_->SetValue(0.0f);
  99. else
  100. horizontalScrollBar_->StepBack();
  101. }
  102. break;
  103. case KEY_RIGHT:
  104. if (horizontalScrollBar_->IsVisible())
  105. {
  106. if (qualifiers & QUAL_CTRL)
  107. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  108. else
  109. horizontalScrollBar_->StepForward();
  110. }
  111. break;
  112. case KEY_UP:
  113. if (verticalScrollBar_->IsVisible())
  114. {
  115. if (qualifiers & QUAL_CTRL)
  116. verticalScrollBar_->SetValue(0.0f);
  117. else
  118. verticalScrollBar_->StepBack();
  119. }
  120. break;
  121. case KEY_DOWN:
  122. if (verticalScrollBar_->IsVisible())
  123. {
  124. if (qualifiers & QUAL_CTRL)
  125. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  126. else
  127. verticalScrollBar_->StepForward();
  128. }
  129. break;
  130. case KEY_PAGEUP:
  131. if (verticalScrollBar_->IsVisible())
  132. verticalScrollBar_->ChangeValue(-pageStep_);
  133. break;
  134. case KEY_PAGEDOWN:
  135. if (verticalScrollBar_->IsVisible())
  136. verticalScrollBar_->ChangeValue(pageStep_);
  137. break;
  138. case KEY_HOME:
  139. if (verticalScrollBar_->IsVisible())
  140. verticalScrollBar_->SetValue(0.0f);
  141. break;
  142. case KEY_END:
  143. if (verticalScrollBar_->IsVisible())
  144. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  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::UpdateViewSize()
  205. {
  206. IntVector2 size(IntVector2::ZERO);
  207. if (contentElement_)
  208. size = contentElement_->GetSize();
  209. IntRect panelBorder = scrollPanel_->GetClipBorder();
  210. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  211. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight() - panelBorder.top_ - panelBorder.bottom_);
  212. UpdateView(viewPosition_);
  213. UpdateScrollBars();
  214. }
  215. void ScrollView::UpdateScrollBars()
  216. {
  217. ignoreEvents_ = true;
  218. IntVector2 size = scrollPanel_->GetSize();
  219. IntRect panelBorder = scrollPanel_->GetClipBorder();
  220. size.x_ -= panelBorder.left_ + panelBorder.right_;
  221. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  222. if (horizontalScrollBar_ && size.x_ > 0 && viewSize_.x_ > 0)
  223. {
  224. horizontalScrollBar_->SetRange((float)viewSize_.x_ / (float)size.x_ - 1.0f);
  225. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  226. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  227. }
  228. if (verticalScrollBar_ && size.y_ > 0 && viewSize_.y_ > 0)
  229. {
  230. verticalScrollBar_->SetRange((float)viewSize_.y_ / (float)size.y_ - 1.0f);
  231. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  232. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  233. }
  234. ignoreEvents_ = false;
  235. }
  236. void ScrollView::UpdateView(const IntVector2& position)
  237. {
  238. IntVector2 oldPosition = viewPosition_;
  239. IntRect panelBorder = scrollPanel_->GetClipBorder();
  240. IntVector2 panelSize(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_, scrollPanel_->GetHeight() -
  241. panelBorder.top_ - panelBorder.bottom_);
  242. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - panelSize.x_);
  243. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - panelSize.y_);
  244. scrollPanel_->SetChildOffset(IntVector2(-viewPosition_.x_ + panelBorder.left_, -viewPosition_.y_ + panelBorder.top_));
  245. if (viewPosition_ != oldPosition)
  246. {
  247. using namespace ViewChanged;
  248. VariantMap eventData;
  249. eventData[P_ELEMENT] = (void*)this;
  250. eventData[P_X] = viewPosition_.x_;
  251. eventData[P_Y] = viewPosition_.y_;
  252. SendEvent(E_VIEWCHANGED, eventData);
  253. }
  254. }
  255. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  256. {
  257. IntVector2 size = scrollPanel_->GetSize();
  258. IntRect panelBorder = scrollPanel_->GetClipBorder();
  259. size.x_ -= panelBorder.left_ + panelBorder.right_;
  260. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  261. if (!ignoreEvents_)
  262. {
  263. UpdateView(IntVector2(
  264. (int)(horizontalScrollBar_->GetValue() * (float)size.x_),
  265. (int)(verticalScrollBar_->GetValue() * (float)size.y_)
  266. ));
  267. }
  268. }
  269. void ScrollView::HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData)
  270. {
  271. // Need to recalculate panel size when scrollbar visibility changes
  272. OnResize();
  273. }
  274. void ScrollView::HandleElementResized(StringHash eventType, VariantMap& eventData)
  275. {
  276. UpdateViewSize();
  277. }
  278. }