ScrollBar.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "Button.h"
  25. #include "ScrollBar.h"
  26. #include "Slider.h"
  27. #include "UIEvents.h"
  28. static const float DEFAULT_SCROLL_STEP = 0.05f;
  29. static const float DEFAULT_REPEAT_DELAY = 0.4f;
  30. static const float DEFAULT_REPEAT_RATE = 20.0f;
  31. ScrollBar::ScrollBar(const std::string& name) :
  32. UIElement(name),
  33. mScrollStep(DEFAULT_SCROLL_STEP),
  34. mNormalizeScrollStep(true),
  35. mLeftRect(IntRect::sZero),
  36. mRightRect(IntRect::sZero),
  37. mUpRect(IntRect::sZero),
  38. mDownRect(IntRect::sZero)
  39. {
  40. mEnabled = true;
  41. mBackButton = new Button();
  42. mBackButton->setRepeat(DEFAULT_REPEAT_DELAY, DEFAULT_REPEAT_RATE);
  43. mForwardButton = new Button();
  44. mForwardButton->setRepeat(DEFAULT_REPEAT_DELAY, DEFAULT_REPEAT_RATE);
  45. mSlider = new Slider();
  46. addChild(mBackButton);
  47. addChild(mSlider);
  48. addChild(mForwardButton);
  49. subscribeToEvent(mBackButton, EVENT_PRESSED, EVENT_HANDLER(ScrollBar, handleBackButtonPressed));
  50. subscribeToEvent(mForwardButton, EVENT_PRESSED, EVENT_HANDLER(ScrollBar, handleForwardButtonPressed));
  51. subscribeToEvent(mSlider, EVENT_SLIDERCHANGED, EVENT_HANDLER(ScrollBar, handleSliderChanged));
  52. // Set default orientation/layout
  53. setOrientation(O_HORIZONTAL);
  54. }
  55. ScrollBar::~ScrollBar()
  56. {
  57. }
  58. void ScrollBar::setStyle(const XMLElement& element, ResourceCache* cache)
  59. {
  60. UIElement::setStyle(element, cache);
  61. if (element.hasChildElement("scrollstep"))
  62. setScrollStep(element.getChildElement("scrollstep").getFloat("value"));
  63. if (element.hasChildElement("normalizescrollstep"))
  64. setNormalizeScrollStep(element.getChildElement("normalizescrollstep").getBool("enable"));
  65. if (element.hasChildElement("range"))
  66. {
  67. XMLElement rangeElem = element.getChildElement("range");
  68. setRange(rangeElem.getFloat("max"));
  69. setValue(rangeElem.getFloat("value"));
  70. }
  71. XMLElement backButtonElem = element.getChildElement("backbutton");
  72. if (backButtonElem)
  73. {
  74. XMLElement imageElem = backButtonElem.getChildElement("imagerect");
  75. if (imageElem.hasAttribute("horizontal"))
  76. mLeftRect = imageElem.getIntRect("horizontal");
  77. if (imageElem.hasAttribute("vertical"))
  78. mUpRect = imageElem.getIntRect("vertical");
  79. if (imageElem.hasAttribute("h"))
  80. mLeftRect = imageElem.getIntRect("h");
  81. if (imageElem.hasAttribute("v"))
  82. mUpRect = imageElem.getIntRect("v");
  83. mBackButton->setStyle(backButtonElem, cache);
  84. }
  85. XMLElement forwardButtonElem = element.getChildElement("forwardbutton");
  86. if (forwardButtonElem)
  87. {
  88. XMLElement imageElem = forwardButtonElem.getChildElement("imagerect");
  89. if (imageElem.hasAttribute("horizontal"))
  90. mRightRect = imageElem.getIntRect("horizontal");
  91. if (imageElem.hasAttribute("vertical"))
  92. mDownRect = imageElem.getIntRect("vertical");
  93. if (imageElem.hasAttribute("h"))
  94. mRightRect = imageElem.getIntRect("h");
  95. if (imageElem.hasAttribute("v"))
  96. mDownRect = imageElem.getIntRect("v");
  97. mForwardButton->setStyle(forwardButtonElem, cache);
  98. }
  99. XMLElement sliderElem = element.getChildElement("slider");
  100. if (sliderElem)
  101. mSlider->setStyle(sliderElem, cache);
  102. if (element.hasChildElement("orientation"))
  103. {
  104. std::string orientation = element.getChildElement("orientation").getStringLower("value");
  105. if ((orientation == "horizontal") || (orientation == "h"))
  106. setOrientation(O_HORIZONTAL);
  107. if ((orientation == "vertical") || (orientation == "v"))
  108. setOrientation(O_VERTICAL);
  109. }
  110. }
  111. void ScrollBar::onResize()
  112. {
  113. // Disable layout operations while setting the button sizes is incomplete
  114. mUpdateLayoutNestingLevel++;
  115. if (mSlider->getOrientation() == O_HORIZONTAL)
  116. {
  117. int height = getHeight();
  118. mBackButton->setFixedSize(height, height);
  119. mForwardButton->setFixedSize(height, height);
  120. }
  121. else
  122. {
  123. int width = getWidth();
  124. mBackButton->setFixedSize(width, width);
  125. mForwardButton->setFixedSize(width, width);
  126. }
  127. mUpdateLayoutNestingLevel--;
  128. }
  129. void ScrollBar::setOrientation(Orientation orientation)
  130. {
  131. mSlider->setOrientation(orientation);
  132. if (orientation == O_HORIZONTAL)
  133. {
  134. mBackButton->setImageRect(mLeftRect);
  135. mForwardButton->setImageRect(mRightRect);
  136. }
  137. else
  138. {
  139. mBackButton->setImageRect(mUpRect);
  140. mForwardButton->setImageRect(mDownRect);
  141. }
  142. onResize();
  143. setLayout(orientation, LM_RESIZECHILDREN, LM_RESIZECHILDREN);
  144. }
  145. void ScrollBar::setRange(float range)
  146. {
  147. mSlider->setRange(range);
  148. }
  149. void ScrollBar::setValue(float value)
  150. {
  151. mSlider->setValue(value);
  152. }
  153. void ScrollBar::changeValue(float delta)
  154. {
  155. mSlider->changeValue(delta);
  156. }
  157. void ScrollBar::setScrollStep(float step)
  158. {
  159. mScrollStep = max(step, 0.0f);
  160. }
  161. void ScrollBar::setNormalizeScrollStep(bool enable)
  162. {
  163. mNormalizeScrollStep = enable;
  164. }
  165. void ScrollBar::stepBack()
  166. {
  167. mSlider->setValue(mSlider->getValue() - getEffectiveScrollStep());
  168. }
  169. void ScrollBar::stepForward()
  170. {
  171. mSlider->setValue(mSlider->getValue() + getEffectiveScrollStep());
  172. }
  173. Orientation ScrollBar::getOrientation() const
  174. {
  175. return mSlider->getOrientation();
  176. }
  177. float ScrollBar::getRange() const
  178. {
  179. return mSlider->getRange();
  180. }
  181. float ScrollBar::getValue() const
  182. {
  183. return mSlider->getValue();
  184. }
  185. float ScrollBar::getEffectiveScrollStep() const
  186. {
  187. if (!mNormalizeScrollStep)
  188. return mScrollStep;
  189. else
  190. return mScrollStep * (mSlider->getRange() + 1.0f);
  191. }
  192. void ScrollBar::handleBackButtonPressed(StringHash eventType, VariantMap& eventData)
  193. {
  194. stepBack();
  195. }
  196. void ScrollBar::handleForwardButtonPressed(StringHash eventType, VariantMap& eventData)
  197. {
  198. stepForward();
  199. }
  200. void ScrollBar::handleSliderChanged(StringHash eventType, VariantMap& eventData)
  201. {
  202. // Send the event forward
  203. VariantMap newEventData;
  204. newEventData[ScrollBarChanged::P_ELEMENT] = (void*)this;
  205. newEventData[ScrollBarChanged::P_VALUE] = mSlider->getValue();
  206. sendEvent(EVENT_SCROLLBARCHANGED, newEventData);
  207. }