Slider.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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 "Context.h"
  25. #include "InputEvents.h"
  26. #include "Log.h"
  27. #include "Slider.h"
  28. #include "UIEvents.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. const char* orientations[] =
  33. {
  34. "Horizontal",
  35. "Vertical",
  36. 0
  37. };
  38. template<> Orientation Variant::Get<Orientation>() const
  39. {
  40. return (Orientation)GetInt();
  41. }
  42. OBJECTTYPESTATIC(Slider);
  43. Slider::Slider(Context* context) :
  44. BorderImage(context),
  45. orientation_(O_HORIZONTAL),
  46. range_(1.0f),
  47. value_(0.0f),
  48. dragSlider_(false)
  49. {
  50. active_ = true;
  51. knob_ = CreateChild<BorderImage>();
  52. knob_->SetInternal(true);
  53. UpdateSlider();
  54. }
  55. Slider::~Slider()
  56. {
  57. }
  58. void Slider::RegisterObject(Context* context)
  59. {
  60. context->RegisterFactory<Slider>();
  61. ENUM_ACCESSOR_ATTRIBUTE(Slider, "Orientation", GetOrientation, SetOrientation, Orientation, orientations, O_HORIZONTAL, AM_FILE);
  62. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Range", GetRange, SetRange, float, 1.0f, AM_FILE);
  63. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Value", GetValue, SetValue, float, 0.0f, AM_FILE);
  64. COPY_BASE_ATTRIBUTES(Slider, BorderImage);
  65. }
  66. void Slider::Update(float timeStep)
  67. {
  68. if (dragSlider_)
  69. hovering_ = true;
  70. // Propagate hover and selection effect to the slider knob
  71. knob_->SetHovering(hovering_);
  72. knob_->SetSelected(selected_);
  73. }
  74. void Slider::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  75. {
  76. // Show hover effect if inside the slider knob
  77. hovering_ = knob_->IsInside(screenPosition, true);
  78. }
  79. void Slider::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  80. {
  81. dragBeginCursor_ = position;
  82. dragBeginPosition_ = knob_->GetPosition();
  83. dragSlider_ = knob_->IsInside(screenPosition, true);
  84. }
  85. void Slider::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  86. {
  87. if (!dragSlider_ || GetSize() == knob_->GetSize())
  88. return;
  89. float newValue = value_;
  90. IntVector2 delta = position - dragBeginCursor_;
  91. if (orientation_ == O_HORIZONTAL)
  92. {
  93. int newX = Clamp(dragBeginPosition_.x_ + delta.x_, 0, GetWidth() - knob_->GetWidth());
  94. knob_->SetPosition(newX, 0);
  95. newValue = (float)newX * range_ / (float)(GetWidth() - knob_->GetWidth());
  96. }
  97. else
  98. {
  99. int newY = Clamp(dragBeginPosition_.y_ + delta.y_, 0, GetHeight() - knob_->GetHeight());
  100. knob_->SetPosition(0, newY);
  101. newValue = (float)newY * range_ / (float)(GetHeight() - knob_->GetHeight());
  102. }
  103. SetValue(newValue);
  104. }
  105. void Slider::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  106. {
  107. dragSlider_ = false;
  108. }
  109. void Slider::OnResize()
  110. {
  111. UpdateSlider();
  112. }
  113. void Slider::SetOrientation(Orientation type)
  114. {
  115. orientation_ = type;
  116. UpdateSlider();
  117. }
  118. void Slider::SetRange(float range)
  119. {
  120. range = Max(range, 0.0f);
  121. if (range != range_)
  122. {
  123. range_ = range;
  124. UpdateSlider();
  125. }
  126. }
  127. void Slider::SetValue(float value)
  128. {
  129. value = Clamp(value, 0.0f, range_);
  130. if (value != value_)
  131. {
  132. value_ = value;
  133. UpdateSlider();
  134. using namespace SliderChanged;
  135. VariantMap eventData;
  136. eventData[P_ELEMENT] = (void*)this;
  137. eventData[P_VALUE] = value_;
  138. SendEvent(E_SLIDERCHANGED, eventData);
  139. }
  140. }
  141. void Slider::ChangeValue(float delta)
  142. {
  143. SetValue(value_ + delta);
  144. }
  145. void Slider::UpdateSlider()
  146. {
  147. const IntRect& border = knob_->GetBorder();
  148. if (range_ > 0.0f)
  149. {
  150. if (orientation_ == O_HORIZONTAL)
  151. {
  152. int sliderLength = (int)Max((float)GetWidth() / (range_ + 1.0f), (float)(border.left_ + border.right_));
  153. float sliderPos = (float)(GetWidth() - sliderLength) * value_ / range_;
  154. knob_->SetSize(sliderLength, GetHeight());
  155. knob_->SetPosition(Clamp((int)(sliderPos + 0.5f), 0, GetWidth() - knob_->GetWidth()), 0);
  156. }
  157. else
  158. {
  159. int sliderLength = (int)Max((float)GetHeight() / (range_ + 1.0f), (float)(border.top_ + border.bottom_));
  160. float sliderPos = (float)(GetHeight() - sliderLength) * value_ / range_;
  161. knob_->SetSize(GetWidth(), sliderLength);
  162. knob_->SetPosition(0, Clamp((int)(sliderPos + 0.5f), 0, GetHeight() - knob_->GetHeight()));
  163. }
  164. }
  165. else
  166. {
  167. knob_->SetSize(GetSize());
  168. knob_->SetPosition(0, 0);
  169. }
  170. }
  171. }