BsGUIGameObjectField.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "BsGUIGameObjectField.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIDropButton.h"
  6. #include "BsGUIButton.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsGUIWidget.h"
  9. #include "BsGUIMouseEvent.h"
  10. #include "BsGUISceneTreeView.h"
  11. #include "BsGUIWidget.h"
  12. #include "BsGameObjectManager.h"
  13. #include "BsRuntimeScriptObjects.h"
  14. #include "BsMonoClass.h"
  15. #include "BsSceneObject.h"
  16. #include "BsManagedComponent.h"
  17. #include "BsEditorGUI.h"
  18. using namespace std::placeholders;
  19. namespace BansheeEngine
  20. {
  21. const UINT32 GUIGameObjectField::DEFAULT_LABEL_WIDTH = 100;
  22. GUIGameObjectField::GUIGameObjectField(const PrivatelyConstruct& dummy, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  23. const String& style, const GUILayoutOptions& layoutOptions, bool withLabel)
  24. :GUIElementContainer(layoutOptions, style), mLabel(nullptr), mClearButton(nullptr), mDropButton(nullptr), mInstanceId(0), mType(type)
  25. {
  26. mLayout = &addLayoutXInternal(this);
  27. if(withLabel)
  28. {
  29. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(EditorGUI::ObjectFieldLabelStyleName));
  30. mLayout->addElement(mLabel);
  31. }
  32. mDropButton = GUIDropButton::create((UINT32)DragAndDropType::SceneObject, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(EditorGUI::ObjectFieldDropBtnStyleName));
  33. mClearButton = GUIButton::create(HString(L""), getSubStyleName(EditorGUI::ObjectFieldClearBtnStyleName));
  34. mLayout->addElement(mDropButton);
  35. mLayout->addElement(mClearButton);
  36. mDropButton->onDataDropped.connect(std::bind(&GUIGameObjectField::dataDropped, this, _1));
  37. }
  38. GUIGameObjectField::~GUIGameObjectField()
  39. {
  40. }
  41. GUIGameObjectField* GUIGameObjectField::create(const String& type, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& layoutOptions,
  42. const String& style)
  43. {
  44. const String* curStyle = &style;
  45. if (*curStyle == StringUtil::BLANK)
  46. curStyle = &EditorGUI::ObjectFieldStyleName;
  47. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, labelContent, labelWidth, *curStyle,
  48. GUILayoutOptions::create(layoutOptions), true);
  49. }
  50. GUIGameObjectField* GUIGameObjectField::create(const String& type, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  51. const String& style)
  52. {
  53. const String* curStyle = &style;
  54. if (*curStyle == StringUtil::BLANK)
  55. curStyle = &EditorGUI::ObjectFieldStyleName;
  56. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  57. GUILayoutOptions::create(layoutOptions), true);
  58. }
  59. GUIGameObjectField* GUIGameObjectField::create(const String& type, const HString& labelText, UINT32 labelWidth, const GUIOptions& layoutOptions,
  60. const String& style)
  61. {
  62. const String* curStyle = &style;
  63. if (*curStyle == StringUtil::BLANK)
  64. curStyle = &EditorGUI::ObjectFieldStyleName;
  65. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, GUIContent(labelText), labelWidth, *curStyle,
  66. GUILayoutOptions::create(layoutOptions), true);
  67. }
  68. GUIGameObjectField* GUIGameObjectField::create(const String& type, const HString& labelText, const GUIOptions& layoutOptions,
  69. const String& style)
  70. {
  71. const String* curStyle = &style;
  72. if (*curStyle == StringUtil::BLANK)
  73. curStyle = &EditorGUI::ObjectFieldStyleName;
  74. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  75. GUILayoutOptions::create(layoutOptions), true);
  76. }
  77. GUIGameObjectField* GUIGameObjectField::create(const String& type, const GUIOptions& layoutOptions, const String& style)
  78. {
  79. const String* curStyle = &style;
  80. if (*curStyle == StringUtil::BLANK)
  81. curStyle = &EditorGUI::ObjectFieldStyleName;
  82. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, GUIContent(), 0, *curStyle,
  83. GUILayoutOptions::create(layoutOptions), false);
  84. }
  85. GUIGameObjectField* GUIGameObjectField::create(const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  86. const String& style)
  87. {
  88. const String* curStyle = &style;
  89. if (*curStyle == StringUtil::BLANK)
  90. curStyle = &EditorGUI::ObjectFieldStyleName;
  91. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, labelContent, labelWidth, *curStyle,
  92. GUILayoutOptions::create(), true);
  93. }
  94. GUIGameObjectField* GUIGameObjectField::create(const String& type, const GUIContent& labelContent,
  95. const String& style)
  96. {
  97. const String* curStyle = &style;
  98. if (*curStyle == StringUtil::BLANK)
  99. curStyle = &EditorGUI::ObjectFieldStyleName;
  100. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  101. GUILayoutOptions::create(), true);
  102. }
  103. GUIGameObjectField* GUIGameObjectField::create(const String& type, const HString& labelText, UINT32 labelWidth,
  104. const String& style)
  105. {
  106. const String* curStyle = &style;
  107. if (*curStyle == StringUtil::BLANK)
  108. curStyle = &EditorGUI::ObjectFieldStyleName;
  109. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, GUIContent(labelText), labelWidth, *curStyle,
  110. GUILayoutOptions::create(), true);
  111. }
  112. GUIGameObjectField* GUIGameObjectField::create(const String& type, const HString& labelText,
  113. const String& style)
  114. {
  115. const String* curStyle = &style;
  116. if (*curStyle == StringUtil::BLANK)
  117. curStyle = &EditorGUI::ObjectFieldStyleName;
  118. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  119. GUILayoutOptions::create(), true);
  120. }
  121. GUIGameObjectField* GUIGameObjectField::create(const String& type, const String& style)
  122. {
  123. const String* curStyle = &style;
  124. if (*curStyle == StringUtil::BLANK)
  125. curStyle = &EditorGUI::ObjectFieldStyleName;
  126. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), type, GUIContent(), 0, *curStyle,
  127. GUILayoutOptions::create(), false);
  128. }
  129. HGameObject GUIGameObjectField::getValue() const
  130. {
  131. HGameObject obj;
  132. if(mInstanceId != 0)
  133. GameObjectManager::instance().tryGetObject(mInstanceId, obj);
  134. return obj;
  135. }
  136. void GUIGameObjectField::setValue(const HGameObject& value)
  137. {
  138. if(value)
  139. {
  140. mInstanceId = value->getInstanceId();
  141. mDropButton->setContent(GUIContent(HString(toWString(value->getName()))));
  142. }
  143. else
  144. {
  145. mInstanceId = 0;
  146. mDropButton->setContent(GUIContent(HString(L"None")));
  147. }
  148. onValueChanged(value);
  149. }
  150. void GUIGameObjectField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  151. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  152. {
  153. mLayout->_updateLayoutInternal(x, y, width, height, clipRect, widgetDepth, areaDepth);
  154. }
  155. Vector2I GUIGameObjectField::_getOptimalSize() const
  156. {
  157. return mLayout->_getOptimalSize();
  158. }
  159. void GUIGameObjectField::dataDropped(void* data)
  160. {
  161. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(data);
  162. if (draggedSceneObjects->numObjects <= 0)
  163. return;
  164. MonoClass* sceneObjectClass = RuntimeScriptObjects::instance().getSceneObjectClass();
  165. if (mType == sceneObjectClass->getFullName()) // A scene object
  166. {
  167. setValue(draggedSceneObjects->objects[0]);
  168. }
  169. else // A component
  170. {
  171. for (UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  172. {
  173. HSceneObject so = draggedSceneObjects->objects[i];
  174. const Vector<HComponent>& components = so->getComponents();
  175. for (auto& component : components)
  176. {
  177. if (component->getTypeId() == TID_ManagedComponent) // We only care about managed components
  178. {
  179. HManagedComponent managedComponent = static_object_cast<ManagedComponent>(component);
  180. if (managedComponent->getManagedFullTypeName() == mType)
  181. {
  182. setValue(managedComponent);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. void GUIGameObjectField::styleUpdated()
  190. {
  191. if (mLabel != nullptr)
  192. mLabel->setStyle(getSubStyleName(EditorGUI::ObjectFieldLabelStyleName));
  193. mDropButton->setStyle(getSubStyleName(EditorGUI::ObjectFieldDropBtnStyleName));
  194. mClearButton->setStyle(getSubStyleName(EditorGUI::ObjectFieldClearBtnStyleName));
  195. }
  196. const String& GUIGameObjectField::getGUITypeName()
  197. {
  198. static String typeName = "GUIGameObjectField";
  199. return typeName;
  200. }
  201. }