BsGUIGameObjectField.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "BsGUIGameObjectField.h"
  2. #include "BsGUILayoutX.h"
  3. #include "BsGUILabel.h"
  4. #include "BsGUIDropButton.h"
  5. #include "BsGUIButton.h"
  6. #include "BsBuiltinResources.h"
  7. #include "BsGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsGUISceneTreeView.h"
  10. #include "BsGUIWidget.h"
  11. #include "BsGameObjectManager.h"
  12. #include "BsScriptAssemblyManager.h"
  13. #include "BsMonoClass.h"
  14. #include "BsSceneObject.h"
  15. #include "BsManagedComponent.h"
  16. #include "BsMonoManager.h"
  17. #include "BsBuiltinEditorResources.h"
  18. #include "BsComponent.h"
  19. #include "BsSelection.h"
  20. using namespace std::placeholders;
  21. namespace BansheeEngine
  22. {
  23. const UINT32 GUIGameObjectField::DEFAULT_LABEL_WIDTH = 100;
  24. GUIGameObjectField::GUIGameObjectField(const PrivatelyConstruct& dummy, const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  25. const String& style, const GUIDimensions& dimensions, bool withLabel)
  26. :GUIElementContainer(dimensions, style), mLabel(nullptr), mClearButton(nullptr), mDropButton(nullptr), mInstanceId(0), mType(type), mNamespace(typeNamespace)
  27. {
  28. mLayout = GUILayoutX::create();
  29. _registerChildElement(mLayout);
  30. if(withLabel)
  31. {
  32. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
  33. mLayout->addElement(mLabel);
  34. }
  35. mDropButton = GUIDropButton::create((UINT32)DragAndDropType::SceneObject, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
  36. mClearButton = GUIButton::create(HString(L""), getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
  37. mClearButton->onClick.connect(std::bind(&GUIGameObjectField::onClearButtonClicked, this));
  38. mLayout->addElement(mDropButton);
  39. mLayout->addElement(mClearButton);
  40. mDropButton->onDataDropped.connect(std::bind(&GUIGameObjectField::dataDropped, this, _1));
  41. mDropButton->onClick.connect(std::bind(&GUIGameObjectField::onDropButtonClicked, this));
  42. }
  43. GUIGameObjectField::~GUIGameObjectField()
  44. {
  45. }
  46. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& options,
  47. const String& style)
  48. {
  49. const String* curStyle = &style;
  50. if (*curStyle == StringUtil::BLANK)
  51. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  52. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
  53. GUIDimensions::create(options), true);
  54. }
  55. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, const GUIOptions& options,
  56. const String& style)
  57. {
  58. const String* curStyle = &style;
  59. if (*curStyle == StringUtil::BLANK)
  60. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  61. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  62. GUIDimensions::create(options), true);
  63. }
  64. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
  65. const String& style)
  66. {
  67. const String* curStyle = &style;
  68. if (*curStyle == StringUtil::BLANK)
  69. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  70. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), labelWidth, *curStyle,
  71. GUIDimensions::create(options), true);
  72. }
  73. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText, const GUIOptions& options,
  74. const String& style)
  75. {
  76. const String* curStyle = &style;
  77. if (*curStyle == StringUtil::BLANK)
  78. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  79. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  80. GUIDimensions::create(options), true);
  81. }
  82. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIOptions& options, const String& style)
  83. {
  84. const String* curStyle = &style;
  85. if (*curStyle == StringUtil::BLANK)
  86. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  87. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
  88. GUIDimensions::create(options), false);
  89. }
  90. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  91. const String& style)
  92. {
  93. const String* curStyle = &style;
  94. if (*curStyle == StringUtil::BLANK)
  95. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  96. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
  97. GUIDimensions::create(), true);
  98. }
  99. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent,
  100. const String& style)
  101. {
  102. const String* curStyle = &style;
  103. if (*curStyle == StringUtil::BLANK)
  104. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  105. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  106. GUIDimensions::create(), true);
  107. }
  108. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText, UINT32 labelWidth,
  109. const String& style)
  110. {
  111. const String* curStyle = &style;
  112. if (*curStyle == StringUtil::BLANK)
  113. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  114. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), labelWidth, *curStyle,
  115. GUIDimensions::create(), true);
  116. }
  117. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText,
  118. const String& style)
  119. {
  120. const String* curStyle = &style;
  121. if (*curStyle == StringUtil::BLANK)
  122. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  123. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  124. GUIDimensions::create(), true);
  125. }
  126. GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const String& style)
  127. {
  128. const String* curStyle = &style;
  129. if (*curStyle == StringUtil::BLANK)
  130. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  131. return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
  132. GUIDimensions::create(), false);
  133. }
  134. HGameObject GUIGameObjectField::getValue() const
  135. {
  136. HGameObject obj;
  137. if(mInstanceId != 0)
  138. GameObjectManager::instance().tryGetObject(mInstanceId, obj);
  139. return obj;
  140. }
  141. void GUIGameObjectField::setValue(const HGameObject& value)
  142. {
  143. if(value)
  144. {
  145. mInstanceId = value->getInstanceId();
  146. mDropButton->setContent(GUIContent(HString(toWString(value->getName()) + L" (" + toWString(mType) + L")")));
  147. }
  148. else
  149. {
  150. mInstanceId = 0;
  151. mDropButton->setContent(GUIContent(HString(L"None (" + toWString(mType) + L")")));
  152. }
  153. onValueChanged(value);
  154. }
  155. void GUIGameObjectField::setTint(const Color& color)
  156. {
  157. if (mLabel != nullptr)
  158. mLabel->setTint(color);
  159. mDropButton->setTint(color);
  160. mClearButton->setTint(color);
  161. }
  162. void GUIGameObjectField::_updateLayoutInternal(const GUILayoutData& data)
  163. {
  164. mLayout->_setLayoutData(data);
  165. mLayout->_updateLayoutInternal(data);
  166. }
  167. Vector2I GUIGameObjectField::_getOptimalSize() const
  168. {
  169. return mLayout->_getOptimalSize();
  170. }
  171. void GUIGameObjectField::onDropButtonClicked()
  172. {
  173. if (mInstanceId == 0)
  174. return;
  175. HGameObject go;
  176. if (GameObjectManager::instance().tryGetObject(mInstanceId, go))
  177. {
  178. HSceneObject so;
  179. if (rtti_is_of_type<SceneObject>(go.get()))
  180. {
  181. so = static_object_cast<SceneObject>(go);
  182. }
  183. else if(rtti_is_subclass<Component>(go.get()))
  184. {
  185. HComponent component = static_object_cast<Component>(go);
  186. so = component->SO();
  187. }
  188. Selection::instance().ping(so);
  189. }
  190. }
  191. void GUIGameObjectField::dataDropped(void* data)
  192. {
  193. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(data);
  194. if (draggedSceneObjects->numObjects <= 0)
  195. return;
  196. MonoClass* sceneObjectClass = ScriptAssemblyManager::instance().getSceneObjectClass();
  197. if (mType == sceneObjectClass->getFullName()) // A scene object
  198. {
  199. setValue(draggedSceneObjects->objects[0]);
  200. }
  201. else // A component
  202. {
  203. for (UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  204. {
  205. HSceneObject so = draggedSceneObjects->objects[i];
  206. const Vector<HComponent>& components = so->getComponents();
  207. for (auto& component : components)
  208. {
  209. if (component->getTypeId() == TID_ManagedComponent) // We only care about managed components
  210. {
  211. HManagedComponent managedComponent = static_object_cast<ManagedComponent>(component);
  212. MonoClass* acceptedClass = MonoManager::instance().findClass(mNamespace, mType);
  213. MonoClass* providedClass = MonoManager::instance().findClass(managedComponent->getManagedNamespace(), managedComponent->getManagedTypeName());
  214. if (acceptedClass != nullptr && providedClass != nullptr)
  215. {
  216. if (providedClass->isSubClassOf(acceptedClass))
  217. {
  218. setValue(managedComponent);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. void GUIGameObjectField::styleUpdated()
  227. {
  228. if (mLabel != nullptr)
  229. mLabel->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
  230. mDropButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
  231. mClearButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
  232. }
  233. void GUIGameObjectField::onClearButtonClicked()
  234. {
  235. setValue(HGameObject());
  236. }
  237. const String& GUIGameObjectField::getGUITypeName()
  238. {
  239. static String typeName = "GUIGameObjectField";
  240. return typeName;
  241. }
  242. }