ScrollView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. scrollPanel_->SetPriority(-1);
  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, SetViewPosition, 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. OnResize();
  80. }
  81. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  82. {
  83. if (delta > 0)
  84. verticalScrollBar_->StepBack();
  85. if (delta < 0)
  86. verticalScrollBar_->StepForward();
  87. }
  88. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  89. {
  90. switch (key)
  91. {
  92. case KEY_LEFT:
  93. if (horizontalScrollBar_->IsVisible())
  94. {
  95. if (qualifiers & QUAL_CTRL)
  96. horizontalScrollBar_->SetValue(0.0f);
  97. else
  98. horizontalScrollBar_->StepBack();
  99. }
  100. break;
  101. case KEY_RIGHT:
  102. if (horizontalScrollBar_->IsVisible())
  103. {
  104. if (qualifiers & QUAL_CTRL)
  105. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  106. else
  107. horizontalScrollBar_->StepForward();
  108. }
  109. break;
  110. case KEY_UP:
  111. if (verticalScrollBar_->IsVisible())
  112. {
  113. if (qualifiers & QUAL_CTRL)
  114. verticalScrollBar_->SetValue(0.0f);
  115. else
  116. verticalScrollBar_->StepBack();
  117. }
  118. break;
  119. case KEY_DOWN:
  120. if (verticalScrollBar_->IsVisible())
  121. {
  122. if (qualifiers & QUAL_CTRL)
  123. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  124. else
  125. verticalScrollBar_->StepForward();
  126. }
  127. break;
  128. case KEY_PAGEUP:
  129. if (verticalScrollBar_->IsVisible())
  130. verticalScrollBar_->ChangeValue(-pageStep_);
  131. break;
  132. case KEY_PAGEDOWN:
  133. if (verticalScrollBar_->IsVisible())
  134. verticalScrollBar_->ChangeValue(pageStep_);
  135. break;
  136. case KEY_HOME:
  137. if (verticalScrollBar_->IsVisible())
  138. verticalScrollBar_->SetValue(0.0f);
  139. break;
  140. case KEY_END:
  141. if (verticalScrollBar_->IsVisible())
  142. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  143. break;
  144. }
  145. }
  146. void ScrollView::OnResize()
  147. {
  148. IntVector2 panelSize = GetSize();
  149. if (verticalScrollBar_->IsVisible())
  150. panelSize.x_ -= verticalScrollBar_->GetWidth();
  151. if (horizontalScrollBar_->IsVisible())
  152. panelSize.y_ -= horizontalScrollBar_->GetHeight();
  153. scrollPanel_->SetSize(panelSize);
  154. horizontalScrollBar_->SetWidth(scrollPanel_->GetWidth());
  155. verticalScrollBar_->SetHeight(scrollPanel_->GetHeight());
  156. UpdateViewSize();
  157. }
  158. void ScrollView::SetContentElement(UIElement* element)
  159. {
  160. if (element == contentElement_)
  161. return;
  162. if (contentElement_)
  163. {
  164. scrollPanel_->RemoveChild(contentElement_);
  165. UnsubscribeFromEvent(contentElement_, E_RESIZED);
  166. }
  167. contentElement_ = element;
  168. if (contentElement_)
  169. {
  170. scrollPanel_->AddChild(contentElement_);
  171. SubscribeToEvent(contentElement_, E_RESIZED, HANDLER(ScrollView, HandleElementResized));
  172. }
  173. UpdateViewSize();
  174. }
  175. void ScrollView::SetViewPosition(const IntVector2& position)
  176. {
  177. UpdateView(position);
  178. UpdateScrollBars();
  179. }
  180. void ScrollView::SetViewPosition(int x, int y)
  181. {
  182. SetViewPosition(IntVector2(x, y));
  183. }
  184. void ScrollView::SetScrollBarsVisible(bool horizontal, bool vertical)
  185. {
  186. horizontalScrollBar_->SetVisible(horizontal);
  187. verticalScrollBar_->SetVisible(vertical);
  188. }
  189. void ScrollView::SetScrollStep(float step)
  190. {
  191. horizontalScrollBar_->SetScrollStep(step);
  192. verticalScrollBar_->SetScrollStep(step);
  193. }
  194. void ScrollView::SetPageStep(float step)
  195. {
  196. pageStep_ = Max(step, 0.0f);
  197. }
  198. float ScrollView::GetScrollStep() const
  199. {
  200. return horizontalScrollBar_->GetScrollStep();
  201. }
  202. void ScrollView::UpdateViewSize()
  203. {
  204. IntVector2 size(IntVector2::ZERO);
  205. if (contentElement_)
  206. size = contentElement_->GetSize();
  207. IntRect panelBorder = scrollPanel_->GetClipBorder();
  208. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  209. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight() - panelBorder.top_ - panelBorder.bottom_);
  210. UpdateView(viewPosition_);
  211. UpdateScrollBars();
  212. }
  213. void ScrollView::UpdateScrollBars()
  214. {
  215. ignoreEvents_ = true;
  216. IntVector2 size = scrollPanel_->GetSize();
  217. IntRect panelBorder = scrollPanel_->GetClipBorder();
  218. size.x_ -= panelBorder.left_ + panelBorder.right_;
  219. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  220. if (horizontalScrollBar_ && size.x_ > 0 && viewSize_.x_ > 0)
  221. {
  222. horizontalScrollBar_->SetRange((float)viewSize_.x_ / (float)size.x_ - 1.0f);
  223. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  224. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  225. }
  226. if (verticalScrollBar_ && size.y_ > 0 && viewSize_.y_ > 0)
  227. {
  228. verticalScrollBar_->SetRange((float)viewSize_.y_ / (float)size.y_ - 1.0f);
  229. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  230. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  231. }
  232. ignoreEvents_ = false;
  233. }
  234. void ScrollView::UpdateView(const IntVector2& position)
  235. {
  236. IntVector2 oldPosition = viewPosition_;
  237. IntRect panelBorder = scrollPanel_->GetClipBorder();
  238. IntVector2 panelSize(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_, scrollPanel_->GetHeight() -
  239. panelBorder.top_ - panelBorder.bottom_);
  240. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - panelSize.x_);
  241. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - panelSize.y_);
  242. scrollPanel_->SetChildOffset(IntVector2(-viewPosition_.x_ + panelBorder.left_, -viewPosition_.y_ + panelBorder.top_));
  243. if (viewPosition_ != oldPosition)
  244. {
  245. using namespace ViewChanged;
  246. VariantMap eventData;
  247. eventData[P_ELEMENT] = (void*)this;
  248. eventData[P_X] = viewPosition_.x_;
  249. eventData[P_Y] = viewPosition_.y_;
  250. SendEvent(E_VIEWCHANGED, eventData);
  251. }
  252. }
  253. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  254. {
  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 (!ignoreEvents_)
  260. {
  261. UpdateView(IntVector2(
  262. (int)(horizontalScrollBar_->GetValue() * (float)size.x_),
  263. (int)(verticalScrollBar_->GetValue() * (float)size.y_)
  264. ));
  265. }
  266. }
  267. void ScrollView::HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData)
  268. {
  269. // Need to recalculate panel size when scrollbar visibility changes
  270. OnResize();
  271. }
  272. void ScrollView::HandleElementResized(StringHash eventType, VariantMap& eventData)
  273. {
  274. UpdateViewSize();
  275. }
  276. }