BsGUIResourceField.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGUIResourceField.h"
  4. #include "BsGUILayoutX.h"
  5. #include "BsGUILabel.h"
  6. #include "BsGUIDropButton.h"
  7. #include "BsGUIButton.h"
  8. #include "BsBuiltinResources.h"
  9. #include "BsGUIResourceTreeView.h"
  10. #include "BsMonoClass.h"
  11. #include "BsMonoManager.h"
  12. #include "BsResources.h"
  13. #include "BsProjectLibrary.h"
  14. #include "BsProjectResourceMeta.h"
  15. #include "BsManagedResourceMetaData.h"
  16. #include "BsBuiltinEditorResources.h"
  17. #include "BsScriptManagedResource.h"
  18. #include "BsSelection.h"
  19. using namespace std::placeholders;
  20. namespace BansheeEngine
  21. {
  22. const UINT32 GUIResourceField::DEFAULT_LABEL_WIDTH = 100;
  23. GUIResourceField::GUIResourceField(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), 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::Resources, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
  35. mClearButton = GUIButton::create(HString(L""), getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
  36. mClearButton->onClick.connect(std::bind(&GUIResourceField::onClearButtonClicked, this));
  37. mLayout->addElement(mDropButton);
  38. mLayout->addElement(mClearButton);
  39. mDropButton->onDataDropped.connect(std::bind(&GUIResourceField::dataDropped, this, _1));
  40. mDropButton->onClick.connect(std::bind(&GUIResourceField::onDropButtonClicked, this));
  41. }
  42. GUIResourceField::~GUIResourceField()
  43. {
  44. }
  45. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
  52. GUIDimensions::create(options), true);
  53. }
  54. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  61. GUIDimensions::create(options), true);
  62. }
  63. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), labelWidth, *curStyle,
  70. GUIDimensions::create(options), true);
  71. }
  72. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  79. GUIDimensions::create(options), true);
  80. }
  81. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
  87. GUIDimensions::create(options), false);
  88. }
  89. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
  96. GUIDimensions::create(), true);
  97. }
  98. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  105. GUIDimensions::create(), true);
  106. }
  107. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), labelWidth, *curStyle,
  114. GUIDimensions::create(), true);
  115. }
  116. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  123. GUIDimensions::create(), true);
  124. }
  125. GUIResourceField* GUIResourceField::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<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
  131. GUIDimensions::create(), false);
  132. }
  133. HResource GUIResourceField::getValue() const
  134. {
  135. if (!mUUID.empty())
  136. return gResources().loadFromUUID(mUUID);
  137. return HResource();
  138. }
  139. void GUIResourceField::setValue(const HResource& value)
  140. {
  141. if (value)
  142. {
  143. Path resPath = gProjectLibrary().uuidToPath(value.getUUID());
  144. if (!resPath.isEmpty() || !value.isLoaded(false))
  145. setUUID(value.getUUID(), false);
  146. else // A non-project library resource
  147. {
  148. if (mUUID == value.getUUID())
  149. return;
  150. mUUID = value.getUUID();
  151. WString title = value->getName() + L" (" + toWString(mType) + L")";
  152. mDropButton->setContent(GUIContent(HEString(title)));
  153. }
  154. }
  155. else
  156. setUUID("", false);
  157. }
  158. WeakResourceHandle<Resource> GUIResourceField::getValueWeak() const
  159. {
  160. if (!mUUID.empty())
  161. return Resources::instance()._getResourceHandle(mUUID).getWeak();
  162. return WeakResourceHandle<Resource>();
  163. }
  164. void GUIResourceField::setValueWeak(const WeakResourceHandle<Resource>& value)
  165. {
  166. if (value)
  167. {
  168. Path resPath = gProjectLibrary().uuidToPath(value.getUUID());
  169. if (!resPath.isEmpty() || !value.isLoaded(false))
  170. setUUID(value.getUUID(), false);
  171. else // A non-project library resource
  172. {
  173. if (mUUID == value.getUUID())
  174. return;
  175. mUUID = value.getUUID();
  176. WString title = value->getName() + L" (" + toWString(mType) + L")";
  177. mDropButton->setContent(GUIContent(HEString(title)));
  178. }
  179. }
  180. else
  181. setUUID("", false);
  182. }
  183. void GUIResourceField::setUUID(const String& uuid, bool triggerEvent)
  184. {
  185. if (mUUID == uuid)
  186. return;
  187. mUUID = uuid;
  188. Path resPath = gProjectLibrary().uuidToPath(mUUID);
  189. if (!resPath.isEmpty())
  190. {
  191. WString title = resPath.getWFilename(false) + L" (" + toWString(mType) + L")";
  192. mDropButton->setContent(GUIContent(HEString(title)));
  193. }
  194. else
  195. mDropButton->setContent(GUIContent(HEString(L"None (" + toWString(mType) + L")")));
  196. if (triggerEvent)
  197. {
  198. HResource handle = gResources()._getResourceHandle(mUUID);
  199. onValueChanged(handle.getWeak());
  200. }
  201. }
  202. void GUIResourceField::setTint(const Color& color)
  203. {
  204. if (mLabel != nullptr)
  205. mLabel->setTint(color);
  206. mDropButton->setTint(color);
  207. mClearButton->setTint(color);
  208. }
  209. void GUIResourceField::_updateLayoutInternal(const GUILayoutData& data)
  210. {
  211. mLayout->_setLayoutData(data);
  212. mLayout->_updateLayoutInternal(data);
  213. }
  214. Vector2I GUIResourceField::_getOptimalSize() const
  215. {
  216. return mLayout->_getOptimalSize();
  217. }
  218. void GUIResourceField::onDropButtonClicked()
  219. {
  220. if (mUUID == "")
  221. return;
  222. Path resPath = gProjectLibrary().uuidToPath(mUUID);
  223. resPath = resPath.getRelative(gProjectLibrary().getResourcesFolder());
  224. Selection::instance().ping(resPath);
  225. }
  226. void GUIResourceField::dataDropped(void* data)
  227. {
  228. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(data);
  229. UINT32 numResources = (UINT32)draggedResources->resourcePaths.size();
  230. if (numResources <= 0)
  231. return;
  232. MonoClass* acceptedClass = MonoManager::instance().findClass(mNamespace, mType);
  233. for (UINT32 i = 0; i < numResources; i++)
  234. {
  235. Path path = draggedResources->resourcePaths[i];
  236. ProjectLibrary::LibraryEntry* libEntry = gProjectLibrary().findEntry(path);
  237. if (libEntry == nullptr || libEntry->type == ProjectLibrary::LibraryEntryType::Directory)
  238. continue;
  239. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(libEntry);
  240. ProjectResourceMetaPtr meta = resEntry->meta;
  241. if (meta == nullptr)
  242. continue;
  243. UINT32 typeId = meta->getTypeID();
  244. String uuid = meta->getUUID();
  245. bool found = false;
  246. MonoClass* scriptClass = ScriptResource::getClassFromTypeId(typeId);
  247. if (scriptClass != nullptr)
  248. {
  249. if (scriptClass->isSubClassOf(acceptedClass))
  250. {
  251. setUUID(uuid);
  252. found = true;
  253. }
  254. }
  255. else if (typeId == TID_ManagedResource)
  256. {
  257. ManagedResourceMetaDataPtr managedResMetaData = std::static_pointer_cast<ManagedResourceMetaData>(meta->getResourceMetaData());
  258. MonoClass* providedClass = MonoManager::instance().findClass(managedResMetaData->typeNamespace, managedResMetaData->typeName);
  259. if (providedClass->isSubClassOf(acceptedClass))
  260. {
  261. setUUID(uuid);
  262. found = true;
  263. }
  264. }
  265. else
  266. {
  267. BS_EXCEPT(NotImplementedException, "Unsupported resource type added to resource field.");
  268. }
  269. if (found)
  270. break;
  271. }
  272. }
  273. void GUIResourceField::styleUpdated()
  274. {
  275. if (mLabel != nullptr)
  276. mLabel->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
  277. mDropButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
  278. mClearButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
  279. }
  280. void GUIResourceField::onClearButtonClicked()
  281. {
  282. setUUID(StringUtil::BLANK);
  283. }
  284. const String& GUIResourceField::getGUITypeName()
  285. {
  286. static String typeName = "GUIResourceField";
  287. return typeName;
  288. }
  289. }