ScrollView.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "InputEvents.h"
  25. #include "ScrollView.h"
  26. #include "Slider.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. ScrollView::ScrollView(const std::string& name) :
  30. BorderImage(name),
  31. mViewPosition(IntVector2::sZero),
  32. mViewSize(IntVector2::sZero),
  33. mLastSize(IntVector2::sZero),
  34. mScrollStep(0.25f),
  35. mPageStep(1.0f)
  36. {
  37. mClipChildren = true;
  38. mEnabled = true;
  39. mFocusable = true;
  40. }
  41. ScrollView::~ScrollView()
  42. {
  43. }
  44. void ScrollView::setStyle(const XMLElement& element, ResourceCache* cache)
  45. {
  46. BorderImage::setStyle(element, cache);
  47. if (element.hasChildElement("viewposition"))
  48. setViewPosition(element.getChildElement("viewposition").getIntVector2("value"));
  49. if (element.hasChildElement("viewsize"))
  50. setViewSize(element.getChildElement("viewsize").getIntVector2("value"));
  51. if (element.hasChildElement("scrollstep"))
  52. setScrollStep(element.getChildElement("scrollstep").getFloat("value"));
  53. if (element.hasChildElement("pagestep"))
  54. setScrollStep(element.getChildElement("pagestep").getFloat("value"));
  55. UIElement* root = getRootElement();
  56. if (root)
  57. {
  58. if (element.hasChildElement("horizontalslider"))
  59. setHorizontalSlider(dynamic_cast<Slider*>(root->getChild(element.getChildElement("horizontalslider").getString("name"), true)));
  60. if (element.hasChildElement("verticalslider"))
  61. setVerticalSlider(dynamic_cast<Slider*>(root->getChild(element.getChildElement("verticalslider").getString("name"), true)));
  62. }
  63. }
  64. void ScrollView::update(float timeStep)
  65. {
  66. // If element size has changed, update view and sliders to reflect the new size. Otherwise get position from sliders
  67. if (getSize() != mLastSize)
  68. {
  69. mLastSize = getSize();
  70. mViewSize.mX = max(mViewSize.mX, mLastSize.mX);
  71. mViewSize.mY = max(mViewSize.mY, mLastSize.mY);
  72. updateView(mViewPosition);
  73. updateSliders();
  74. }
  75. else
  76. updateViewFromSliders();
  77. }
  78. void ScrollView::onKey(int key, int buttons, int qualifiers)
  79. {
  80. switch (key)
  81. {
  82. case KEY_LEFT:
  83. if (mHorizontalSlider)
  84. {
  85. if (qualifiers & QUAL_CTRL)
  86. mHorizontalSlider->setValue(0.0f);
  87. else
  88. mHorizontalSlider->setValue(mHorizontalSlider->getValue() - mScrollStep);
  89. }
  90. break;
  91. case KEY_RIGHT:
  92. if (mHorizontalSlider)
  93. {
  94. if (qualifiers & QUAL_CTRL)
  95. mHorizontalSlider->setValue(mHorizontalSlider->getRange());
  96. else
  97. mHorizontalSlider->setValue(mHorizontalSlider->getValue() + mScrollStep);
  98. }
  99. break;
  100. case KEY_UP:
  101. if (mVerticalSlider)
  102. {
  103. if (qualifiers & QUAL_CTRL)
  104. mVerticalSlider->setValue(0.0f);
  105. else
  106. mVerticalSlider->setValue(mVerticalSlider->getValue() - mScrollStep);
  107. }
  108. break;
  109. case KEY_DOWN:
  110. if (mVerticalSlider)
  111. {
  112. if (qualifiers & QUAL_CTRL)
  113. mVerticalSlider->setValue(mVerticalSlider->getRange());
  114. else
  115. mVerticalSlider->setValue(mVerticalSlider->getValue() + mScrollStep);
  116. }
  117. break;
  118. case KEY_PAGEUP:
  119. if (mVerticalSlider)
  120. mVerticalSlider->setValue(mVerticalSlider->getValue() - mPageStep);
  121. break;
  122. case KEY_PAGEDOWN:
  123. if (mVerticalSlider)
  124. mVerticalSlider->setValue(mVerticalSlider->getValue() + mPageStep);
  125. break;
  126. case KEY_HOME:
  127. if (mVerticalSlider)
  128. mVerticalSlider->setValue(0.0f);
  129. break;
  130. case KEY_END:
  131. if (mVerticalSlider)
  132. mVerticalSlider->setValue(mVerticalSlider->getRange());
  133. break;
  134. }
  135. }
  136. void ScrollView::onFocus()
  137. {
  138. // Set selected state (constant hover) on the sliders to show they are now under key control
  139. if (mHorizontalSlider)
  140. mHorizontalSlider->setSelected(true);
  141. if (mVerticalSlider)
  142. mVerticalSlider->setSelected(true);
  143. }
  144. void ScrollView::onDefocus()
  145. {
  146. if (mHorizontalSlider)
  147. mHorizontalSlider->setSelected(false);
  148. if (mVerticalSlider)
  149. mVerticalSlider->setSelected(false);
  150. }
  151. void ScrollView::setViewPosition(const IntVector2& position)
  152. {
  153. updateView(position);
  154. updateSliders();
  155. }
  156. void ScrollView::setViewPosition(int x, int y)
  157. {
  158. setViewPosition(IntVector2(x, y));
  159. }
  160. void ScrollView::setViewSize(const IntVector2& size)
  161. {
  162. mLastSize = getSize();
  163. mViewSize.mX = max(size.mX, getWidth());
  164. mViewSize.mY = max(size.mY, getHeight());
  165. updateView(mViewPosition);
  166. updateSliders();
  167. }
  168. void ScrollView::setViewSize(int x, int y)
  169. {
  170. setViewSize(IntVector2(x, y));
  171. }
  172. void ScrollView::setHorizontalSlider(Slider* slider)
  173. {
  174. mHorizontalSlider = slider;
  175. updateSliders();
  176. }
  177. void ScrollView::setVerticalSlider(Slider* slider)
  178. {
  179. mVerticalSlider = slider;
  180. updateSliders();
  181. }
  182. void ScrollView::setScrollStep(float step)
  183. {
  184. mScrollStep = max(step, 0.0f);
  185. }
  186. void ScrollView::setPageStep(float step)
  187. {
  188. mPageStep = max(step, 0.0f);
  189. }
  190. void ScrollView::updateViewFromSliders()
  191. {
  192. if ((!mHorizontalSlider) && (!mVerticalSlider))
  193. return;
  194. IntVector2 oldPosition = mViewPosition;
  195. IntVector2 newPosition = mViewPosition;
  196. const IntVector2& size = getSize();
  197. if (mHorizontalSlider)
  198. newPosition.mX = (int)(mHorizontalSlider->getValue() * (float)size.mX);
  199. if (mVerticalSlider)
  200. newPosition.mY = (int)(mVerticalSlider->getValue() * (float)size.mY);
  201. updateView(newPosition);
  202. if (mViewPosition != oldPosition)
  203. {
  204. using namespace ViewChanged;
  205. VariantMap eventData;
  206. eventData[P_ELEMENT] = (void*)this;
  207. eventData[P_X] = mViewPosition.mX;
  208. eventData[P_Y] = mViewPosition.mY;
  209. sendEvent(EVENT_VIEWCHANGED, eventData);
  210. }
  211. }
  212. void ScrollView::updateSliders()
  213. {
  214. const IntVector2& size = getSize();
  215. if ((mHorizontalSlider) && (size.mX > 0) && (mViewSize.mX > 0))
  216. {
  217. mHorizontalSlider->setRange((float)mViewSize.mX / (float)size.mX - 1.0f);
  218. mHorizontalSlider->setValue((float)mViewPosition.mX / (float)size.mX);
  219. }
  220. if ((mVerticalSlider) && (size.mY > 0) && (mViewSize.mY > 0))
  221. {
  222. mVerticalSlider->setRange((float)mViewSize.mY / (float)size.mY - 1.0f);
  223. mVerticalSlider->setValue((float)mViewPosition.mY / (float)size.mY);
  224. }
  225. }
  226. void ScrollView::updateView(const IntVector2& position)
  227. {
  228. mViewPosition.mX = clamp(position.mX, 0, mViewSize.mX - getWidth());
  229. mViewPosition.mY = clamp(position.mY, 0, mViewSize.mY - getHeight());
  230. setChildOffset(-mViewPosition);
  231. }