Slider.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. extern const char* UI_CATEGORY;
  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. repeatRate_(0.0f)
  50. {
  51. enabled_ = true;
  52. knob_ = CreateChild<BorderImage>("S_Knob");
  53. knob_->SetInternal(true);
  54. UpdateSlider();
  55. }
  56. Slider::~Slider()
  57. {
  58. }
  59. void Slider::RegisterObject(Context* context)
  60. {
  61. context->RegisterFactory<Slider>(UI_CATEGORY);
  62. COPY_BASE_ATTRIBUTES(Slider, BorderImage);
  63. UPDATE_ATTRIBUTE_DEFAULT_VALUE(Slider, "Is Enabled", true);
  64. ENUM_ACCESSOR_ATTRIBUTE(Slider, "Orientation", GetOrientation, SetOrientation, Orientation, orientations, O_HORIZONTAL, AM_FILE);
  65. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Range", GetRange, SetRange, float, 1.0f, AM_FILE);
  66. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Value", GetValue, SetValue, float, 0.0f, AM_FILE);
  67. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Repeat Rate", GetRepeatRate, SetRepeatRate, float, 0.0f, AM_FILE);
  68. }
  69. void Slider::Update(float timeStep)
  70. {
  71. if (dragSlider_)
  72. hovering_ = true;
  73. // Propagate hover effect to the slider knob
  74. knob_->SetHovering(hovering_);
  75. knob_->SetSelected(hovering_);
  76. }
  77. void Slider::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  78. {
  79. BorderImage::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  80. // Show hover effect if inside the slider knob
  81. hovering_ = knob_->IsInside(screenPosition, true);
  82. // If not hovering on the knob, send it as page event
  83. if (!hovering_)
  84. Page(position, buttons, qualifiers);
  85. }
  86. void Slider::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  87. {
  88. BorderImage::OnClick(position, screenPosition, buttons, qualifiers, cursor);
  89. selected_ = true;
  90. hovering_ = knob_->IsInside(screenPosition, true);
  91. if (!hovering_)
  92. Page(position, buttons, qualifiers);
  93. }
  94. void Slider::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  95. {
  96. dragBeginCursor_ = position;
  97. dragBeginPosition_ = knob_->GetPosition();
  98. dragSlider_ = knob_->IsInside(screenPosition, true);
  99. }
  100. void Slider::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  101. {
  102. if (!dragSlider_ || GetSize() == knob_->GetSize())
  103. return;
  104. float newValue = value_;
  105. IntVector2 delta = position - dragBeginCursor_;
  106. if (orientation_ == O_HORIZONTAL)
  107. {
  108. int newX = Clamp(dragBeginPosition_.x_ + delta.x_, 0, GetWidth() - knob_->GetWidth());
  109. knob_->SetPosition(newX, 0);
  110. newValue = (float)newX * range_ / (float)(GetWidth() - knob_->GetWidth());
  111. }
  112. else
  113. {
  114. int newY = Clamp(dragBeginPosition_.y_ + delta.y_, 0, GetHeight() - knob_->GetHeight());
  115. knob_->SetPosition(0, newY);
  116. newValue = (float)newY * range_ / (float)(GetHeight() - knob_->GetHeight());
  117. }
  118. SetValue(newValue);
  119. }
  120. void Slider::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  121. {
  122. dragSlider_ = false;
  123. selected_ = false;
  124. }
  125. void Slider::OnResize()
  126. {
  127. UpdateSlider();
  128. }
  129. void Slider::SetOrientation(Orientation type)
  130. {
  131. orientation_ = type;
  132. UpdateSlider();
  133. }
  134. void Slider::SetRange(float range)
  135. {
  136. range = Max(range, 0.0f);
  137. if (range != range_)
  138. {
  139. range_ = range;
  140. UpdateSlider();
  141. }
  142. }
  143. void Slider::SetValue(float value)
  144. {
  145. value = Clamp(value, 0.0f, range_);
  146. if (value != value_)
  147. {
  148. value_ = value;
  149. UpdateSlider();
  150. using namespace SliderChanged;
  151. VariantMap eventData;
  152. eventData[P_ELEMENT] = (void*)this;
  153. eventData[P_VALUE] = value_;
  154. SendEvent(E_SLIDERCHANGED, eventData);
  155. }
  156. }
  157. void Slider::ChangeValue(float delta)
  158. {
  159. SetValue(value_ + delta);
  160. }
  161. void Slider::SetRepeatRate(float rate)
  162. {
  163. repeatRate_ = Max(rate, 0.0f);
  164. }
  165. bool Slider::FilterImplicitAttributes(XMLElement& dest) const
  166. {
  167. if (!BorderImage::FilterImplicitAttributes(dest))
  168. return false;
  169. XMLElement childElem = dest.GetChild("element");
  170. if (!childElem)
  171. return false;
  172. if (!RemoveChildXML(childElem, "Name", "S_Knob"))
  173. return false;
  174. if (!RemoveChildXML(childElem, "Position"))
  175. return false;
  176. if (!RemoveChildXML(childElem, "Size"))
  177. return false;
  178. return true;
  179. }
  180. void Slider::UpdateSlider()
  181. {
  182. const IntRect& border = knob_->GetBorder();
  183. if (range_ > 0.0f)
  184. {
  185. if (orientation_ == O_HORIZONTAL)
  186. {
  187. int sliderLength = (int)Max((float)GetWidth() / (range_ + 1.0f), (float)(border.left_ + border.right_));
  188. float sliderPos = (float)(GetWidth() - sliderLength) * value_ / range_;
  189. knob_->SetSize(sliderLength, GetHeight());
  190. knob_->SetPosition(Clamp((int)(sliderPos + 0.5f), 0, GetWidth() - knob_->GetWidth()), 0);
  191. }
  192. else
  193. {
  194. int sliderLength = (int)Max((float)GetHeight() / (range_ + 1.0f), (float)(border.top_ + border.bottom_));
  195. float sliderPos = (float)(GetHeight() - sliderLength) * value_ / range_;
  196. knob_->SetSize(GetWidth(), sliderLength);
  197. knob_->SetPosition(0, Clamp((int)(sliderPos + 0.5f), 0, GetHeight() - knob_->GetHeight()));
  198. }
  199. }
  200. else
  201. {
  202. knob_->SetSize(GetSize());
  203. knob_->SetPosition(0, 0);
  204. }
  205. }
  206. void Slider::Page(const IntVector2& position, int buttons, int qualifiers)
  207. {
  208. IntVector2 offsetXY = position - knob_->GetPosition() - knob_->GetSize() / 2;
  209. int offset = orientation_ == O_HORIZONTAL ? offsetXY.x_ : offsetXY.y_;
  210. float length = (float)(orientation_ == O_HORIZONTAL ? GetWidth() : GetHeight());
  211. using namespace SliderPaged;
  212. VariantMap eventData;
  213. eventData[P_ELEMENT] = (void*)this;
  214. eventData[P_OFFSET] = offset;
  215. // Only generate the 'click' variant of the event when the slider is selected
  216. // i.e. when it has received the first initial click.
  217. if (selected_ && repeatRate_ > 0.0f && repeatTimer_.GetMSec(false) >= Lerp(1000.0f / repeatRate_, 0, Abs(offset) / length))
  218. {
  219. repeatTimer_.Reset();
  220. eventData[P_BUTTONS] = buttons;
  221. eventData[P_QUALIFIERS] = qualifiers;
  222. }
  223. // When without buttons & qualifiers parameters, the receiver should interpret
  224. // this event as just mouse hovering on slider's 'paging' area instead
  225. SendEvent(E_SLIDERPAGED, eventData);
  226. }
  227. }