BsGUIToggleField.cpp 5.7 KB

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