Slider.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "Log.h"
  26. #include "Slider.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. Slider::Slider(const std::string& name) :
  30. BorderImage(name),
  31. mOrientation(O_HORIZONTAL),
  32. mRange(1.0f),
  33. mValue(0.0f),
  34. mDragSlider(false)
  35. {
  36. mEnabled = true;
  37. mKnob = new BorderImage();
  38. addChild(mKnob);
  39. updateSlider();
  40. }
  41. Slider::~Slider()
  42. {
  43. }
  44. void Slider::setStyle(const XMLElement& element, ResourceCache* cache)
  45. {
  46. BorderImage::setStyle(element, cache);
  47. if (element.hasChildElement("orientation"))
  48. {
  49. std::string orientation = element.getChildElement("orientation").getStringLower("value");
  50. if ((orientation == "horizontal") || (orientation == "h"))
  51. setOrientation(O_HORIZONTAL);
  52. if ((orientation == "vertical") || (orientation == "v"))
  53. setOrientation(O_VERTICAL);
  54. }
  55. if (element.hasChildElement("range"))
  56. {
  57. XMLElement rangeElem = element.getChildElement("range");
  58. setRange(rangeElem.getFloat("max"));
  59. setValue(rangeElem.getFloat("value"));
  60. }
  61. XMLElement knobElem = element.getChildElement("knob");
  62. if (knobElem)
  63. mKnob->setStyle(knobElem, cache);
  64. }
  65. void Slider::update(float timeStep)
  66. {
  67. if (mDragSlider)
  68. mHovering = true;
  69. // Propagate hover and selection effect to the slider knob
  70. mKnob->setHovering(mHovering);
  71. mKnob->setSelected(mSelected);
  72. }
  73. void Slider::onHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  74. {
  75. // Show hover effect if inside the slider knob
  76. mHovering = mKnob->isInside(screenPosition, true);
  77. }
  78. void Slider::onDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  79. {
  80. mOriginalPosition = position;
  81. mOriginalSliderPosition = mKnob->getPosition();
  82. mDragSlider = mKnob->isInside(screenPosition, true);
  83. }
  84. void Slider::onDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  85. {
  86. if (!mDragSlider)
  87. return;
  88. float newValue = mValue;
  89. IntVector2 delta = position - mOriginalPosition;
  90. if (mOrientation == O_HORIZONTAL)
  91. {
  92. int newX = clamp(mOriginalSliderPosition.mX + delta.mX, 0, getWidth() - mKnob->getWidth());
  93. mKnob->setPosition(newX, mOriginalSliderPosition.mY);
  94. newValue = clamp((float)newX * (mRange + 1.0f) / (float)getWidth(), 0.0f, mRange);
  95. }
  96. else
  97. {
  98. int newY = clamp(mOriginalSliderPosition.mY + delta.mY, 0, getHeight() - mKnob->getHeight());
  99. mKnob->setPosition(mOriginalSliderPosition.mX, newY);
  100. newValue = clamp((float)newY * (mRange + 1.0f) / (float)getHeight(), 0.0f, mRange);
  101. }
  102. setValue(newValue);
  103. }
  104. void Slider::onDragEnd(const IntVector2& position, const IntVector2& screenPosition)
  105. {
  106. mDragSlider = false;
  107. }
  108. void Slider::onResize()
  109. {
  110. updateSlider();
  111. }
  112. void Slider::setOrientation(Orientation type)
  113. {
  114. mOrientation = type;
  115. updateSlider();
  116. }
  117. void Slider::setRange(float range)
  118. {
  119. range = max(range, 0.0f);
  120. if (range != mRange)
  121. {
  122. mRange = range;
  123. updateSlider();
  124. }
  125. }
  126. void Slider::setValue(float value)
  127. {
  128. value = clamp(value, 0.0f, mRange);
  129. if (value != mValue)
  130. {
  131. mValue = value;
  132. updateSlider();
  133. using namespace SliderChanged;
  134. VariantMap eventData;
  135. eventData[P_ELEMENT] = (void*)this;
  136. eventData[P_VALUE] = mValue;
  137. sendEvent(EVENT_SLIDERCHANGED, eventData);
  138. }
  139. }
  140. void Slider::changeValue(float delta)
  141. {
  142. setValue(mValue + delta);
  143. }
  144. void Slider::updateSlider()
  145. {
  146. if (mOrientation == O_HORIZONTAL)
  147. {
  148. float width = (float)getWidth();
  149. if (width < M_EPSILON)
  150. return;
  151. float sliderLength = width / (mRange + 1.0f);
  152. float sliderPos = width * mValue / (mRange + 1.0f);
  153. mKnob->setSize((int)sliderLength, getHeight());
  154. mKnob->setPosition(clamp((int)(sliderPos + 0.5f), 0, getWidth() - mKnob->getWidth()), 0);
  155. }
  156. else
  157. {
  158. float height = (float)getHeight();
  159. if (height < M_EPSILON)
  160. return;
  161. float sliderLength = height / (mRange + 1.0f);
  162. float sliderPos = height * mValue / (mRange + 1.0f);
  163. mKnob->setSize(getWidth(), (int)sliderLength);
  164. mKnob->setPosition(0, clamp((int)(sliderPos + 0.5f), 0, getHeight() - mKnob->getHeight()));
  165. }
  166. }