BsGUIColorField.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. using namespace CamelotFramework;
  11. using namespace BansheeEngine;
  12. namespace BansheeEditor
  13. {
  14. GUIColorField::GUIColorField(const PrivatelyConstruct& dummy, GUIWidget& parent, const GUIContent& labelContent,
  15. GUIElementStyle* labelStyle, GUIElementStyle* colorStyle, const GUILayoutOptions& layoutOptions)
  16. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mColor(nullptr), mLabelWidth(100)
  17. {
  18. const GUIElementStyle* curLabelStyle = labelStyle;
  19. const GUIElementStyle* curColorStyle = colorStyle;
  20. if(curLabelStyle == nullptr)
  21. curLabelStyle = parent.getSkin().getStyle("Label");
  22. if(curColorStyle == nullptr)
  23. curColorStyle = parent.getSkin().getStyle("Color");
  24. mLabel = GUILabel::create(parent, labelContent, curLabelStyle);
  25. mColor = GUIColor::create(parent, curColorStyle);
  26. _registerChildElement(mLabel);
  27. _registerChildElement(mColor);
  28. }
  29. GUIColorField::GUIColorField(const PrivatelyConstruct& dummy, GUIWidget& parent,
  30. GUIElementStyle* labelStyle, GUIElementStyle* colorStyle, const GUILayoutOptions& layoutOptions)
  31. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mColor(nullptr), mLabelWidth(100)
  32. {
  33. const GUIElementStyle* curColorStyle = colorStyle;
  34. if(curColorStyle == nullptr)
  35. curColorStyle = parent.getSkin().getStyle("Color");
  36. mColor = GUIColor::create(parent, curColorStyle);
  37. _registerChildElement(mColor);
  38. }
  39. GUIColorField::~GUIColorField()
  40. {
  41. }
  42. GUIColorField* GUIColorField::create(GUIWidget& parent, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  43. GUIElementStyle* labelStyle, GUIElementStyle* toggleStyle)
  44. {
  45. return cm_new<GUIColorField>(PrivatelyConstruct(), parent, labelContent, labelStyle, toggleStyle,
  46. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  47. }
  48. GUIColorField* GUIColorField::create(GUIWidget& parent, const GUIContent& labelContent, GUIElementStyle* labelStyle,
  49. GUIElementStyle* toggleStyle)
  50. {
  51. return cm_new<GUIColorField>(PrivatelyConstruct(), parent, labelContent, labelStyle, toggleStyle,
  52. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  53. }
  54. GUIColorField* GUIColorField::create(GUIWidget& parent, const HString& labelContent, const GUIOptions& layoutOptions,
  55. GUIElementStyle* labelStyle, GUIElementStyle* toggleStyle)
  56. {
  57. return cm_new<GUIColorField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle,
  58. toggleStyle, GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  59. }
  60. GUIColorField* GUIColorField::create(GUIWidget& parent, const HString& labelContent, GUIElementStyle* labelStyle,
  61. GUIElementStyle* toggleStyle)
  62. {
  63. return cm_new<GUIColorField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle, toggleStyle,
  64. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  65. }
  66. GUIColorField* GUIColorField::create(GUIWidget& parent, const GUIOptions& layoutOptions, GUIElementStyle* labelStyle,
  67. GUIElementStyle* toggleStyle)
  68. {
  69. return cm_new<GUIColorField>(PrivatelyConstruct(), parent, labelStyle, toggleStyle,
  70. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  71. }
  72. GUIColorField* GUIColorField::create(GUIWidget& parent, GUIElementStyle* labelStyle, GUIElementStyle* toggleStyle)
  73. {
  74. return cm_new<GUIColorField>(PrivatelyConstruct(), parent, labelStyle, toggleStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  75. }
  76. Color GUIColorField::getValue() const
  77. {
  78. return mColor->getColor();
  79. }
  80. void GUIColorField::setValue(const CM::Color& color)
  81. {
  82. mColor->setColor(color);
  83. }
  84. void GUIColorField::setLabelWidth(UINT32 width)
  85. {
  86. mLabelWidth = width;
  87. markContentAsDirty();
  88. }
  89. void GUIColorField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  90. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  91. {
  92. UINT32 colorOffset = 0;
  93. UINT32 colorWidth = width;
  94. if(mLabel != nullptr)
  95. {
  96. UINT32 labelWidth = mLabelWidth;
  97. Vector2I optimalSize = mLabel->_getOptimalSize();
  98. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  99. Vector2I offset(x, y + yOffset);
  100. mLabel->_setOffset(offset);
  101. mLabel->_setWidth(labelWidth);
  102. mLabel->_setHeight(optimalSize.y);
  103. mLabel->_setAreaDepth(areaDepth);
  104. mLabel->_setWidgetDepth(widgetDepth);
  105. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  106. mLabel->_setClipRect(elemClipRect);
  107. colorOffset = labelWidth;
  108. colorWidth = width - labelWidth;
  109. }
  110. {
  111. Vector2I optimalSize = mColor->_getOptimalSize();
  112. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  113. Vector2I offset(x + colorOffset, y + yOffset);
  114. mColor->_setOffset(offset);
  115. mColor->_setWidth(colorWidth);
  116. mColor->_setHeight(optimalSize.y);
  117. mColor->_setAreaDepth(areaDepth);
  118. mColor->_setWidgetDepth(widgetDepth);
  119. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  120. mColor->_setClipRect(elemClipRect);
  121. }
  122. }
  123. Vector2I GUIColorField::_getOptimalSize() const
  124. {
  125. Vector2I optimalsize = mColor->_getOptimalSize();
  126. if(mLabel != nullptr)
  127. {
  128. optimalsize.x += mLabel->_getOptimalSize().x;
  129. optimalsize.y = std::max(optimalsize.y, mLabel->_getOptimalSize().y);
  130. }
  131. return optimalsize;
  132. }
  133. const String& GUIColorField::getGUITypeName()
  134. {
  135. static String typeName = "GUIColorField";
  136. return typeName;
  137. }
  138. }