ScrollView.cpp 12 KB

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