BsGUIVector2Field.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "BsGUIVector2Field.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIFloatField.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. const UINT32 GUIVector2Field::ELEMENT_LABEL_WIDTH = 10;
  15. GUIVector2Field::GUIVector2Field(const PrivatelyConstruct& dummy, GUIWidget& parent, const GUIContent& labelContent,
  16. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  17. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mFieldX(nullptr), mFieldY(nullptr)
  18. {
  19. construct(parent, labelContent, labelStyle, inputBoxStyle, layoutOptions, true);
  20. }
  21. GUIVector2Field::GUIVector2Field(const PrivatelyConstruct& dummy, GUIWidget& parent,
  22. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  23. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mFieldX(nullptr), mFieldY(nullptr)
  24. {
  25. construct(parent, GUIContent(HString()), labelStyle, inputBoxStyle, layoutOptions, false);
  26. }
  27. GUIVector2Field::~GUIVector2Field()
  28. {
  29. }
  30. GUIVector2Field* GUIVector2Field::create(GUIWidget& parent, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  31. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  32. {
  33. return cm_new<GUIVector2Field>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  34. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  35. }
  36. GUIVector2Field* GUIVector2Field::create(GUIWidget& parent, const GUIContent& labelContent, GUIElementStyle* labelStyle,
  37. GUIElementStyle* inputBoxStyle)
  38. {
  39. return cm_new<GUIVector2Field>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  40. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  41. }
  42. GUIVector2Field* GUIVector2Field::create(GUIWidget& parent, const HString& labelContent, const GUIOptions& layoutOptions,
  43. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  44. {
  45. return cm_new<GUIVector2Field>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle,
  46. inputBoxStyle, GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  47. }
  48. GUIVector2Field* GUIVector2Field::create(GUIWidget& parent, const HString& labelContent, GUIElementStyle* labelStyle,
  49. GUIElementStyle* inputBoxStyle)
  50. {
  51. return cm_new<GUIVector2Field>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle, inputBoxStyle,
  52. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  53. }
  54. GUIVector2Field* GUIVector2Field::create(GUIWidget& parent, const GUIOptions& layoutOptions, GUIElementStyle* labelStyle,
  55. GUIElementStyle* inputBoxStyle)
  56. {
  57. return cm_new<GUIVector2Field>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle,
  58. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  59. }
  60. GUIVector2Field* GUIVector2Field::create(GUIWidget& parent, GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  61. {
  62. return cm_new<GUIVector2Field>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  63. }
  64. void GUIVector2Field::construct(GUIWidget& parent, const GUIContent& labelContent, GUIElementStyle* labelStyle,
  65. GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions, bool withLabel)
  66. {
  67. if(withLabel)
  68. {
  69. const GUIElementStyle* curLabelStyle = labelStyle;
  70. if(curLabelStyle == nullptr)
  71. curLabelStyle = parent.getSkin().getStyle("Label");
  72. mLabel = GUILabel::create(parent, labelContent, curLabelStyle);
  73. }
  74. mFieldX = GUIFloatField::create(parent, HString(L"X"), ELEMENT_LABEL_WIDTH, labelStyle, inputBoxStyle);
  75. mFieldY = GUIFloatField::create(parent, HString(L"Y"), ELEMENT_LABEL_WIDTH, labelStyle, inputBoxStyle);
  76. _registerChildElement(mLabel);
  77. _registerChildElement(mFieldX);
  78. _registerChildElement(mFieldY);
  79. }
  80. Vector2 GUIVector2Field::getValue() const
  81. {
  82. Vector2 value;
  83. value.x = mFieldX->getValue();
  84. value.y = mFieldY->getValue();
  85. return value;
  86. }
  87. void GUIVector2Field::setValue(const Vector2& value)
  88. {
  89. mFieldX->setValue(value.x);
  90. mFieldY->setValue(value.y);
  91. }
  92. void GUIVector2Field::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  93. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  94. {
  95. UINT32 inputBoxYOffset = 0;
  96. if(mLabel != nullptr)
  97. {
  98. Vector2I optimalSize = mLabel->_getOptimalSize();
  99. Vector2I offset(x, y);
  100. mLabel->_setOffset(offset);
  101. mLabel->_setWidth(width);
  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. inputBoxYOffset = optimalSize.y;
  108. }
  109. GUIFloatField* fields[] = { mFieldX, mFieldY };
  110. UINT32 numFields = sizeof(fields) / sizeof(fields[0]);
  111. UINT32 sizePerField = width / numFields;
  112. UINT32 inputBoxXOffset = 0;
  113. for(UINT32 i = 0; i < numFields; i++)
  114. {
  115. Vector2I optimalSize = fields[i]->_getOptimalSize();
  116. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  117. Vector2I offset(x + inputBoxXOffset, y + inputBoxYOffset);
  118. fields[i]->_setOffset(offset);
  119. fields[i]->_setWidth(sizePerField);
  120. fields[i]->_setHeight(optimalSize.y);
  121. fields[i]->_setAreaDepth(areaDepth);
  122. fields[i]->_setWidgetDepth(widgetDepth);
  123. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  124. fields[i]->_setClipRect(elemClipRect);
  125. RectI newClipRect(offset.x, offset.y, sizePerField, optimalSize.y);
  126. newClipRect.clip(clipRect);
  127. fields[i]->_updateLayoutInternal(offset.x, offset.y, sizePerField, optimalSize.y, newClipRect, widgetDepth, areaDepth);
  128. inputBoxXOffset += sizePerField;
  129. }
  130. }
  131. Vector2I GUIVector2Field::_getOptimalSize() const
  132. {
  133. GUIFloatField* fields[] = { mFieldX, mFieldY };
  134. UINT32 numFields = sizeof(fields) / sizeof(fields[0]);
  135. Vector2I optimalSize;
  136. for(UINT32 i = 0; i < numFields; i++)
  137. {
  138. optimalSize.x += fields[i]->_getOptimalSize().x;
  139. optimalSize.y = std::max(optimalSize.y, fields[i]->_getOptimalSize().y);
  140. }
  141. if(mLabel != nullptr)
  142. {
  143. optimalSize.x = std::max(optimalSize.x, mLabel->_getOptimalSize().x);
  144. optimalSize.y += mLabel->_getOptimalSize().y;
  145. }
  146. return optimalSize;
  147. }
  148. const String& GUIVector2Field::getGUITypeName()
  149. {
  150. static String typeName = "GUIVector2Field";
  151. return typeName;
  152. }
  153. }