import_defaults_editor.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**************************************************************************/
  2. /* import_defaults_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "import_defaults_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/resource_importer.h"
  33. #include "editor/action_map_editor.h"
  34. #include "editor/editor_inspector.h"
  35. #include "editor/editor_sectioned_inspector.h"
  36. #include "scene/gui/center_container.h"
  37. class ImportDefaultsEditorSettings : public Object {
  38. GDCLASS(ImportDefaultsEditorSettings, Object)
  39. friend class ImportDefaultsEditor;
  40. List<PropertyInfo> properties;
  41. HashMap<StringName, Variant> values;
  42. HashMap<StringName, Variant> default_values;
  43. Ref<ResourceImporter> importer;
  44. protected:
  45. bool _set(const StringName &p_name, const Variant &p_value) {
  46. if (values.has(p_name)) {
  47. values[p_name] = p_value;
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. bool _get(const StringName &p_name, Variant &r_ret) const {
  54. if (values.has(p_name)) {
  55. r_ret = values[p_name];
  56. return true;
  57. } else {
  58. r_ret = Variant();
  59. return false;
  60. }
  61. }
  62. void _get_property_list(List<PropertyInfo> *p_list) const {
  63. if (importer.is_null()) {
  64. return;
  65. }
  66. for (const PropertyInfo &E : properties) {
  67. if (importer->get_option_visibility("", E.name, values)) {
  68. p_list->push_back(E);
  69. }
  70. }
  71. }
  72. };
  73. void ImportDefaultsEditor::_notification(int p_what) {
  74. switch (p_what) {
  75. case NOTIFICATION_PREDELETE: {
  76. inspector->edit(nullptr);
  77. } break;
  78. }
  79. }
  80. void ImportDefaultsEditor::_reset() {
  81. if (settings->importer.is_valid()) {
  82. settings->values = settings->default_values;
  83. settings->notify_property_list_changed();
  84. }
  85. }
  86. void ImportDefaultsEditor::_save() {
  87. if (settings->importer.is_valid()) {
  88. Dictionary modified;
  89. for (const KeyValue<StringName, Variant> &E : settings->values) {
  90. if (E.value != settings->default_values[E.key]) {
  91. modified[E.key] = E.value;
  92. }
  93. }
  94. if (modified.size()) {
  95. ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), modified);
  96. } else {
  97. ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), Variant());
  98. }
  99. ProjectSettings::get_singleton()->save();
  100. }
  101. }
  102. void ImportDefaultsEditor::_update_importer() {
  103. List<Ref<ResourceImporter>> importer_list;
  104. ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
  105. Ref<ResourceImporter> importer;
  106. for (const Ref<ResourceImporter> &E : importer_list) {
  107. if (E->get_visible_name() == importers->get_item_text(importers->get_selected())) {
  108. importer = E;
  109. break;
  110. }
  111. }
  112. settings->properties.clear();
  113. settings->values.clear();
  114. settings->importer = importer;
  115. if (importer.is_valid()) {
  116. List<ResourceImporter::ImportOption> options;
  117. importer->get_import_options("", &options);
  118. Dictionary d;
  119. if (ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  120. d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  121. }
  122. for (const ResourceImporter::ImportOption &E : options) {
  123. settings->properties.push_back(E.option);
  124. if (d.has(E.option.name)) {
  125. settings->values[E.option.name] = d[E.option.name];
  126. } else {
  127. settings->values[E.option.name] = E.default_value;
  128. }
  129. settings->default_values[E.option.name] = E.default_value;
  130. }
  131. save_defaults->set_disabled(false);
  132. reset_defaults->set_disabled(false);
  133. } else {
  134. save_defaults->set_disabled(true);
  135. reset_defaults->set_disabled(true);
  136. }
  137. settings->notify_property_list_changed();
  138. // Set the importer class to fetch the correct class in the XML class reference.
  139. // This allows tooltips to display when hovering properties.
  140. inspector->set_object_class(importer->get_class_name());
  141. inspector->edit(settings);
  142. }
  143. void ImportDefaultsEditor::_importer_selected(int p_index) {
  144. _update_importer();
  145. }
  146. void ImportDefaultsEditor::clear() {
  147. String last_selected;
  148. if (importers->get_selected() >= 0) {
  149. last_selected = importers->get_item_text(importers->get_selected());
  150. }
  151. importers->clear();
  152. List<Ref<ResourceImporter>> importer_list;
  153. ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
  154. Vector<String> names;
  155. for (const Ref<ResourceImporter> &E : importer_list) {
  156. String vn = E->get_visible_name();
  157. names.push_back(vn);
  158. }
  159. names.sort();
  160. // `last_selected.is_empty()` means it's the first time being called.
  161. if (last_selected.is_empty() && !names.is_empty()) {
  162. last_selected = names[0];
  163. }
  164. for (int i = 0; i < names.size(); i++) {
  165. importers->add_item(names[i]);
  166. if (names[i] == last_selected) {
  167. importers->select(i);
  168. _update_importer();
  169. }
  170. }
  171. }
  172. ImportDefaultsEditor::ImportDefaultsEditor() {
  173. ProjectSettings::get_singleton()->add_hidden_prefix("importer_defaults/");
  174. HBoxContainer *hb = memnew(HBoxContainer);
  175. hb->add_child(memnew(Label(TTR("Importer:"))));
  176. importers = memnew(OptionButton);
  177. hb->add_child(importers);
  178. hb->add_spacer();
  179. importers->connect(SceneStringName(item_selected), callable_mp(this, &ImportDefaultsEditor::_importer_selected));
  180. reset_defaults = memnew(Button);
  181. reset_defaults->set_text(TTR("Reset to Defaults"));
  182. reset_defaults->set_disabled(true);
  183. reset_defaults->connect(SceneStringName(pressed), callable_mp(this, &ImportDefaultsEditor::_reset));
  184. hb->add_child(reset_defaults);
  185. add_child(hb);
  186. inspector = memnew(EditorInspector);
  187. add_child(inspector);
  188. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  189. // Make it possible to display tooltips stored in the XML class reference.
  190. // The object name is set when the importer changes in `_update_importer()`.
  191. inspector->set_use_doc_hints(true);
  192. CenterContainer *cc = memnew(CenterContainer);
  193. save_defaults = memnew(Button);
  194. save_defaults->set_text(TTR("Save"));
  195. save_defaults->connect(SceneStringName(pressed), callable_mp(this, &ImportDefaultsEditor::_save));
  196. cc->add_child(save_defaults);
  197. add_child(cc);
  198. settings = memnew(ImportDefaultsEditorSettings);
  199. }
  200. ImportDefaultsEditor::~ImportDefaultsEditor() {
  201. memdelete(settings);
  202. }