Slider.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. mSlider = new BorderImage();
  38. addChild(mSlider);
  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. XMLElement sliderElem = element.getChildElement("slider");
  56. if (sliderElem)
  57. {
  58. mSlider->setStyle(sliderElem, cache);
  59. setRange(sliderElem.getFloat("range"));
  60. setValue(sliderElem.getFloat("value"));
  61. }
  62. }
  63. void Slider::update(float timeStep)
  64. {
  65. if (mDragSlider)
  66. mHovering = true;
  67. // Copy hover and selection effect to the slider button
  68. mSlider->setHovering(mHovering);
  69. mSlider->setSelected(mSelected);
  70. }
  71. void Slider::onHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  72. {
  73. // Show hover effect if inside the slider button
  74. mHovering = mSlider->isInside(screenPosition, true);
  75. }
  76. void Slider::onDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  77. {
  78. mOriginalPosition = position;
  79. mOriginalSliderPosition = mSlider->getPosition();
  80. mDragSlider = mSlider->isInside(screenPosition, true);
  81. }
  82. void Slider::onDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  83. {
  84. if (!mDragSlider)
  85. return;
  86. float newValue = mValue;
  87. IntVector2 delta = position - mOriginalPosition;
  88. if (mOrientation == O_HORIZONTAL)
  89. {
  90. int newX = clamp(mOriginalSliderPosition.mX + delta.mX, 0, getWidth() - mSlider->getWidth());
  91. mSlider->setPosition(newX, mOriginalSliderPosition.mY);
  92. newValue = clamp((float)newX * (mRange + 1.0f) / (float)getWidth(), 0.0f, mRange);
  93. }
  94. else
  95. {
  96. int newY = clamp(mOriginalSliderPosition.mY + delta.mY, 0, getHeight() - mSlider->getHeight());
  97. mSlider->setPosition(mOriginalSliderPosition.mX, newY);
  98. newValue = clamp((float)newY * (mRange + 1.0f) / (float)getHeight(), 0.0f, mRange);
  99. }
  100. if (newValue != mValue)
  101. {
  102. mValue = newValue;
  103. using namespace SliderChanged;
  104. VariantMap eventData;
  105. eventData[P_ELEMENT] = (void*)this;
  106. eventData[P_VALUE] = mValue;
  107. sendEvent(EVENT_SLIDERCHANGED, eventData);
  108. }
  109. }
  110. void Slider::onDragEnd(const IntVector2& position, const IntVector2& screenPosition)
  111. {
  112. mDragSlider = false;
  113. }
  114. void Slider::setOrientation(UIElementOrientation orientation)
  115. {
  116. mOrientation = orientation;
  117. updateSlider();
  118. }
  119. void Slider::setRange(float range)
  120. {
  121. range = max(range, 0.0f);
  122. if (range != mRange)
  123. {
  124. mRange = range;
  125. updateSlider();
  126. }
  127. }
  128. void Slider::setValue(float value)
  129. {
  130. value = clamp(value, 0.0f, mRange);
  131. if (value != mValue)
  132. {
  133. mValue = value;
  134. updateSlider();
  135. }
  136. }
  137. void Slider::updateSlider()
  138. {
  139. if (mOrientation == O_HORIZONTAL)
  140. {
  141. float width = (float)getWidth();
  142. if (width < M_EPSILON)
  143. return;
  144. float sliderLength = width / (mRange + 1.0f);
  145. float sliderPos = width * mValue / (mRange + 1.0f);
  146. mSlider->setSize((int)sliderLength, getHeight());
  147. mSlider->setPosition(clamp((int)(sliderPos + 0.5f), 0, getWidth() - mSlider->getWidth()), 0);
  148. }
  149. else
  150. {
  151. float height = (float)getHeight();
  152. if (height < M_EPSILON)
  153. return;
  154. float sliderLength = height / (mRange + 1.0f);
  155. float sliderPos = height * mValue / (mRange + 1.0f);
  156. mSlider->setSize(getWidth(), (int)sliderLength);
  157. mSlider->setPosition(0, clamp((int)(sliderPos + 0.5f), 0, getHeight() - mSlider->getHeight()));
  158. }
  159. }