Slider.cpp 5.4 KB

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