import_defaults_editor.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_autoload_settings.h"
  35. #include "editor/editor_sectioned_inspector.h"
  36. #include "editor/localization_editor.h"
  37. #include "editor/plugins/editor_plugin_settings.h"
  38. #include "editor/shader_globals_editor.h"
  39. #include "scene/gui/center_container.h"
  40. class ImportDefaultsEditorSettings : public Object {
  41. GDCLASS(ImportDefaultsEditorSettings, Object)
  42. friend class ImportDefaultsEditor;
  43. List<PropertyInfo> properties;
  44. HashMap<StringName, Variant> values;
  45. HashMap<StringName, Variant> default_values;
  46. Ref<ResourceImporter> importer;
  47. protected:
  48. bool _set(const StringName &p_name, const Variant &p_value) {
  49. if (values.has(p_name)) {
  50. values[p_name] = p_value;
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. bool _get(const StringName &p_name, Variant &r_ret) const {
  57. if (values.has(p_name)) {
  58. r_ret = values[p_name];
  59. return true;
  60. } else {
  61. r_ret = Variant();
  62. return false;
  63. }
  64. }
  65. void _get_property_list(List<PropertyInfo> *p_list) const {
  66. if (importer.is_null()) {
  67. return;
  68. }
  69. for (const PropertyInfo &E : properties) {
  70. if (importer->get_option_visibility("", E.name, values)) {
  71. p_list->push_back(E);
  72. }
  73. }
  74. }
  75. };
  76. void ImportDefaultsEditor::_notification(int p_what) {
  77. switch (p_what) {
  78. case NOTIFICATION_PREDELETE: {
  79. inspector->edit(nullptr);
  80. } break;
  81. }
  82. }
  83. void ImportDefaultsEditor::_reset() {
  84. if (settings->importer.is_valid()) {
  85. settings->values = settings->default_values;
  86. settings->notify_property_list_changed();
  87. }
  88. }
  89. void ImportDefaultsEditor::_save() {
  90. if (settings->importer.is_valid()) {
  91. Dictionary modified;
  92. for (const KeyValue<StringName, Variant> &E : settings->values) {
  93. if (E.value != settings->default_values[E.key]) {
  94. modified[E.key] = E.value;
  95. }
  96. }
  97. if (modified.size()) {
  98. ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), modified);
  99. } else {
  100. ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), Variant());
  101. }
  102. ProjectSettings::get_singleton()->save();
  103. }
  104. }
  105. void ImportDefaultsEditor::_update_importer() {
  106. List<Ref<ResourceImporter>> importer_list;
  107. ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
  108. Ref<ResourceImporter> importer;
  109. for (const Ref<ResourceImporter> &E : importer_list) {
  110. if (E->get_visible_name() == importers->get_item_text(importers->get_selected())) {
  111. importer = E;
  112. break;
  113. }
  114. }
  115. settings->properties.clear();
  116. settings->values.clear();
  117. settings->importer = importer;
  118. if (importer.is_valid()) {
  119. List<ResourceImporter::ImportOption> options;
  120. importer->get_import_options("", &options);
  121. Dictionary d;
  122. if (ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  123. d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  124. }
  125. for (const ResourceImporter::ImportOption &E : options) {
  126. settings->properties.push_back(E.option);
  127. if (d.has(E.option.name)) {
  128. settings->values[E.option.name] = d[E.option.name];
  129. } else {
  130. settings->values[E.option.name] = E.default_value;
  131. }
  132. settings->default_values[E.option.name] = E.default_value;
  133. }
  134. save_defaults->set_disabled(false);
  135. reset_defaults->set_disabled(false);
  136. } else {
  137. save_defaults->set_disabled(true);
  138. reset_defaults->set_disabled(true);
  139. }
  140. settings->notify_property_list_changed();
  141. // Set the importer class to fetch the correct class in the XML class reference.
  142. // This allows tooltips to display when hovering properties.
  143. inspector->set_object_class(importer->get_class_name());
  144. inspector->edit(settings);
  145. }
  146. void ImportDefaultsEditor::_importer_selected(int p_index) {
  147. _update_importer();
  148. }
  149. void ImportDefaultsEditor::clear() {
  150. String last_selected;
  151. if (importers->get_selected() >= 0) {
  152. last_selected = importers->get_item_text(importers->get_selected());
  153. }
  154. importers->clear();
  155. List<Ref<ResourceImporter>> importer_list;
  156. ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
  157. Vector<String> names;
  158. for (const Ref<ResourceImporter> &E : importer_list) {
  159. String vn = E->get_visible_name();
  160. names.push_back(vn);
  161. }
  162. names.sort();
  163. // `last_selected.is_empty()` means it's the first time being called.
  164. if (last_selected.is_empty() && !names.is_empty()) {
  165. last_selected = names[0];
  166. }
  167. for (int i = 0; i < names.size(); i++) {
  168. importers->add_item(names[i]);
  169. if (names[i] == last_selected) {
  170. importers->select(i);
  171. _update_importer();
  172. }
  173. }
  174. }
  175. ImportDefaultsEditor::ImportDefaultsEditor() {
  176. ProjectSettings::get_singleton()->add_hidden_prefix("importer_defaults/");
  177. HBoxContainer *hb = memnew(HBoxContainer);
  178. hb->add_child(memnew(Label(TTR("Importer:"))));
  179. importers = memnew(OptionButton);
  180. hb->add_child(importers);
  181. hb->add_spacer();
  182. importers->connect("item_selected", callable_mp(this, &ImportDefaultsEditor::_importer_selected));
  183. reset_defaults = memnew(Button);
  184. reset_defaults->set_text(TTR("Reset to Defaults"));
  185. reset_defaults->set_disabled(true);
  186. reset_defaults->connect(SceneStringName(pressed), callable_mp(this, &ImportDefaultsEditor::_reset));
  187. hb->add_child(reset_defaults);
  188. add_child(hb);
  189. inspector = memnew(EditorInspector);
  190. add_child(inspector);
  191. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  192. // Make it possible to display tooltips stored in the XML class reference.
  193. // The object name is set when the importer changes in `_update_importer()`.
  194. inspector->set_use_doc_hints(true);
  195. CenterContainer *cc = memnew(CenterContainer);
  196. save_defaults = memnew(Button);
  197. save_defaults->set_text(TTR("Save"));
  198. save_defaults->connect(SceneStringName(pressed), callable_mp(this, &ImportDefaultsEditor::_save));
  199. cc->add_child(save_defaults);
  200. add_child(cc);
  201. settings = memnew(ImportDefaultsEditorSettings);
  202. }
  203. ImportDefaultsEditor::~ImportDefaultsEditor() {
  204. memdelete(settings);
  205. }