ScrollView.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // If sliders specified, search for them on own hierarchy level
  48. if (element.hasChildElement("horizontalslider"))
  49. {
  50. if (mParent)
  51. setHorizontalSlider(dynamic_cast<Slider*>(mParent->getChild(element.getChildElement("horizontalslider").getString("name"))));
  52. }
  53. if (element.hasChildElement("verticalslider"))
  54. {
  55. if (mParent)
  56. setVerticalSlider(dynamic_cast<Slider*>(mParent->getChild(element.getChildElement("verticalslider").getString("name"))));
  57. }
  58. }
  59. void ScrollView::update(float timeStep)
  60. {
  61. // If element size has changed, update view and sliders to reflect the new size. Otherwise get position from sliders
  62. if (getSize() != mLastSize)
  63. {
  64. mLastSize = getSize();
  65. mViewSize.mX = max(mViewSize.mX, mLastSize.mX);
  66. mViewSize.mY = max(mViewSize.mY, mLastSize.mY);
  67. updateView(mViewPosition);
  68. updateSliders();
  69. }
  70. else
  71. updateViewFromSliders();
  72. }
  73. void ScrollView::setViewPosition(const IntVector2& position)
  74. {
  75. updateView(position);
  76. updateSliders();
  77. }
  78. void ScrollView::setViewPosition(int x, int y)
  79. {
  80. setViewPosition(IntVector2(x, y));
  81. }
  82. void ScrollView::setViewSize(const IntVector2& size)
  83. {
  84. mLastSize = getSize();
  85. mViewSize.mX = max(size.mX, getWidth());
  86. mViewSize.mY = max(size.mY, getHeight());
  87. updateView(mViewPosition);
  88. updateSliders();
  89. }
  90. void ScrollView::setViewSize(int x, int y)
  91. {
  92. setViewSize(IntVector2(x, y));
  93. }
  94. void ScrollView::setHorizontalSlider(Slider* slider)
  95. {
  96. mHorizontalSlider = slider;
  97. updateSliders();
  98. }
  99. void ScrollView::setVerticalSlider(Slider* slider)
  100. {
  101. mVerticalSlider = slider;
  102. updateSliders();
  103. }
  104. void ScrollView::updateViewFromSliders()
  105. {
  106. if ((!mHorizontalSlider) && (!mVerticalSlider))
  107. return;
  108. IntVector2 oldPosition = mViewPosition;
  109. IntVector2 newPosition = mViewPosition;
  110. const IntVector2& size = getSize();
  111. if (mHorizontalSlider)
  112. newPosition.mX = (int)(mHorizontalSlider->getValue() * (float)size.mX);
  113. if (mVerticalSlider)
  114. newPosition.mY = (int)(mVerticalSlider->getValue() * (float)size.mY);
  115. updateView(newPosition);
  116. if (mViewPosition != oldPosition)
  117. {
  118. using namespace ViewChanged;
  119. VariantMap eventData;
  120. eventData[P_ELEMENT] = (void*)this;
  121. eventData[P_X] = mViewPosition.mX;
  122. eventData[P_Y] = mViewPosition.mY;
  123. sendEvent(EVENT_VIEWCHANGED, eventData);
  124. }
  125. }
  126. void ScrollView::updateSliders()
  127. {
  128. const IntVector2& size = getSize();
  129. if ((mHorizontalSlider) && (size.mX > 0) && (mViewSize.mX > 0))
  130. {
  131. mHorizontalSlider->setRange((float)mViewSize.mX / (float)size.mX - 1.0f);
  132. mHorizontalSlider->setValue((float)mViewPosition.mX / (float)size.mX);
  133. }
  134. if ((mVerticalSlider) && (size.mY > 0) && (mViewSize.mY > 0))
  135. {
  136. mVerticalSlider->setRange((float)mViewSize.mY / (float)size.mY - 1.0f);
  137. mVerticalSlider->setValue((float)mViewPosition.mY / (float)size.mY);
  138. }
  139. }
  140. void ScrollView::updateView(const IntVector2& position)
  141. {
  142. mViewPosition.mX = clamp(position.mX, 0, mViewSize.mX - getWidth());
  143. mViewPosition.mY = clamp(position.mY, 0, mViewSize.mY - getHeight());
  144. setChildOffset(-mViewPosition);
  145. }