BsGUIResourceField.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "BsEditorGUI.h"
  18. using namespace std::placeholders;
  19. namespace BansheeEngine
  20. {
  21. const UINT32 GUIResourceField::DEFAULT_LABEL_WIDTH = 100;
  22. GUIResourceField::GUIResourceField(const PrivatelyConstruct& dummy, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  23. const String& style, const GUILayoutOptions& layoutOptions, bool withLabel)
  24. :GUIElementContainer(layoutOptions, style), mLabel(nullptr), mClearButton(nullptr), mDropButton(nullptr), mType(type)
  25. {
  26. mLayout = &addLayoutXInternal(this);
  27. if (withLabel)
  28. {
  29. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(EditorGUI::ObjectFieldLabelStyleName));
  30. mLayout->addElement(mLabel);
  31. }
  32. mDropButton = GUIDropButton::create((UINT32)DragAndDropType::SceneObject, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(EditorGUI::ObjectFieldDropBtnStyleName));
  33. mClearButton = GUIButton::create(HString(L""), getSubStyleName(EditorGUI::ObjectFieldClearBtnStyleName));
  34. mLayout->addElement(mDropButton);
  35. mLayout->addElement(mClearButton);
  36. mDropButton->onDataDropped.connect(std::bind(&GUIResourceField::dataDropped, this, _1));
  37. }
  38. GUIResourceField::~GUIResourceField()
  39. {
  40. }
  41. GUIResourceField* GUIResourceField::create(const String& type, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& layoutOptions,
  42. const String& style)
  43. {
  44. const String* curStyle = &style;
  45. if (*curStyle == StringUtil::BLANK)
  46. curStyle = &EditorGUI::ObjectFieldStyleName;
  47. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, labelWidth, *curStyle,
  48. GUILayoutOptions::create(layoutOptions), true);
  49. }
  50. GUIResourceField* GUIResourceField::create(const String& type, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  51. const String& style)
  52. {
  53. const String* curStyle = &style;
  54. if (*curStyle == StringUtil::BLANK)
  55. curStyle = &EditorGUI::ObjectFieldStyleName;
  56. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  57. GUILayoutOptions::create(layoutOptions), true);
  58. }
  59. GUIResourceField* GUIResourceField::create(const String& type, const HString& labelText, UINT32 labelWidth, const GUIOptions& layoutOptions,
  60. const String& style)
  61. {
  62. const String* curStyle = &style;
  63. if (*curStyle == StringUtil::BLANK)
  64. curStyle = &EditorGUI::ObjectFieldStyleName;
  65. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), labelWidth, *curStyle,
  66. GUILayoutOptions::create(layoutOptions), true);
  67. }
  68. GUIResourceField* GUIResourceField::create(const String& type, const HString& labelText, const GUIOptions& layoutOptions,
  69. const String& style)
  70. {
  71. const String* curStyle = &style;
  72. if (*curStyle == StringUtil::BLANK)
  73. curStyle = &EditorGUI::ObjectFieldStyleName;
  74. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  75. GUILayoutOptions::create(layoutOptions), true);
  76. }
  77. GUIResourceField* GUIResourceField::create(const String& type, const GUIOptions& layoutOptions, const String& style)
  78. {
  79. const String* curStyle = &style;
  80. if (*curStyle == StringUtil::BLANK)
  81. curStyle = &EditorGUI::ObjectFieldStyleName;
  82. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(), 0, *curStyle,
  83. GUILayoutOptions::create(layoutOptions), false);
  84. }
  85. GUIResourceField* GUIResourceField::create(const String& type, const GUIContent& labelContent, UINT32 labelWidth,
  86. const String& style)
  87. {
  88. const String* curStyle = &style;
  89. if (*curStyle == StringUtil::BLANK)
  90. curStyle = &EditorGUI::ObjectFieldStyleName;
  91. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, labelWidth, *curStyle,
  92. GUILayoutOptions::create(), true);
  93. }
  94. GUIResourceField* GUIResourceField::create(const String& type, const GUIContent& labelContent,
  95. const String& style)
  96. {
  97. const String* curStyle = &style;
  98. if (*curStyle == StringUtil::BLANK)
  99. curStyle = &EditorGUI::ObjectFieldStyleName;
  100. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  101. GUILayoutOptions::create(), true);
  102. }
  103. GUIResourceField* GUIResourceField::create(const String& type, const HString& labelText, UINT32 labelWidth,
  104. const String& style)
  105. {
  106. const String* curStyle = &style;
  107. if (*curStyle == StringUtil::BLANK)
  108. curStyle = &EditorGUI::ObjectFieldStyleName;
  109. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), labelWidth, *curStyle,
  110. GUILayoutOptions::create(), true);
  111. }
  112. GUIResourceField* GUIResourceField::create(const String& type, const HString& labelText,
  113. const String& style)
  114. {
  115. const String* curStyle = &style;
  116. if (*curStyle == StringUtil::BLANK)
  117. curStyle = &EditorGUI::ObjectFieldStyleName;
  118. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  119. GUILayoutOptions::create(), true);
  120. }
  121. GUIResourceField* GUIResourceField::create(const String& type, const String& style)
  122. {
  123. const String* curStyle = &style;
  124. if (*curStyle == StringUtil::BLANK)
  125. curStyle = &EditorGUI::ObjectFieldStyleName;
  126. return bs_new<GUIResourceField>(PrivatelyConstruct(), type, GUIContent(), 0, *curStyle,
  127. GUILayoutOptions::create(), false);
  128. }
  129. HResource GUIResourceField::getValue() const
  130. {
  131. return Resources::instance().loadFromUUID(mUUID);
  132. }
  133. void GUIResourceField::setValue(const HResource& value)
  134. {
  135. if (value)
  136. setUUID(value.getUUID());
  137. else
  138. setUUID("");
  139. }
  140. void GUIResourceField::setUUID(const String& uuid)
  141. {
  142. mUUID = uuid;
  143. Path filePath;
  144. if (Resources::instance().getFilePathFromUUID(mUUID, filePath))
  145. {
  146. mDropButton->setContent(GUIContent(filePath.getFilename(false)));
  147. }
  148. else
  149. mDropButton->setContent(GUIContent(HString(L"None")));
  150. onValueChanged(mUUID);
  151. }
  152. void GUIResourceField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  153. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  154. {
  155. mLayout->_updateLayoutInternal(x, y, width, height, clipRect, widgetDepth, areaDepth);
  156. }
  157. Vector2I GUIResourceField::_getOptimalSize() const
  158. {
  159. return mLayout->_getOptimalSize();
  160. }
  161. void GUIResourceField::dataDropped(void* data)
  162. {
  163. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(data);
  164. UINT32 numResources = (UINT32)draggedResources->resourceUUIDs.size();
  165. if (numResources <= 0)
  166. return;
  167. for (UINT32 i = 0; i < numResources; i++)
  168. {
  169. // TODO - I need to be able to check resource type without loading it
  170. }
  171. }
  172. void GUIResourceField::styleUpdated()
  173. {
  174. if (mLabel != nullptr)
  175. mLabel->setStyle(getSubStyleName(EditorGUI::ObjectFieldLabelStyleName));
  176. mDropButton->setStyle(getSubStyleName(EditorGUI::ObjectFieldDropBtnStyleName));
  177. mClearButton->setStyle(getSubStyleName(EditorGUI::ObjectFieldClearBtnStyleName));
  178. }
  179. const String& GUIResourceField::getGUITypeName()
  180. {
  181. static String typeName = "GUIResourceField";
  182. return typeName;
  183. }
  184. }