level_editor.vala 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. using Gdk; // Pixbuf
  6. using Gee;
  7. using Gtk;
  8. namespace Crown
  9. {
  10. const int WINDOW_DEFAULT_WIDTH = 1280;
  11. const int WINDOW_DEFAULT_HEIGHT = 720;
  12. public class LevelEditorWindow : Gtk.ApplicationWindow
  13. {
  14. private const GLib.ActionEntry[] action_entries =
  15. {
  16. { "fullscreen", on_fullscreen, null, null }
  17. };
  18. public bool _fullscreen;
  19. public LevelEditorWindow(Gtk.Application app)
  20. {
  21. Object(application: app);
  22. this.add_action_entries(action_entries, this);
  23. this.title = "Level Editor";
  24. this.key_press_event.connect(this.on_key_press);
  25. this.key_release_event.connect(this.on_key_release);
  26. this.window_state_event.connect(this.on_window_state_event);
  27. this.show.connect(this.on_show);
  28. this.delete_event.connect(this.on_delete_event);
  29. this.set_default_size(WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT);
  30. this.focus_out_event.connect(this.on_focus_out);
  31. _fullscreen = false;
  32. }
  33. private void on_fullscreen(GLib.SimpleAction action, GLib.Variant? param)
  34. {
  35. if (_fullscreen)
  36. unfullscreen();
  37. else
  38. fullscreen();
  39. }
  40. private bool on_key_press(Gdk.EventKey ev)
  41. {
  42. LevelEditorApplication app = (LevelEditorApplication)application;
  43. if (ev.keyval == Gdk.Key.Control_L)
  44. app._editor.send_script(LevelEditorApi.key_down("ctrl_left"));
  45. else if (ev.keyval == Gdk.Key.Shift_L)
  46. app._editor.send_script(LevelEditorApi.key_down("shift_left"));
  47. else if (ev.keyval == Gdk.Key.Alt_L)
  48. app._editor.send_script(LevelEditorApi.key_down("alt_left"));
  49. return false;
  50. }
  51. private bool on_key_release(Gdk.EventKey ev)
  52. {
  53. LevelEditorApplication app = (LevelEditorApplication)application;
  54. if (ev.keyval == Gdk.Key.Control_L)
  55. app._editor.send_script(LevelEditorApi.key_up("ctrl_left"));
  56. else if (ev.keyval == Gdk.Key.Shift_L)
  57. app._editor.send_script(LevelEditorApi.key_up("shift_left"));
  58. else if (ev.keyval == Gdk.Key.Alt_L)
  59. app._editor.send_script(LevelEditorApi.key_up("alt_left"));
  60. return false;
  61. }
  62. private bool on_window_state_event(EventWindowState ev)
  63. {
  64. _fullscreen = (ev.new_window_state & WindowState.FULLSCREEN) != 0;
  65. return true;
  66. }
  67. private void on_show()
  68. {
  69. this.maximize();
  70. }
  71. private bool on_delete_event()
  72. {
  73. LevelEditorApplication app = (LevelEditorApplication)application;
  74. if (app.should_quit())
  75. {
  76. app.close_all();
  77. return false; // Quit application
  78. }
  79. return true; // Keep alive
  80. }
  81. private bool on_focus_out(Gdk.EventFocus ev)
  82. {
  83. LevelEditorApplication app = (LevelEditorApplication)application;
  84. app._editor.send_script(LevelEditorApi.key_up("ctrl_left"));
  85. app._editor.send_script(LevelEditorApi.key_up("shift_left"));
  86. app._editor.send_script(LevelEditorApi.key_up("alt_left"));
  87. return true;
  88. }
  89. }
  90. public enum ToolType
  91. {
  92. PLACE,
  93. MOVE,
  94. ROTATE,
  95. SCALE
  96. }
  97. public enum SnapMode
  98. {
  99. RELATIVE,
  100. ABSOLUTE
  101. }
  102. public enum ReferenceSystem
  103. {
  104. LOCAL,
  105. WORLD
  106. }
  107. public enum StartGame
  108. {
  109. NORMAL,
  110. TEST
  111. }
  112. public class LevelEditorApplication : Gtk.Application
  113. {
  114. // Constants
  115. private const GLib.ActionEntry[] action_entries =
  116. {
  117. // parameter type
  118. // name activate() | state
  119. // | | | |
  120. { "menu-file", null, null, null },
  121. { "new-level", on_new_level, null, null },
  122. { "open-level", on_open_level, "s", null },
  123. { "open-project", on_open_project, null, null },
  124. { "save", on_save, null, null },
  125. { "save-as", on_save_as, null, null },
  126. { "import", on_import, null, null },
  127. { "preferences", on_preferences, null, null },
  128. { "deploy", on_deploy, null, null },
  129. { "quit", on_quit, null, null },
  130. { "menu-edit", null, null, null },
  131. { "undo", on_undo, null, null },
  132. { "redo", on_redo, null, null },
  133. { "duplicate", on_duplicate, null, null },
  134. { "delete", on_delete, null, null },
  135. { "tool", on_tool_changed, "s", "'move'" },
  136. { "snap", on_snap_mode_changed, "s", "'relative'" },
  137. { "reference-system", on_reference_system_changed, "s", "'local'" },
  138. { "snap-to-grid", on_snap_to_grid, null, "true" },
  139. { "menu-grid", null, null, null },
  140. { "grid-show", on_show_grid, null, "true" },
  141. { "grid-custom", on_custom_grid, null, null },
  142. { "grid-preset", on_grid_changed, "s", "'1'" },
  143. { "menu-rotation-snap", null, null, null },
  144. { "rotation-snap-custom", on_rotation_snap, null, null },
  145. { "rotation-snap-preset", on_rotation_snap_changed, "s", "'15'" },
  146. { "menu-create", null, null, null },
  147. { "menu-primitives", null, null, null },
  148. { "primitive-cube", on_create_primitive, null, null },
  149. { "primitive-sphere", on_create_primitive, null, null },
  150. { "primitive-cone", on_create_primitive, null, null },
  151. { "primitive-cylinder", on_create_primitive, null, null },
  152. { "primitive-plane", on_create_primitive, null, null },
  153. { "camera", on_create_primitive, null, null },
  154. { "light", on_create_primitive, null, null },
  155. { "sound-source", on_create_primitive, null, null },
  156. { "menu-camera", null, null, null },
  157. { "camera-view", on_camera_view, "s", "'perspective'" },
  158. { "menu-engine", null, null, null },
  159. { "restart", on_editor_restart, null, null },
  160. { "reload-lua", on_refresh_lua, null, null },
  161. { "menu-view", null, null, null },
  162. { "resource-chooser", on_resource_chooser, null, null },
  163. { "project-browser", on_project_browser, null, null },
  164. { "console", on_console, null, null },
  165. { "statusbar", on_statusbar, null, null },
  166. { "inspector", on_inspector, null, null },
  167. { "menu-run", null, null, null },
  168. { "test-level", on_run_game, null, null },
  169. { "run-game", on_run_game, null, null },
  170. { "menu-help", null, null, null },
  171. { "manual", on_manual, null, null },
  172. { "report-issue", on_report_issue, null, null },
  173. { "open-last-log", on_open_last_log, null, null },
  174. { "changelog", on_changelog, null, null },
  175. { "about", on_about, null, null },
  176. { "debug-render-world", on_debug_render_world, null, "false" },
  177. { "debug-physics-world", on_debug_physics_world, null, "false" }
  178. };
  179. // Command line options
  180. private string _source_dir = "";
  181. private string _toolchain_dir = "";
  182. private string _level_resource = "";
  183. private bool _create_initial_files = false;
  184. // Global paths
  185. private GLib.File _config_dir;
  186. private GLib.File _settings_file;
  187. // Editor state
  188. private double _grid_size;
  189. private double _rotation_snap;
  190. private bool _show_grid;
  191. private bool _snap_to_grid;
  192. private bool _debug_render_world;
  193. private bool _debug_physics_world;
  194. private ToolType _tool_type;
  195. private SnapMode _snap_mode;
  196. private ReferenceSystem _reference_system;
  197. // Engine connections
  198. private GLib.Subprocess _compiler_process;
  199. private GLib.Subprocess _editor_process;
  200. private GLib.Subprocess _game_process;
  201. private ConsoleClient _compiler;
  202. public ConsoleClient _editor;
  203. private ConsoleClient _game;
  204. // Level data
  205. private Database _database;
  206. private Project _project;
  207. private ProjectStore _project_store;
  208. private Level _level;
  209. private DataCompiler _data_compiler;
  210. // Widgets
  211. private ConsoleView _console_view;
  212. private ProjectBrowser _project_browser;
  213. private EditorView _editor_view;
  214. private LevelTreeView _level_treeview;
  215. private LevelLayersTreeView _level_layers_treeview;
  216. private PropertiesView _properties_view;
  217. private PreferencesDialog _preferences_dialog;
  218. private ResourceChooser _resource_chooser;
  219. private Gtk.Popover _resource_popover;
  220. private Gtk.Overlay _editor_view_overlay;
  221. private Slide _project_slide;
  222. private Slide _editor_slide;
  223. private Slide _inspector_slide;
  224. private Gtk.Toolbar _toolbar;
  225. private Gtk.ToolButton _toolbar_run;
  226. private Gtk.Notebook _level_tree_view_notebook;
  227. private Gtk.Paned _editor_pane;
  228. private Gtk.Paned _content_pane;
  229. private Gtk.Paned _inspector_pane;
  230. private Gtk.Paned _main_pane;
  231. private Statusbar _statusbar;
  232. private Gtk.Box _main_vbox;
  233. private Gtk.FileFilter _file_filter;
  234. private Gtk.ComboBoxText _combo;
  235. private uint _save_timer_id;
  236. public LevelEditorApplication()
  237. {
  238. Object(application_id: "org.crown.level_editor"
  239. , flags: GLib.ApplicationFlags.FLAGS_NONE
  240. );
  241. }
  242. protected override void startup()
  243. {
  244. base.startup();
  245. Intl.setlocale(LocaleCategory.ALL, "C");
  246. Gtk.Settings.get_default().gtk_theme_name = "Adwaita";
  247. Gtk.Settings.get_default().gtk_application_prefer_dark_theme = true;
  248. Gtk.CssProvider provider = new Gtk.CssProvider();
  249. Gdk.Screen screen = Gdk.Display.get_default().get_default_screen();
  250. Gtk.StyleContext.add_provider_for_screen(screen, provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
  251. provider.load_from_resource("/org/crown/level_editor/css/style.css");
  252. this.add_action_entries(action_entries, this);
  253. if (_source_dir == "")
  254. {
  255. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Select folder where to create new project..."
  256. , null
  257. , FileChooserAction.SELECT_FOLDER
  258. , "Cancel"
  259. , ResponseType.CANCEL
  260. , "Select"
  261. , ResponseType.ACCEPT
  262. );
  263. if (fcd.run() != (int)ResponseType.ACCEPT)
  264. {
  265. fcd.destroy();
  266. return;
  267. }
  268. string dir = fcd.get_filename();
  269. fcd.destroy();
  270. if (GLib.FileUtils.test(dir, FileTest.IS_REGULAR))
  271. {
  272. stdout.printf("Source directory can't be a regular file\n");
  273. return;
  274. }
  275. _source_dir = dir;
  276. _create_initial_files = true;
  277. }
  278. _compiler = new ConsoleClient();
  279. _compiler.connected.connect(on_compiler_connected);
  280. _compiler.disconnected.connect(on_compiler_disconnected_unexpected);
  281. _compiler.message_received.connect(on_message_received);
  282. _data_compiler = new DataCompiler(_compiler);
  283. _project = new Project(_data_compiler);
  284. _project.load(_source_dir, _toolchain_dir);
  285. if (_create_initial_files)
  286. _project.create_initial_files();
  287. _database = new Database();
  288. _editor = new ConsoleClient();
  289. _editor.connected.connect(on_editor_connected);
  290. _editor.disconnected.connect(on_editor_disconnected_unexpected);
  291. _editor.message_received.connect(on_message_received);
  292. _game = new ConsoleClient();
  293. _game.connected.connect(on_game_connected);
  294. _game.disconnected.connect(on_game_disconnected);
  295. _game.message_received.connect(on_message_received);
  296. _level = new Level(_database, _editor, _project);
  297. // Global paths
  298. _config_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_config_dir(), "crown"));
  299. _settings_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "settings.sjson"));
  300. // Editor state
  301. _grid_size = 1.0;
  302. _rotation_snap = 15.0;
  303. _show_grid = true;
  304. _snap_to_grid = true;
  305. _debug_render_world = false;
  306. _debug_physics_world = false;
  307. _tool_type = ToolType.MOVE;
  308. _snap_mode = SnapMode.RELATIVE;
  309. _reference_system = ReferenceSystem.LOCAL;
  310. // Engine connections
  311. _compiler_process = null;
  312. _editor_process = null;
  313. _game_process = null;
  314. _project_store = new ProjectStore(_project);
  315. // Widgets
  316. _combo = new Gtk.ComboBoxText();
  317. _combo.append("editor", "Editor");
  318. _combo.append("game", "Game");
  319. _combo.set_active_id("editor");
  320. _console_view = new ConsoleView(_project, _combo);
  321. _project_browser = new ProjectBrowser(_project, _project_store);
  322. _level_treeview = new LevelTreeView(_database, _level);
  323. _level_layers_treeview = new LevelLayersTreeView(_database, _level);
  324. _properties_view = new PropertiesView(_level, _project_store);
  325. _project_slide = new Slide();
  326. _editor_slide = new Slide();
  327. _inspector_slide = new Slide();
  328. Gtk.Builder builder = new Gtk.Builder.from_resource("/org/crown/level_editor/ui/toolbar.ui");
  329. _toolbar = builder.get_object("toolbar") as Gtk.Toolbar;
  330. _toolbar_run = builder.get_object("run") as Gtk.ToolButton;
  331. _editor_view_overlay = new Gtk.Overlay();
  332. _editor_view_overlay.add_overlay(_toolbar);
  333. _resource_popover = new Gtk.Popover(_toolbar);
  334. _resource_popover.delete_event.connect(() => { _resource_popover.hide(); return true; });
  335. _resource_popover.modal = true;
  336. _preferences_dialog = new PreferencesDialog(this);
  337. _preferences_dialog.set_transient_for(this.active_window);
  338. _preferences_dialog.delete_event.connect(() => { _preferences_dialog.hide(); return true; });
  339. _resource_chooser = new ResourceChooser(_project, _project_store, true);
  340. _resource_chooser.resource_selected.connect(on_resource_browser_resource_selected);
  341. _resource_chooser.resource_selected.connect(() => { _resource_popover.hide(); });
  342. _resource_popover.add(_resource_chooser);
  343. _level_tree_view_notebook = new Notebook();
  344. _level_tree_view_notebook.show_border = false;
  345. _level_tree_view_notebook.append_page(_level_treeview, new Gtk.Image.from_icon_name("level-tree", IconSize.SMALL_TOOLBAR));
  346. _level_tree_view_notebook.append_page(_level_layers_treeview, new Gtk.Image.from_icon_name("level-layers", IconSize.SMALL_TOOLBAR));
  347. _editor_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  348. _editor_pane.pack1(_project_slide, false, false);
  349. _editor_pane.pack2(_editor_slide, true, false);
  350. _editor_pane.set_position(210);
  351. _content_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  352. _content_pane.pack1(_editor_pane, true, false);
  353. _content_pane.pack2(_console_view, false, false);
  354. _content_pane.set_position(500);
  355. _inspector_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  356. _inspector_pane.pack1(_level_tree_view_notebook, true, false);
  357. _inspector_pane.pack2(_properties_view, false, false);
  358. _inspector_pane.set_position(250);
  359. _main_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  360. _main_pane.pack1(_content_pane, true, false);
  361. _main_pane.pack2(_inspector_slide, false, false);
  362. _main_pane.set_position(WINDOW_DEFAULT_WIDTH - 375);
  363. _statusbar = new Statusbar();
  364. _main_vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  365. _main_vbox.pack_start(_main_pane, true, true, 0);
  366. _main_vbox.pack_start(_statusbar, false, false, 0);
  367. _file_filter = new Gtk.FileFilter();
  368. _file_filter.set_filter_name("Level (*.level)");
  369. _file_filter.add_pattern("*.level");
  370. if (_level_resource != "")
  371. {
  372. string level_path = Path.build_filename(_project.source_dir(), _level_resource + ".level");
  373. if (!GLib.FileUtils.test(level_path, FileTest.EXISTS) || !GLib.FileUtils.test(level_path, FileTest.IS_REGULAR))
  374. {
  375. stdout.printf("Level resource '%s' does not exist.\n", _level_resource);
  376. return;
  377. }
  378. _level.load(level_path);
  379. }
  380. else
  381. {
  382. _level.load_empty_level();
  383. }
  384. try
  385. {
  386. _config_dir.make_directory();
  387. }
  388. catch (Error e)
  389. {
  390. // Nobody cares
  391. }
  392. load_settings();
  393. restart_compiler();
  394. }
  395. public void load_settings()
  396. {
  397. Hashtable settings = SJSON.load(_settings_file.get_path());
  398. _preferences_dialog.load(settings.has_key("preferences") ? (Hashtable)settings["preferences"] : new Hashtable());
  399. }
  400. public void save_settings()
  401. {
  402. Hashtable preferences = new Hashtable();
  403. _preferences_dialog.save(preferences);
  404. Hashtable settings = new Hashtable();
  405. settings["preferences"] = preferences;
  406. SJSON.save(settings, _settings_file.get_path());
  407. }
  408. protected override void activate()
  409. {
  410. if (_source_dir == "")
  411. return;
  412. if (this.active_window == null)
  413. {
  414. LevelEditorWindow win = new LevelEditorWindow(this);
  415. win.add(_main_vbox);
  416. try
  417. {
  418. win.icon = IconTheme.get_default().load_icon("pepper", 48, 0);
  419. }
  420. catch (Error e)
  421. {
  422. _console_view.loge("editor", e.message);
  423. }
  424. }
  425. this.active_window.show_all();
  426. }
  427. protected override bool local_command_line(ref unowned string[] args, out int exit_status)
  428. {
  429. if (args.length > 1)
  430. {
  431. if (!GLib.FileUtils.test(args[1], FileTest.EXISTS) || !GLib.FileUtils.test(args[1], FileTest.IS_DIR))
  432. {
  433. stdout.printf("Source directory does not exist or it is not a directory\n");
  434. exit_status = 1;
  435. return true;
  436. }
  437. _source_dir = args[1];
  438. }
  439. if (args.length > 2)
  440. {
  441. // Validation is done below after the Project object instantiation
  442. _level_resource = args[2];
  443. }
  444. if (args.length > 3)
  445. {
  446. if (!GLib.FileUtils.test(args[3], FileTest.EXISTS) || !GLib.FileUtils.test(args[3], FileTest.IS_DIR))
  447. {
  448. stdout.printf("Toolchain directory does not exist or it is not a directory\n");
  449. exit_status = 1;
  450. return true;
  451. }
  452. _toolchain_dir = args[3];
  453. }
  454. else
  455. {
  456. bool found = false;
  457. /// More desirable paths come first
  458. string toolchain_paths[] =
  459. {
  460. "../..",
  461. "../../../samples"
  462. };
  463. for (int i = 0; i < toolchain_paths.length; ++i)
  464. {
  465. string path = Path.build_filename(toolchain_paths[i], "core");
  466. // Try to locate the toolchain directory
  467. if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR))
  468. {
  469. _toolchain_dir = toolchain_paths[i];
  470. found = true;
  471. break;
  472. }
  473. }
  474. if (!found)
  475. {
  476. stdout.printf("Unable to find the toolchain directory\n");
  477. exit_status = 1;
  478. return true;
  479. }
  480. }
  481. exit_status = 0;
  482. return false;
  483. }
  484. protected override int command_line(ApplicationCommandLine command_line)
  485. {
  486. this.activate();
  487. return 0;
  488. }
  489. public ConsoleClient? current_selected_client()
  490. {
  491. if (_combo.get_active_id() == "editor")
  492. return _editor;
  493. else if (_combo.get_active_id() == "game")
  494. return _game;
  495. else
  496. return null;
  497. }
  498. private void on_resource_browser_resource_selected(string type, string name)
  499. {
  500. _editor.send_script(LevelEditorApi.set_placeable(type, name));
  501. activate_action("tool", new GLib.Variant.string("place"));
  502. }
  503. private void on_compiler_connected(string address, int port)
  504. {
  505. _console_view.logi("editor", "Connected to data_compiler@%s:%d".printf(address, port));
  506. _compiler.receive_async();
  507. }
  508. private void on_compiler_disconnected()
  509. {
  510. _console_view.logi("editor", "Disconnected from data_compiler");
  511. }
  512. private void on_compiler_disconnected_unexpected()
  513. {
  514. on_compiler_disconnected();
  515. stop_game();
  516. stop_editor();
  517. // Reset the callback
  518. _data_compiler.finished(false);
  519. _project_slide.show_widget(compiler_crashed_label());
  520. _editor_slide.show_widget(compiler_crashed_label());
  521. _inspector_slide.show_widget(compiler_crashed_label());
  522. }
  523. private void on_editor_connected(string address, int port)
  524. {
  525. _console_view.logi("editor", "Connected to level_editor@%s:%d".printf(address, port));
  526. _editor.receive_async();
  527. }
  528. private void on_editor_disconnected()
  529. {
  530. _console_view.logi("editor", "Disconnected from editor");
  531. }
  532. private void on_editor_disconnected_unexpected()
  533. {
  534. on_editor_disconnected();
  535. Gtk.Label label = new Gtk.Label(null);
  536. label.set_markup("Something went wrong.\rTry to <a href=\"restart\">restart</a> this view.");
  537. label.activate_link.connect(() => {
  538. activate_action("restart", null);
  539. return true;
  540. });
  541. _editor_slide.show_widget(label);
  542. }
  543. private void on_game_connected(string address, int port)
  544. {
  545. _console_view.logi("editor", "Connected to game@%s:%d".printf(address, port));
  546. _game.receive_async();
  547. }
  548. private void on_game_disconnected()
  549. {
  550. _console_view.logi("editor", "Disconnected from game");
  551. _toolbar_run.icon_name = "game-run";
  552. _project.delete_garbage();
  553. _combo.set_active_id("editor");
  554. }
  555. private void on_message_received(ConsoleClient client, uint8[] json)
  556. {
  557. Hashtable msg = JSON.decode(json) as Hashtable;
  558. string msg_type = msg["type"] as string;
  559. if (msg_type == "message")
  560. {
  561. _console_view.log((string)msg["system"], (string)msg["message"], (string)msg["severity"]);
  562. }
  563. else if (msg_type == "add_file")
  564. {
  565. string path = (string)msg["path"];
  566. _project.add_file(path);
  567. }
  568. else if (msg_type == "remove_file")
  569. {
  570. string path = (string)msg["path"];
  571. _project.remove_file(path);
  572. }
  573. else if (msg_type == "add_tree")
  574. {
  575. string path = (string)msg["path"];
  576. _project.add_tree(path);
  577. }
  578. else if (msg_type == "remove_tree")
  579. {
  580. string path = (string)msg["path"];
  581. _project.remove_tree(path);
  582. }
  583. else if (msg_type == "compile")
  584. {
  585. // Guid id = Guid.parse((string)msg["id"]);
  586. if (msg.has_key("start"))
  587. {
  588. // FIXME
  589. }
  590. else if (msg.has_key("success"))
  591. {
  592. _data_compiler.finished((bool)msg["success"]);
  593. }
  594. }
  595. else if (msg_type == "unit_spawned")
  596. {
  597. string id = (string) msg["id"];
  598. string name = (string) msg["name"];
  599. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  600. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  601. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  602. _level.on_unit_spawned(Guid.parse(id)
  603. , name
  604. , Vector3.from_array(pos)
  605. , Quaternion.from_array(rot)
  606. , Vector3.from_array(scl)
  607. );
  608. }
  609. else if (msg_type == "sound_spawned")
  610. {
  611. string id = (string) msg["id"];
  612. string name = (string) msg["name"];
  613. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  614. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  615. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  616. double range = (double) msg["range"];
  617. double volume = (double) msg["volume"];
  618. bool loop = (bool) msg["loop"];
  619. _level.on_sound_spawned(Guid.parse(id)
  620. , name
  621. , Vector3.from_array(pos)
  622. , Quaternion.from_array(rot)
  623. , Vector3.from_array(scl)
  624. , range
  625. , volume
  626. , loop
  627. );
  628. }
  629. else if (msg_type == "move_objects")
  630. {
  631. Hashtable ids = (Hashtable)msg["ids"];
  632. Hashtable new_positions = (Hashtable)msg["new_positions"];
  633. Hashtable new_rotations = (Hashtable)msg["new_rotations"];
  634. Hashtable new_scales = (Hashtable)msg["new_scales"];
  635. ArrayList<string> keys = new ArrayList<string>.wrap(ids.keys.to_array());
  636. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  637. Guid[] n_ids = new Guid[keys.size];
  638. Vector3[] n_positions = new Vector3[keys.size];
  639. Quaternion[] n_rotations = new Quaternion[keys.size];
  640. Vector3[] n_scales = new Vector3[keys.size];
  641. for (int i = 0; i < keys.size; ++i)
  642. {
  643. string k = keys[i];
  644. n_ids[i] = Guid.parse((string)ids[k]);
  645. n_positions[i] = Vector3.from_array((ArrayList<Value?>)(new_positions[k]));
  646. n_rotations[i] = Quaternion.from_array((ArrayList<Value?>)new_rotations[k]);
  647. n_scales[i] = Vector3.from_array((ArrayList<Value?>)new_scales[k]);
  648. }
  649. _level.on_move_objects(n_ids, n_positions, n_rotations, n_scales);
  650. }
  651. else if (msg_type == "selection")
  652. {
  653. Hashtable objects = (Hashtable)msg["objects"];
  654. ArrayList<string> keys = new ArrayList<string>.wrap(objects.keys.to_array());
  655. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  656. Guid[] ids = new Guid[keys.size];
  657. for (int i = 0; i < keys.size; ++i)
  658. {
  659. string k = keys[i];
  660. ids[i] = Guid.parse((string)objects[k]);
  661. }
  662. _level.on_selection(ids);
  663. }
  664. else if (msg_type == "error")
  665. {
  666. _console_view.loge("editor", "Error: " + (string)msg["message"]);
  667. }
  668. else
  669. {
  670. _console_view.loge("editor", "Unknown message type: " + msg_type);
  671. }
  672. // Receive next message
  673. client.receive_async();
  674. }
  675. private void send_state()
  676. {
  677. StringBuilder sb = new StringBuilder();
  678. sb.append(LevelEditorApi.set_grid_size(_grid_size));
  679. sb.append(LevelEditorApi.set_rotation_snap(_rotation_snap));
  680. sb.append(LevelEditorApi.enable_show_grid(_show_grid));
  681. sb.append(LevelEditorApi.enable_snap_to_grid(_snap_to_grid));
  682. sb.append(LevelEditorApi.enable_debug_render_world(_debug_render_world));
  683. sb.append(LevelEditorApi.enable_debug_physics_world(_debug_physics_world));
  684. sb.append(LevelEditorApi.set_tool_type(_tool_type));
  685. sb.append(LevelEditorApi.set_snap_mode(_snap_mode));
  686. sb.append(LevelEditorApi.set_reference_system(_reference_system));
  687. _editor.send_script(sb.str);
  688. }
  689. private bool on_button_press(EventButton ev)
  690. {
  691. return true;
  692. }
  693. private bool on_button_release(EventButton ev)
  694. {
  695. return true;
  696. }
  697. Gtk.Widget starting_compiler_label()
  698. {
  699. return new Gtk.Label("Compiling resources, please wait...");
  700. }
  701. Gtk.Widget compiler_crashed_label()
  702. {
  703. Gtk.Label label = new Gtk.Label(null);
  704. label.set_markup("Data Compiler disconnected.\rTry to <a href=\"restart\">restart</a> compiler to continue.");
  705. label.activate_link.connect(() => {
  706. restart_compiler();
  707. return true;
  708. });
  709. return label;
  710. }
  711. Gtk.Widget compiler_failed_compilation_label()
  712. {
  713. Gtk.Label label = new Gtk.Label(null);
  714. label.set_markup("Data compilation failed.\rFix errors and <a href=\"restart\">restart</a> compiler to continue.");
  715. label.activate_link.connect(() => {
  716. restart_compiler();
  717. return true;
  718. });
  719. return label;
  720. }
  721. private void restart_compiler()
  722. {
  723. stop_compiler();
  724. _project.reset();
  725. _project_slide.show_widget(starting_compiler_label());
  726. _editor_slide.show_widget(starting_compiler_label());
  727. _inspector_slide.show_widget(starting_compiler_label());
  728. string args[] =
  729. {
  730. ENGINE_EXE,
  731. "--source-dir", _project.source_dir(),
  732. "--data-dir", _project.data_dir(),
  733. "--map-source-dir", "core", _project.toolchain_dir(),
  734. "--server",
  735. "--wait-console",
  736. null
  737. };
  738. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  739. sl.set_cwd(ENGINE_DIR);
  740. try
  741. {
  742. _compiler_process = sl.spawnv(args);
  743. }
  744. catch (Error e)
  745. {
  746. _console_view.loge("editor", e.message);
  747. }
  748. // It is an error if compiler disconnects after here.
  749. _compiler.disconnected.disconnect(on_compiler_disconnected);
  750. _compiler.disconnected.connect(on_compiler_disconnected_unexpected);
  751. for (int tries = 0; !_compiler.is_connected() && tries < 5; ++tries)
  752. {
  753. _compiler.connect("127.0.0.1", CROWN_DEFAULT_SERVER_PORT);
  754. GLib.Thread.usleep(250*1000);
  755. }
  756. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  757. if (_data_compiler.compile.end(res))
  758. {
  759. restart_editor();
  760. _project_slide.show_widget(_project_browser);
  761. _inspector_slide.show_widget(_inspector_pane);
  762. }
  763. else
  764. {
  765. _project_slide.show_widget(compiler_failed_compilation_label());
  766. _editor_slide.show_widget(compiler_failed_compilation_label());
  767. _inspector_slide.show_widget(compiler_failed_compilation_label());
  768. }
  769. });
  770. }
  771. private void stop_compiler()
  772. {
  773. stop_game();
  774. stop_editor();
  775. if (_compiler != null)
  776. {
  777. // Explicit call to this function should not produce error messages.
  778. _compiler.disconnected.disconnect(on_compiler_disconnected_unexpected);
  779. _compiler.disconnected.connect(on_compiler_disconnected);
  780. _compiler.send(DataCompilerApi.quit());
  781. _compiler.close();
  782. }
  783. if (_compiler_process != null)
  784. {
  785. try
  786. {
  787. _compiler_process.wait();
  788. }
  789. catch (Error e)
  790. {
  791. _console_view.loge("editor", e.message);
  792. }
  793. }
  794. }
  795. private void start_editor(uint window_xid)
  796. {
  797. if (window_xid == 0)
  798. return;
  799. string args[] =
  800. {
  801. ENGINE_EXE,
  802. "--data-dir", _project.data_dir(),
  803. "--boot-dir", LEVEL_EDITOR_BOOT_DIR,
  804. "--parent-window", window_xid.to_string(),
  805. "--wait-console",
  806. null
  807. };
  808. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  809. sl.set_cwd(ENGINE_DIR);
  810. try
  811. {
  812. _editor_process = sl.spawnv(args);
  813. }
  814. catch (Error e)
  815. {
  816. _console_view.loge("editor", e.message);
  817. }
  818. // It is an error if editor disconnects after here.
  819. _editor.disconnected.disconnect(on_editor_disconnected);
  820. _editor.disconnected.connect(on_editor_disconnected_unexpected);
  821. for (int tries = 0; !_editor.is_connected() && tries < 10; ++tries)
  822. {
  823. _editor.connect("127.0.0.1", 10001);
  824. GLib.Thread.usleep(500*1000);
  825. }
  826. _level.send_level();
  827. send_state();
  828. _preferences_dialog.apply();
  829. }
  830. private void stop_editor()
  831. {
  832. _resource_chooser.stop_editor();
  833. if (_editor != null)
  834. {
  835. // Explicit call to this function should not produce error messages.
  836. _editor.disconnected.disconnect(on_editor_disconnected_unexpected);
  837. _editor.disconnected.connect(on_editor_disconnected);
  838. _editor.send_script("Device.quit()");
  839. _editor.close();
  840. }
  841. if (_editor_process != null)
  842. {
  843. try
  844. {
  845. _editor_process.wait();
  846. }
  847. catch (Error e)
  848. {
  849. _console_view.loge("editor", e.message);
  850. }
  851. }
  852. _editor_slide.show_widget(new Gtk.Label("Disconnected."));
  853. }
  854. private void restart_editor()
  855. {
  856. stop_editor();
  857. if (_editor_view != null)
  858. {
  859. _editor_view_overlay.remove(_editor_view);
  860. _editor_view = null;
  861. }
  862. _editor_view = new EditorView(_editor);
  863. _editor_view.realized.connect(on_editor_view_realized);
  864. _editor_view.button_press_event.connect(on_button_press);
  865. _editor_view.button_release_event.connect(on_button_release);
  866. _editor_view_overlay.add(_editor_view);
  867. _editor_slide.show_widget(_editor_view_overlay);
  868. _resource_chooser.restart_editor();
  869. }
  870. private void start_game(StartGame sg)
  871. {
  872. _project.dump_test_level(_database);
  873. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  874. if (_data_compiler.compile.end(res))
  875. {
  876. string args[] =
  877. {
  878. ENGINE_EXE,
  879. "--data-dir", _project.data_dir(),
  880. "--console-port", "12345",
  881. "--wait-console",
  882. "--lua-string", sg == StartGame.TEST ? "TEST=true" : "",
  883. null
  884. };
  885. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  886. sl.set_cwd(ENGINE_DIR);
  887. try
  888. {
  889. _game_process = sl.spawnv(args);
  890. }
  891. catch (Error e)
  892. {
  893. _console_view.loge("editor", e.message);
  894. }
  895. for (int tries = 0; !_game.is_connected() && tries < 10; ++tries)
  896. {
  897. _game.connect("127.0.0.1", 12345);
  898. GLib.Thread.usleep(500*1000);
  899. }
  900. _combo.set_active_id("game");
  901. }
  902. });
  903. }
  904. private void stop_game()
  905. {
  906. if (_game != null)
  907. {
  908. _game.send_script("Device.quit()");
  909. _game.close();
  910. }
  911. if (_game_process != null)
  912. {
  913. try
  914. {
  915. _game_process.wait();
  916. }
  917. catch (Error e)
  918. {
  919. _console_view.loge("editor", e.message);
  920. }
  921. }
  922. }
  923. private void deploy_game()
  924. {
  925. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Select destination directory..."
  926. , this.active_window
  927. , FileChooserAction.SELECT_FOLDER
  928. , "Cancel"
  929. , ResponseType.CANCEL
  930. , "Open"
  931. , ResponseType.ACCEPT
  932. );
  933. if (fcd.run() == (int)ResponseType.ACCEPT)
  934. {
  935. GLib.File data_dir = File.new_for_path(fcd.get_filename());
  936. string args[] =
  937. {
  938. ENGINE_EXE,
  939. "--source-dir", _project.source_dir(),
  940. "--map-source-dir", "core", _project.toolchain_dir(),
  941. "--data-dir", data_dir.get_path(),
  942. "--compile",
  943. null
  944. };
  945. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  946. sl.set_cwd(ENGINE_DIR);
  947. try
  948. {
  949. GLib.Subprocess compiler = sl.spawnv(args);
  950. compiler.wait();
  951. if (compiler.get_exit_status() == 0)
  952. {
  953. GLib.File engine_exe = File.new_for_path(ENGINE_EXE);
  954. GLib.File engine_exe_dest = File.new_for_path(Path.build_filename(data_dir.get_path(), ENGINE_EXE));
  955. engine_exe.copy(engine_exe_dest, FileCopyFlags.OVERWRITE);
  956. _console_view.logi("editor", "Project deployed to `%s`".printf(data_dir.get_path()));
  957. }
  958. }
  959. catch (Error e)
  960. {
  961. _console_view.logi("editor", "%s".printf(e.message));
  962. _console_view.logi("editor", "Failed to deploy project");
  963. }
  964. }
  965. fcd.destroy();
  966. }
  967. private void on_editor_view_realized()
  968. {
  969. start_editor(_editor_view.window_id);
  970. }
  971. private void on_tool_changed(GLib.SimpleAction action, GLib.Variant? param)
  972. {
  973. string name = param.get_string();
  974. if (name == "place")
  975. _tool_type = ToolType.PLACE;
  976. else if (name == "move")
  977. _tool_type = ToolType.MOVE;
  978. else if (name == "rotate")
  979. _tool_type = ToolType.ROTATE;
  980. else if (name == "scale")
  981. _tool_type = ToolType.SCALE;
  982. send_state();
  983. action.set_state(param);
  984. }
  985. private void on_snap_mode_changed(GLib.SimpleAction action, GLib.Variant? param)
  986. {
  987. string name = param.get_string();
  988. if (name == "relative")
  989. _snap_mode = SnapMode.RELATIVE;
  990. else if (name == "absolute")
  991. _snap_mode = SnapMode.ABSOLUTE;
  992. send_state();
  993. action.set_state(param);
  994. }
  995. private void on_reference_system_changed(GLib.SimpleAction action, GLib.Variant? param)
  996. {
  997. string name = param.get_string();
  998. if (name == "local")
  999. _reference_system = ReferenceSystem.LOCAL;
  1000. else if (name == "world")
  1001. _reference_system = ReferenceSystem.WORLD;
  1002. send_state();
  1003. action.set_state(param);
  1004. }
  1005. private void on_grid_changed(GLib.SimpleAction action, GLib.Variant? param)
  1006. {
  1007. _grid_size = float.parse(param.get_string());
  1008. send_state();
  1009. action.set_state(param);
  1010. }
  1011. private void on_rotation_snap_changed(GLib.SimpleAction action, GLib.Variant? param)
  1012. {
  1013. _rotation_snap = float.parse(param.get_string());
  1014. send_state();
  1015. action.set_state(param);
  1016. }
  1017. private void new_level()
  1018. {
  1019. _level.load_empty_level();
  1020. _level.send_level();
  1021. }
  1022. private void load_level(string level)
  1023. {
  1024. string filename = level;
  1025. if (filename == "")
  1026. {
  1027. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Open Level..."
  1028. , this.active_window
  1029. , FileChooserAction.OPEN
  1030. , "Cancel"
  1031. , ResponseType.CANCEL
  1032. , "Open"
  1033. , ResponseType.ACCEPT
  1034. );
  1035. fcd.add_filter(_file_filter);
  1036. fcd.set_current_folder(_project.source_dir());
  1037. if (fcd.run() == (int)ResponseType.ACCEPT)
  1038. filename = fcd.get_filename();
  1039. fcd.destroy();
  1040. }
  1041. if (filename == "")
  1042. return;
  1043. if (!_project.path_is_within_dir(filename, _project.source_dir()))
  1044. {
  1045. _console_view.loge("editor", "File must be within `%s`".printf(_project.source_dir()));
  1046. return;
  1047. }
  1048. if (filename.has_suffix(".level") && filename != _level._filename)
  1049. {
  1050. _level.load(filename);
  1051. _level.send_level();
  1052. send_state();
  1053. }
  1054. }
  1055. private void load_project()
  1056. {
  1057. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Open Project..."
  1058. , this.active_window
  1059. , FileChooserAction.SELECT_FOLDER
  1060. , "Cancel"
  1061. , ResponseType.CANCEL
  1062. , "Open"
  1063. , ResponseType.ACCEPT
  1064. );
  1065. int rt = fcd.run();
  1066. if (rt != (int)ResponseType.ACCEPT)
  1067. {
  1068. fcd.destroy();
  1069. return;
  1070. }
  1071. string filename = fcd.get_filename();
  1072. fcd.destroy();
  1073. if (filename == _project.source_dir())
  1074. return;
  1075. _console_view.logi("editor", "Loading `%s`...".printf(filename));
  1076. _project.load(filename, _project.toolchain_dir());
  1077. _level.load_empty_level();
  1078. restart_compiler();
  1079. }
  1080. private bool save_as(string? filename)
  1081. {
  1082. string path;
  1083. if (filename != null)
  1084. {
  1085. path = filename;
  1086. }
  1087. else
  1088. {
  1089. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Save As..."
  1090. , this.active_window
  1091. , FileChooserAction.SAVE
  1092. , "Cancel"
  1093. , ResponseType.CANCEL
  1094. , "Save"
  1095. , ResponseType.ACCEPT
  1096. );
  1097. fcd.add_filter(_file_filter);
  1098. fcd.set_current_folder(_project.source_dir());
  1099. int rt = fcd.run();
  1100. if (rt != (int)ResponseType.ACCEPT)
  1101. {
  1102. fcd.destroy();
  1103. return false;
  1104. }
  1105. path = fcd.get_filename();
  1106. fcd.destroy();
  1107. }
  1108. if (!_project.path_is_within_dir(path, _project.source_dir()))
  1109. {
  1110. _console_view.loge("editor", "File must be within `%s`".printf(_project.source_dir()));
  1111. return false;
  1112. }
  1113. _level.save(path.has_suffix(".level") ? path : path + ".level");
  1114. _statusbar.set_temporary_message("Saved %s".printf(_level._filename));
  1115. return true;
  1116. }
  1117. private bool save()
  1118. {
  1119. return save_as(_level._filename);
  1120. }
  1121. private bool save_timeout()
  1122. {
  1123. if (_level._filename != null)
  1124. save();
  1125. return true;
  1126. }
  1127. public void close_all()
  1128. {
  1129. save_settings();
  1130. if (_save_timer_id > 0)
  1131. GLib.Source.remove(_save_timer_id);
  1132. if (_resource_chooser != null)
  1133. _resource_chooser.destroy();
  1134. if (_preferences_dialog != null)
  1135. _preferences_dialog.destroy();
  1136. stop_compiler();
  1137. }
  1138. protected override void shutdown()
  1139. {
  1140. base.shutdown();
  1141. }
  1142. // Returns true if the level has been saved or the user decided it
  1143. // should be discarded.
  1144. public bool should_quit()
  1145. {
  1146. if (!_database.changed())
  1147. return true;
  1148. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  1149. , Gtk.DialogFlags.MODAL
  1150. , Gtk.MessageType.WARNING
  1151. , Gtk.ButtonsType.NONE
  1152. , "File changed, save?"
  1153. );
  1154. md.add_button("Quit _without Saving", ResponseType.NO);
  1155. md.add_button("_Cancel", ResponseType.CANCEL);
  1156. md.add_button("_Save", ResponseType.YES);
  1157. md.set_default_response(ResponseType.YES);
  1158. int rt = md.run();
  1159. md.destroy();
  1160. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  1161. return true;
  1162. return false;
  1163. }
  1164. private void on_new_level(GLib.SimpleAction action, GLib.Variant? param)
  1165. {
  1166. if (!_database.changed())
  1167. {
  1168. new_level();
  1169. send_state();
  1170. return;
  1171. }
  1172. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  1173. , Gtk.DialogFlags.MODAL
  1174. , Gtk.MessageType.WARNING
  1175. , Gtk.ButtonsType.NONE
  1176. , "File changed, save?"
  1177. );
  1178. md.add_button("New _without Saving", ResponseType.NO);
  1179. md.add_button("_Cancel", ResponseType.CANCEL);
  1180. md.add_button("_Save", ResponseType.YES);
  1181. md.set_default_response(ResponseType.YES);
  1182. int rt = md.run();
  1183. md.destroy();
  1184. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  1185. {
  1186. new_level();
  1187. send_state();
  1188. }
  1189. }
  1190. private void on_open_level(GLib.SimpleAction action, GLib.Variant? param)
  1191. {
  1192. if (!_database.changed())
  1193. {
  1194. load_level(param.get_string());
  1195. return;
  1196. }
  1197. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  1198. , Gtk.DialogFlags.MODAL
  1199. , Gtk.MessageType.WARNING
  1200. , Gtk.ButtonsType.NONE
  1201. , "File changed, save?"
  1202. );
  1203. md.add_button("Open _without Saving", ResponseType.NO);
  1204. md.add_button("_Cancel", ResponseType.CANCEL);
  1205. md.add_button("_Save", ResponseType.YES);
  1206. md.set_default_response(ResponseType.YES);
  1207. int rt = md.run();
  1208. md.destroy();
  1209. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  1210. load_level(param.get_string());
  1211. }
  1212. private void on_open_project(GLib.SimpleAction action, GLib.Variant? param)
  1213. {
  1214. if (!_database.changed())
  1215. {
  1216. load_project();
  1217. return;
  1218. }
  1219. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  1220. , Gtk.DialogFlags.MODAL
  1221. , Gtk.MessageType.WARNING
  1222. , Gtk.ButtonsType.NONE
  1223. , "File changed, save?"
  1224. );
  1225. md.add_button("Open _without Saving", ResponseType.NO);
  1226. md.add_button("_Cancel", ResponseType.CANCEL);
  1227. md.add_button("_Save", ResponseType.YES);
  1228. md.set_default_response(ResponseType.YES);
  1229. int rt = md.run();
  1230. md.destroy();
  1231. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  1232. load_project();
  1233. }
  1234. private void on_save(GLib.SimpleAction action, GLib.Variant? param)
  1235. {
  1236. save();
  1237. }
  1238. private void on_save_as(GLib.SimpleAction action, GLib.Variant? param)
  1239. {
  1240. save_as(null);
  1241. }
  1242. private void on_import(GLib.SimpleAction action, GLib.Variant? param)
  1243. {
  1244. _project.import(null, this.active_window);
  1245. }
  1246. private void on_preferences(GLib.SimpleAction action, GLib.Variant? param)
  1247. {
  1248. _preferences_dialog.show_all();
  1249. }
  1250. private void on_deploy(GLib.SimpleAction action, GLib.Variant? param)
  1251. {
  1252. deploy_game();
  1253. }
  1254. private void on_quit(GLib.SimpleAction action, GLib.Variant? param)
  1255. {
  1256. this.active_window.close();
  1257. }
  1258. private void on_show_grid(GLib.SimpleAction action, GLib.Variant? param)
  1259. {
  1260. _show_grid = !action.get_state().get_boolean();
  1261. send_state();
  1262. action.set_state(new GLib.Variant.boolean(_show_grid));
  1263. }
  1264. private void on_custom_grid()
  1265. {
  1266. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Grid size"
  1267. , this.active_window
  1268. , DialogFlags.MODAL
  1269. , "Cancel"
  1270. , ResponseType.CANCEL
  1271. , "Ok"
  1272. , ResponseType.OK
  1273. , null
  1274. );
  1275. EntryDouble sb = new EntryDouble(_grid_size, 0.1, 1000);
  1276. sb.activate.connect(() => { dg.response(ResponseType.OK); });
  1277. dg.get_content_area().add(sb);
  1278. dg.skip_taskbar_hint = true;
  1279. dg.show_all();
  1280. if (dg.run() == (int)ResponseType.OK)
  1281. {
  1282. _grid_size = sb.value;
  1283. send_state();
  1284. }
  1285. dg.destroy();
  1286. }
  1287. private void on_rotation_snap(GLib.SimpleAction action, GLib.Variant? param)
  1288. {
  1289. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Rotation snap"
  1290. , this.active_window
  1291. , DialogFlags.MODAL
  1292. , "Cancel"
  1293. , ResponseType.CANCEL
  1294. , "Ok"
  1295. , ResponseType.OK
  1296. , null
  1297. );
  1298. EntryDouble sb = new EntryDouble(_rotation_snap, 1.0, 180.0);
  1299. sb.activate.connect(() => { dg.response(ResponseType.OK); });
  1300. dg.get_content_area().add(sb);
  1301. dg.skip_taskbar_hint = true;
  1302. dg.show_all();
  1303. if (dg.run() == (int)ResponseType.OK)
  1304. {
  1305. _rotation_snap = sb.value;
  1306. send_state();
  1307. }
  1308. dg.destroy();
  1309. }
  1310. private void on_create_primitive(GLib.SimpleAction action, GLib.Variant? param)
  1311. {
  1312. if (action.name == "primitive-cube")
  1313. _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cube"));
  1314. else if (action.name == "primitive-sphere")
  1315. _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/sphere"));
  1316. else if (action.name == "primitive-cone")
  1317. _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cone"));
  1318. else if (action.name == "primitive-cylinder")
  1319. _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cylinder"));
  1320. else if (action.name == "primitive-plane")
  1321. _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/plane"));
  1322. else if (action.name == "camera")
  1323. _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/camera"));
  1324. else if (action.name == "light")
  1325. _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/light"));
  1326. else if (action.name == "sound-source")
  1327. _editor.send_script(LevelEditorApi.set_placeable("sound", ""));
  1328. activate_action("tool", new GLib.Variant.string("place"));
  1329. }
  1330. private void on_camera_view(GLib.SimpleAction action, GLib.Variant? param)
  1331. {
  1332. string name = param.get_string();
  1333. if (name == "perspective")
  1334. _editor.send_script("LevelEditor:camera_view_perspective()");
  1335. else if (name == "front")
  1336. _editor.send_script("LevelEditor:camera_view_front()");
  1337. else if (name == "back")
  1338. _editor.send_script("LevelEditor:camera_view_back()");
  1339. else if (name == "right")
  1340. _editor.send_script("LevelEditor:camera_view_right()");
  1341. else if (name == "left")
  1342. _editor.send_script("LevelEditor:camera_view_left()");
  1343. else if (name == "top")
  1344. _editor.send_script("LevelEditor:camera_view_top()");
  1345. else if (name == "bottom")
  1346. _editor.send_script("LevelEditor:camera_view_bottom()");
  1347. action.set_state(param);
  1348. }
  1349. private void on_resource_chooser(GLib.SimpleAction action, GLib.Variant? param)
  1350. {
  1351. _resource_popover.show_all();
  1352. }
  1353. private void on_project_browser(GLib.SimpleAction action, GLib.Variant? param)
  1354. {
  1355. if (_project_slide.is_visible())
  1356. {
  1357. _project_slide.hide();
  1358. }
  1359. else
  1360. {
  1361. _project_slide.show_all();
  1362. }
  1363. }
  1364. private void on_console(GLib.SimpleAction action, GLib.Variant? param)
  1365. {
  1366. if (_console_view.is_visible())
  1367. {
  1368. if (_console_view._entry.has_focus)
  1369. _console_view.hide();
  1370. else
  1371. _console_view._entry.grab_focus();
  1372. }
  1373. else
  1374. {
  1375. _console_view.show_all();
  1376. }
  1377. }
  1378. private void on_statusbar(GLib.SimpleAction action, GLib.Variant? param)
  1379. {
  1380. if (_statusbar.is_visible())
  1381. {
  1382. _statusbar.hide();
  1383. }
  1384. else
  1385. {
  1386. _statusbar.show_all();
  1387. }
  1388. }
  1389. private void on_inspector(GLib.SimpleAction action, GLib.Variant? param)
  1390. {
  1391. if (_inspector_slide.is_visible())
  1392. {
  1393. _inspector_slide.hide();
  1394. }
  1395. else
  1396. {
  1397. _inspector_slide.show_all();
  1398. }
  1399. }
  1400. private void on_editor_restart(GLib.SimpleAction action, GLib.Variant? param)
  1401. {
  1402. restart_editor();
  1403. }
  1404. private void on_refresh_lua(GLib.SimpleAction action, GLib.Variant? param)
  1405. {
  1406. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  1407. if (_data_compiler.compile.end(res))
  1408. {
  1409. _editor.send(DeviceApi.refresh());
  1410. _game.send(DeviceApi.refresh());
  1411. }
  1412. });
  1413. }
  1414. private void on_snap_to_grid(GLib.SimpleAction action, GLib.Variant? param)
  1415. {
  1416. _snap_to_grid = !action.get_state().get_boolean();
  1417. send_state();
  1418. action.set_state(new GLib.Variant.boolean(_snap_to_grid));
  1419. }
  1420. private void on_debug_render_world(GLib.SimpleAction action, GLib.Variant? param)
  1421. {
  1422. _debug_render_world = !action.get_state().get_boolean();
  1423. send_state();
  1424. action.set_state(new GLib.Variant.boolean(_debug_render_world));
  1425. }
  1426. private void on_debug_physics_world(GLib.SimpleAction action, GLib.Variant? param)
  1427. {
  1428. _debug_physics_world = !action.get_state().get_boolean();
  1429. send_state();
  1430. action.set_state(new GLib.Variant.boolean(_debug_physics_world));
  1431. }
  1432. private void on_run_game(GLib.SimpleAction action, GLib.Variant? param)
  1433. {
  1434. if (_game.is_connected())
  1435. {
  1436. stop_game();
  1437. _toolbar_run.icon_name = "game-run";
  1438. }
  1439. else
  1440. {
  1441. start_game(action.name == "test-level" ? StartGame.TEST : StartGame.NORMAL);
  1442. _toolbar_run.icon_name = "game-stop";
  1443. }
  1444. }
  1445. private void on_undo(GLib.SimpleAction action, GLib.Variant? param)
  1446. {
  1447. int id = _database.undo();
  1448. if (id != -1)
  1449. _statusbar.set_temporary_message("Undo: " + ActionNames[id]);
  1450. }
  1451. private void on_redo(GLib.SimpleAction action, GLib.Variant? param)
  1452. {
  1453. int id = _database.redo();
  1454. if (id != -1)
  1455. _statusbar.set_temporary_message("Redo: " + ActionNames[id]);
  1456. }
  1457. private void on_duplicate(GLib.SimpleAction action, GLib.Variant? param)
  1458. {
  1459. _level.duplicate_selected_objects();
  1460. }
  1461. private void on_delete(GLib.SimpleAction action, GLib.Variant? param)
  1462. {
  1463. _level.destroy_selected_objects();
  1464. }
  1465. private void on_manual(GLib.SimpleAction action, GLib.Variant? param)
  1466. {
  1467. try
  1468. {
  1469. AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION, null);
  1470. }
  1471. catch (Error e)
  1472. {
  1473. _console_view.loge("editor", e.message);
  1474. }
  1475. }
  1476. private void on_report_issue(GLib.SimpleAction action, GLib.Variant? param)
  1477. {
  1478. try
  1479. {
  1480. AppInfo.launch_default_for_uri("https://github.com/dbartolini/crown/issues", null);
  1481. }
  1482. catch (Error e)
  1483. {
  1484. _console_view.loge("editor", e.message);
  1485. }
  1486. }
  1487. private void on_open_last_log(GLib.SimpleAction action, GLib.Variant? param)
  1488. {
  1489. File file = File.new_for_path(_project.data_dir() + "/last.log");
  1490. try
  1491. {
  1492. AppInfo.launch_default_for_uri(file.get_uri(), null);
  1493. }
  1494. catch (Error e)
  1495. {
  1496. _console_view.loge("editor", e.message);
  1497. }
  1498. }
  1499. private void on_changelog(GLib.SimpleAction action, GLib.Variant? param)
  1500. {
  1501. try
  1502. {
  1503. AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION + "/changelog.html", null);
  1504. }
  1505. catch (Error e)
  1506. {
  1507. _console_view.loge("editor", e.message);
  1508. }
  1509. }
  1510. private void on_about(GLib.SimpleAction action, GLib.Variant? param)
  1511. {
  1512. Gtk.AboutDialog dlg = new Gtk.AboutDialog();
  1513. dlg.set_destroy_with_parent(true);
  1514. dlg.set_transient_for(this.active_window);
  1515. dlg.set_modal(true);
  1516. dlg.set_logo_icon_name("pepper");
  1517. dlg.program_name = "Crown Game Engine";
  1518. dlg.version = CROWN_VERSION;
  1519. dlg.website = "https://github.com/dbartolini/crown";
  1520. dlg.copyright = "Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.";
  1521. dlg.license = "Crown Game Engine.\n"
  1522. + "Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.\n"
  1523. + "\n"
  1524. + "This program is free software; you can redistribute it and/or\n"
  1525. + "modify it under the terms of the GNU General Public License\n"
  1526. + "as published by the Free Software Foundation; either version 2\n"
  1527. + "of the License, or (at your option) any later version.\n"
  1528. + "\n"
  1529. + "This program is distributed in the hope that it will be useful,\n"
  1530. + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1531. + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1532. + "GNU General Public License for more details.\n"
  1533. + "\n"
  1534. + "You should have received a copy of the GNU General Public License\n"
  1535. + "along with this program; if not, write to the Free Software\n"
  1536. + "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
  1537. ;
  1538. dlg.run();
  1539. dlg.destroy();
  1540. }
  1541. public void set_autosave_timer(uint minutes)
  1542. {
  1543. if (_save_timer_id > 0)
  1544. GLib.Source.remove(_save_timer_id);
  1545. _save_timer_id = GLib.Timeout.add_seconds(minutes*60, save_timeout);
  1546. }
  1547. }
  1548. public static GLib.SubprocessFlags subprocess_flags()
  1549. {
  1550. GLib.SubprocessFlags flags = SubprocessFlags.NONE;
  1551. #if !CROWN_DEBUG
  1552. flags |= SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE;
  1553. #endif
  1554. return flags;
  1555. }
  1556. public static int main(string[] args)
  1557. {
  1558. LevelEditorApplication app = new LevelEditorApplication();
  1559. return app.run(args);
  1560. }
  1561. }