BsGUISlider.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "BsGUISlider.h"
  2. #include "BsGUIWidget.h"
  3. #include "BsGUISkin.h"
  4. #include "BsGUISliderHandle.h"
  5. #include "BsGUITexture.h"
  6. #include "BsSpriteTexture.h"
  7. #include "BsGUILayoutOptions.h"
  8. using namespace std::placeholders;
  9. namespace BansheeEngine
  10. {
  11. GUISlider::GUISlider(bool horizontal, const String& styleName, const GUILayoutOptions& layoutOptions)
  12. :GUIElementContainer(layoutOptions, styleName)
  13. {
  14. mSliderHandle = GUISliderHandle::create(horizontal, getSubStyleName(getHandleStyleType()));
  15. mBackground = GUITexture::create(getSubStyleName(getBackgroundStyleType()));
  16. mBackground->_setElementDepth(1);
  17. _registerChildElement(mSliderHandle);
  18. _registerChildElement(mBackground);
  19. mHandleMovedConn = mSliderHandle->onHandleMoved.connect(std::bind(&GUISlider::onHandleMoved, this, _1));
  20. }
  21. GUISlider::~GUISlider()
  22. {
  23. mHandleMovedConn.disconnect();
  24. }
  25. const String& GUISlider::getHandleStyleType()
  26. {
  27. static String HANDLE_STYLE_TYPE = "SliderHandle";
  28. return HANDLE_STYLE_TYPE;
  29. }
  30. const String& GUISlider::getBackgroundStyleType()
  31. {
  32. static String BACKGROUND_STYLE_TYPE = "SliderBackground";
  33. return BACKGROUND_STYLE_TYPE;
  34. }
  35. Vector2I GUISlider::_getOptimalSize() const
  36. {
  37. Vector2I optimalSize = mSliderHandle->_getOptimalSize();
  38. Vector2I backgroundSize = mBackground->_getOptimalSize();
  39. optimalSize.x = std::max(optimalSize.x, backgroundSize.x);
  40. optimalSize.y = std::max(optimalSize.y, backgroundSize.y);
  41. return optimalSize;
  42. }
  43. void GUISlider::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  44. Rect2I clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  45. {
  46. Vector2I offset(x, y);
  47. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  48. mBackground->_setOffset(offset);
  49. mBackground->_setWidth(width);
  50. mBackground->_setHeight(height);
  51. mBackground->_setAreaDepth(areaDepth);
  52. mBackground->_setWidgetDepth(widgetDepth);
  53. mBackground->_setClipRect(elemClipRect);
  54. mSliderHandle->_setOffset(offset);
  55. mSliderHandle->_setWidth(width);
  56. mSliderHandle->_setHeight(height);
  57. mSliderHandle->_setAreaDepth(areaDepth);
  58. mSliderHandle->_setWidgetDepth(widgetDepth);
  59. mSliderHandle->_setClipRect(elemClipRect);
  60. }
  61. void GUISlider::setPercent(float pct)
  62. {
  63. mSliderHandle->setHandlePos(pct);
  64. }
  65. float GUISlider::getPercent() const
  66. {
  67. return mSliderHandle->getHandlePos();
  68. }
  69. void GUISlider::onHandleMoved(float newPosition)
  70. {
  71. onChanged(newPosition);
  72. }
  73. GUISliderHorz::GUISliderHorz(const String& styleName, const GUILayoutOptions& layoutOptions)
  74. :GUISlider(true, styleName, layoutOptions)
  75. {
  76. }
  77. GUISliderHorz* GUISliderHorz::create(const String& styleName)
  78. {
  79. return new (bs_alloc<GUISliderHorz, PoolAlloc>()) GUISliderHorz(getStyleName<GUISliderHorz>(styleName), GUILayoutOptions::create());
  80. }
  81. GUISliderHorz* GUISliderHorz::create(const GUIOptions& layoutOptions, const String& styleName)
  82. {
  83. return new (bs_alloc<GUISliderHorz, PoolAlloc>()) GUISliderHorz(getStyleName<GUISliderHorz>(styleName), GUILayoutOptions::create(layoutOptions));
  84. }
  85. const String& GUISliderHorz::getGUITypeName()
  86. {
  87. static String typeName = "SliderHorz";
  88. return typeName;
  89. }
  90. GUISliderVert::GUISliderVert(const String& styleName, const GUILayoutOptions& layoutOptions)
  91. :GUISlider(false, styleName, layoutOptions)
  92. {
  93. }
  94. GUISliderVert* GUISliderVert::create(const String& styleName)
  95. {
  96. return new (bs_alloc<GUISliderVert, PoolAlloc>()) GUISliderVert(getStyleName<GUISliderVert>(styleName), GUILayoutOptions::create());
  97. }
  98. GUISliderVert* GUISliderVert::create(const GUIOptions& layoutOptions, const String& styleName)
  99. {
  100. return new (bs_alloc<GUISliderVert, PoolAlloc>()) GUISliderVert(getStyleName<GUISliderVert>(styleName), GUILayoutOptions::create(layoutOptions));
  101. }
  102. const String& GUISliderVert::getGUITypeName()
  103. {
  104. static String typeName = "SliderVert";
  105. return typeName;
  106. }
  107. }