BsGUIColorField.cpp 4.9 KB

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