panel_new_project.vala 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2012-2024 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gtk;
  6. using Gee;
  7. namespace Crown
  8. {
  9. public class PanelNewProject : Gtk.Viewport
  10. {
  11. // Data
  12. User _user;
  13. Project _project;
  14. // Widgets
  15. public Gtk.Label _new_project_label;
  16. public Gtk.Label _name_label;
  17. public EntryText _entry_name;
  18. public Gtk.Label _location_label;
  19. public Gtk.FileChooserButton _file_chooser_button_location;
  20. public Gtk.Label _template_label;
  21. public ComboBoxMap _combo_box_map_template;
  22. public Gtk.Label _label_message;
  23. public Gtk.Button _button_back;
  24. public Gtk.Button _button_create;
  25. public Gtk.Box _buttons_box;
  26. public Gtk.Box _box;
  27. public Gtk.Grid _grid;
  28. public Clamp _clamp;
  29. public PanelNewProject(User user, Project project)
  30. {
  31. this.shadow_type = Gtk.ShadowType.NONE;
  32. // Data
  33. _user = user;
  34. _project = project;
  35. _new_project_label = new Gtk.Label(null);
  36. _new_project_label.xalign = 0;
  37. _new_project_label.set_markup("<span font_weight=\"bold\" size=\"x-large\">New Project</span>");
  38. _name_label = new Gtk.Label("Name");
  39. _name_label.xalign = 1;
  40. _entry_name = new EntryText();
  41. _location_label = new Gtk.Label("Location");
  42. _location_label.xalign = 1;
  43. _file_chooser_button_location = new Gtk.FileChooserButton("Select Folder", Gtk.FileChooserAction.SELECT_FOLDER);
  44. _file_chooser_button_location.set_current_folder(_documents_dir.get_path());
  45. _template_label = new Gtk.Label("Template");
  46. _template_label.xalign = 1;
  47. _combo_box_map_template = new ComboBoxMap();
  48. _combo_box_map_template.hexpand = true;
  49. _combo_box_map_template.append("", "None");
  50. _combo_box_map_template.value = "";
  51. _label_message = new Gtk.Label("");
  52. _label_message.xalign = 1;
  53. _button_back = new Gtk.Button.with_label("Back");
  54. _button_back.clicked.connect(() => {
  55. var app = (LevelEditorApplication)GLib.Application.get_default();
  56. app.show_panel("panel_welcome", StackTransitionType.SLIDE_UP);
  57. });
  58. _button_create = new Gtk.Button.with_label("Create");
  59. _button_create.get_style_context().add_class(Gtk.STYLE_CLASS_SUGGESTED_ACTION);
  60. _button_create.clicked.connect(() => {
  61. if (_entry_name.text == "") {
  62. _label_message.label = "Choose project name";
  63. return;
  64. }
  65. GLib.File location = _file_chooser_button_location.get_file();
  66. string? source_dir = location.get_path();
  67. if (source_dir == null) {
  68. _label_message.label = "Location is not valid";
  69. return;
  70. }
  71. if (GLib.FileUtils.test(source_dir, FileTest.IS_REGULAR)) {
  72. _label_message.label = "Location must be an empty directory";
  73. return;
  74. }
  75. if (!is_directory_empty(source_dir)) {
  76. _label_message.label = "Location must be an empty directory";
  77. return;
  78. }
  79. _label_message.label = "";
  80. _user.add_or_touch_recent_project(source_dir, _entry_name.text);
  81. var app = (LevelEditorApplication)GLib.Application.get_default();
  82. app.show_panel("main_vbox");
  83. if (_combo_box_map_template.value == "")
  84. _project.create_initial_files(source_dir);
  85. else
  86. copy_template_to_source_dir(source_dir, _combo_box_map_template.value);
  87. app.restart_backend.begin(source_dir, LEVEL_NONE);
  88. });
  89. _buttons_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  90. _buttons_box.spacing = 6;
  91. _buttons_box.pack_end(_button_create, false, true);
  92. _buttons_box.pack_end(_button_back, false, true);
  93. _grid = new Gtk.Grid();
  94. _grid.hexpand = true;
  95. _grid.row_spacing = 6;
  96. _grid.column_spacing = 12;
  97. _grid.attach(_name_label, 0, 0);
  98. _grid.attach(_entry_name, 1, 0);
  99. _grid.attach(_location_label, 0, 1);
  100. _grid.attach(_file_chooser_button_location, 1, 1);
  101. _grid.attach(_template_label, 0, 2);
  102. _grid.attach(_combo_box_map_template, 1, 2);
  103. _grid.attach(_buttons_box, 1, 3);
  104. _box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  105. _box.margin_start = 12;
  106. _box.margin_end = 12;
  107. _box.margin_top = 32;
  108. _box.margin_bottom = 32;
  109. _box.spacing = 12;
  110. _box.pack_start(_new_project_label, false, true);
  111. _box.pack_start(_grid, false, true);
  112. _box.pack_start(_label_message, false, true);
  113. _clamp = new Clamp();
  114. _clamp.add(_box);
  115. this.add(_clamp);
  116. }
  117. public void fill_templates_list(string path)
  118. {
  119. GLib.File file = GLib.File.new_for_path(path);
  120. try {
  121. FileEnumerator enumerator = file.enumerate_children("standard::*"
  122. , FileQueryInfoFlags.NOFOLLOW_SYMLINKS
  123. );
  124. for (GLib.FileInfo? info = enumerator.next_file(); info != null ; info = enumerator.next_file()) {
  125. GLib.File source_dir = GLib.File.new_for_path(GLib.Path.build_filename(path, info.get_name()));
  126. _combo_box_map_template.append(source_dir.get_path(), info.get_name());
  127. }
  128. } catch (GLib.Error e) {
  129. loge(e.message);
  130. }
  131. }
  132. public void copy_recursive(GLib.File dst, GLib.File src, GLib.FileCopyFlags flags = GLib.FileCopyFlags.NONE)
  133. {
  134. try {
  135. GLib.FileType src_type = src.query_file_type(GLib.FileQueryInfoFlags.NONE);
  136. if (src_type == GLib.FileType.DIRECTORY) {
  137. if (dst.query_exists() == false)
  138. dst.make_directory();
  139. string dst_path = dst.get_path();
  140. string src_path = src.get_path();
  141. GLib.FileEnumerator enum = src.enumerate_children(GLib.FileAttribute.STANDARD_NAME, GLib.FileQueryInfoFlags.NONE);
  142. for (GLib.FileInfo? info = enum.next_file(); info != null; info = enum.next_file()) {
  143. copy_recursive(GLib.File.new_for_path(GLib.Path.build_filename(dst_path, info.get_name()))
  144. , GLib.File.new_for_path(GLib.Path.build_filename(src_path, info.get_name()))
  145. , flags
  146. );
  147. }
  148. } else if (src_type == GLib.FileType.REGULAR) {
  149. src.copy(dst, flags);
  150. }
  151. } catch (Error e) {
  152. loge(e.message);
  153. }
  154. }
  155. public void copy_template_to_source_dir(string source_dir, string template_dir)
  156. {
  157. GLib.File dst = GLib.File.new_for_path(source_dir);
  158. GLib.File src = GLib.File.new_for_path(template_dir);
  159. copy_recursive(dst, src);
  160. }
  161. }
  162. } /* namespace Crown */