Slider.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "InputEvents.h"
  25. #include "Log.h"
  26. #include "Slider.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. const char* orientations[] =
  32. {
  33. "Horizontal",
  34. "Vertical",
  35. 0
  36. };
  37. template<> Orientation Variant::Get<Orientation>() const
  38. {
  39. return (Orientation)GetInt();
  40. }
  41. OBJECTTYPESTATIC(Slider);
  42. Slider::Slider(Context* context) :
  43. BorderImage(context),
  44. orientation_(O_HORIZONTAL),
  45. range_(1.0f),
  46. value_(0.0f),
  47. dragSlider_(false)
  48. {
  49. active_ = true;
  50. knob_ = CreateChild<BorderImage>();
  51. knob_->SetInternal(true);
  52. UpdateSlider();
  53. }
  54. Slider::~Slider()
  55. {
  56. }
  57. void Slider::RegisterObject(Context* context)
  58. {
  59. context->RegisterFactory<Slider>();
  60. ENUM_ACCESSOR_ATTRIBUTE(Slider, "Orientation", GetOrientation, SetOrientation, Orientation, orientations, O_HORIZONTAL, AM_FILE);
  61. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Range", GetRange, SetRange, float, 1.0f, AM_FILE);
  62. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Value", GetValue, SetValue, float, 0.0f, AM_FILE);
  63. COPY_BASE_ATTRIBUTES(Slider, BorderImage);
  64. }
  65. void Slider::Update(float timeStep)
  66. {
  67. if (dragSlider_)
  68. hovering_ = true;
  69. // Propagate hover and selection effect to the slider knob
  70. knob_->SetHovering(hovering_);
  71. knob_->SetSelected(selected_);
  72. }
  73. void Slider::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  74. {
  75. // Show hover effect if inside the slider knob
  76. hovering_ = knob_->IsInside(screenPosition, true);
  77. }
  78. void Slider::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  79. {
  80. dragBeginCursor_ = position;
  81. dragBeginPosition_ = knob_->GetPosition();
  82. dragSlider_ = knob_->IsInside(screenPosition, true);
  83. }
  84. void Slider::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  85. {
  86. if (!dragSlider_ || GetSize() == knob_->GetSize())
  87. return;
  88. float newValue = value_;
  89. IntVector2 delta = position - dragBeginCursor_;
  90. if (orientation_ == O_HORIZONTAL)
  91. {
  92. int newX = Clamp(dragBeginPosition_.x_ + delta.x_, 0, GetWidth() - knob_->GetWidth());
  93. knob_->SetPosition(newX, 0);
  94. newValue = (float)newX * range_ / (float)(GetWidth() - knob_->GetWidth());
  95. }
  96. else
  97. {
  98. int newY = Clamp(dragBeginPosition_.y_ + delta.y_, 0, GetHeight() - knob_->GetHeight());
  99. knob_->SetPosition(0, newY);
  100. newValue = (float)newY * range_ / (float)(GetHeight() - knob_->GetHeight());
  101. }
  102. SetValue(newValue);
  103. }
  104. void Slider::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  105. {
  106. dragSlider_ = false;
  107. }
  108. void Slider::OnResize()
  109. {
  110. UpdateSlider();
  111. }
  112. void Slider::SetOrientation(Orientation type)
  113. {
  114. orientation_ = type;
  115. UpdateSlider();
  116. }
  117. void Slider::SetRange(float range)
  118. {
  119. range = Max(range, 0.0f);
  120. if (range != range_)
  121. {
  122. range_ = range;
  123. UpdateSlider();
  124. }
  125. }
  126. void Slider::SetValue(float value)
  127. {
  128. value = Clamp(value, 0.0f, range_);
  129. if (value != value_)
  130. {
  131. value_ = value;
  132. UpdateSlider();
  133. using namespace SliderChanged;
  134. VariantMap eventData;
  135. eventData[P_ELEMENT] = (void*)this;
  136. eventData[P_VALUE] = value_;
  137. SendEvent(E_SLIDERCHANGED, eventData);
  138. }
  139. }
  140. void Slider::ChangeValue(float delta)
  141. {
  142. SetValue(value_ + delta);
  143. }
  144. void Slider::UpdateSlider()
  145. {
  146. const IntRect& border = knob_->GetBorder();
  147. if (range_ > 0.0f)
  148. {
  149. if (orientation_ == O_HORIZONTAL)
  150. {
  151. int sliderLength = (int)Max((float)GetWidth() / (range_ + 1.0f), (float)(border.left_ + border.right_));
  152. float sliderPos = (float)(GetWidth() - sliderLength) * value_ / range_;
  153. knob_->SetSize(sliderLength, GetHeight());
  154. knob_->SetPosition(Clamp((int)(sliderPos + 0.5f), 0, GetWidth() - knob_->GetWidth()), 0);
  155. }
  156. else
  157. {
  158. int sliderLength = (int)Max((float)GetHeight() / (range_ + 1.0f), (float)(border.top_ + border.bottom_));
  159. float sliderPos = (float)(GetHeight() - sliderLength) * value_ / range_;
  160. knob_->SetSize(GetWidth(), sliderLength);
  161. knob_->SetPosition(0, Clamp((int)(sliderPos + 0.5f), 0, GetHeight() - knob_->GetHeight()));
  162. }
  163. }
  164. else
  165. {
  166. knob_->SetSize(GetSize());
  167. knob_->SetPosition(0, 0);
  168. }
  169. }
  170. }