ScrollView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. }
  68. void ScrollView::SetStyle(const XMLElement& element)
  69. {
  70. UIElement::SetStyle(element);
  71. if (element.HasChild("viewposition"))
  72. SetViewPosition(element.GetChild("viewposition").GetIntVector2("value"));
  73. if (element.HasChild("scrollstep"))
  74. SetScrollStep(element.GetChild("scrollstep").GetFloat("value"));
  75. if (element.HasChild("pagestep"))
  76. SetScrollStep(element.GetChild("pagestep").GetFloat("value"));
  77. XMLElement horizElem = element.GetChild("horizontalscrollbar");
  78. if (horizElem)
  79. horizontalScrollBar_->SetStyle(horizElem);
  80. XMLElement vertElem = element.GetChild("verticalscrollbar");
  81. if (vertElem)
  82. verticalScrollBar_->SetStyle(vertElem);
  83. XMLElement panelElem = element.GetChild("scrollpanel");
  84. if (panelElem)
  85. scrollPanel_->SetStyle(panelElem);
  86. UIElement* root = GetRoot();
  87. if (root && element.HasChild("contentelement"))
  88. SetContentElement(root->GetChild(element.GetChild("contentelement").GetAttribute("name"), true));
  89. // Set the scrollbar orientations again and perform size update now that the style is known
  90. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  91. verticalScrollBar_->SetOrientation(O_VERTICAL);
  92. OnResize();
  93. }
  94. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  95. {
  96. if (delta > 0)
  97. verticalScrollBar_->StepBack();
  98. if (delta < 0)
  99. verticalScrollBar_->StepForward();
  100. }
  101. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  102. {
  103. switch (key)
  104. {
  105. case KEY_LEFT:
  106. if (horizontalScrollBar_->IsVisible())
  107. {
  108. if (qualifiers & QUAL_CTRL)
  109. horizontalScrollBar_->SetValue(0.0f);
  110. else
  111. horizontalScrollBar_->StepBack();
  112. }
  113. break;
  114. case KEY_RIGHT:
  115. if (horizontalScrollBar_->IsVisible())
  116. {
  117. if (qualifiers & QUAL_CTRL)
  118. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  119. else
  120. horizontalScrollBar_->StepForward();
  121. }
  122. break;
  123. case KEY_UP:
  124. if (verticalScrollBar_->IsVisible())
  125. {
  126. if (qualifiers & QUAL_CTRL)
  127. verticalScrollBar_->SetValue(0.0f);
  128. else
  129. verticalScrollBar_->StepBack();
  130. }
  131. break;
  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. case KEY_HOME:
  150. if (verticalScrollBar_->IsVisible())
  151. verticalScrollBar_->SetValue(0.0f);
  152. break;
  153. case KEY_END:
  154. if (verticalScrollBar_->IsVisible())
  155. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  156. break;
  157. }
  158. }
  159. void ScrollView::OnResize()
  160. {
  161. IntVector2 panelSize = GetSize();
  162. if (verticalScrollBar_->IsVisible())
  163. panelSize.x_ -= verticalScrollBar_->GetWidth();
  164. if (horizontalScrollBar_->IsVisible())
  165. panelSize.y_ -= horizontalScrollBar_->GetHeight();
  166. scrollPanel_->SetSize(panelSize);
  167. horizontalScrollBar_->SetWidth(scrollPanel_->GetWidth());
  168. verticalScrollBar_->SetHeight(scrollPanel_->GetHeight());
  169. UpdateViewSize();
  170. }
  171. void ScrollView::SetContentElement(UIElement* element)
  172. {
  173. if (element == contentElement_)
  174. return;
  175. if (contentElement_)
  176. {
  177. scrollPanel_->RemoveChild(contentElement_);
  178. UnsubscribeFromEvent(contentElement_, E_RESIZED);
  179. }
  180. contentElement_ = element;
  181. if (contentElement_)
  182. {
  183. scrollPanel_->AddChild(contentElement_);
  184. SubscribeToEvent(contentElement_, E_RESIZED, HANDLER(ScrollView, HandleElementResized));
  185. }
  186. UpdateViewSize();
  187. }
  188. void ScrollView::SetViewPosition(const IntVector2& position)
  189. {
  190. UpdateView(position);
  191. UpdateScrollBars();
  192. }
  193. void ScrollView::SetViewPosition(int x, int y)
  194. {
  195. SetViewPosition(IntVector2(x, y));
  196. }
  197. void ScrollView::SetScrollBarsVisible(bool horizontal, bool vertical)
  198. {
  199. horizontalScrollBar_->SetVisible(horizontal);
  200. verticalScrollBar_->SetVisible(vertical);
  201. }
  202. void ScrollView::SetScrollStep(float step)
  203. {
  204. horizontalScrollBar_->SetScrollStep(step);
  205. verticalScrollBar_->SetScrollStep(step);
  206. }
  207. void ScrollView::SetPageStep(float step)
  208. {
  209. pageStep_ = Max(step, 0.0f);
  210. }
  211. float ScrollView::GetScrollStep() const
  212. {
  213. return horizontalScrollBar_->GetScrollStep();
  214. }
  215. void ScrollView::UpdateViewSize()
  216. {
  217. IntVector2 size(IntVector2::ZERO);
  218. if (contentElement_)
  219. size = contentElement_->GetSize();
  220. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth());
  221. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight());
  222. UpdateView(viewPosition_);
  223. UpdateScrollBars();
  224. }
  225. void ScrollView::UpdateScrollBars()
  226. {
  227. ignoreEvents_ = true;
  228. const IntVector2& size = scrollPanel_->GetSize();
  229. if (horizontalScrollBar_ && size.x_ > 0 && viewSize_.x_ > 0)
  230. {
  231. horizontalScrollBar_->SetRange((float)viewSize_.x_ / (float)size.x_ - 1.0f);
  232. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  233. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  234. }
  235. if (verticalScrollBar_ && size.y_ > 0 && viewSize_.y_ > 0)
  236. {
  237. verticalScrollBar_->SetRange((float)viewSize_.y_ / (float)size.y_ - 1.0f);
  238. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  239. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  240. }
  241. ignoreEvents_ = false;
  242. }
  243. void ScrollView::UpdateView(const IntVector2& position)
  244. {
  245. IntVector2 oldPosition = viewPosition_;
  246. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - scrollPanel_->GetWidth());
  247. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - scrollPanel_->GetHeight());
  248. scrollPanel_->SetChildOffset(-viewPosition_);
  249. if (viewPosition_ != oldPosition)
  250. {
  251. using namespace ViewChanged;
  252. VariantMap eventData;
  253. eventData[P_ELEMENT] = (void*)this;
  254. eventData[P_X] = viewPosition_.x_;
  255. eventData[P_Y] = viewPosition_.y_;
  256. SendEvent(E_VIEWCHANGED, eventData);
  257. }
  258. }
  259. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  260. {
  261. if (!ignoreEvents_)
  262. {
  263. UpdateView(IntVector2(
  264. (int)(horizontalScrollBar_->GetValue() * (float)scrollPanel_->GetWidth()),
  265. (int)(verticalScrollBar_->GetValue() * (float)scrollPanel_->GetHeight())
  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. }