import_dock.cpp 15 KB

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