scene_create_dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/create_dialog.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "scene/2d/node_2d.h"
  36. #include "scene/3d/node_3d.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/check_box.h"
  39. #include "scene/gui/grid_container.h"
  40. #include "scene/gui/line_edit.h"
  41. #include "scene/gui/option_button.h"
  42. #include "scene/gui/panel_container.h"
  43. #include "scene/resources/packed_scene.h"
  44. void SceneCreateDialog::_notification(int p_what) {
  45. switch (p_what) {
  46. case NOTIFICATION_ENTER_TREE:
  47. case NOTIFICATION_THEME_CHANGED: {
  48. select_node_button->set_icon(get_theme_icon(SNAME("ClassList"), SNAME("EditorIcons")));
  49. node_type_2d->set_icon(get_theme_icon(SNAME("Node2D"), SNAME("EditorIcons")));
  50. node_type_3d->set_icon(get_theme_icon(SNAME("Node3D"), SNAME("EditorIcons")));
  51. node_type_gui->set_icon(get_theme_icon(SNAME("Control"), SNAME("EditorIcons")));
  52. node_type_other->add_theme_icon_override(SNAME("icon"), get_theme_icon(SNAME("Node"), SNAME("EditorIcons")));
  53. status_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  54. } break;
  55. }
  56. }
  57. void SceneCreateDialog::config(const String &p_dir) {
  58. directory = p_dir;
  59. root_name_edit->set_text("");
  60. scene_name_edit->set_text("");
  61. scene_name_edit->call_deferred(SNAME("grab_focus"));
  62. update_dialog();
  63. }
  64. void SceneCreateDialog::accept_create() {
  65. if (!get_ok_button()->is_disabled()) {
  66. hide();
  67. emit_signal(SNAME("confirmed"));
  68. }
  69. }
  70. void SceneCreateDialog::browse_types() {
  71. select_node_dialog->popup_create(true);
  72. select_node_dialog->set_title(TTR("Pick Root Node Type"));
  73. select_node_dialog->set_ok_button_text(TTR("Pick"));
  74. }
  75. void SceneCreateDialog::on_type_picked() {
  76. other_type_display->set_text(select_node_dialog->get_selected_type().get_slice(" ", 0));
  77. if (node_type_other->is_pressed()) {
  78. update_dialog();
  79. } else {
  80. node_type_other->set_pressed(true); // Calls update_dialog() via group.
  81. }
  82. }
  83. void SceneCreateDialog::update_dialog() {
  84. scene_name = scene_name_edit->get_text().strip_edges();
  85. update_error(file_error_label, MSG_OK, TTR("Scene name is valid."));
  86. bool is_valid = true;
  87. if (scene_name.is_empty()) {
  88. update_error(file_error_label, MSG_ERROR, TTR("Scene name is empty."));
  89. is_valid = false;
  90. }
  91. if (is_valid) {
  92. if (!scene_name.ends_with(".")) {
  93. scene_name += ".";
  94. }
  95. scene_name += scene_extension_picker->get_selected_metadata().operator String();
  96. }
  97. if (is_valid && !scene_name.is_valid_filename()) {
  98. update_error(file_error_label, MSG_ERROR, TTR("File name invalid."));
  99. is_valid = false;
  100. }
  101. if (is_valid) {
  102. scene_name = directory.path_join(scene_name);
  103. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  104. if (da->file_exists(scene_name)) {
  105. update_error(file_error_label, MSG_ERROR, TTR("File already exists."));
  106. is_valid = false;
  107. }
  108. }
  109. const StringName root_type_name = StringName(other_type_display->get_text());
  110. if (has_theme_icon(root_type_name, SNAME("EditorIcons"))) {
  111. node_type_other->set_icon(get_theme_icon(root_type_name, SNAME("EditorIcons")));
  112. } else {
  113. node_type_other->set_icon(nullptr);
  114. }
  115. update_error(node_error_label, MSG_OK, TTR("Root node valid."));
  116. root_name = root_name_edit->get_text().strip_edges();
  117. if (root_name.is_empty()) {
  118. root_name = scene_name.get_file().get_basename();
  119. }
  120. if (root_name.is_empty() || root_name.validate_node_name().size() != root_name.size()) {
  121. update_error(node_error_label, MSG_ERROR, TTR("Invalid root node name."));
  122. is_valid = false;
  123. }
  124. get_ok_button()->set_disabled(!is_valid);
  125. }
  126. void SceneCreateDialog::update_error(Label *p_label, MsgType p_type, const String &p_msg) {
  127. p_label->set_text(String::utf8("• ") + p_msg);
  128. switch (p_type) {
  129. case MSG_OK:
  130. p_label->add_theme_color_override("font_color", get_theme_color(SNAME("success_color"), SNAME("Editor")));
  131. break;
  132. case MSG_ERROR:
  133. p_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  134. break;
  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->select_base();
  171. select_node_dialog->connect("create", callable_mp(this, &SceneCreateDialog::on_type_picked));
  172. VBoxContainer *main_vb = memnew(VBoxContainer);
  173. add_child(main_vb);
  174. GridContainer *gc = memnew(GridContainer);
  175. main_vb->add_child(gc);
  176. gc->set_columns(2);
  177. {
  178. Label *label = memnew(Label(TTR("Root Type:")));
  179. gc->add_child(label);
  180. label->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
  181. VBoxContainer *vb = memnew(VBoxContainer);
  182. gc->add_child(vb);
  183. node_type_group.instantiate();
  184. node_type_2d = memnew(CheckBox);
  185. vb->add_child(node_type_2d);
  186. node_type_2d->set_text(TTR("2D Scene"));
  187. node_type_2d->set_button_group(node_type_group);
  188. node_type_2d->set_meta(type_meta, ROOT_2D_SCENE);
  189. node_type_2d->set_pressed(true);
  190. node_type_3d = memnew(CheckBox);
  191. vb->add_child(node_type_3d);
  192. node_type_3d->set_text(TTR("3D Scene"));
  193. node_type_3d->set_button_group(node_type_group);
  194. node_type_3d->set_meta(type_meta, ROOT_3D_SCENE);
  195. node_type_gui = memnew(CheckBox);
  196. vb->add_child(node_type_gui);
  197. node_type_gui->set_text(TTR("User Interface"));
  198. node_type_gui->set_button_group(node_type_group);
  199. node_type_gui->set_meta(type_meta, ROOT_USER_INTERFACE);
  200. HBoxContainer *hb = memnew(HBoxContainer);
  201. vb->add_child(hb);
  202. node_type_other = memnew(CheckBox);
  203. hb->add_child(node_type_other);
  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. hb->add_child(other_type_display);
  211. other_type_display->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  212. other_type_display->set_editable(false);
  213. other_type_display->set_text("Node");
  214. select_node_button = memnew(Button);
  215. hb->add_child(select_node_button);
  216. select_node_button->connect("pressed", callable_mp(this, &SceneCreateDialog::browse_types));
  217. node_type_group->connect("pressed", callable_mp(this, &SceneCreateDialog::update_dialog).unbind(1));
  218. }
  219. {
  220. Label *label = memnew(Label(TTR("Scene Name:")));
  221. gc->add_child(label);
  222. HBoxContainer *hb = memnew(HBoxContainer);
  223. gc->add_child(hb);
  224. scene_name_edit = memnew(LineEdit);
  225. hb->add_child(scene_name_edit);
  226. scene_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  227. scene_name_edit->connect("text_changed", callable_mp(this, &SceneCreateDialog::update_dialog).unbind(1));
  228. scene_name_edit->connect("text_submitted", callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
  229. List<String> extensions;
  230. Ref<PackedScene> sd = memnew(PackedScene);
  231. ResourceSaver::get_recognized_extensions(sd, &extensions);
  232. scene_extension_picker = memnew(OptionButton);
  233. hb->add_child(scene_extension_picker);
  234. for (const String &E : extensions) {
  235. scene_extension_picker->add_item("." + E);
  236. scene_extension_picker->set_item_metadata(-1, E);
  237. }
  238. }
  239. {
  240. Label *label = memnew(Label(TTR("Root Name:")));
  241. gc->add_child(label);
  242. root_name_edit = memnew(LineEdit);
  243. gc->add_child(root_name_edit);
  244. root_name_edit->set_placeholder(TTR("Leave empty to use scene name"));
  245. root_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  246. root_name_edit->connect("text_changed", callable_mp(this, &SceneCreateDialog::update_dialog).unbind(1));
  247. root_name_edit->connect("text_submitted", callable_mp(this, &SceneCreateDialog::accept_create).unbind(1));
  248. }
  249. Control *spacing = memnew(Control);
  250. main_vb->add_child(spacing);
  251. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  252. status_panel = memnew(PanelContainer);
  253. main_vb->add_child(status_panel);
  254. status_panel->set_h_size_flags(Control::SIZE_FILL);
  255. status_panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  256. VBoxContainer *status_vb = memnew(VBoxContainer);
  257. status_panel->add_child(status_vb);
  258. file_error_label = memnew(Label);
  259. status_vb->add_child(file_error_label);
  260. node_error_label = memnew(Label);
  261. status_vb->add_child(node_error_label);
  262. set_title(TTR("Create New Scene"));
  263. set_min_size(Size2i(400 * EDSCALE, 0));
  264. }