BsGUIGameObjectField.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "BsCGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsGUISceneTreeView.h"
  10. #include "BsCGUIWidget.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. if (mInstanceId == value.getInstanceId())
  146. return;
  147. mInstanceId = value->getInstanceId();
  148. mDropButton->setContent(GUIContent(HString(toWString(value->getName()) + L" (" + toWString(mType) + L")")));
  149. }
  150. else
  151. {
  152. if (mInstanceId == 0)
  153. return;
  154. mInstanceId = 0;
  155. mDropButton->setContent(GUIContent(HString(L"None (" + toWString(mType) + L")")));
  156. }
  157. onValueChanged(value);
  158. }
  159. void GUIGameObjectField::setTint(const Color& color)
  160. {
  161. if (mLabel != nullptr)
  162. mLabel->setTint(color);
  163. mDropButton->setTint(color);
  164. mClearButton->setTint(color);
  165. }
  166. void GUIGameObjectField::_updateLayoutInternal(const GUILayoutData& data)
  167. {
  168. mLayout->_setLayoutData(data);
  169. mLayout->_updateLayoutInternal(data);
  170. }
  171. Vector2I GUIGameObjectField::_getOptimalSize() const
  172. {
  173. return mLayout->_getOptimalSize();
  174. }
  175. void GUIGameObjectField::onDropButtonClicked()
  176. {
  177. if (mInstanceId == 0)
  178. return;
  179. HGameObject go;
  180. if (GameObjectManager::instance().tryGetObject(mInstanceId, go))
  181. {
  182. HSceneObject so;
  183. if (rtti_is_of_type<SceneObject>(go.get()))
  184. {
  185. so = static_object_cast<SceneObject>(go);
  186. }
  187. else if(rtti_is_subclass<Component>(go.get()))
  188. {
  189. HComponent component = static_object_cast<Component>(go);
  190. so = component->SO();
  191. }
  192. Selection::instance().ping(so);
  193. }
  194. }
  195. void GUIGameObjectField::dataDropped(void* data)
  196. {
  197. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(data);
  198. if (draggedSceneObjects->numObjects <= 0)
  199. return;
  200. MonoClass* sceneObjectClass = ScriptAssemblyManager::instance().getSceneObjectClass();
  201. if (mType == sceneObjectClass->getFullName()) // A scene object
  202. {
  203. setValue(draggedSceneObjects->objects[0]);
  204. }
  205. else // A component
  206. {
  207. for (UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  208. {
  209. HSceneObject so = draggedSceneObjects->objects[i];
  210. const Vector<HComponent>& components = so->getComponents();
  211. for (auto& component : components)
  212. {
  213. if (component->getTypeId() == TID_ManagedComponent) // We only care about managed components
  214. {
  215. HManagedComponent managedComponent = static_object_cast<ManagedComponent>(component);
  216. MonoClass* acceptedClass = MonoManager::instance().findClass(mNamespace, mType);
  217. MonoClass* providedClass = MonoManager::instance().findClass(managedComponent->getManagedNamespace(), managedComponent->getManagedTypeName());
  218. if (acceptedClass != nullptr && providedClass != nullptr)
  219. {
  220. if (providedClass->isSubClassOf(acceptedClass))
  221. {
  222. setValue(managedComponent);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. void GUIGameObjectField::styleUpdated()
  231. {
  232. if (mLabel != nullptr)
  233. mLabel->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
  234. mDropButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
  235. mClearButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
  236. }
  237. void GUIGameObjectField::onClearButtonClicked()
  238. {
  239. setValue(HGameObject());
  240. }
  241. const String& GUIGameObjectField::getGUITypeName()
  242. {
  243. static String typeName = "GUIGameObjectField";
  244. return typeName;
  245. }
  246. }