BsGUIResourceField.cpp 7.3 KB

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