ScrollBar.cpp 7.4 KB

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