ScrollBar.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Button.h"
  25. #include "Context.h"
  26. #include "ScrollBar.h"
  27. #include "Slider.h"
  28. #include "UIEvents.h"
  29. namespace Urho3D
  30. {
  31. static const float DEFAULT_SCROLL_STEP = 0.1f;
  32. static const float DEFAULT_REPEAT_DELAY = 0.4f;
  33. static const float DEFAULT_REPEAT_RATE = 20.0f;
  34. OBJECTTYPESTATIC(ScrollBar);
  35. ScrollBar::ScrollBar(Context* context) :
  36. UIElement(context),
  37. scrollStep_(DEFAULT_SCROLL_STEP),
  38. stepFactor_(1.0f),
  39. leftRect_(IntRect::ZERO),
  40. rightRect_(IntRect::ZERO),
  41. upRect_(IntRect::ZERO),
  42. downRect_(IntRect::ZERO)
  43. {
  44. active_ = true;
  45. backButton_ = CreateChild<Button>();
  46. backButton_->SetInternal(true);
  47. backButton_->SetRepeat(DEFAULT_REPEAT_DELAY, DEFAULT_REPEAT_RATE);
  48. slider_ = CreateChild<Slider>();
  49. slider_->SetInternal(true);
  50. forwardButton_ = CreateChild<Button>();
  51. forwardButton_->SetInternal(true);
  52. forwardButton_->SetRepeat(DEFAULT_REPEAT_DELAY, DEFAULT_REPEAT_RATE);
  53. SubscribeToEvent(backButton_, E_PRESSED, HANDLER(ScrollBar, HandleBackButtonPressed));
  54. SubscribeToEvent(forwardButton_, E_PRESSED, HANDLER(ScrollBar, HandleForwardButtonPressed));
  55. SubscribeToEvent(slider_, E_SLIDERCHANGED, HANDLER(ScrollBar, HandleSliderChanged));
  56. // Set default orientation/layout
  57. SetOrientation(O_HORIZONTAL);
  58. }
  59. ScrollBar::~ScrollBar()
  60. {
  61. }
  62. void ScrollBar::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<ScrollBar>();
  65. }
  66. void ScrollBar::SetStyle(const XMLElement& element)
  67. {
  68. UIElement::SetStyle(element);
  69. if (element.HasChild("range"))
  70. {
  71. XMLElement rangeElem = element.GetChild("range");
  72. SetRange(rangeElem.GetFloat("max"));
  73. SetValue(rangeElem.GetFloat("value"));
  74. }
  75. if (element.HasChild("scrollstep"))
  76. SetScrollStep(element.GetChild("scrollstep").GetFloat("value"));
  77. if (element.HasChild("stepfactor"))
  78. SetStepFactor(element.GetChild("stepfactor").GetFloat("value"));
  79. XMLElement backButtonElem = element.GetChild("backbutton");
  80. if (backButtonElem)
  81. {
  82. XMLElement imageElem = backButtonElem.GetChild("imagerect");
  83. if (imageElem.HasAttribute("horizontal"))
  84. leftRect_ = imageElem.GetIntRect("horizontal");
  85. if (imageElem.HasAttribute("vertical"))
  86. upRect_ = imageElem.GetIntRect("vertical");
  87. if (imageElem.HasAttribute("h"))
  88. leftRect_ = imageElem.GetIntRect("h");
  89. if (imageElem.HasAttribute("v"))
  90. upRect_ = imageElem.GetIntRect("v");
  91. backButton_->SetStyle(backButtonElem);
  92. }
  93. XMLElement forwardButtonElem = element.GetChild("forwardbutton");
  94. if (forwardButtonElem)
  95. {
  96. XMLElement imageElem = forwardButtonElem.GetChild("imagerect");
  97. if (imageElem.HasAttribute("horizontal"))
  98. rightRect_ = imageElem.GetIntRect("horizontal");
  99. if (imageElem.HasAttribute("vertical"))
  100. downRect_ = imageElem.GetIntRect("vertical");
  101. if (imageElem.HasAttribute("h"))
  102. rightRect_ = imageElem.GetIntRect("h");
  103. if (imageElem.HasAttribute("v"))
  104. downRect_ = imageElem.GetIntRect("v");
  105. forwardButton_->SetStyle(forwardButtonElem);
  106. }
  107. XMLElement sliderElem = element.GetChild("slider");
  108. if (sliderElem)
  109. slider_->SetStyle(sliderElem);
  110. if (element.HasChild("orientation"))
  111. {
  112. String orientation = element.GetChild("orientation").GetAttributeLower("value");
  113. if (orientation == "horizontal" || orientation == "h")
  114. SetOrientation(O_HORIZONTAL);
  115. if (orientation == "vertical" || orientation == "v")
  116. SetOrientation(O_VERTICAL);
  117. }
  118. }
  119. void ScrollBar::OnResize()
  120. {
  121. // Disable layout operations while setting the button sizes is incomplete
  122. DisableLayoutUpdate();
  123. if (slider_->GetOrientation() == O_HORIZONTAL)
  124. {
  125. int height = GetHeight();
  126. backButton_->SetFixedSize(height, height);
  127. forwardButton_->SetFixedSize(height, height);
  128. }
  129. else
  130. {
  131. int width = GetWidth();
  132. backButton_->SetFixedSize(width, width);
  133. forwardButton_->SetFixedSize(width, width);
  134. }
  135. EnableLayoutUpdate();
  136. }
  137. void ScrollBar::SetOrientation(Orientation orientation)
  138. {
  139. slider_->SetOrientation(orientation);
  140. if (orientation == O_HORIZONTAL)
  141. {
  142. backButton_->SetImageRect(leftRect_);
  143. forwardButton_->SetImageRect(rightRect_);
  144. }
  145. else
  146. {
  147. backButton_->SetImageRect(upRect_);
  148. forwardButton_->SetImageRect(downRect_);
  149. }
  150. OnResize();
  151. if (orientation == O_HORIZONTAL)
  152. SetLayout(LM_HORIZONTAL);
  153. else
  154. SetLayout(LM_VERTICAL);
  155. }
  156. void ScrollBar::SetRange(float range)
  157. {
  158. slider_->SetRange(range);
  159. }
  160. void ScrollBar::SetValue(float value)
  161. {
  162. slider_->SetValue(value);
  163. }
  164. void ScrollBar::ChangeValue(float delta)
  165. {
  166. slider_->ChangeValue(delta);
  167. }
  168. void ScrollBar::SetScrollStep(float step)
  169. {
  170. scrollStep_ = Max(step, 0.0f);
  171. }
  172. void ScrollBar::SetStepFactor(float factor)
  173. {
  174. stepFactor_ = Max(factor, M_EPSILON);
  175. }
  176. void ScrollBar::StepBack()
  177. {
  178. slider_->SetValue(slider_->GetValue() - GetEffectiveScrollStep());
  179. }
  180. void ScrollBar::StepForward()
  181. {
  182. slider_->SetValue(slider_->GetValue() + GetEffectiveScrollStep());
  183. }
  184. Orientation ScrollBar::GetOrientation() const
  185. {
  186. return slider_->GetOrientation();
  187. }
  188. float ScrollBar::GetRange() const
  189. {
  190. return slider_->GetRange();
  191. }
  192. float ScrollBar::GetValue() const
  193. {
  194. return slider_->GetValue();
  195. }
  196. float ScrollBar::GetEffectiveScrollStep() const
  197. {
  198. return scrollStep_ * stepFactor_;
  199. }
  200. void ScrollBar::HandleBackButtonPressed(StringHash eventType, VariantMap& eventData)
  201. {
  202. StepBack();
  203. }
  204. void ScrollBar::HandleForwardButtonPressed(StringHash eventType, VariantMap& eventData)
  205. {
  206. StepForward();
  207. }
  208. void ScrollBar::HandleSliderChanged(StringHash eventType, VariantMap& eventData)
  209. {
  210. // Send the event forward
  211. VariantMap newEventData;
  212. newEventData[ScrollBarChanged::P_ELEMENT] = (void*)this;
  213. newEventData[ScrollBarChanged::P_VALUE] = slider_->GetValue();
  214. SendEvent(E_SCROLLBARCHANGED, newEventData);
  215. }
  216. }