import_dock.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*************************************************************************/
  2. /* import_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "editor_resource_preview.h"
  33. class ImportDockParameters : public Object {
  34. GDCLASS(ImportDockParameters, Object);
  35. public:
  36. Map<StringName, Variant> values;
  37. List<PropertyInfo> properties;
  38. Ref<ResourceImporter> importer;
  39. Vector<String> paths;
  40. Set<StringName> checked;
  41. bool checking;
  42. bool _set(const StringName &p_name, const Variant &p_value) {
  43. if (values.has(p_name)) {
  44. values[p_name] = p_value;
  45. if (checking) {
  46. checked.insert(p_name);
  47. _change_notify();
  48. }
  49. return true;
  50. }
  51. return false;
  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. }
  58. return false;
  59. }
  60. void _get_property_list(List<PropertyInfo> *p_list) const {
  61. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  62. if (!importer->get_option_visibility(E->get().name, values))
  63. continue;
  64. PropertyInfo pi = E->get();
  65. if (checking) {
  66. pi.usage |= PROPERTY_USAGE_CHECKABLE;
  67. if (checked.has(E->get().name)) {
  68. pi.usage |= PROPERTY_USAGE_CHECKED;
  69. }
  70. }
  71. p_list->push_back(pi);
  72. }
  73. }
  74. void update() {
  75. _change_notify();
  76. }
  77. ImportDockParameters() {
  78. checking = false;
  79. }
  80. };
  81. void ImportDock::set_edit_path(const String &p_path) {
  82. Ref<ConfigFile> config;
  83. config.instance();
  84. Error err = config->load(p_path + ".import");
  85. if (err != OK) {
  86. clear();
  87. return;
  88. }
  89. params->importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(config->get_value("remap", "importer"));
  90. if (params->importer.is_null()) {
  91. clear();
  92. return;
  93. }
  94. params->paths.clear();
  95. params->paths.push_back(p_path);
  96. _update_options(config);
  97. List<Ref<ResourceImporter> > importers;
  98. ResourceFormatImporter::get_singleton()->get_importers_for_extension(p_path.get_extension(), &importers);
  99. List<Pair<String, String> > importer_names;
  100. for (List<Ref<ResourceImporter> >::Element *E = importers.front(); E; E = E->next()) {
  101. importer_names.push_back(Pair<String, String>(E->get()->get_visible_name(), E->get()->get_importer_name()));
  102. }
  103. importer_names.sort_custom<PairSort<String, String> >();
  104. import_as->clear();
  105. for (List<Pair<String, String> >::Element *E = importer_names.front(); E; E = E->next()) {
  106. import_as->add_item(E->get().first);
  107. import_as->set_item_metadata(import_as->get_item_count() - 1, E->get().second);
  108. if (E->get().second == params->importer->get_importer_name()) {
  109. import_as->select(import_as->get_item_count() - 1);
  110. }
  111. }
  112. import->set_disabled(false);
  113. import_as->set_disabled(false);
  114. preset->set_disabled(false);
  115. imported->set_text(p_path.get_file());
  116. }
  117. void ImportDock::_update_options(const Ref<ConfigFile> &p_config) {
  118. List<ResourceImporter::ImportOption> options;
  119. params->importer->get_import_options(&options);
  120. params->properties.clear();
  121. params->values.clear();
  122. params->checking = params->paths.size() > 1;
  123. params->checked.clear();
  124. for (List<ResourceImporter::ImportOption>::Element *E = options.front(); E; E = E->next()) {
  125. params->properties.push_back(E->get().option);
  126. if (p_config.is_valid() && p_config->has_section_key("params", E->get().option.name)) {
  127. params->values[E->get().option.name] = p_config->get_value("params", E->get().option.name);
  128. } else {
  129. params->values[E->get().option.name] = E->get().default_value;
  130. }
  131. }
  132. params->update();
  133. _update_preset_menu();
  134. }
  135. void ImportDock::set_edit_multiple_paths(const Vector<String> &p_paths) {
  136. clear();
  137. // Use the value that is repeated the most.
  138. Map<String, Dictionary> value_frequency;
  139. for (int i = 0; i < p_paths.size(); i++) {
  140. Ref<ConfigFile> config;
  141. config.instance();
  142. Error err = config->load(p_paths[i] + ".import");
  143. ERR_CONTINUE(err != OK);
  144. if (i == 0) {
  145. params->importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(config->get_value("remap", "importer"));
  146. if (params->importer.is_null()) {
  147. clear();
  148. return;
  149. }
  150. }
  151. List<String> keys;
  152. config->get_section_keys("params", &keys);
  153. for (List<String>::Element *E = keys.front(); E; E = E->next()) {
  154. if (!value_frequency.has(E->get())) {
  155. value_frequency[E->get()] = Dictionary();
  156. }
  157. Variant value = config->get_value("params", E->get());
  158. if (value_frequency[E->get()].has(value)) {
  159. value_frequency[E->get()][value] = int(value_frequency[E->get()][value]) + 1;
  160. } else {
  161. value_frequency[E->get()][value] = 1;
  162. }
  163. }
  164. }
  165. ERR_FAIL_COND(params->importer.is_null());
  166. List<ResourceImporter::ImportOption> options;
  167. params->importer->get_import_options(&options);
  168. params->properties.clear();
  169. params->values.clear();
  170. params->checking = true;
  171. params->checked.clear();
  172. for (List<ResourceImporter::ImportOption>::Element *E = options.front(); E; E = E->next()) {
  173. params->properties.push_back(E->get().option);
  174. if (value_frequency.has(E->get().option.name)) {
  175. Dictionary d = value_frequency[E->get().option.name];
  176. int freq = 0;
  177. List<Variant> v;
  178. d.get_key_list(&v);
  179. Variant value;
  180. for (List<Variant>::Element *F = v.front(); F; F = F->next()) {
  181. int f = d[F->get()];
  182. if (f > freq) {
  183. value = F->get();
  184. }
  185. }
  186. params->values[E->get().option.name] = value;
  187. } else {
  188. params->values[E->get().option.name] = E->get().default_value;
  189. }
  190. }
  191. params->update();
  192. List<Ref<ResourceImporter> > importers;
  193. ResourceFormatImporter::get_singleton()->get_importers_for_extension(p_paths[0].get_extension(), &importers);
  194. List<Pair<String, String> > importer_names;
  195. for (List<Ref<ResourceImporter> >::Element *E = importers.front(); E; E = E->next()) {
  196. importer_names.push_back(Pair<String, String>(E->get()->get_visible_name(), E->get()->get_importer_name()));
  197. }
  198. importer_names.sort_custom<PairSort<String, String> >();
  199. import_as->clear();
  200. for (List<Pair<String, String> >::Element *E = importer_names.front(); E; E = E->next()) {
  201. import_as->add_item(E->get().first);
  202. import_as->set_item_metadata(import_as->get_item_count() - 1, E->get().second);
  203. if (E->get().second == params->importer->get_importer_name()) {
  204. import_as->select(import_as->get_item_count() - 1);
  205. }
  206. }
  207. _update_preset_menu();
  208. params->paths = p_paths;
  209. import->set_disabled(false);
  210. import_as->set_disabled(false);
  211. preset->set_disabled(false);
  212. imported->set_text(vformat(TTR("%d Files"), p_paths.size()));
  213. }
  214. void ImportDock::_update_preset_menu() {
  215. preset->get_popup()->clear();
  216. if (params->importer->get_preset_count() == 0) {
  217. preset->get_popup()->add_item(TTR("Default"));
  218. } else {
  219. for (int i = 0; i < params->importer->get_preset_count(); i++) {
  220. preset->get_popup()->add_item(params->importer->get_preset_name(i));
  221. }
  222. }
  223. preset->get_popup()->add_separator();
  224. preset->get_popup()->add_item(vformat(TTR("Set as Default for '%s'"), params->importer->get_visible_name()), ITEM_SET_AS_DEFAULT);
  225. if (ProjectSettings::get_singleton()->has_setting("importer_defaults/" + params->importer->get_importer_name())) {
  226. preset->get_popup()->add_item(TTR("Load Default"), ITEM_LOAD_DEFAULT);
  227. preset->get_popup()->add_separator();
  228. preset->get_popup()->add_item(vformat(TTR("Clear Default for '%s'"), params->importer->get_visible_name()), ITEM_CLEAR_DEFAULT);
  229. }
  230. }
  231. void ImportDock::_importer_selected(int i_idx) {
  232. String name = import_as->get_selected_metadata();
  233. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(name);
  234. ERR_FAIL_COND(importer.is_null());
  235. params->importer = importer;
  236. Ref<ConfigFile> config;
  237. if (params->paths.size()) {
  238. config.instance();
  239. Error err = config->load(params->paths[0] + ".import");
  240. if (err != OK) {
  241. config.unref();
  242. }
  243. }
  244. _update_options(config);
  245. }
  246. void ImportDock::_preset_selected(int p_idx) {
  247. int item_id = preset->get_popup()->get_item_id(p_idx);
  248. switch (item_id) {
  249. case ITEM_SET_AS_DEFAULT: {
  250. Dictionary d;
  251. for (const List<PropertyInfo>::Element *E = params->properties.front(); E; E = E->next()) {
  252. d[E->get().name] = params->values[E->get().name];
  253. }
  254. ProjectSettings::get_singleton()->set("importer_defaults/" + params->importer->get_importer_name(), d);
  255. ProjectSettings::get_singleton()->save();
  256. _update_preset_menu();
  257. } break;
  258. case ITEM_LOAD_DEFAULT: {
  259. ERR_FAIL_COND(!ProjectSettings::get_singleton()->has_setting("importer_defaults/" + params->importer->get_importer_name()));
  260. Dictionary d = ProjectSettings::get_singleton()->get("importer_defaults/" + params->importer->get_importer_name());
  261. List<Variant> v;
  262. d.get_key_list(&v);
  263. if (params->checking) {
  264. params->checked.clear();
  265. }
  266. for (List<Variant>::Element *E = v.front(); E; E = E->next()) {
  267. params->values[E->get()] = d[E->get()];
  268. if (params->checking) {
  269. params->checked.insert(E->get());
  270. }
  271. }
  272. params->update();
  273. } break;
  274. case ITEM_CLEAR_DEFAULT: {
  275. ProjectSettings::get_singleton()->set("importer_defaults/" + params->importer->get_importer_name(), Variant());
  276. ProjectSettings::get_singleton()->save();
  277. _update_preset_menu();
  278. } break;
  279. default: {
  280. List<ResourceImporter::ImportOption> options;
  281. params->importer->get_import_options(&options, p_idx);
  282. if (params->checking) {
  283. params->checked.clear();
  284. }
  285. for (List<ResourceImporter::ImportOption>::Element *E = options.front(); E; E = E->next()) {
  286. params->values[E->get().option.name] = E->get().default_value;
  287. if (params->checking) {
  288. params->checked.insert(E->get().option.name);
  289. }
  290. }
  291. params->update();
  292. } break;
  293. }
  294. }
  295. void ImportDock::clear() {
  296. imported->set_text("");
  297. import->set_disabled(true);
  298. import_as->clear();
  299. import_as->set_disabled(true);
  300. preset->set_disabled(true);
  301. params->values.clear();
  302. params->properties.clear();
  303. params->update();
  304. preset->get_popup()->clear();
  305. }
  306. static bool _find_owners(EditorFileSystemDirectory *efsd, const String &p_path) {
  307. if (!efsd)
  308. return false;
  309. for (int i = 0; i < efsd->get_subdir_count(); i++) {
  310. if (_find_owners(efsd->get_subdir(i), p_path)) {
  311. return true;
  312. }
  313. }
  314. for (int i = 0; i < efsd->get_file_count(); i++) {
  315. Vector<String> deps = efsd->get_file_deps(i);
  316. if (deps.find(p_path) != -1)
  317. return true;
  318. }
  319. return false;
  320. }
  321. void ImportDock::_reimport_attempt() {
  322. bool need_restart = false;
  323. bool used_in_resources = false;
  324. for (int i = 0; i < params->paths.size(); i++) {
  325. Ref<ConfigFile> config;
  326. config.instance();
  327. Error err = config->load(params->paths[i] + ".import");
  328. ERR_CONTINUE(err != OK);
  329. String imported_with = config->get_value("remap", "importer");
  330. if (imported_with != params->importer->get_importer_name()) {
  331. need_restart = true;
  332. if (_find_owners(EditorFileSystem::get_singleton()->get_filesystem(), params->paths[i])) {
  333. used_in_resources = true;
  334. }
  335. }
  336. }
  337. if (need_restart) {
  338. label_warning->set_visible(used_in_resources);
  339. reimport_confirm->popup_centered_minsize();
  340. return;
  341. }
  342. _reimport();
  343. }
  344. void ImportDock::_reimport_and_restart() {
  345. EditorNode::get_singleton()->save_all_scenes();
  346. EditorResourcePreview::get_singleton()->stop(); //don't try to re-create previews after import
  347. _reimport();
  348. EditorNode::get_singleton()->restart_editor();
  349. }
  350. void ImportDock::_reimport() {
  351. for (int i = 0; i < params->paths.size(); i++) {
  352. Ref<ConfigFile> config;
  353. config.instance();
  354. Error err = config->load(params->paths[i] + ".import");
  355. ERR_CONTINUE(err != OK);
  356. String importer_name = params->importer->get_importer_name();
  357. if (params->checking && config->get_value("remap", "importer") == params->importer->get_importer_name()) {
  358. //update only what is edited (checkboxes) if the importer is the same
  359. for (List<PropertyInfo>::Element *E = params->properties.front(); E; E = E->next()) {
  360. if (params->checked.has(E->get().name)) {
  361. config->set_value("params", E->get().name, params->values[E->get().name]);
  362. }
  363. }
  364. } else {
  365. //override entirely
  366. config->set_value("remap", "importer", importer_name);
  367. if (config->has_section("params")) {
  368. config->erase_section("params");
  369. }
  370. for (List<PropertyInfo>::Element *E = params->properties.front(); E; E = E->next()) {
  371. config->set_value("params", E->get().name, params->values[E->get().name]);
  372. }
  373. }
  374. //handle group file
  375. Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
  376. ERR_CONTINUE(!importer.is_valid());
  377. String group_file_property = importer->get_option_group_file();
  378. if (group_file_property != String()) {
  379. //can import from a group (as in, atlas)
  380. ERR_CONTINUE(!params->values.has(group_file_property));
  381. String group_file = params->values[group_file_property];
  382. config->set_value("remap", "group_file", group_file);
  383. } else {
  384. config->set_value("remap", "group_file", Variant()); //clear group file if unused
  385. }
  386. config->save(params->paths[i] + ".import");
  387. }
  388. EditorFileSystem::get_singleton()->reimport_files(params->paths);
  389. EditorFileSystem::get_singleton()->emit_signal("filesystem_changed"); //it changed, so force emitting the signal
  390. }
  391. void ImportDock::_notification(int p_what) {
  392. switch (p_what) {
  393. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  394. imported->add_style_override("normal", get_stylebox("normal", "LineEdit"));
  395. } break;
  396. case NOTIFICATION_ENTER_TREE: {
  397. import_opts->edit(params);
  398. label_warning->add_color_override("font_color", get_color("warning_color", "Editor"));
  399. } break;
  400. }
  401. }
  402. void ImportDock::_property_toggled(const StringName &p_prop, bool p_checked) {
  403. if (p_checked) {
  404. params->checked.insert(p_prop);
  405. } else {
  406. params->checked.erase(p_prop);
  407. }
  408. }
  409. void ImportDock::_bind_methods() {
  410. ClassDB::bind_method(D_METHOD("_reimport"), &ImportDock::_reimport);
  411. ClassDB::bind_method(D_METHOD("_preset_selected"), &ImportDock::_preset_selected);
  412. ClassDB::bind_method(D_METHOD("_importer_selected"), &ImportDock::_importer_selected);
  413. ClassDB::bind_method(D_METHOD("_property_toggled"), &ImportDock::_property_toggled);
  414. ClassDB::bind_method(D_METHOD("_reimport_and_restart"), &ImportDock::_reimport_and_restart);
  415. ClassDB::bind_method(D_METHOD("_reimport_attempt"), &ImportDock::_reimport_attempt);
  416. }
  417. void ImportDock::initialize_import_options() const {
  418. ERR_FAIL_COND(!import_opts || !params);
  419. import_opts->edit(params);
  420. }
  421. ImportDock::ImportDock() {
  422. set_name("Import");
  423. imported = memnew(Label);
  424. imported->add_style_override("normal", EditorNode::get_singleton()->get_gui_base()->get_stylebox("normal", "LineEdit"));
  425. imported->set_clip_text(true);
  426. add_child(imported);
  427. HBoxContainer *hb = memnew(HBoxContainer);
  428. add_margin_child(TTR("Import As:"), hb);
  429. import_as = memnew(OptionButton);
  430. import_as->set_disabled(true);
  431. import_as->connect("item_selected", this, "_importer_selected");
  432. hb->add_child(import_as);
  433. import_as->set_h_size_flags(SIZE_EXPAND_FILL);
  434. preset = memnew(MenuButton);
  435. preset->set_text(TTR("Preset"));
  436. preset->set_disabled(true);
  437. preset->get_popup()->connect("index_pressed", this, "_preset_selected");
  438. hb->add_child(preset);
  439. import_opts = memnew(EditorInspector);
  440. add_child(import_opts);
  441. import_opts->set_v_size_flags(SIZE_EXPAND_FILL);
  442. import_opts->connect("property_toggled", this, "_property_toggled");
  443. hb = memnew(HBoxContainer);
  444. add_child(hb);
  445. import = memnew(Button);
  446. import->set_text(TTR("Reimport"));
  447. import->set_disabled(true);
  448. import->connect("pressed", this, "_reimport_attempt");
  449. hb->add_spacer();
  450. hb->add_child(import);
  451. hb->add_spacer();
  452. reimport_confirm = memnew(ConfirmationDialog);
  453. reimport_confirm->get_ok()->set_text(TTR("Save Scenes, Re-Import, and Restart"));
  454. add_child(reimport_confirm);
  455. reimport_confirm->connect("confirmed", this, "_reimport_and_restart");
  456. VBoxContainer *vbc_confirm = memnew(VBoxContainer());
  457. vbc_confirm->add_child(memnew(Label(TTR("Changing the type of an imported file requires editor restart."))));
  458. label_warning = memnew(Label(TTR("WARNING: Assets exist that use this resource, they may stop loading properly.")));
  459. vbc_confirm->add_child(label_warning);
  460. reimport_confirm->add_child(vbc_confirm);
  461. params = memnew(ImportDockParameters);
  462. }
  463. ImportDock::~ImportDock() {
  464. memdelete(params);
  465. }