scene_create_dialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**************************************************************************/
  2. /* scene_create_dialog.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 "scene_create_dialog.h"
  31. #include "core/io/dir_access.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/gui/create_dialog.h"
  35. #include "editor/gui/editor_validation_panel.h"
  36. #include "editor/settings/editor_feature_profile.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/2d/node_2d.h"
  39. #include "scene/3d/node_3d.h"
  40. #include "scene/gui/box_container.h"
  41. #include "scene/gui/check_box.h"
  42. #include "scene/gui/grid_container.h"
  43. #include "scene/gui/line_edit.h"
  44. #include "scene/gui/option_button.h"
  45. #include "scene/resources/packed_scene.h"
  46. void SceneCreateDialog::_notification(int p_what) {
  47. switch (p_what) {
  48. case NOTIFICATION_THEME_CHANGED: {
  49. select_node_button->set_button_icon(get_editor_theme_icon(SNAME("ClassList")));
  50. node_type_2d->set_button_icon(get_editor_theme_icon(SNAME("Node2D")));
  51. node_type_3d->set_button_icon(get_editor_theme_icon(SNAME("Node3D")));
  52. node_type_gui->set_button_icon(get_editor_theme_icon(SNAME("Control")));
  53. node_type_other->add_theme_icon_override(SNAME("icon"), get_editor_theme_icon(SNAME("Node")));
  54. } break;
  55. case NOTIFICATION_READY: {
  56. select_node_dialog->select_base();
  57. } break;
  58. }
  59. }
  60. void SceneCreateDialog::config(const String &p_dir) {
  61. directory = p_dir;
  62. root_name_edit->set_text("");
  63. scene_name_edit->set_text("");
  64. callable_mp((Control *)scene_name_edit, &Control::grab_focus).call_deferred();
  65. validation_panel->update();
  66. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  67. node_type_3d->set_visible(profile.is_null() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D));
  68. if (!node_type_3d->is_visible() && node_type_3d->is_pressed()) {
  69. node_type_2d->set_pressed(true);
  70. }
  71. }
  72. void SceneCreateDialog::accept_create() {
  73. if (!get_ok_button()->is_disabled()) {
  74. hide();
  75. emit_signal(SceneStringName(confirmed));
  76. }
  77. }
  78. void SceneCreateDialog::browse_types() {
  79. select_node_dialog->popup_create(true);
  80. select_node_dialog->set_title(TTR("Pick Root Node Type"));
  81. select_node_dialog->set_ok_button_text(TTR("Pick"));
  82. }
  83. void SceneCreateDialog::on_type_picked() {
  84. other_type_display->set_text(select_node_dialog->get_selected_type().get_slicec(' ', 0));
  85. if (node_type_other->is_pressed()) {
  86. validation_panel->update();
  87. } else {
  88. node_type_other->set_pressed(true); // Calls validation_panel->update() via group.
  89. }
  90. }
  91. void SceneCreateDialog::update_dialog() {
  92. scene_name = scene_name_edit->get_text().strip_edges();
  93. if (scene_name.is_empty()) {
  94. validation_panel->set_message(MSG_ID_PATH, TTR("Scene name is empty."), EditorValidationPanel::MSG_ERROR);
  95. }
  96. if (validation_panel->is_valid()) {
  97. if (!scene_name.ends_with(".")) {
  98. scene_name += ".";
  99. }
  100. scene_name += scene_extension_picker->get_selected_metadata().operator String();
  101. }
  102. if (validation_panel->is_valid() && !scene_name.is_valid_filename()) {
  103. validation_panel->set_message(MSG_ID_PATH, TTR("File name invalid."), EditorValidationPanel::MSG_ERROR);
  104. } else if (validation_panel->is_valid() && scene_name[0] == '.') {
  105. validation_panel->set_message(MSG_ID_PATH, TTR("File name begins with a dot."), EditorValidationPanel::MSG_ERROR);
  106. }
  107. if (validation_panel->is_valid()) {
  108. scene_name = directory.path_join(scene_name);
  109. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  110. if (da->file_exists(scene_name)) {
  111. validation_panel->set_message(MSG_ID_PATH, TTR("File already exists."), EditorValidationPanel::MSG_ERROR);
  112. }
  113. }
  114. const StringName root_type_name = StringName(other_type_display->get_text());
  115. if (has_theme_icon(root_type_name, EditorStringName(EditorIcons))) {
  116. node_type_other->set_button_icon(get_editor_theme_icon(root_type_name));
  117. } else {
  118. node_type_other->set_button_icon(nullptr);
  119. }
  120. root_name = root_name_edit->get_text().strip_edges();
  121. if (root_name.is_empty()) {
  122. root_name = scene_name_edit->get_text().strip_edges();
  123. if (root_name.is_empty()) {
  124. root_name_edit->set_placeholder(TTR("Leave empty to derive from scene name"));
  125. } else {
  126. // Respect the desired root node casing from ProjectSettings.
  127. root_name = Node::adjust_name_casing(root_name);
  128. root_name_edit->set_placeholder(root_name.validate_node_name());
  129. }
  130. }
  131. if (root_name.is_empty()) {
  132. validation_panel->set_message(MSG_ID_ROOT, TTR("Invalid root node name."), EditorValidationPanel::MSG_ERROR);
  133. } else if (root_name != root_name.validate_node_name()) {
  134. validation_panel->set_message(MSG_ID_ROOT, TTR("Invalid root node name characters have been replaced."), EditorValidationPanel::MSG_WARNING);
  135. }
  136. }
  137. String SceneCreateDialog::get_scene_path() const {
  138. return scene_name;
  139. }
  140. Node *SceneCreateDialog::create_scene_root() {
  141. ERR_FAIL_NULL_V(node_type_group->get_pressed_button(), nullptr);
  142. RootType type = (RootType)node_type_group->get_pressed_button()->get_meta(type_meta).operator int();
  143. Node *root = nullptr;
  144. switch (type) {
  145. case ROOT_2D_SCENE:
  146. root = memnew(Node2D);
  147. break;
  148. case ROOT_3D_SCENE:
  149. root = memnew(Node3D);
  150. break;
  151. case ROOT_USER_INTERFACE: {
  152. Control *gui_ctl = memnew(Control);
  153. // Making the root control full rect by default is more useful for resizable UIs.
  154. gui_ctl->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  155. gui_ctl->set_grow_direction_preset(Control::PRESET_FULL_RECT);
  156. root = gui_ctl;
  157. } break;
  158. case ROOT_OTHER:
  159. root = Object::cast_to<Node>(select_node_dialog->instantiate_selected());
  160. break;
  161. }
  162. ERR_FAIL_NULL_V(root, nullptr);
  163. root->set_name(root_name);
  164. return root;
  165. }
  166. SceneCreateDialog::SceneCreateDialog() {
  167. select_node_dialog = memnew(CreateDialog);
  168. add_child(select_node_dialog);
  169. select_node_dialog->set_base_type("Node");
  170. select_node_dialog->connect("create", callable_mp(this, &SceneCreateDialog::on_type_picked));
  171. VBoxContainer *main_vb = memnew(VBoxContainer);
  172. add_child(main_vb);
  173. GridContainer *gc = memnew(GridContainer);
  174. main_vb->add_child(gc);
  175. gc->set_columns(2);
  176. {
  177. Label *label = memnew(Label(TTR("Root Type:")));
  178. gc->add_child(label);
  179. label->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
  180. VBoxContainer *vb = memnew(VBoxContainer);
  181. gc->add_child(vb);
  182. node_type_group.instantiate();
  183. node_type_2d = memnew(CheckBox);
  184. vb->add_child(node_type_2d);
  185. node_type_2d->set_text(TTR("2D Scene"));
  186. node_type_2d->set_button_group(node_type_group);
  187. node_type_2d->set_meta(type_meta, ROOT_2D_SCENE);
  188. node_type_2d->set_pressed(true);
  189. node_type_3d = memnew(CheckBox);
  190. vb->add_child(node_type_3d);
  191. node_type_3d->set_text(TTR("3D Scene"));
  192. node_type_3d->set_button_group(node_type_group);
  193. node_type_3d->set_meta(type_meta, ROOT_3D_SCENE);
  194. node_type_gui = memnew(CheckBox);
  195. vb->add_child(node_type_gui);
  196. node_type_gui->set_text(TTR("User Interface"));
  197. node_type_gui->set_button_group(node_type_group);
  198. node_type_gui->set_meta(type_meta, ROOT_USER_INTERFACE);
  199. HBoxContainer *hb = memnew(HBoxContainer);
  200. vb->add_child(hb);
  201. node_type_other = memnew(CheckBox);
  202. hb->add_child(node_type_other);
  203. node_type_other->set_accessibility_name(TTRC("Other Type"));
  204. node_type_other->set_button_group(node_type_group);
  205. node_type_other->set_meta(type_meta, ROOT_OTHER);
  206. Control *spacing = memnew(Control);
  207. hb->add_child(spacing);
  208. spacing->set_custom_minimum_size(Size2(4 * EDSCALE, 0));
  209. other_type_display = memnew(LineEdit);
  210. other_type_display->set_accessibility_name(TTRC("Other Type"));
  211. hb->add_child(other_type_display);
  212. other_type_display->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  213. other_type_display->set_editable(false);
  214. other_type_display->set_text("Node");
  215. select_node_button = memnew(Button);
  216. select_node_button->set_accessibility_name(TTRC("Select Node"));
  217. hb->add_child(select_node_button);
  218. select_node_button->connect(SceneStringName(pressed), callable_mp(this, &SceneCreateDialog::browse_types));
  219. }
  220. {
  221. Label *label = memnew(Label(TTR("Scene Name:")));
  222. gc->add_child(label);
  223. HBoxContainer *hb = memnew(HBoxContainer);
  224. gc->add_child(hb);
  225. scene_name_edit = memnew(LineEdit);
  226. hb->add_child(scene_name_edit);
  227. scene_name_edit->set_accessibility_name(TTRC("Scene Name:"));
  228. scene_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  229. scene_name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
  230. List<String> extensions;
  231. Ref<PackedScene> sd = memnew(PackedScene);
  232. ResourceSaver::get_recognized_extensions(sd, &extensions);
  233. scene_extension_picker = memnew(OptionButton);
  234. scene_extension_picker->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  235. hb->add_child(scene_extension_picker);
  236. for (const String &E : extensions) {
  237. scene_extension_picker->add_item("." + E);
  238. scene_extension_picker->set_item_metadata(-1, E);
  239. }
  240. }
  241. {
  242. Label *label = memnew(Label(TTR("Root Name:")));
  243. gc->add_child(label);
  244. root_name_edit = memnew(LineEdit);
  245. gc->add_child(root_name_edit);
  246. root_name_edit->set_tooltip_text(TTR("When empty, the root node name is derived from the scene name based on the \"editor/naming/node_name_casing\" project setting."));
  247. root_name_edit->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  248. root_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  249. root_name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
  250. }
  251. Control *spacing = memnew(Control);
  252. main_vb->add_child(spacing);
  253. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  254. validation_panel = memnew(EditorValidationPanel);
  255. main_vb->add_child(validation_panel);
  256. validation_panel->add_line(MSG_ID_PATH, TTR("Scene name is valid."));
  257. validation_panel->add_line(MSG_ID_ROOT, TTR("Root node valid."));
  258. validation_panel->set_update_callback(callable_mp(this, &SceneCreateDialog::update_dialog));
  259. validation_panel->set_accept_button(get_ok_button());
  260. node_type_group->connect(SceneStringName(pressed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  261. scene_name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  262. root_name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  263. set_title(TTR("Create New Scene"));
  264. set_min_size(Size2i(400 * EDSCALE, 0));
  265. }