BsGUIColorField.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "BsGUIColorField.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIColor.h"
  6. #include "BsBuiltinResources.h"
  7. #include "BsGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsGUIWidget.h"
  10. namespace BansheeEngine
  11. {
  12. GUIColorField::GUIColorField(const PrivatelyConstruct& dummy, const GUIContent& labelContent,
  13. const String& labelStyle, const String& colorStyle, const GUILayoutOptions& layoutOptions)
  14. :GUIElementContainer(layoutOptions), mLabel(nullptr), mColor(nullptr), mLabelWidth(100)
  15. {
  16. mLabel = GUILabel::create(labelContent, labelStyle);
  17. mColor = GUIColor::create(colorStyle);
  18. _registerChildElement(mLabel);
  19. _registerChildElement(mColor);
  20. }
  21. GUIColorField::GUIColorField(const PrivatelyConstruct& dummy,
  22. const String& labelStyle, const String& colorStyle, const GUILayoutOptions& layoutOptions)
  23. :GUIElementContainer(layoutOptions), mLabel(nullptr), mColor(nullptr), mLabelWidth(100)
  24. {
  25. mColor = GUIColor::create(colorStyle);
  26. _registerChildElement(mColor);
  27. }
  28. GUIColorField::~GUIColorField()
  29. {
  30. }
  31. GUIColorField* GUIColorField::create(const GUIContent& labelContent, const GUIOptions& layoutOptions,
  32. const String& labelStyle, const String& toggleStyle)
  33. {
  34. return bs_new<GUIColorField>(PrivatelyConstruct(), labelContent, labelStyle, toggleStyle,
  35. GUILayoutOptions::create(layoutOptions));
  36. }
  37. GUIColorField* GUIColorField::create(const GUIContent& labelContent, const String& labelStyle,
  38. const String& toggleStyle)
  39. {
  40. return bs_new<GUIColorField>(PrivatelyConstruct(), labelContent, labelStyle, toggleStyle,
  41. GUILayoutOptions::create());
  42. }
  43. GUIColorField* GUIColorField::create(const HString& labelContent, const GUIOptions& layoutOptions,
  44. const String& labelStyle, const String& toggleStyle)
  45. {
  46. return bs_new<GUIColorField>(PrivatelyConstruct(), GUIContent(labelContent), labelStyle,
  47. toggleStyle, GUILayoutOptions::create(layoutOptions));
  48. }
  49. GUIColorField* GUIColorField::create( const HString& labelContent, const String& labelStyle,
  50. const String& toggleStyle)
  51. {
  52. return bs_new<GUIColorField>(PrivatelyConstruct(), GUIContent(labelContent), labelStyle, toggleStyle,
  53. GUILayoutOptions::create());
  54. }
  55. GUIColorField* GUIColorField::create(const GUIOptions& layoutOptions, const String& labelStyle,
  56. const String& toggleStyle)
  57. {
  58. return bs_new<GUIColorField>(PrivatelyConstruct(), labelStyle, toggleStyle,
  59. GUILayoutOptions::create(layoutOptions));
  60. }
  61. GUIColorField* GUIColorField::create(const String& labelStyle, const String& toggleStyle)
  62. {
  63. return bs_new<GUIColorField>(PrivatelyConstruct(), labelStyle, toggleStyle, GUILayoutOptions::create());
  64. }
  65. Color GUIColorField::getValue() const
  66. {
  67. return mColor->getColor();
  68. }
  69. void GUIColorField::setValue(const Color& color)
  70. {
  71. mColor->setColor(color);
  72. }
  73. void GUIColorField::setLabelWidth(UINT32 width)
  74. {
  75. mLabelWidth = width;
  76. markContentAsDirty();
  77. }
  78. void GUIColorField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  79. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  80. {
  81. UINT32 colorOffset = 0;
  82. UINT32 colorWidth = width;
  83. if(mLabel != nullptr)
  84. {
  85. UINT32 labelWidth = mLabelWidth;
  86. Vector2I optimalSize = mLabel->_getOptimalSize();
  87. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  88. Vector2I offset(x, y + yOffset);
  89. mLabel->_setOffset(offset);
  90. mLabel->_setWidth(labelWidth);
  91. mLabel->_setHeight(optimalSize.y);
  92. mLabel->_setAreaDepth(areaDepth);
  93. mLabel->_setWidgetDepth(widgetDepth);
  94. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  95. mLabel->_setClipRect(elemClipRect);
  96. colorOffset = labelWidth;
  97. colorWidth = width - labelWidth;
  98. }
  99. {
  100. Vector2I optimalSize = mColor->_getOptimalSize();
  101. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  102. Vector2I offset(x + colorOffset, y + yOffset);
  103. mColor->_setOffset(offset);
  104. mColor->_setWidth(colorWidth);
  105. mColor->_setHeight(optimalSize.y);
  106. mColor->_setAreaDepth(areaDepth);
  107. mColor->_setWidgetDepth(widgetDepth);
  108. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  109. mColor->_setClipRect(elemClipRect);
  110. }
  111. }
  112. Vector2I GUIColorField::_getOptimalSize() const
  113. {
  114. Vector2I optimalsize = mColor->_getOptimalSize();
  115. if(mLabel != nullptr)
  116. {
  117. optimalsize.x += mLabel->_getOptimalSize().x;
  118. optimalsize.y = std::max(optimalsize.y, mLabel->_getOptimalSize().y);
  119. }
  120. return optimalsize;
  121. }
  122. const String& GUIColorField::getGUITypeName()
  123. {
  124. static String typeName = "GUIColorField";
  125. return typeName;
  126. }
  127. }