import_dock.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************/
  2. /* import_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_dock.h"
  31. #include "editor_node.h"
  32. class ImportDockParameters : public Object {
  33. GDCLASS(ImportDockParameters, Object)
  34. public:
  35. Map<StringName, Variant> values;
  36. List<PropertyInfo> properties;
  37. Ref<ResourceImporter> importer;
  38. Vector<String> paths;
  39. bool _set(const StringName &p_name, const Variant &p_value) {
  40. if (values.has(p_name)) {
  41. values[p_name] = p_value;
  42. return true;
  43. }
  44. return false;
  45. }
  46. bool _get(const StringName &p_name, Variant &r_ret) const {
  47. if (values.has(p_name)) {
  48. r_ret = values[p_name];
  49. return true;
  50. }
  51. return false;
  52. }
  53. void _get_property_list(List<PropertyInfo> *p_list) const {
  54. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  55. if (!importer->get_option_visibility(E->get().name, values))
  56. continue;
  57. p_list->push_back(E->get());
  58. }
  59. }
  60. void update() {
  61. _change_notify();
  62. }
  63. };
  64. void ImportDock::set_edit_path(const String &p_path) {
  65. Ref<ConfigFile> config;
  66. config.instance();
  67. Error err = config->load(p_path + ".import");
  68. if (err != OK) {
  69. clear();
  70. return;
  71. }
  72. params->importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(config->get_value("remap", "importer"));
  73. if (params->importer.is_null()) {
  74. clear();
  75. return;
  76. }
  77. List<ResourceImporter::ImportOption> options;
  78. params->importer->get_import_options(&options);
  79. params->properties.clear();
  80. params->values.clear();
  81. for (List<ResourceImporter::ImportOption>::Element *E = options.front(); E; E = E->next()) {
  82. params->properties.push_back(E->get().option);
  83. if (config->has_section_key("params", E->get().option.name)) {
  84. params->values[E->get().option.name] = config->get_value("params", E->get().option.name);
  85. } else {
  86. params->values[E->get().option.name] = E->get().default_value;
  87. }
  88. }
  89. params->update();
  90. List<Ref<ResourceImporter> > importers;
  91. ResourceFormatImporter::get_singleton()->get_importers_for_extension(p_path.get_extension(), &importers);
  92. List<Pair<String, String> > importer_names;
  93. for (List<Ref<ResourceImporter> >::Element *E = importers.front(); E; E = E->next()) {
  94. importer_names.push_back(Pair<String, String>(E->get()->get_visible_name(), E->get()->get_importer_name()));
  95. }
  96. importer_names.sort_custom<PairSort<String, String> >();
  97. import_as->clear();
  98. for (List<Pair<String, String> >::Element *E = importer_names.front(); E; E = E->next()) {
  99. import_as->add_item(E->get().first);
  100. import_as->set_item_metadata(import_as->get_item_count() - 1, E->get().second);
  101. if (E->get().second == params->importer->get_importer_name()) {
  102. import_as->select(import_as->get_item_count() - 1);
  103. }
  104. }
  105. preset->get_popup()->clear();
  106. if (params->importer->get_preset_count() == 0) {
  107. preset->get_popup()->add_item(TTR("Default"));
  108. } else {
  109. for (int i = 0; i < params->importer->get_preset_count(); i++) {
  110. preset->get_popup()->add_item(params->importer->get_preset_name(i));
  111. }
  112. }
  113. preset->get_popup()->add_separator();
  114. preset->get_popup()->add_item(vformat(TTR("Set as Default for '%s'"), params->importer->get_visible_name()), ITEM_SET_AS_DEFAULT);
  115. if (ProjectSettings::get_singleton()->has_setting("importer_defaults/" + params->importer->get_importer_name())) {
  116. preset->get_popup()->add_item(TTR("Load Default"), ITEM_LOAD_DEFAULT);
  117. preset->get_popup()->add_separator();
  118. preset->get_popup()->add_item(vformat(TTR("Clear Default for '%s'"), params->importer->get_visible_name()), ITEM_CLEAR_DEFAULT);
  119. }
  120. params->paths.clear();
  121. params->paths.push_back(p_path);
  122. import->set_disabled(false);
  123. import_as->set_disabled(false);
  124. imported->set_text(p_path.get_file());
  125. }
  126. void ImportDock::set_edit_multiple_paths(const Vector<String> &p_paths) {
  127. clear();
  128. //use the value that is repeated the mot
  129. Map<String, Dictionary> value_frequency;
  130. for (int i = 0; i < p_paths.size(); i++) {
  131. Ref<ConfigFile> config;
  132. config.instance();
  133. Error err = config->load(p_paths[i] + ".import");
  134. ERR_CONTINUE(err != OK);
  135. if (i == 0) {
  136. params->importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(config->get_value("remap", "importer"));
  137. if (params->importer.is_null()) {
  138. clear();
  139. return;
  140. }
  141. }
  142. List<String> keys;
  143. config->get_section_keys("params", &keys);
  144. for (List<String>::Element *E = keys.front(); E; E = E->next()) {
  145. if (!value_frequency.has(E->get())) {
  146. value_frequency[E->get()] = Dictionary();
  147. }
  148. Variant value = config->get_value("params", E->get());
  149. if (value_frequency[E->get()].has(value)) {
  150. value_frequency[E->get()][value] = int(value_frequency[E->get()][value]) + 1;
  151. } else {
  152. value_frequency[E->get()][value] = 1;
  153. }
  154. }
  155. }
  156. ERR_FAIL_COND(params->importer.is_null());
  157. List<ResourceImporter::ImportOption> options;
  158. params->importer->get_import_options(&options);
  159. params->properties.clear();
  160. params->values.clear();
  161. for (List<ResourceImporter::ImportOption>::Element *E = options.front(); E; E = E->next()) {
  162. params->properties.push_back(E->get().option);
  163. if (value_frequency.has(E->get().option.name)) {
  164. Dictionary d = value_frequency[E->get().option.name];
  165. int freq = 0;
  166. List<Variant> v;
  167. d.get_key_list(&v);
  168. Variant value;
  169. for (List<Variant>::Element *F = v.front(); F; F = F->next()) {
  170. int f = d[F->get()];
  171. if (f > freq) {
  172. value = F->get();
  173. }
  174. }
  175. params->values[E->get().option.name] = value;
  176. } else {
  177. params->values[E->get().option.name] = E->get().default_value;
  178. }
  179. }
  180. params->update();
  181. List<Ref<ResourceImporter> > importers;
  182. ResourceFormatImporter::get_singleton()->get_importers_for_extension(p_paths[0].get_extension(), &importers);
  183. List<Pair<String, String> > importer_names;
  184. for (List<Ref<ResourceImporter> >::Element *E = importers.front(); E; E = E->next()) {
  185. importer_names.push_back(Pair<String, String>(E->get()->get_visible_name(), E->get()->get_importer_name()));
  186. }
  187. importer_names.sort_custom<PairSort<String, String> >();
  188. import_as->clear();
  189. for (List<Pair<String, String> >::Element *E = importer_names.front(); E; E = E->next()) {
  190. import_as->add_item(E->get().first);
  191. import_as->set_item_metadata(import_as->get_item_count() - 1, E->get().second);
  192. if (E->get().second == params->importer->get_importer_name()) {
  193. import_as->select(import_as->get_item_count() - 1);
  194. }
  195. }
  196. preset->get_popup()->clear();
  197. if (params->importer->get_preset_count() == 0) {
  198. preset->get_popup()->add_item(TTR("Default"));
  199. } else {
  200. for (int i = 0; i < params->importer->get_preset_count(); i++) {
  201. preset->get_popup()->add_item(params->importer->get_preset_name(i));
  202. }
  203. }
  204. params->paths = p_paths;
  205. import->set_disabled(false);
  206. import_as->set_disabled(false);
  207. imported->set_text(itos(p_paths.size()) + TTR(" Files"));
  208. }
  209. void ImportDock::_preset_selected(int p_idx) {
  210. int item_id = preset->get_popup()->get_item_id(p_idx);
  211. switch (item_id) {
  212. case ITEM_SET_AS_DEFAULT: {
  213. Dictionary d;
  214. for (const List<PropertyInfo>::Element *E = params->properties.front(); E; E = E->next()) {
  215. d[E->get().name] = params->values[E->get().name];
  216. }
  217. ProjectSettings::get_singleton()->set("importer_defaults/" + params->importer->get_importer_name(), d);
  218. ProjectSettings::get_singleton()->save();
  219. } break;
  220. case ITEM_LOAD_DEFAULT: {
  221. ERR_FAIL_COND(!ProjectSettings::get_singleton()->has_setting("importer_defaults/" + params->importer->get_importer_name()));
  222. Dictionary d = ProjectSettings::get_singleton()->get("importer_defaults/" + params->importer->get_importer_name());
  223. List<Variant> v;
  224. d.get_key_list(&v);
  225. for (List<Variant>::Element *E = v.front(); E; E = E->next()) {
  226. params->values[E->get()] = d[E->get()];
  227. }
  228. params->update();
  229. } break;
  230. case ITEM_CLEAR_DEFAULT: {
  231. ProjectSettings::get_singleton()->set("importer_defaults/" + params->importer->get_importer_name(), Variant());
  232. ProjectSettings::get_singleton()->save();
  233. } break;
  234. default: {
  235. List<ResourceImporter::ImportOption> options;
  236. params->importer->get_import_options(&options, p_idx);
  237. for (List<ResourceImporter::ImportOption>::Element *E = options.front(); E; E = E->next()) {
  238. params->values[E->get().option.name] = E->get().default_value;
  239. }
  240. params->update();
  241. } break;
  242. }
  243. }
  244. void ImportDock::clear() {
  245. imported->set_text("");
  246. import->set_disabled(true);
  247. import_as->clear();
  248. import_as->set_disabled(true);
  249. params->values.clear();
  250. params->properties.clear();
  251. params->update();
  252. preset->get_popup()->clear();
  253. }
  254. void ImportDock::_reimport() {
  255. for (int i = 0; i < params->paths.size(); i++) {
  256. Ref<ConfigFile> config;
  257. config.instance();
  258. Error err = config->load(params->paths[i] + ".import");
  259. ERR_CONTINUE(err != OK);
  260. config->erase_section("params");
  261. for (List<PropertyInfo>::Element *E = params->properties.front(); E; E = E->next()) {
  262. config->set_value("params", E->get().name, params->values[E->get().name]);
  263. }
  264. config->save(params->paths[i] + ".import");
  265. }
  266. EditorFileSystem::get_singleton()->reimport_files(params->paths);
  267. EditorFileSystem::get_singleton()->emit_signal("filesystem_changed"); //it changed, so force emitting the signal
  268. }
  269. void ImportDock::_notification(int p_what) {
  270. switch (p_what) {
  271. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  272. imported->add_style_override("normal", get_stylebox("normal", "LineEdit"));
  273. } break;
  274. }
  275. }
  276. void ImportDock::_bind_methods() {
  277. ClassDB::bind_method(D_METHOD("_reimport"), &ImportDock::_reimport);
  278. ClassDB::bind_method(D_METHOD("_preset_selected"), &ImportDock::_preset_selected);
  279. }
  280. void ImportDock::initialize_import_options() const {
  281. ERR_FAIL_COND(!import_opts || !params);
  282. import_opts->edit(params);
  283. }
  284. ImportDock::ImportDock() {
  285. imported = memnew(Label);
  286. imported->add_style_override("normal", EditorNode::get_singleton()->get_gui_base()->get_stylebox("normal", "LineEdit"));
  287. add_child(imported);
  288. HBoxContainer *hb = memnew(HBoxContainer);
  289. add_margin_child(TTR("Import As:"), hb);
  290. import_as = memnew(OptionButton);
  291. hb->add_child(import_as);
  292. import_as->set_h_size_flags(SIZE_EXPAND_FILL);
  293. preset = memnew(MenuButton);
  294. preset->set_text(TTR("Preset.."));
  295. preset->get_popup()->connect("index_pressed", this, "_preset_selected");
  296. hb->add_child(preset);
  297. import_opts = memnew(PropertyEditor);
  298. add_child(import_opts);
  299. import_opts->set_v_size_flags(SIZE_EXPAND_FILL);
  300. import_opts->hide_top_label();
  301. hb = memnew(HBoxContainer);
  302. add_child(hb);
  303. import = memnew(Button);
  304. import->set_text(TTR("Reimport"));
  305. import->connect("pressed", this, "_reimport");
  306. hb->add_spacer();
  307. hb->add_child(import);
  308. hb->add_spacer();
  309. params = memnew(ImportDockParameters);
  310. }
  311. ImportDock::~ImportDock() {
  312. memdelete(params);
  313. }