BsGUIGameObjectField.cpp 9.3 KB

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