BsGUIResourceField.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "BsGUIResourceField.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 "BsGUIResourceTreeView.h"
  11. #include "BsGUIWidget.h"
  12. #include "BsGameObjectManager.h"
  13. #include "BsRuntimeScriptObjects.h"
  14. #include "BsMonoClass.h"
  15. #include "BsResources.h"
  16. #include "BsProjectLibrary.h"
  17. #include "BsProjectResourceMeta.h"
  18. #include "BsManagedResourceMetaData.h"
  19. #include "BsEditorGUI.h"
  20. using namespace std::placeholders;
  21. namespace BansheeEngine
  22. {
  23. const UINT32 GUIResourceField::DEFAULT_LABEL_WIDTH = 100;
  24. GUIResourceField::GUIResourceField(const PrivatelyConstruct& dummy, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  25. const String& style, const GUILayoutOptions& layoutOptions, bool withLabel)
  26. :GUIElementContainer(layoutOptions, style), mLabel(nullptr), mClearButton(nullptr), mDropButton(nullptr), mType(type)
  27. {
  28. mLayout = &addLayoutXInternal(this);
  29. if (withLabel)
  30. {
  31. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(EditorGUI::ObjectFieldLabelStyleName));
  32. mLayout->addElement(mLabel);
  33. }
  34. mDropButton = GUIDropButton::create((UINT32)DragAndDropType::SceneObject, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(EditorGUI::ObjectFieldDropBtnStyleName));
  35. mClearButton = GUIButton::create(HString(L""), getSubStyleName(EditorGUI::ObjectFieldClearBtnStyleName));
  36. mLayout->addElement(mDropButton);
  37. mLayout->addElement(mClearButton);
  38. mDropButton->onDataDropped.connect(std::bind(&GUIResourceField::dataDropped, this, _1));
  39. }
  40. GUIResourceField::~GUIResourceField()
  41. {
  42. }
  43. GUIResourceField* GUIResourceField::create(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 = &EditorGUI::ObjectFieldStyleName;
  49. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, labelWidth, *curStyle,
  50. GUILayoutOptions::create(layoutOptions), true);
  51. }
  52. GUIResourceField* GUIResourceField::create(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 = &EditorGUI::ObjectFieldStyleName;
  58. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  59. GUILayoutOptions::create(layoutOptions), true);
  60. }
  61. GUIResourceField* GUIResourceField::create(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 = &EditorGUI::ObjectFieldStyleName;
  67. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), labelWidth, *curStyle,
  68. GUILayoutOptions::create(layoutOptions), true);
  69. }
  70. GUIResourceField* GUIResourceField::create(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 = &EditorGUI::ObjectFieldStyleName;
  76. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  77. GUILayoutOptions::create(layoutOptions), true);
  78. }
  79. GUIResourceField* GUIResourceField::create(const String& type, const GUIOptions& layoutOptions, const String& style)
  80. {
  81. const String* curStyle = &style;
  82. if (*curStyle == StringUtil::BLANK)
  83. curStyle = &EditorGUI::ObjectFieldStyleName;
  84. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(), 0, *curStyle,
  85. GUILayoutOptions::create(layoutOptions), false);
  86. }
  87. GUIResourceField* GUIResourceField::create(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 = &EditorGUI::ObjectFieldStyleName;
  93. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, labelWidth, *curStyle,
  94. GUILayoutOptions::create(), true);
  95. }
  96. GUIResourceField* GUIResourceField::create(const String& type, const GUIContent& labelContent,
  97. const String& style)
  98. {
  99. const String* curStyle = &style;
  100. if (*curStyle == StringUtil::BLANK)
  101. curStyle = &EditorGUI::ObjectFieldStyleName;
  102. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  103. GUILayoutOptions::create(), true);
  104. }
  105. GUIResourceField* GUIResourceField::create(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 = &EditorGUI::ObjectFieldStyleName;
  111. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), labelWidth, *curStyle,
  112. GUILayoutOptions::create(), true);
  113. }
  114. GUIResourceField* GUIResourceField::create(const String& type, const HString& labelText,
  115. const String& style)
  116. {
  117. const String* curStyle = &style;
  118. if (*curStyle == StringUtil::BLANK)
  119. curStyle = &EditorGUI::ObjectFieldStyleName;
  120. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  121. GUILayoutOptions::create(), true);
  122. }
  123. GUIResourceField* GUIResourceField::create(const String& type, const String& style)
  124. {
  125. const String* curStyle = &style;
  126. if (*curStyle == StringUtil::BLANK)
  127. curStyle = &EditorGUI::ObjectFieldStyleName;
  128. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(), 0, *curStyle,
  129. GUILayoutOptions::create(), false);
  130. }
  131. HResource GUIResourceField::getValue() const
  132. {
  133. return Resources::instance().loadFromUUID(mUUID);
  134. }
  135. void GUIResourceField::setValue(const HResource& value)
  136. {
  137. if (value)
  138. setUUID(value.getUUID());
  139. else
  140. setUUID("");
  141. }
  142. void GUIResourceField::setUUID(const String& uuid)
  143. {
  144. mUUID = uuid;
  145. Path filePath;
  146. if (Resources::instance().getFilePathFromUUID(mUUID, filePath))
  147. {
  148. mDropButton->setContent(GUIContent(filePath.getFilename(false)));
  149. }
  150. else
  151. mDropButton->setContent(GUIContent(HString(L"None")));
  152. onValueChanged(mUUID);
  153. }
  154. void GUIResourceField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  155. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  156. {
  157. mLayout->_updateLayoutInternal(x, y, width, height, clipRect, widgetDepth, areaDepth);
  158. }
  159. Vector2I GUIResourceField::_getOptimalSize() const
  160. {
  161. return mLayout->_getOptimalSize();
  162. }
  163. void GUIResourceField::dataDropped(void* data)
  164. {
  165. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(data);
  166. UINT32 numResources = (UINT32)draggedResources->resourceUUIDs.size();
  167. if (numResources <= 0)
  168. return;
  169. for (UINT32 i = 0; i < numResources; i++)
  170. {
  171. String uuid = draggedResources->resourceUUIDs[i];
  172. ProjectResourceMetaPtr meta = ProjectLibrary::instance().findResourceMeta(uuid);
  173. if (meta == nullptr)
  174. continue;
  175. bool found = false;
  176. UINT32 typeId = meta->getTypeID();
  177. switch (typeId)
  178. {
  179. case TID_Texture:
  180. {
  181. const String& texTypeName = RuntimeScriptObjects::instance().getTextureClass()->getFullName();
  182. if (texTypeName == mType)
  183. {
  184. setUUID(uuid);
  185. found = true;
  186. }
  187. }
  188. break;
  189. case TID_SpriteTexture:
  190. {
  191. const String& spriteTexTypeName = RuntimeScriptObjects::instance().getSpriteTextureClass()->getFullName();
  192. if (spriteTexTypeName == mType)
  193. {
  194. setUUID(uuid);
  195. found = true;
  196. }
  197. }
  198. break;
  199. case TID_ManagedResource:
  200. {
  201. ManagedResourceMetaDataPtr managedResMetaData = std::static_pointer_cast<ManagedResourceMetaData>(meta->getResourceMetaData());
  202. String fullTypeName = managedResMetaData->typeNamespace + "." + managedResMetaData->typeName;
  203. if (fullTypeName == mType)
  204. {
  205. setUUID(uuid);
  206. found = true;
  207. }
  208. }
  209. break;
  210. default:
  211. BS_EXCEPT(NotImplementedException, "Unsupported resource type added to resource field.");
  212. }
  213. if (found)
  214. break;
  215. }
  216. }
  217. void GUIResourceField::styleUpdated()
  218. {
  219. if (mLabel != nullptr)
  220. mLabel->setStyle(getSubStyleName(EditorGUI::ObjectFieldLabelStyleName));
  221. mDropButton->setStyle(getSubStyleName(EditorGUI::ObjectFieldDropBtnStyleName));
  222. mClearButton->setStyle(getSubStyleName(EditorGUI::ObjectFieldClearBtnStyleName));
  223. }
  224. const String& GUIResourceField::getGUITypeName()
  225. {
  226. static String typeName = "GUIResourceField";
  227. return typeName;
  228. }
  229. }