fbx_importer_manager.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**************************************************************************/
  2. /* fbx_importer_manager.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 "fbx_importer_manager.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_string_names.h"
  36. #include "scene/gui/link_button.h"
  37. void FBXImporterManager::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_READY: {
  40. connect("confirmed", callable_mp(this, &FBXImporterManager::_path_confirmed));
  41. } break;
  42. }
  43. }
  44. void FBXImporterManager::show_dialog(bool p_exclusive) {
  45. String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");
  46. fbx_path->set_text(fbx2gltf_path);
  47. _validate_path(fbx2gltf_path);
  48. // If exclusive, we're importing a FBX file, there's no exit.
  49. is_importing = p_exclusive;
  50. set_flag(Window::FLAG_BORDERLESS, p_exclusive); // Avoid closing accidentally.
  51. set_close_on_escape(!p_exclusive);
  52. if (is_importing) {
  53. get_cancel_button()->set_text(TTR("Disable FBX & Restart"));
  54. get_cancel_button()->set_tooltip_text(TTR("Canceling this dialog will disable the FBX importer.\nYou can re-enable it in the Project Settings under Filesystem > Import > FBX > Enabled.\n\nThe editor will restart as importers are registered when the editor starts."));
  55. } else {
  56. get_cancel_button()->set_text(TTR("Cancel"));
  57. get_cancel_button()->set_tooltip_text("");
  58. }
  59. popup_centered();
  60. }
  61. void FBXImporterManager::_validate_path(const String &p_path) {
  62. String error;
  63. bool success = false;
  64. if (p_path == "") {
  65. error = TTR("Path to FBX2glTF executable is empty.");
  66. } else if (!FileAccess::exists(p_path)) {
  67. error = TTR("Path to FBX2glTF executable is invalid.");
  68. } else {
  69. List<String> args;
  70. args.push_back("--version");
  71. int exitcode;
  72. Error err = OS::get_singleton()->execute(p_path, args, nullptr, &exitcode);
  73. if (err == OK && exitcode == 0) {
  74. success = true;
  75. } else {
  76. error = TTR("Error executing this file (wrong version or architecture).");
  77. }
  78. }
  79. if (success) {
  80. path_status->set_text(TTR("FBX2glTF executable is valid."));
  81. path_status->add_theme_color_override("font_color", path_status->get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
  82. get_ok_button()->set_disabled(false);
  83. } else {
  84. path_status->set_text(error);
  85. path_status->add_theme_color_override("font_color", path_status->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  86. get_ok_button()->set_disabled(true);
  87. }
  88. }
  89. void FBXImporterManager::_select_file(const String &p_path) {
  90. fbx_path->set_text(p_path);
  91. _validate_path(p_path);
  92. }
  93. void FBXImporterManager::_path_confirmed() {
  94. String path = fbx_path->get_text();
  95. EditorSettings::get_singleton()->set("filesystem/import/fbx/fbx2gltf_path", path);
  96. EditorSettings::get_singleton()->save();
  97. }
  98. void FBXImporterManager::_cancel_setup() {
  99. if (!is_importing) {
  100. return; // No worry.
  101. }
  102. // No escape.
  103. ProjectSettings::get_singleton()->set("filesystem/import/fbx/enabled", false);
  104. ProjectSettings::get_singleton()->save();
  105. EditorNode::get_singleton()->save_all_scenes();
  106. EditorNode::get_singleton()->restart_editor();
  107. }
  108. void FBXImporterManager::_browse_install() {
  109. if (fbx_path->get_text() != String()) {
  110. browse_dialog->set_current_file(fbx_path->get_text());
  111. }
  112. browse_dialog->popup_centered_ratio();
  113. }
  114. FBXImporterManager *FBXImporterManager::singleton = nullptr;
  115. FBXImporterManager::FBXImporterManager() {
  116. singleton = this;
  117. set_title(TTR("Configure FBX Importer"));
  118. VBoxContainer *vb = memnew(VBoxContainer);
  119. vb->add_child(memnew(Label(TTR("FBX2glTF is required for importing FBX files.\nPlease download it and provide a valid path to the binary:"))));
  120. LinkButton *lb = memnew(LinkButton);
  121. lb->set_text(TTR("Click this link to download FBX2glTF"));
  122. lb->set_uri("https://godotengine.org/fbx-import");
  123. vb->add_child(lb);
  124. HBoxContainer *hb = memnew(HBoxContainer);
  125. fbx_path = memnew(LineEdit);
  126. fbx_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  127. hb->add_child(fbx_path);
  128. fbx_path_browse = memnew(Button);
  129. hb->add_child(fbx_path_browse);
  130. fbx_path_browse->set_text(TTR("Browse"));
  131. fbx_path_browse->connect("pressed", callable_mp(this, &FBXImporterManager::_browse_install));
  132. hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  133. hb->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
  134. vb->add_child(hb);
  135. path_status = memnew(Label);
  136. vb->add_child(path_status);
  137. add_child(vb);
  138. fbx_path->connect("text_changed", callable_mp(this, &FBXImporterManager::_validate_path));
  139. get_ok_button()->set_text(TTR("Confirm Path"));
  140. get_cancel_button()->connect("pressed", callable_mp(this, &FBXImporterManager::_cancel_setup));
  141. browse_dialog = memnew(EditorFileDialog);
  142. browse_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  143. browse_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  144. #ifdef WINDOWS_ENABLED
  145. browse_dialog->add_filter("*.exe");
  146. #endif
  147. browse_dialog->connect("file_selected", callable_mp(this, &FBXImporterManager::_select_file));
  148. add_child(browse_dialog);
  149. }