ScrollBar.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "ScrollBar.h"
  26. #include "Slider.h"
  27. #include "UIEvents.h"
  28. ScrollBar::ScrollBar(const std::string& name) :
  29. UIElement(name),
  30. mScrollStep(0.1f),
  31. mLeftRect(IntRect::sZero),
  32. mRightRect(IntRect::sZero),
  33. mUpRect(IntRect::sZero),
  34. mDownRect(IntRect::sZero)
  35. {
  36. mEnabled = true;
  37. mBackButton = new Button();
  38. mForwardButton = new Button();
  39. mSlider = new Slider();
  40. addChild(mBackButton);
  41. addChild(mSlider);
  42. addChild(mForwardButton);
  43. subscribeToEvent(mBackButton, EVENT_PRESSED, EVENT_HANDLER(ScrollBar, handleBackButtonPressed));
  44. subscribeToEvent(mForwardButton, EVENT_PRESSED, EVENT_HANDLER(ScrollBar, handleForwardButtonPressed));
  45. subscribeToEvent(mSlider, EVENT_SLIDERCHANGED, EVENT_HANDLER(ScrollBar, handleSliderChanged));
  46. // Set default orientation/layout
  47. setOrientation(O_HORIZONTAL);
  48. }
  49. ScrollBar::~ScrollBar()
  50. {
  51. }
  52. void ScrollBar::setStyle(const XMLElement& element, ResourceCache* cache)
  53. {
  54. UIElement::setStyle(element, cache);
  55. XMLElement backButtonElem = element.getChildElement("backbutton");
  56. if (backButtonElem)
  57. {
  58. XMLElement imageElem = backButtonElem.getChildElement("imagerect");
  59. if (imageElem.hasAttribute("horizontal"))
  60. mLeftRect = imageElem.getIntRect("horizontal");
  61. if (imageElem.hasAttribute("vertical"))
  62. mUpRect = imageElem.getIntRect("vertical");
  63. if (imageElem.hasAttribute("h"))
  64. mLeftRect = imageElem.getIntRect("h");
  65. if (imageElem.hasAttribute("v"))
  66. mUpRect = imageElem.getIntRect("v");
  67. mBackButton->setStyle(backButtonElem, cache);
  68. }
  69. XMLElement forwardButtonElem = element.getChildElement("forwardbutton");
  70. if (forwardButtonElem)
  71. {
  72. XMLElement imageElem = forwardButtonElem.getChildElement("imagerect");
  73. if (imageElem.hasAttribute("horizontal"))
  74. mRightRect = imageElem.getIntRect("horizontal");
  75. if (imageElem.hasAttribute("vertical"))
  76. mDownRect = imageElem.getIntRect("vertical");
  77. if (imageElem.hasAttribute("h"))
  78. mRightRect = imageElem.getIntRect("h");
  79. if (imageElem.hasAttribute("v"))
  80. mDownRect = imageElem.getIntRect("v");
  81. mForwardButton->setStyle(forwardButtonElem, cache);
  82. }
  83. XMLElement sliderElem = element.getChildElement("slider");
  84. if (sliderElem)
  85. mSlider->setStyle(sliderElem, cache);
  86. if (element.hasChildElement("range"))
  87. {
  88. XMLElement rangeElem = element.getChildElement("range");
  89. setRange(rangeElem.getFloat("max"));
  90. setValue(rangeElem.getFloat("value"));
  91. }
  92. if (element.hasChildElement("orientation"))
  93. {
  94. std::string orientation = element.getChildElement("orientation").getStringLower("value");
  95. if ((orientation == "horizontal") || (orientation == "h"))
  96. setOrientation(O_HORIZONTAL);
  97. if ((orientation == "vertical") || (orientation == "v"))
  98. setOrientation(O_VERTICAL);
  99. }
  100. }
  101. void ScrollBar::onResize()
  102. {
  103. // Disable layout operations while setting the button sizes is incomplete
  104. mUpdateLayoutNestingLevel++;
  105. if (mSlider->getOrientation() == O_HORIZONTAL)
  106. {
  107. int height = getHeight();
  108. mBackButton->setFixedSize(height, height);
  109. mForwardButton->setFixedSize(height, height);
  110. }
  111. else
  112. {
  113. int width = getWidth();
  114. mBackButton->setFixedSize(width, width);
  115. mForwardButton->setFixedSize(width, width);
  116. }
  117. mUpdateLayoutNestingLevel--;
  118. }
  119. void ScrollBar::setOrientation(Orientation orientation)
  120. {
  121. mSlider->setOrientation(orientation);
  122. if (orientation == O_HORIZONTAL)
  123. {
  124. mBackButton->setImageRect(mLeftRect);
  125. mForwardButton->setImageRect(mRightRect);
  126. }
  127. else
  128. {
  129. mBackButton->setImageRect(mUpRect);
  130. mForwardButton->setImageRect(mDownRect);
  131. }
  132. onResize();
  133. setLayout(orientation, LM_RESIZECHILDREN, LM_RESIZECHILDREN);
  134. }
  135. void ScrollBar::setRange(float range)
  136. {
  137. mSlider->setRange(range);
  138. }
  139. void ScrollBar::setValue(float value)
  140. {
  141. mSlider->setValue(value);
  142. }
  143. void ScrollBar::setScrollStep(float step)
  144. {
  145. mScrollStep = max(step, 0.0f);
  146. }
  147. Orientation ScrollBar::getOrientation() const
  148. {
  149. return mSlider->getOrientation();
  150. }
  151. float ScrollBar::getRange() const
  152. {
  153. return mSlider->getRange();
  154. }
  155. float ScrollBar::getValue() const
  156. {
  157. return mSlider->getValue();
  158. }
  159. void ScrollBar::handleBackButtonPressed(StringHash eventType, VariantMap& eventData)
  160. {
  161. mSlider->setValue(mSlider->getValue() - mScrollStep);
  162. }
  163. void ScrollBar::handleForwardButtonPressed(StringHash eventType, VariantMap& eventData)
  164. {
  165. mSlider->setValue(mSlider->getValue() + mScrollStep);
  166. }
  167. void ScrollBar::handleSliderChanged(StringHash eventType, VariantMap& eventData)
  168. {
  169. // Send the event forward
  170. VariantMap newEventData;
  171. newEventData[ScrollBarChanged::P_ELEMENT] = (void*)this;
  172. newEventData[ScrollBarChanged::P_VALUE] = mSlider->getValue();
  173. sendEvent(EVENT_SCROLLBARCHANGED, newEventData);
  174. }