BsGUIResourceField.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include "BsGUIResourceField.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 "BsGUIResourceTreeView.h"
  10. #include "BsCGUIWidget.h"
  11. #include "BsGameObjectManager.h"
  12. #include "BsScriptAssemblyManager.h"
  13. #include "BsMonoClass.h"
  14. #include "BsMonoManager.h"
  15. #include "BsResources.h"
  16. #include "BsProjectLibrary.h"
  17. #include "BsProjectResourceMeta.h"
  18. #include "BsManagedResourceMetaData.h"
  19. #include "BsBuiltinEditorResources.h"
  20. #include "BsScriptTexture2D.h"
  21. #include "BsScriptTexture3D.h"
  22. #include "BsScriptTextureCube.h"
  23. #include "BsScriptSpriteTexture.h"
  24. #include "BsScriptMaterial.h"
  25. #include "BsScriptMesh.h"
  26. #include "BsScriptFont.h"
  27. #include "BsScriptShader.h"
  28. #include "BsScriptShaderInclude.h"
  29. #include "BsScriptPlainText.h"
  30. #include "BsScriptScriptCode.h"
  31. #include "BsScriptStringTable.h"
  32. #include "BsScriptGUISkin.h"
  33. #include "BsScriptPrefab.h"
  34. #include "BsScriptManagedResource.h"
  35. #include "BsSelection.h"
  36. using namespace std::placeholders;
  37. namespace BansheeEngine
  38. {
  39. const UINT32 GUIResourceField::DEFAULT_LABEL_WIDTH = 100;
  40. GUIResourceField::GUIResourceField(const PrivatelyConstruct& dummy, const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  41. const String& style, const GUIDimensions& dimensions, bool withLabel)
  42. :GUIElementContainer(dimensions, style), mLabel(nullptr), mClearButton(nullptr), mDropButton(nullptr), mType(type), mNamespace(typeNamespace)
  43. {
  44. mLayout = GUILayoutX::create();
  45. _registerChildElement(mLayout);
  46. if (withLabel)
  47. {
  48. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
  49. mLayout->addElement(mLabel);
  50. }
  51. mDropButton = GUIDropButton::create((UINT32)DragAndDropType::Resources, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
  52. mClearButton = GUIButton::create(HString(L""), getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
  53. mClearButton->onClick.connect(std::bind(&GUIResourceField::onClearButtonClicked, this));
  54. mLayout->addElement(mDropButton);
  55. mLayout->addElement(mClearButton);
  56. mDropButton->onDataDropped.connect(std::bind(&GUIResourceField::dataDropped, this, _1));
  57. mDropButton->onClick.connect(std::bind(&GUIResourceField::onDropButtonClicked, this));
  58. }
  59. GUIResourceField::~GUIResourceField()
  60. {
  61. }
  62. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& options,
  63. const String& style)
  64. {
  65. const String* curStyle = &style;
  66. if (*curStyle == StringUtil::BLANK)
  67. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  68. return bs_new<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
  69. GUIDimensions::create(options), true);
  70. }
  71. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, const GUIOptions& options,
  72. const String& style)
  73. {
  74. const String* curStyle = &style;
  75. if (*curStyle == StringUtil::BLANK)
  76. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  77. return bs_new<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  78. GUIDimensions::create(options), true);
  79. }
  80. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
  81. 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(labelText), labelWidth, *curStyle,
  87. GUIDimensions::create(options), true);
  88. }
  89. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const HString& labelText, const GUIOptions& options,
  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, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  96. GUIDimensions::create(options), true);
  97. }
  98. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const GUIOptions& options, const String& style)
  99. {
  100. const String* curStyle = &style;
  101. if (*curStyle == StringUtil::BLANK)
  102. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  103. return bs_new<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
  104. GUIDimensions::create(options), false);
  105. }
  106. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  107. const String& style)
  108. {
  109. const String* curStyle = &style;
  110. if (*curStyle == StringUtil::BLANK)
  111. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  112. return bs_new<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
  113. GUIDimensions::create(), true);
  114. }
  115. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent,
  116. const String& style)
  117. {
  118. const String* curStyle = &style;
  119. if (*curStyle == StringUtil::BLANK)
  120. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  121. return bs_new<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  122. GUIDimensions::create(), true);
  123. }
  124. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const HString& labelText, UINT32 labelWidth,
  125. 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(labelText), labelWidth, *curStyle,
  131. GUIDimensions::create(), true);
  132. }
  133. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const HString& labelText,
  134. const String& style)
  135. {
  136. const String* curStyle = &style;
  137. if (*curStyle == StringUtil::BLANK)
  138. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  139. return bs_new<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  140. GUIDimensions::create(), true);
  141. }
  142. GUIResourceField* GUIResourceField::create(const String& typeNamespace, const String& type, const String& style)
  143. {
  144. const String* curStyle = &style;
  145. if (*curStyle == StringUtil::BLANK)
  146. curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
  147. return bs_new<GUIResourceField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
  148. GUIDimensions::create(), false);
  149. }
  150. HResource GUIResourceField::getValue() const
  151. {
  152. if (!mUUID.empty())
  153. return gResources().loadFromUUID(mUUID);
  154. return HResource();
  155. }
  156. void GUIResourceField::setValue(const HResource& value)
  157. {
  158. if (value)
  159. {
  160. Path resPath = gProjectLibrary().uuidToPath(value.getUUID());
  161. if (!resPath.isEmpty())
  162. setUUID(value.getUUID(), false);
  163. else // A non-project library resource
  164. {
  165. if (mUUID == value.getUUID())
  166. return;
  167. mUUID = value.getUUID();
  168. WString title = value->getName() + L" (" + toWString(mType) + L")";
  169. mDropButton->setContent(GUIContent(HEString(title)));
  170. }
  171. }
  172. else
  173. setUUID("", false);
  174. }
  175. void GUIResourceField::setUUID(const String& uuid, bool triggerEvent)
  176. {
  177. if (mUUID == uuid)
  178. return;
  179. mUUID = uuid;
  180. Path resPath = gProjectLibrary().uuidToPath(mUUID);
  181. if (!resPath.isEmpty())
  182. {
  183. WString title = resPath.getWFilename(false) + L" (" + toWString(mType) + L")";
  184. mDropButton->setContent(GUIContent(HEString(title)));
  185. }
  186. else
  187. mDropButton->setContent(GUIContent(HEString(L"None (" + toWString(mType) + L")")));
  188. if (triggerEvent)
  189. onValueChanged(mUUID);
  190. }
  191. void GUIResourceField::setTint(const Color& color)
  192. {
  193. if (mLabel != nullptr)
  194. mLabel->setTint(color);
  195. mDropButton->setTint(color);
  196. mClearButton->setTint(color);
  197. }
  198. void GUIResourceField::_updateLayoutInternal(const GUILayoutData& data)
  199. {
  200. mLayout->_setLayoutData(data);
  201. mLayout->_updateLayoutInternal(data);
  202. }
  203. Vector2I GUIResourceField::_getOptimalSize() const
  204. {
  205. return mLayout->_getOptimalSize();
  206. }
  207. void GUIResourceField::onDropButtonClicked()
  208. {
  209. if (mUUID == "")
  210. return;
  211. Path resPath = gProjectLibrary().uuidToPath(mUUID);
  212. resPath = resPath.getRelative(gProjectLibrary().getResourcesFolder());
  213. Selection::instance().ping(resPath);
  214. }
  215. void GUIResourceField::dataDropped(void* data)
  216. {
  217. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(data);
  218. UINT32 numResources = (UINT32)draggedResources->resourcePaths.size();
  219. if (numResources <= 0)
  220. return;
  221. MonoClass* acceptedClass = MonoManager::instance().findClass(mNamespace, mType);
  222. for (UINT32 i = 0; i < numResources; i++)
  223. {
  224. Path path = draggedResources->resourcePaths[i];
  225. ProjectLibrary::LibraryEntry* libEntry = gProjectLibrary().findEntry(path);
  226. if (libEntry == nullptr || libEntry->type == ProjectLibrary::LibraryEntryType::Directory)
  227. continue;
  228. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(libEntry);
  229. ProjectResourceMetaPtr meta = resEntry->meta;
  230. if (meta == nullptr)
  231. continue;
  232. UINT32 typeId = meta->getTypeID();
  233. String uuid = meta->getUUID();
  234. bool found = false;
  235. switch (typeId)
  236. {
  237. case TID_Texture:
  238. {
  239. // TODO - Need to distinguish from 2D/3D/Cube
  240. if (ScriptTexture2D::getMetaData()->scriptClass->isSubClassOf(acceptedClass) ||
  241. ScriptTexture3D::getMetaData()->scriptClass->isSubClassOf(acceptedClass) ||
  242. ScriptTextureCube::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  243. {
  244. setUUID(uuid);
  245. found = true;
  246. }
  247. }
  248. break;
  249. case TID_SpriteTexture:
  250. {
  251. if (ScriptSpriteTexture::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  252. {
  253. setUUID(uuid);
  254. found = true;
  255. }
  256. }
  257. break;
  258. case TID_Font:
  259. {
  260. if (ScriptFont::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  261. {
  262. setUUID(uuid);
  263. found = true;
  264. }
  265. }
  266. break;
  267. case TID_PlainText:
  268. {
  269. if (ScriptPlainText::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  270. {
  271. setUUID(uuid);
  272. found = true;
  273. }
  274. }
  275. break;
  276. case TID_ScriptCode:
  277. {
  278. if (ScriptScriptCode::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  279. {
  280. setUUID(uuid);
  281. found = true;
  282. }
  283. }
  284. break;
  285. case TID_Shader:
  286. {
  287. if (ScriptShader::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  288. {
  289. setUUID(uuid);
  290. found = true;
  291. }
  292. }
  293. break;
  294. case TID_ShaderInclude:
  295. {
  296. if (ScriptShaderInclude::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  297. {
  298. setUUID(uuid);
  299. found = true;
  300. }
  301. }
  302. break;
  303. case TID_Material:
  304. {
  305. if (ScriptMaterial::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  306. {
  307. setUUID(uuid);
  308. found = true;
  309. }
  310. }
  311. break;
  312. case TID_Mesh:
  313. {
  314. if (ScriptMesh::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  315. {
  316. setUUID(uuid);
  317. found = true;
  318. }
  319. }
  320. break;
  321. case TID_Prefab:
  322. {
  323. if (ScriptPrefab::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  324. {
  325. setUUID(uuid);
  326. found = true;
  327. }
  328. }
  329. case TID_StringTable:
  330. {
  331. if (ScriptStringTable::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  332. {
  333. setUUID(uuid);
  334. found = true;
  335. }
  336. }
  337. break;
  338. case TID_GUISkin:
  339. {
  340. if (ScriptGUISkin::getMetaData()->scriptClass->isSubClassOf(acceptedClass))
  341. {
  342. setUUID(uuid);
  343. found = true;
  344. }
  345. }
  346. break;
  347. case TID_ManagedResource:
  348. {
  349. ManagedResourceMetaDataPtr managedResMetaData = std::static_pointer_cast<ManagedResourceMetaData>(meta->getResourceMetaData());
  350. MonoClass* providedClass = MonoManager::instance().findClass(managedResMetaData->typeNamespace, managedResMetaData->typeName);
  351. if (providedClass->isSubClassOf(acceptedClass))
  352. {
  353. setUUID(uuid);
  354. found = true;
  355. }
  356. }
  357. break;
  358. default:
  359. BS_EXCEPT(NotImplementedException, "Unsupported resource type added to resource field.");
  360. }
  361. if (found)
  362. break;
  363. }
  364. }
  365. void GUIResourceField::styleUpdated()
  366. {
  367. if (mLabel != nullptr)
  368. mLabel->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
  369. mDropButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
  370. mClearButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
  371. }
  372. void GUIResourceField::onClearButtonClicked()
  373. {
  374. setUUID(StringUtil::BLANK);
  375. }
  376. const String& GUIResourceField::getGUITypeName()
  377. {
  378. static String typeName = "GUIResourceField";
  379. return typeName;
  380. }
  381. }