BsGUIColorField.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include "BsCmdInputFieldValueChange.h"
  11. using namespace std::placeholders;
  12. namespace BansheeEngine
  13. {
  14. GUIColorField::GUIColorField(const PrivatelyConstruct& dummy, const GUIContent& labelContent,
  15. const String& style, const GUILayoutOptions& layoutOptions)
  16. :GUIElementContainer(layoutOptions, style), mLabel(nullptr), mColor(nullptr), mLabelWidth(100)
  17. {
  18. mLabel = GUILabel::create(labelContent, getSubStyleName(getLabelStyleType()));
  19. mColor = GUIColor::create(getSubStyleName(getColorInputStyleType()));
  20. mColor->onValueChanged.connect(std::bind(&GUIColorField::valueChanged, this, _1));
  21. _registerChildElement(mLabel);
  22. _registerChildElement(mColor);
  23. }
  24. GUIColorField::GUIColorField(const PrivatelyConstruct& dummy,
  25. const String& style, const GUILayoutOptions& layoutOptions)
  26. :GUIElementContainer(layoutOptions, style), mLabel(nullptr), mColor(nullptr), mLabelWidth(100)
  27. {
  28. mColor = GUIColor::create(style);
  29. _registerChildElement(mColor);
  30. }
  31. GUIColorField::~GUIColorField()
  32. {
  33. }
  34. GUIColorField* GUIColorField::create(const GUIContent& labelContent, const GUIOptions& layoutOptions,
  35. const String& style)
  36. {
  37. const String* curStyle = &style;
  38. if (*curStyle == StringUtil::BLANK)
  39. curStyle = &getGUITypeName();
  40. return bs_new<GUIColorField>(PrivatelyConstruct(), labelContent, *curStyle,
  41. GUILayoutOptions::create(layoutOptions));
  42. }
  43. GUIColorField* GUIColorField::create(const GUIContent& labelContent, const String& style)
  44. {
  45. const String* curStyle = &style;
  46. if (*curStyle == StringUtil::BLANK)
  47. curStyle = &getGUITypeName();
  48. return bs_new<GUIColorField>(PrivatelyConstruct(), labelContent, *curStyle,
  49. GUILayoutOptions::create());
  50. }
  51. GUIColorField* GUIColorField::create(const HString& labelContent, const GUIOptions& layoutOptions,
  52. const String& style)
  53. {
  54. const String* curStyle = &style;
  55. if (*curStyle == StringUtil::BLANK)
  56. curStyle = &getGUITypeName();
  57. return bs_new<GUIColorField>(PrivatelyConstruct(), GUIContent(labelContent), *curStyle,
  58. GUILayoutOptions::create(layoutOptions));
  59. }
  60. GUIColorField* GUIColorField::create(const HString& labelContent, const String& style)
  61. {
  62. const String* curStyle = &style;
  63. if (*curStyle == StringUtil::BLANK)
  64. curStyle = &getGUITypeName();
  65. return bs_new<GUIColorField>(PrivatelyConstruct(), GUIContent(labelContent), *curStyle,
  66. GUILayoutOptions::create());
  67. }
  68. GUIColorField* GUIColorField::create(const GUIOptions& layoutOptions, const String& style)
  69. {
  70. const String* curStyle = &style;
  71. if (*curStyle == StringUtil::BLANK)
  72. curStyle = &getGUITypeName();
  73. return bs_new<GUIColorField>(PrivatelyConstruct(), *curStyle,
  74. GUILayoutOptions::create(layoutOptions));
  75. }
  76. GUIColorField* GUIColorField::create(const String& style)
  77. {
  78. const String* curStyle = &style;
  79. if (*curStyle == StringUtil::BLANK)
  80. curStyle = &getGUITypeName();
  81. return bs_new<GUIColorField>(PrivatelyConstruct(), *curStyle, GUILayoutOptions::create());
  82. }
  83. void GUIColorField::setValue(const Color& color)
  84. {
  85. mValue = color;
  86. mColor->setColor(color);
  87. }
  88. void GUIColorField::setLabelWidth(UINT32 width)
  89. {
  90. mLabelWidth = width;
  91. markContentAsDirty();
  92. }
  93. void GUIColorField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  94. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  95. {
  96. UINT32 colorOffset = 0;
  97. UINT32 colorWidth = width;
  98. if(mLabel != nullptr)
  99. {
  100. UINT32 labelWidth = mLabelWidth;
  101. Vector2I optimalSize = mLabel->_getOptimalSize();
  102. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  103. Vector2I offset(x, y + yOffset);
  104. mLabel->_setOffset(offset);
  105. mLabel->_setWidth(labelWidth);
  106. mLabel->_setHeight(optimalSize.y);
  107. mLabel->_setAreaDepth(areaDepth);
  108. mLabel->_setWidgetDepth(widgetDepth);
  109. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  110. mLabel->_setClipRect(elemClipRect);
  111. colorOffset = labelWidth;
  112. colorWidth = width - labelWidth;
  113. }
  114. {
  115. Vector2I optimalSize = mColor->_getOptimalSize();
  116. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  117. Vector2I offset(x + colorOffset, y + yOffset);
  118. mColor->_setOffset(offset);
  119. mColor->_setWidth(colorWidth);
  120. mColor->_setHeight(optimalSize.y);
  121. mColor->_setAreaDepth(areaDepth);
  122. mColor->_setWidgetDepth(widgetDepth);
  123. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  124. mColor->_setClipRect(elemClipRect);
  125. }
  126. }
  127. Vector2I GUIColorField::_getOptimalSize() const
  128. {
  129. Vector2I optimalsize = mColor->_getOptimalSize();
  130. if(mLabel != nullptr)
  131. {
  132. optimalsize.x += mLabel->_getOptimalSize().x;
  133. optimalsize.y = std::max(optimalsize.y, mLabel->_getOptimalSize().y);
  134. }
  135. return optimalsize;
  136. }
  137. void GUIColorField::styleUpdated()
  138. {
  139. if (mLabel != nullptr)
  140. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  141. mColor->setStyle(getSubStyleName(getColorInputStyleType()));
  142. }
  143. void GUIColorField::valueChanged(const Color& newValue)
  144. {
  145. setValue(newValue);
  146. if (!onValueChanged.empty())
  147. onValueChanged(newValue);
  148. }
  149. const String& GUIColorField::getGUITypeName()
  150. {
  151. static String typeName = "GUIColorField";
  152. return typeName;
  153. }
  154. const String& GUIColorField::getLabelStyleType()
  155. {
  156. static String STYLE_TYPE = "EditorFieldLabel";
  157. return STYLE_TYPE;
  158. }
  159. const String& GUIColorField::getColorInputStyleType()
  160. {
  161. static String STYLE_TYPE = "EditorFieldColor";
  162. return STYLE_TYPE;
  163. }
  164. }