ScrollView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. static const float STEP_FACTOR = 300.0f;
  32. OBJECTTYPESTATIC(ScrollView);
  33. ScrollView::ScrollView(Context* context) :
  34. UIElement(context),
  35. viewPosition_(IntVector2::ZERO),
  36. viewSize_(IntVector2::ZERO),
  37. pageStep_(1.0f)
  38. {
  39. clipChildren_ = true;
  40. active_ = true;
  41. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  42. horizontalScrollBar_ = new ScrollBar(context_);
  43. horizontalScrollBar_->SetAlignment(HA_LEFT, VA_BOTTOM);
  44. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  45. verticalScrollBar_ = new ScrollBar(context_);
  46. verticalScrollBar_->SetAlignment(HA_RIGHT, VA_TOP);
  47. verticalScrollBar_->SetOrientation(O_VERTICAL);
  48. scrollPanel_ = new BorderImage(context_);
  49. scrollPanel_->SetActive(true);
  50. scrollPanel_->SetClipChildren(true);
  51. AddChild(horizontalScrollBar_);
  52. AddChild(verticalScrollBar_);
  53. AddChild(scrollPanel_);
  54. SubscribeToEvent(horizontalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  55. SubscribeToEvent(horizontalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  56. SubscribeToEvent(verticalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
  57. SubscribeToEvent(verticalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
  58. }
  59. ScrollView::~ScrollView()
  60. {
  61. }
  62. void ScrollView::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<ScrollView>();
  65. }
  66. void ScrollView::SetStyle(const XMLElement& element)
  67. {
  68. UIElement::SetStyle(element);
  69. if (element.HasChild("viewposition"))
  70. SetViewPosition(element.GetChild("viewposition").GetIntVector2("value"));
  71. if (element.HasChild("scrollstep"))
  72. SetScrollStep(element.GetChild("scrollstep").GetFloat("value"));
  73. if (element.HasChild("pagestep"))
  74. SetScrollStep(element.GetChild("pagestep").GetFloat("value"));
  75. XMLElement horizElem = element.GetChild("horizontalscrollbar");
  76. if (horizElem)
  77. horizontalScrollBar_->SetStyle(horizElem);
  78. XMLElement vertElem = element.GetChild("verticalscrollbar");
  79. if (vertElem)
  80. verticalScrollBar_->SetStyle(vertElem);
  81. XMLElement panelElem = element.GetChild("scrollpanel");
  82. if (panelElem)
  83. scrollPanel_->SetStyle(panelElem);
  84. UIElement* root = GetRoot();
  85. if (root && element.HasChild("contentelement"))
  86. SetContentElement(root->GetChild(element.GetChild("contentelement").GetString("name"), true));
  87. // Set the scrollbar orientations again and perform size update now that the style is known
  88. horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
  89. verticalScrollBar_->SetOrientation(O_VERTICAL);
  90. OnResize();
  91. }
  92. void ScrollView::OnWheel(int delta, int buttons, int qualifiers)
  93. {
  94. if (delta > 0)
  95. verticalScrollBar_->StepBack();
  96. if (delta < 0)
  97. verticalScrollBar_->StepForward();
  98. }
  99. void ScrollView::OnKey(int key, int buttons, int qualifiers)
  100. {
  101. switch (key)
  102. {
  103. case KEY_LEFT:
  104. if (horizontalScrollBar_->IsVisible())
  105. {
  106. if (qualifiers & QUAL_CTRL)
  107. horizontalScrollBar_->SetValue(0.0f);
  108. else
  109. horizontalScrollBar_->StepBack();
  110. }
  111. break;
  112. case KEY_RIGHT:
  113. if (horizontalScrollBar_->IsVisible())
  114. {
  115. if (qualifiers & QUAL_CTRL)
  116. horizontalScrollBar_->SetValue(horizontalScrollBar_->GetRange());
  117. else
  118. horizontalScrollBar_->StepForward();
  119. }
  120. break;
  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_DOWN:
  131. if (verticalScrollBar_->IsVisible())
  132. {
  133. if (qualifiers & QUAL_CTRL)
  134. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  135. else
  136. verticalScrollBar_->StepForward();
  137. }
  138. break;
  139. case KEY_PAGEUP:
  140. if (verticalScrollBar_->IsVisible())
  141. verticalScrollBar_->ChangeValue(-pageStep_);
  142. break;
  143. case KEY_PAGEDOWN:
  144. if (verticalScrollBar_->IsVisible())
  145. verticalScrollBar_->ChangeValue(pageStep_);
  146. break;
  147. case KEY_HOME:
  148. if (verticalScrollBar_->IsVisible())
  149. verticalScrollBar_->SetValue(0.0f);
  150. break;
  151. case KEY_END:
  152. if (verticalScrollBar_->IsVisible())
  153. verticalScrollBar_->SetValue(verticalScrollBar_->GetRange());
  154. break;
  155. }
  156. }
  157. void ScrollView::OnResize()
  158. {
  159. IntVector2 panelSize = GetSize();
  160. if (verticalScrollBar_->IsVisible())
  161. panelSize.x_ -= verticalScrollBar_->GetWidth();
  162. if (horizontalScrollBar_->IsVisible())
  163. panelSize.y_ -= horizontalScrollBar_->GetHeight();
  164. scrollPanel_->SetSize(panelSize);
  165. horizontalScrollBar_->SetWidth(scrollPanel_->GetWidth());
  166. verticalScrollBar_->SetHeight(scrollPanel_->GetHeight());
  167. UpdateViewSize();
  168. }
  169. void ScrollView::SetContentElement(UIElement* element)
  170. {
  171. if (element == contentElement_)
  172. return;
  173. if (contentElement_)
  174. {
  175. scrollPanel_->RemoveChild(contentElement_);
  176. UnsubscribeFromEvent(contentElement_, E_RESIZED);
  177. }
  178. contentElement_ = element;
  179. if (contentElement_)
  180. {
  181. scrollPanel_->AddChild(contentElement_);
  182. SubscribeToEvent(contentElement_, E_RESIZED, HANDLER(ScrollView, HandleElementResized));
  183. }
  184. UpdateViewSize();
  185. }
  186. void ScrollView::SetViewPosition(const IntVector2& position)
  187. {
  188. UpdateView(position);
  189. UpdateScrollBars();
  190. }
  191. void ScrollView::SetViewPosition(int x, int y)
  192. {
  193. SetViewPosition(IntVector2(x, y));
  194. }
  195. void ScrollView::SetScrollBarsVisible(bool horizontal, bool vertical)
  196. {
  197. horizontalScrollBar_->SetVisible(horizontal);
  198. verticalScrollBar_->SetVisible(vertical);
  199. }
  200. void ScrollView::SetScrollStep(float step)
  201. {
  202. horizontalScrollBar_->SetScrollStep(step);
  203. verticalScrollBar_->SetScrollStep(step);
  204. }
  205. void ScrollView::SetPageStep(float step)
  206. {
  207. pageStep_ = Max(step, 0.0f);
  208. }
  209. float ScrollView::GetScrollStep() const
  210. {
  211. return horizontalScrollBar_->GetScrollStep();
  212. }
  213. void ScrollView::UpdateViewSize()
  214. {
  215. IntVector2 size(IntVector2::ZERO);
  216. if (contentElement_)
  217. size = contentElement_->GetSize();
  218. viewSize_.x_ = Max(size.x_, scrollPanel_->GetWidth());
  219. viewSize_.y_ = Max(size.y_, scrollPanel_->GetHeight());
  220. UpdateView(viewPosition_);
  221. UpdateScrollBars();
  222. }
  223. void ScrollView::UpdateScrollBars()
  224. {
  225. ignoreEvents_ = true;
  226. const IntVector2& size = scrollPanel_->GetSize();
  227. if (horizontalScrollBar_ && size.x_ > 0 && viewSize_.x_ > 0)
  228. {
  229. horizontalScrollBar_->SetRange((float)viewSize_.x_ / (float)size.x_ - 1.0f);
  230. horizontalScrollBar_->SetValue((float)viewPosition_.x_ / (float)size.x_);
  231. horizontalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.x_);
  232. }
  233. if (verticalScrollBar_ && size.y_ > 0 && viewSize_.y_ > 0)
  234. {
  235. verticalScrollBar_->SetRange((float)viewSize_.y_ / (float)size.y_ - 1.0f);
  236. verticalScrollBar_->SetValue((float)viewPosition_.y_ / (float)size.y_);
  237. verticalScrollBar_->SetStepFactor(STEP_FACTOR / (float)size.y_);
  238. }
  239. ignoreEvents_ = false;
  240. }
  241. void ScrollView::UpdateView(const IntVector2& position)
  242. {
  243. IntVector2 oldPosition = viewPosition_;
  244. viewPosition_.x_ = Clamp(position.x_, 0, viewSize_.x_ - scrollPanel_->GetWidth());
  245. viewPosition_.y_ = Clamp(position.y_, 0, viewSize_.y_ - scrollPanel_->GetHeight());
  246. scrollPanel_->SetChildOffset(-viewPosition_);
  247. if (viewPosition_ != oldPosition)
  248. {
  249. using namespace ViewChanged;
  250. VariantMap eventData;
  251. eventData[P_ELEMENT] = (void*)this;
  252. eventData[P_X] = viewPosition_.x_;
  253. eventData[P_Y] = viewPosition_.y_;
  254. SendEvent(E_VIEWCHANGED, eventData);
  255. }
  256. }
  257. void ScrollView::HandleScrollBarChanged(StringHash eventType, VariantMap& eventData)
  258. {
  259. if (!ignoreEvents_)
  260. {
  261. UpdateView(IntVector2(
  262. (int)(horizontalScrollBar_->GetValue() * (float)scrollPanel_->GetWidth()),
  263. (int)(verticalScrollBar_->GetValue() * (float)scrollPanel_->GetHeight())
  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. }