ScrollView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. 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. scrollBarsAutoVisible_ = false;
  194. }
  195. void ScrollView::SetScrollBarsAutoVisible(bool enable)
  196. {
  197. scrollBarsAutoVisible_ = enable;
  198. }
  199. void ScrollView::SetScrollStep(float step)
  200. {
  201. horizontalScrollBar_->SetScrollStep(step);
  202. verticalScrollBar_->SetScrollStep(step);
  203. }
  204. void ScrollView::SetPageStep(float step)
  205. {
  206. pageStep_ = Max(step, 0.0f);
  207. }
  208. float ScrollView::GetScrollStep() const
  209. {
  210. return horizontalScrollBar_->GetScrollStep();
  211. }
  212. void ScrollView::SetViewPositionAttr(const IntVector2& value)
  213. {
  214. viewPositionAttr_ = value;
  215. SetViewPosition(value);
  216. }
  217. void ScrollView::UpdateViewSize()
  218. {
  219. IntVector2 size(IntVector2::ZERO);
  220. if (contentElement_)
  221. size = contentElement_->GetSize();
  222. IntRect panelBorder = scrollPanel_->GetClipBorder();
  223. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  224. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight() - panelBorder.top_ - panelBorder.bottom_);
  225. UpdateView(viewPosition_);
  226. UpdateScrollBars();
  227. }
  228. void ScrollView::UpdateScrollBars()
  229. {
  230. bool needResize = false;
  231. ignoreEvents_ = true;
  232. IntVector2 size = scrollPanel_->GetSize();
  233. IntRect panelBorder = scrollPanel_->GetClipBorder();
  234. size.x_ -= panelBorder.left_ + panelBorder.right_;
  235. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  236. if (horizontalScrollBar_ && size.x_ > 0 && viewSize_.x_ > 0)
  237. {
  238. float range = (float)viewSize_.x_ / (float)size.x_ - 1.0f;
  239. horizontalScrollBar_->SetRange(range);
  240. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  241. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  242. // Hide/show the horizontal scroll bar as needed
  243. if (scrollBarsAutoVisible_)
  244. needResize = SetScrollBarVisible(horizontalScrollBar_, range > 0.0f);
  245. }
  246. if (verticalScrollBar_ && size.y_ > 0 && viewSize_.y_ > 0)
  247. {
  248. float range = (float)viewSize_.y_ / (float)size.y_ - 1.0f;
  249. verticalScrollBar_->SetRange(range);
  250. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  251. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  252. // Hide/show the vertical scroll bar as needed
  253. if (scrollBarsAutoVisible_)
  254. needResize = SetScrollBarVisible(verticalScrollBar_, range > 0.0f) || needResize;
  255. }
  256. ignoreEvents_ = false;
  257. // Since the scrollbar visibility changed event is intentionally ignored to prevent
  258. // infinite loop in the above code, call OnResize now if needed
  259. if (needResize)
  260. OnResize();
  261. }
  262. void ScrollView::UpdateView(const IntVector2& position)
  263. {
  264. IntVector2 oldPosition = viewPosition_;
  265. IntRect panelBorder = scrollPanel_->GetClipBorder();
  266. IntVector2 panelSize(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_, scrollPanel_->GetHeight() -
  267. panelBorder.top_ - panelBorder.bottom_);
  268. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - panelSize.x_);
  269. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - panelSize.y_);
  270. scrollPanel_->SetChildOffset(IntVector2(-viewPosition_.x_ + panelBorder.left_, -viewPosition_.y_ + panelBorder.top_));
  271. if (viewPosition_ != oldPosition)
  272. {
  273. using namespace ViewChanged;
  274. VariantMap eventData;
  275. eventData[P_ELEMENT] = (void*)this;
  276. eventData[P_X] = viewPosition_.x_;
  277. eventData[P_Y] = viewPosition_.y_;
  278. SendEvent(E_VIEWCHANGED, eventData);
  279. }
  280. }
  281. bool ScrollView::SetScrollBarVisible(ScrollBar* scrollBar, bool visible)
  282. {
  283. bool oldVisible = scrollBar->IsVisible();
  284. scrollBar->SetVisible(visible);
  285. return oldVisible != scrollBar->IsVisible();
  286. }
  287. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  288. {
  289. IntVector2 size = scrollPanel_->GetSize();
  290. IntRect panelBorder = scrollPanel_->GetClipBorder();
  291. size.x_ -= panelBorder.left_ + panelBorder.right_;
  292. size.y_ -= panelBorder.top_ + panelBorder.bottom_;
  293. if (!ignoreEvents_)
  294. {
  295. UpdateView(IntVector2(
  296. (int)(horizontalScrollBar_->GetValue() * (float)size.x_),
  297. (int)(verticalScrollBar_->GetValue() * (float)size.y_)
  298. ));
  299. }
  300. }
  301. void ScrollView::HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData)
  302. {
  303. // Need to recalculate panel size when scrollbar visibility changes
  304. if (!ignoreEvents_)
  305. OnResize();
  306. }
  307. void ScrollView::HandleElementResized(StringHash eventType, VariantMap& eventData)
  308. {
  309. UpdateViewSize();
  310. }
  311. }