ScrollView.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "ScrollView.h"
  25. #include "Slider.h"
  26. #include "UIEvents.h"
  27. #include "DebugNew.h"
  28. ScrollView::ScrollView(const std::string& name) :
  29. BorderImage(name),
  30. mViewPosition(IntVector2::sZero),
  31. mViewSize(IntVector2::sZero),
  32. mLastSize(IntVector2::sZero)
  33. {
  34. mClipChildren = true;
  35. mEnabled = true;
  36. }
  37. ScrollView::~ScrollView()
  38. {
  39. }
  40. void ScrollView::setStyle(const XMLElement& element, ResourceCache* cache)
  41. {
  42. BorderImage::setStyle(element, cache);
  43. if (element.hasChildElement("viewposition"))
  44. setViewPosition(element.getChildElement("viewposition").getIntVector2("value"));
  45. if (element.hasChildElement("viewsize"))
  46. setViewSize(element.getChildElement("viewsize").getIntVector2("value"));
  47. UIElement* root = getRootElement();
  48. if (root)
  49. {
  50. if (element.hasChildElement("horizontalslider"))
  51. setHorizontalSlider(dynamic_cast<Slider*>(root->getChild(element.getChildElement("horizontalslider").getString("name"), true)));
  52. if (element.hasChildElement("verticalslider"))
  53. setVerticalSlider(dynamic_cast<Slider*>(root->getChild(element.getChildElement("verticalslider").getString("name"), true)));
  54. }
  55. }
  56. void ScrollView::update(float timeStep)
  57. {
  58. // If element size has changed, update view and sliders to reflect the new size. Otherwise get position from sliders
  59. if (getSize() != mLastSize)
  60. {
  61. mLastSize = getSize();
  62. mViewSize.mX = max(mViewSize.mX, mLastSize.mX);
  63. mViewSize.mY = max(mViewSize.mY, mLastSize.mY);
  64. updateView(mViewPosition);
  65. updateSliders();
  66. }
  67. else
  68. updateViewFromSliders();
  69. }
  70. void ScrollView::setViewPosition(const IntVector2& position)
  71. {
  72. updateView(position);
  73. updateSliders();
  74. }
  75. void ScrollView::setViewPosition(int x, int y)
  76. {
  77. setViewPosition(IntVector2(x, y));
  78. }
  79. void ScrollView::setViewSize(const IntVector2& size)
  80. {
  81. mLastSize = getSize();
  82. mViewSize.mX = max(size.mX, getWidth());
  83. mViewSize.mY = max(size.mY, getHeight());
  84. updateView(mViewPosition);
  85. updateSliders();
  86. }
  87. void ScrollView::setViewSize(int x, int y)
  88. {
  89. setViewSize(IntVector2(x, y));
  90. }
  91. void ScrollView::setHorizontalSlider(Slider* slider)
  92. {
  93. mHorizontalSlider = slider;
  94. updateSliders();
  95. }
  96. void ScrollView::setVerticalSlider(Slider* slider)
  97. {
  98. mVerticalSlider = slider;
  99. updateSliders();
  100. }
  101. void ScrollView::updateViewFromSliders()
  102. {
  103. if ((!mHorizontalSlider) && (!mVerticalSlider))
  104. return;
  105. IntVector2 oldPosition = mViewPosition;
  106. IntVector2 newPosition = mViewPosition;
  107. const IntVector2& size = getSize();
  108. if (mHorizontalSlider)
  109. newPosition.mX = (int)(mHorizontalSlider->getValue() * (float)size.mX);
  110. if (mVerticalSlider)
  111. newPosition.mY = (int)(mVerticalSlider->getValue() * (float)size.mY);
  112. updateView(newPosition);
  113. if (mViewPosition != oldPosition)
  114. {
  115. using namespace ViewChanged;
  116. VariantMap eventData;
  117. eventData[P_ELEMENT] = (void*)this;
  118. eventData[P_X] = mViewPosition.mX;
  119. eventData[P_Y] = mViewPosition.mY;
  120. sendEvent(EVENT_VIEWCHANGED, eventData);
  121. }
  122. }
  123. void ScrollView::updateSliders()
  124. {
  125. const IntVector2& size = getSize();
  126. if ((mHorizontalSlider) && (size.mX > 0) && (mViewSize.mX > 0))
  127. {
  128. mHorizontalSlider->setRange((float)mViewSize.mX / (float)size.mX - 1.0f);
  129. mHorizontalSlider->setValue((float)mViewPosition.mX / (float)size.mX);
  130. }
  131. if ((mVerticalSlider) && (size.mY > 0) && (mViewSize.mY > 0))
  132. {
  133. mVerticalSlider->setRange((float)mViewSize.mY / (float)size.mY - 1.0f);
  134. mVerticalSlider->setValue((float)mViewPosition.mY / (float)size.mY);
  135. }
  136. }
  137. void ScrollView::updateView(const IntVector2& position)
  138. {
  139. mViewPosition.mX = clamp(position.mX, 0, mViewSize.mX - getWidth());
  140. mViewPosition.mY = clamp(position.mY, 0, mViewSize.mY - getHeight());
  141. setChildOffset(-mViewPosition);
  142. }