import_defaults_editor.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*************************************************************************/
  2. /* import_defaults_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_plugin_settings.h"
  36. #include "editor/editor_sectioned_inspector.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/localization_editor.h"
  39. #include "editor/shader_globals_editor.h"
  40. #include "scene/gui/center_container.h"
  41. class ImportDefaultsEditorSettings : public Object {
  42. GDCLASS(ImportDefaultsEditorSettings, Object)
  43. friend class ImportDefaultsEditor;
  44. List<PropertyInfo> properties;
  45. HashMap<StringName, Variant> values;
  46. HashMap<StringName, Variant> default_values;
  47. Ref<ResourceImporter> importer;
  48. protected:
  49. bool _set(const StringName &p_name, const Variant &p_value) {
  50. if (values.has(p_name)) {
  51. values[p_name] = p_value;
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. bool _get(const StringName &p_name, Variant &r_ret) const {
  58. if (values.has(p_name)) {
  59. r_ret = values[p_name];
  60. return true;
  61. } else {
  62. r_ret = Variant();
  63. return false;
  64. }
  65. }
  66. void _get_property_list(List<PropertyInfo> *p_list) const {
  67. if (importer.is_null()) {
  68. return;
  69. }
  70. for (const PropertyInfo &E : properties) {
  71. if (importer->get_option_visibility("", E.name, values)) {
  72. p_list->push_back(E);
  73. }
  74. }
  75. }
  76. };
  77. void ImportDefaultsEditor::_notification(int p_what) {
  78. switch (p_what) {
  79. case NOTIFICATION_ENTER_TREE:
  80. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  81. inspector->set_property_name_style(EditorPropertyNameProcessor::get_settings_style());
  82. } break;
  83. case NOTIFICATION_PREDELETE: {
  84. inspector->edit(nullptr);
  85. } break;
  86. }
  87. }
  88. void ImportDefaultsEditor::_reset() {
  89. if (settings->importer.is_valid()) {
  90. settings->values = settings->default_values;
  91. settings->notify_property_list_changed();
  92. }
  93. }
  94. void ImportDefaultsEditor::_save() {
  95. if (settings->importer.is_valid()) {
  96. Dictionary modified;
  97. for (const KeyValue<StringName, Variant> &E : settings->values) {
  98. if (E.value != settings->default_values[E.key]) {
  99. modified[E.key] = E.value;
  100. }
  101. }
  102. if (modified.size()) {
  103. ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), modified);
  104. } else {
  105. ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), Variant());
  106. }
  107. emit_signal(SNAME("project_settings_changed"));
  108. }
  109. }
  110. void ImportDefaultsEditor::_update_importer() {
  111. List<Ref<ResourceImporter>> importer_list;
  112. ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
  113. Ref<ResourceImporter> importer;
  114. for (const Ref<ResourceImporter> &E : importer_list) {
  115. if (E->get_visible_name() == importers->get_item_text(importers->get_selected())) {
  116. importer = E;
  117. break;
  118. }
  119. }
  120. settings->properties.clear();
  121. settings->values.clear();
  122. settings->importer = importer;
  123. if (importer.is_valid()) {
  124. List<ResourceImporter::ImportOption> options;
  125. importer->get_import_options("", &options);
  126. Dictionary d;
  127. if (ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
  128. d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
  129. }
  130. for (const ResourceImporter::ImportOption &E : options) {
  131. settings->properties.push_back(E.option);
  132. if (d.has(E.option.name)) {
  133. settings->values[E.option.name] = d[E.option.name];
  134. } else {
  135. settings->values[E.option.name] = E.default_value;
  136. }
  137. settings->default_values[E.option.name] = E.default_value;
  138. }
  139. save_defaults->set_disabled(false);
  140. reset_defaults->set_disabled(false);
  141. } else {
  142. save_defaults->set_disabled(true);
  143. reset_defaults->set_disabled(true);
  144. }
  145. settings->notify_property_list_changed();
  146. inspector->edit(settings);
  147. }
  148. void ImportDefaultsEditor::_importer_selected(int p_index) {
  149. _update_importer();
  150. }
  151. void ImportDefaultsEditor::clear() {
  152. String last_selected;
  153. if (importers->get_selected() > 0) {
  154. last_selected = importers->get_item_text(importers->get_selected());
  155. }
  156. importers->clear();
  157. importers->add_item("<" + TTR("Select Importer") + ">");
  158. importers->set_item_disabled(0, true);
  159. List<Ref<ResourceImporter>> importer_list;
  160. ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
  161. Vector<String> names;
  162. for (const Ref<ResourceImporter> &E : importer_list) {
  163. String vn = E->get_visible_name();
  164. names.push_back(vn);
  165. }
  166. names.sort();
  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 + 1);
  171. }
  172. }
  173. }
  174. void ImportDefaultsEditor::_bind_methods() {
  175. ADD_SIGNAL(MethodInfo("project_settings_changed"));
  176. }
  177. ImportDefaultsEditor::ImportDefaultsEditor() {
  178. HBoxContainer *hb = memnew(HBoxContainer);
  179. hb->add_child(memnew(Label(TTR("Importer:"))));
  180. importers = memnew(OptionButton);
  181. hb->add_child(importers);
  182. hb->add_spacer();
  183. importers->connect("item_selected", callable_mp(this, &ImportDefaultsEditor::_importer_selected));
  184. reset_defaults = memnew(Button);
  185. reset_defaults->set_text(TTR("Reset to Defaults"));
  186. reset_defaults->set_disabled(true);
  187. reset_defaults->connect("pressed", callable_mp(this, &ImportDefaultsEditor::_reset));
  188. hb->add_child(reset_defaults);
  189. add_child(hb);
  190. inspector = memnew(EditorInspector);
  191. add_child(inspector);
  192. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  193. CenterContainer *cc = memnew(CenterContainer);
  194. save_defaults = memnew(Button);
  195. save_defaults->set_text(TTR("Save"));
  196. save_defaults->connect("pressed", callable_mp(this, &ImportDefaultsEditor::_save));
  197. cc->add_child(save_defaults);
  198. add_child(cc);
  199. settings = memnew(ImportDefaultsEditorSettings);
  200. }
  201. ImportDefaultsEditor::~ImportDefaultsEditor() {
  202. memdelete(settings);
  203. }