BsGUIGameObjectField.cpp 9.4 KB

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