scene_create_dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/os/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/spatial.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_icon("ClassList", "EditorIcons"));
  49. node_type_2d->set_icon(get_icon("Node2D", "EditorIcons"));
  50. node_type_3d->set_icon(get_icon("Spatial", "EditorIcons"));
  51. node_type_gui->set_icon(get_icon("Control", "EditorIcons"));
  52. node_type_other->add_icon_override("icon", get_icon("Node", "EditorIcons"));
  53. status_panel->add_style_override("panel", get_stylebox("bg", "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("grab_focus");
  62. update_dialog();
  63. }
  64. void SceneCreateDialog::accept_create(Variant p_discard) {
  65. if (!get_ok()->is_disabled()) {
  66. hide();
  67. emit_signal("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->get_ok()->set_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(Variant p_discard) {
  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.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 && scene_name[0] == '.') {
  102. update_error(file_error_label, MSG_ERROR, TTR("File name begins with a dot."));
  103. is_valid = false;
  104. }
  105. if (is_valid) {
  106. scene_name = directory.plus_file(scene_name);
  107. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  108. if (da->file_exists(scene_name)) {
  109. update_error(file_error_label, MSG_ERROR, TTR("File already exists."));
  110. is_valid = false;
  111. }
  112. }
  113. const StringName root_type_name = StringName(other_type_display->get_text());
  114. if (has_icon(root_type_name, "EditorIcons")) {
  115. node_type_other->set_icon(get_icon(root_type_name, "EditorIcons"));
  116. } else {
  117. node_type_other->set_icon(nullptr);
  118. }
  119. update_error(node_error_label, MSG_OK, "Root node valid.");
  120. root_name = root_name_edit->get_text().strip_edges();
  121. if (root_name.empty()) {
  122. root_name = scene_name.get_file().get_basename();
  123. }
  124. if (!root_name.is_valid_identifier()) {
  125. update_error(node_error_label, MSG_ERROR, TTR("Invalid root node name."));
  126. is_valid = false;
  127. }
  128. get_ok()->set_disabled(!is_valid);
  129. }
  130. void SceneCreateDialog::update_error(Label *p_label, MsgType p_type, const String &p_msg) {
  131. p_label->set_text(String::utf8("• ") + p_msg);
  132. switch (p_type) {
  133. case MSG_OK:
  134. p_label->add_color_override("font_color", get_color("success_color", "Editor"));
  135. break;
  136. case MSG_ERROR:
  137. p_label->add_color_override("font_color", get_color("error_color", "Editor"));
  138. break;
  139. }
  140. }
  141. String SceneCreateDialog::get_scene_path() const {
  142. return scene_name;
  143. }
  144. Node *SceneCreateDialog::create_scene_root() {
  145. ERR_FAIL_NULL_V(node_type_group->get_pressed_button(), nullptr);
  146. RootType type = (RootType)node_type_group->get_pressed_button()->get_meta(type_meta).operator int();
  147. Node *root = nullptr;
  148. switch (type) {
  149. case ROOT_2D_SCENE:
  150. root = memnew(Node2D);
  151. break;
  152. case ROOT_3D_SCENE:
  153. root = memnew(Spatial);
  154. break;
  155. case ROOT_USER_INTERFACE: {
  156. Control *gui = memnew(Control);
  157. gui->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  158. root = gui;
  159. } break;
  160. case ROOT_OTHER:
  161. root = Object::cast_to<Node>(select_node_dialog->instance_selected());
  162. break;
  163. }
  164. ERR_FAIL_NULL_V(root, nullptr);
  165. root->set_name(root_name);
  166. return root;
  167. }
  168. void SceneCreateDialog::_bind_methods() {
  169. ClassDB::bind_method(D_METHOD("on_type_picked"), &SceneCreateDialog::on_type_picked);
  170. ClassDB::bind_method(D_METHOD("browse_types"), &SceneCreateDialog::browse_types);
  171. ClassDB::bind_method(D_METHOD("update_dialog"), &SceneCreateDialog::update_dialog);
  172. ClassDB::bind_method(D_METHOD("accept_create"), &SceneCreateDialog::accept_create);
  173. }
  174. SceneCreateDialog::SceneCreateDialog() {
  175. select_node_dialog = memnew(CreateDialog);
  176. add_child(select_node_dialog);
  177. select_node_dialog->set_base_type("Node");
  178. select_node_dialog->select_base();
  179. select_node_dialog->connect("create", this, "on_type_picked");
  180. VBoxContainer *main_vb = memnew(VBoxContainer);
  181. add_child(main_vb);
  182. GridContainer *gc = memnew(GridContainer);
  183. main_vb->add_child(gc);
  184. gc->set_columns(2);
  185. {
  186. Label *label = memnew(Label(TTR("Root Type:")));
  187. gc->add_child(label);
  188. label->set_v_size_flags(0);
  189. VBoxContainer *vb = memnew(VBoxContainer);
  190. gc->add_child(vb);
  191. node_type_group.instance();
  192. node_type_2d = memnew(CheckBox);
  193. vb->add_child(node_type_2d);
  194. node_type_2d->set_text(TTR("2D Scene"));
  195. node_type_2d->set_button_group(node_type_group);
  196. node_type_2d->set_meta(type_meta, ROOT_2D_SCENE);
  197. node_type_2d->set_pressed(true);
  198. node_type_3d = memnew(CheckBox);
  199. vb->add_child(node_type_3d);
  200. node_type_3d->set_text(TTR("3D Scene"));
  201. node_type_3d->set_button_group(node_type_group);
  202. node_type_3d->set_meta(type_meta, ROOT_3D_SCENE);
  203. node_type_gui = memnew(CheckBox);
  204. vb->add_child(node_type_gui);
  205. node_type_gui->set_text(TTR("User Interface"));
  206. node_type_gui->set_button_group(node_type_group);
  207. node_type_gui->set_meta(type_meta, ROOT_USER_INTERFACE);
  208. HBoxContainer *hb = memnew(HBoxContainer);
  209. vb->add_child(hb);
  210. node_type_other = memnew(CheckBox);
  211. hb->add_child(node_type_other);
  212. node_type_other->set_button_group(node_type_group);
  213. node_type_other->set_meta(type_meta, ROOT_OTHER);
  214. Control *spacing = memnew(Control);
  215. hb->add_child(spacing);
  216. spacing->set_custom_minimum_size(Size2(4 * EDSCALE, 0));
  217. other_type_display = memnew(LineEdit);
  218. hb->add_child(other_type_display);
  219. other_type_display->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  220. other_type_display->set_editable(false);
  221. other_type_display->set_text("Node");
  222. select_node_button = memnew(Button);
  223. hb->add_child(select_node_button);
  224. select_node_button->connect("pressed", this, "browse_types");
  225. node_type_group->connect("pressed", this, "update_dialog");
  226. }
  227. {
  228. Label *label = memnew(Label(TTR("Scene Name:")));
  229. gc->add_child(label);
  230. HBoxContainer *hb = memnew(HBoxContainer);
  231. gc->add_child(hb);
  232. scene_name_edit = memnew(LineEdit);
  233. hb->add_child(scene_name_edit);
  234. scene_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  235. scene_name_edit->connect("text_changed", this, "update_dialog");
  236. scene_name_edit->connect("text_entered", this, "accept_create");
  237. List<String> extensions;
  238. Ref<PackedScene> sd = memnew(PackedScene);
  239. ResourceSaver::get_recognized_extensions(sd, &extensions);
  240. scene_extension_picker = memnew(OptionButton);
  241. hb->add_child(scene_extension_picker);
  242. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  243. scene_extension_picker->add_item("." + E->get());
  244. scene_extension_picker->set_item_metadata(scene_extension_picker->get_item_count() - 1, E->get());
  245. }
  246. }
  247. {
  248. Label *label = memnew(Label(TTR("Root Name:")));
  249. gc->add_child(label);
  250. root_name_edit = memnew(LineEdit);
  251. gc->add_child(root_name_edit);
  252. root_name_edit->set_placeholder(TTR("Leave empty to use scene name"));
  253. root_name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  254. root_name_edit->connect("text_changed", this, "update_dialog");
  255. root_name_edit->connect("text_entered", this, "accept_create");
  256. }
  257. Control *spacing = memnew(Control);
  258. main_vb->add_child(spacing);
  259. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  260. status_panel = memnew(PanelContainer);
  261. main_vb->add_child(status_panel);
  262. status_panel->set_h_size_flags(Control::SIZE_FILL);
  263. status_panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  264. VBoxContainer *status_vb = memnew(VBoxContainer);
  265. status_panel->add_child(status_vb);
  266. file_error_label = memnew(Label);
  267. status_vb->add_child(file_error_label);
  268. node_error_label = memnew(Label);
  269. status_vb->add_child(node_error_label);
  270. set_title(TTR("Create New Scene"));
  271. set_custom_minimum_size(Size2i(400 * EDSCALE, 0));
  272. }