BsGUIGameObjectField.cpp 11 KB

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