level_editor.vala 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  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. { "build-data", on_build_data, null, null },
  161. { "reload-lua", on_refresh_lua, null, null },
  162. { "menu-view", null, null, null },
  163. { "resource-chooser", on_resource_chooser, null, null },
  164. { "project-browser", on_project_browser, null, null },
  165. { "console", on_console, null, null },
  166. { "statusbar", on_statusbar, null, null },
  167. { "inspector", on_inspector, null, null },
  168. { "menu-run", null, null, null },
  169. { "test-level", on_run_game, null, null },
  170. { "run-game", on_run_game, null, null },
  171. { "menu-help", null, null, null },
  172. { "manual", on_manual, null, null },
  173. { "report-issue", on_report_issue, null, null },
  174. { "browse-logs", on_browse_logs, null, null },
  175. { "changelog", on_changelog, null, null },
  176. { "about", on_about, null, null },
  177. { "debug-render-world", on_debug_render_world, null, "false" },
  178. { "debug-physics-world", on_debug_physics_world, null, "false" }
  179. };
  180. // Command line options
  181. private string _source_dir = "";
  182. private string _toolchain_dir = "";
  183. private string _level_resource = "";
  184. private bool _create_initial_files = false;
  185. // Editor state
  186. private double _grid_size;
  187. private double _rotation_snap;
  188. private bool _show_grid;
  189. private bool _snap_to_grid;
  190. private bool _debug_render_world;
  191. private bool _debug_physics_world;
  192. private ToolType _tool_type;
  193. private SnapMode _snap_mode;
  194. private ReferenceSystem _reference_system;
  195. // Engine connections
  196. private GLib.Subprocess _compiler_process;
  197. private GLib.Subprocess _editor_process;
  198. private GLib.Subprocess _game_process;
  199. private ConsoleClient _compiler;
  200. public ConsoleClient _editor;
  201. private ConsoleClient _game;
  202. // Level data
  203. private Database _database;
  204. private Project _project;
  205. private ProjectStore _project_store;
  206. private Level _level;
  207. private DataCompiler _data_compiler;
  208. // Widgets
  209. private ProjectBrowser _project_browser;
  210. private EditorView _editor_view;
  211. private LevelTreeView _level_treeview;
  212. private LevelLayersTreeView _level_layers_treeview;
  213. private PropertiesView _properties_view;
  214. private PreferencesDialog _preferences_dialog;
  215. private ResourceChooser _resource_chooser;
  216. private Gtk.Popover _resource_popover;
  217. private Gtk.Overlay _editor_view_overlay;
  218. private Slide _project_slide;
  219. private Slide _editor_slide;
  220. private Slide _inspector_slide;
  221. private Gtk.Toolbar _toolbar;
  222. private Gtk.ToolButton _toolbar_run;
  223. private Gtk.Notebook _level_tree_view_notebook;
  224. private Gtk.Paned _editor_pane;
  225. private Gtk.Paned _content_pane;
  226. private Gtk.Paned _inspector_pane;
  227. private Gtk.Paned _main_pane;
  228. private Statusbar _statusbar;
  229. private Gtk.Box _main_vbox;
  230. private Gtk.FileFilter _file_filter;
  231. private Gtk.ComboBoxText _combo;
  232. private uint _save_timer_id;
  233. public LevelEditorApplication()
  234. {
  235. Object(application_id: "org.crown.level_editor"
  236. , flags: GLib.ApplicationFlags.FLAGS_NONE
  237. );
  238. }
  239. protected override void startup()
  240. {
  241. base.startup();
  242. Gtk.Settings.get_default().gtk_theme_name = "Adwaita";
  243. Gtk.Settings.get_default().gtk_application_prefer_dark_theme = true;
  244. Gtk.CssProvider provider = new Gtk.CssProvider();
  245. Gdk.Screen screen = Gdk.Display.get_default().get_default_screen();
  246. Gtk.StyleContext.add_provider_for_screen(screen, provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
  247. provider.load_from_resource("/org/crown/level_editor/css/style.css");
  248. this.add_action_entries(action_entries, this);
  249. if (_source_dir == "")
  250. {
  251. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Select folder where to create new project..."
  252. , null
  253. , FileChooserAction.SELECT_FOLDER
  254. , "Cancel"
  255. , ResponseType.CANCEL
  256. , "Select"
  257. , ResponseType.ACCEPT
  258. );
  259. if (fcd.run() != (int)ResponseType.ACCEPT)
  260. {
  261. fcd.destroy();
  262. return;
  263. }
  264. string dir = fcd.get_filename();
  265. fcd.destroy();
  266. if (GLib.FileUtils.test(dir, FileTest.IS_REGULAR))
  267. {
  268. stdout.printf("Source directory can't be a regular file\n");
  269. return;
  270. }
  271. _source_dir = dir;
  272. _create_initial_files = true;
  273. }
  274. _compiler = new ConsoleClient();
  275. _compiler.connected.connect(on_compiler_connected);
  276. _compiler.disconnected.connect(on_compiler_disconnected_unexpected);
  277. _compiler.message_received.connect(on_message_received);
  278. _data_compiler = new DataCompiler(_compiler);
  279. _project = new Project(_data_compiler);
  280. _project.load(_source_dir, _toolchain_dir);
  281. if (_create_initial_files)
  282. _project.create_initial_files();
  283. _database = new Database();
  284. _editor = new ConsoleClient();
  285. _editor.connected.connect(on_editor_connected);
  286. _editor.disconnected.connect(on_editor_disconnected_unexpected);
  287. _editor.message_received.connect(on_message_received);
  288. _game = new ConsoleClient();
  289. _game.connected.connect(on_game_connected);
  290. _game.disconnected.connect(on_game_disconnected);
  291. _game.message_received.connect(on_message_received);
  292. _level = new Level(_database, _editor, _project);
  293. // Editor state
  294. _grid_size = 1.0;
  295. _rotation_snap = 15.0;
  296. _show_grid = true;
  297. _snap_to_grid = true;
  298. _debug_render_world = false;
  299. _debug_physics_world = false;
  300. _tool_type = ToolType.MOVE;
  301. _snap_mode = SnapMode.RELATIVE;
  302. _reference_system = ReferenceSystem.LOCAL;
  303. // Engine connections
  304. _compiler_process = null;
  305. _editor_process = null;
  306. _game_process = null;
  307. _project_store = new ProjectStore(_project);
  308. // Widgets
  309. _combo = new Gtk.ComboBoxText();
  310. _combo.append("editor", "Editor");
  311. _combo.append("game", "Game");
  312. _combo.set_active_id("editor");
  313. _console_view = new ConsoleView(_project, _combo);
  314. _project_browser = new ProjectBrowser(_project, _project_store);
  315. _level_treeview = new LevelTreeView(_database, _level);
  316. _level_layers_treeview = new LevelLayersTreeView(_database, _level);
  317. _properties_view = new PropertiesView(_level, _project_store);
  318. _project_slide = new Slide();
  319. _editor_slide = new Slide();
  320. _inspector_slide = new Slide();
  321. Gtk.Builder builder = new Gtk.Builder.from_resource("/org/crown/level_editor/ui/toolbar.ui");
  322. _toolbar = builder.get_object("toolbar") as Gtk.Toolbar;
  323. _toolbar_run = builder.get_object("run") as Gtk.ToolButton;
  324. _editor_view_overlay = new Gtk.Overlay();
  325. _editor_view_overlay.add_overlay(_toolbar);
  326. _resource_popover = new Gtk.Popover(_toolbar);
  327. _resource_popover.delete_event.connect(() => { _resource_popover.hide(); return true; });
  328. _resource_popover.modal = true;
  329. _preferences_dialog = new PreferencesDialog(this);
  330. _preferences_dialog.set_transient_for(this.active_window);
  331. _preferences_dialog.delete_event.connect(() => { _preferences_dialog.hide(); return true; });
  332. _resource_chooser = new ResourceChooser(_project, _project_store, true);
  333. _resource_chooser.resource_selected.connect(on_resource_browser_resource_selected);
  334. _resource_chooser.resource_selected.connect(() => { _resource_popover.hide(); });
  335. _resource_popover.add(_resource_chooser);
  336. _level_tree_view_notebook = new Notebook();
  337. _level_tree_view_notebook.show_border = false;
  338. _level_tree_view_notebook.append_page(_level_treeview, new Gtk.Image.from_icon_name("level-tree", IconSize.SMALL_TOOLBAR));
  339. _level_tree_view_notebook.append_page(_level_layers_treeview, new Gtk.Image.from_icon_name("level-layers", IconSize.SMALL_TOOLBAR));
  340. _editor_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  341. _editor_pane.pack1(_project_slide, false, false);
  342. _editor_pane.pack2(_editor_slide, true, false);
  343. _editor_pane.set_position(210);
  344. _content_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  345. _content_pane.pack1(_editor_pane, true, false);
  346. _content_pane.pack2(_console_view, false, false);
  347. _content_pane.set_position(500);
  348. _inspector_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  349. _inspector_pane.pack1(_level_tree_view_notebook, true, false);
  350. _inspector_pane.pack2(_properties_view, false, false);
  351. _inspector_pane.set_position(250);
  352. _main_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  353. _main_pane.pack1(_content_pane, true, false);
  354. _main_pane.pack2(_inspector_slide, false, false);
  355. _main_pane.set_position(WINDOW_DEFAULT_WIDTH - 375);
  356. _statusbar = new Statusbar();
  357. _main_vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  358. _main_vbox.pack_start(_main_pane, true, true, 0);
  359. _main_vbox.pack_start(_statusbar, false, false, 0);
  360. _file_filter = new Gtk.FileFilter();
  361. _file_filter.set_filter_name("Level (*.level)");
  362. _file_filter.add_pattern("*.level");
  363. if (_level_resource != "")
  364. {
  365. string level_path = Path.build_filename(_project.source_dir(), _level_resource + ".level");
  366. if (!GLib.FileUtils.test(level_path, FileTest.EXISTS) || !GLib.FileUtils.test(level_path, FileTest.IS_REGULAR))
  367. {
  368. loge("Level resource '%s' does not exist.".printf(_level_resource));
  369. return;
  370. }
  371. _level.load(level_path);
  372. }
  373. else
  374. {
  375. _level.load_empty_level();
  376. }
  377. load_settings();
  378. restart_compiler();
  379. }
  380. public void load_settings()
  381. {
  382. Hashtable settings = SJSON.load(_settings_file.get_path());
  383. _preferences_dialog.load(settings.has_key("preferences") ? (Hashtable)settings["preferences"] : new Hashtable());
  384. }
  385. public void save_settings()
  386. {
  387. Hashtable preferences = new Hashtable();
  388. _preferences_dialog.save(preferences);
  389. Hashtable settings = new Hashtable();
  390. settings["preferences"] = preferences;
  391. SJSON.save(settings, _settings_file.get_path());
  392. }
  393. protected override void activate()
  394. {
  395. if (_source_dir == "")
  396. return;
  397. if (this.active_window == null)
  398. {
  399. LevelEditorWindow win = new LevelEditorWindow(this);
  400. win.add(_main_vbox);
  401. try
  402. {
  403. win.icon = IconTheme.get_default().load_icon("pepper", 48, 0);
  404. }
  405. catch (Error e)
  406. {
  407. loge(e.message);
  408. }
  409. }
  410. this.active_window.show_all();
  411. }
  412. protected override bool local_command_line(ref unowned string[] args, out int exit_status)
  413. {
  414. if (args.length > 1)
  415. {
  416. if (!GLib.FileUtils.test(args[1], FileTest.EXISTS) || !GLib.FileUtils.test(args[1], FileTest.IS_DIR))
  417. {
  418. loge("Source directory does not exist or it is not a directory\n");
  419. exit_status = 1;
  420. return true;
  421. }
  422. _source_dir = args[1];
  423. }
  424. if (args.length > 2)
  425. {
  426. // Validation is done below after the Project object instantiation
  427. _level_resource = args[2];
  428. }
  429. if (args.length > 3)
  430. {
  431. if (!GLib.FileUtils.test(args[3], FileTest.EXISTS) || !GLib.FileUtils.test(args[3], FileTest.IS_DIR))
  432. {
  433. loge("Toolchain directory does not exist or it is not a directory\n");
  434. exit_status = 1;
  435. return true;
  436. }
  437. _toolchain_dir = args[3];
  438. }
  439. else
  440. {
  441. bool found = false;
  442. /// More desirable paths come first
  443. string toolchain_paths[] =
  444. {
  445. "../..",
  446. "../../../samples"
  447. };
  448. for (int i = 0; i < toolchain_paths.length; ++i)
  449. {
  450. string path = Path.build_filename(toolchain_paths[i], "core");
  451. // Try to locate the toolchain directory
  452. if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR))
  453. {
  454. _toolchain_dir = toolchain_paths[i];
  455. found = true;
  456. break;
  457. }
  458. }
  459. if (!found)
  460. {
  461. loge("Unable to find the toolchain directory\n");
  462. exit_status = 1;
  463. return true;
  464. }
  465. }
  466. exit_status = 0;
  467. return false;
  468. }
  469. protected override int command_line(ApplicationCommandLine command_line)
  470. {
  471. this.activate();
  472. return 0;
  473. }
  474. public ConsoleClient? current_selected_client()
  475. {
  476. if (_combo.get_active_id() == "editor")
  477. return _editor;
  478. else if (_combo.get_active_id() == "game")
  479. return _game;
  480. else
  481. return null;
  482. }
  483. private void on_resource_browser_resource_selected(string type, string name)
  484. {
  485. _editor.send_script(LevelEditorApi.set_placeable(type, name));
  486. activate_action("tool", new GLib.Variant.string("place"));
  487. }
  488. private void on_compiler_connected(string address, int port)
  489. {
  490. logi("Connected to data_compiler@%s:%d".printf(address, port));
  491. _compiler.receive_async();
  492. }
  493. private void on_compiler_disconnected()
  494. {
  495. logi("Disconnected from data_compiler");
  496. }
  497. private void on_compiler_disconnected_unexpected()
  498. {
  499. on_compiler_disconnected();
  500. stop_game();
  501. stop_editor();
  502. // Reset the callback
  503. _data_compiler.finished(false);
  504. _project_slide.show_widget(compiler_crashed_label());
  505. _editor_slide.show_widget(compiler_crashed_label());
  506. _inspector_slide.show_widget(compiler_crashed_label());
  507. }
  508. private void on_editor_connected(string address, int port)
  509. {
  510. logi("Connected to level_editor@%s:%d".printf(address, port));
  511. _editor.receive_async();
  512. }
  513. private void on_editor_disconnected()
  514. {
  515. logi("Disconnected from editor");
  516. }
  517. private void on_editor_disconnected_unexpected()
  518. {
  519. on_editor_disconnected();
  520. Gtk.Label label = new Gtk.Label(null);
  521. label.set_markup("Something went wrong.\rTry to <a href=\"restart\">restart</a> this view.");
  522. label.activate_link.connect(() => {
  523. activate_action("restart", null);
  524. return true;
  525. });
  526. _editor_slide.show_widget(label);
  527. }
  528. private void on_game_connected(string address, int port)
  529. {
  530. logi("Connected to game@%s:%d".printf(address, port));
  531. _game.receive_async();
  532. _combo.set_active_id("game");
  533. }
  534. private void on_game_disconnected()
  535. {
  536. logi("Disconnected from game");
  537. _project.delete_garbage();
  538. _combo.set_active_id("editor");
  539. _toolbar_run.icon_name = "game-run";
  540. }
  541. private void on_message_received(ConsoleClient client, uint8[] json)
  542. {
  543. Hashtable msg = JSON.decode(json) as Hashtable;
  544. string msg_type = msg["type"] as string;
  545. if (msg_type == "message")
  546. {
  547. log((string)msg["system"], (string)msg["severity"], (string)msg["message"]);
  548. }
  549. else if (msg_type == "add_file")
  550. {
  551. string path = (string)msg["path"];
  552. _project.add_file(path);
  553. }
  554. else if (msg_type == "remove_file")
  555. {
  556. string path = (string)msg["path"];
  557. _project.remove_file(path);
  558. }
  559. else if (msg_type == "add_tree")
  560. {
  561. string path = (string)msg["path"];
  562. _project.add_tree(path);
  563. }
  564. else if (msg_type == "remove_tree")
  565. {
  566. string path = (string)msg["path"];
  567. _project.remove_tree(path);
  568. }
  569. else if (msg_type == "compile")
  570. {
  571. // Guid id = Guid.parse((string)msg["id"]);
  572. if (msg.has_key("start"))
  573. {
  574. // FIXME
  575. }
  576. else if (msg.has_key("success"))
  577. {
  578. _data_compiler.finished((bool)msg["success"]);
  579. }
  580. }
  581. else if (msg_type == "unit_spawned")
  582. {
  583. string id = (string) msg["id"];
  584. string name = (string) msg["name"];
  585. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  586. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  587. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  588. _level.on_unit_spawned(Guid.parse(id)
  589. , name
  590. , Vector3.from_array(pos)
  591. , Quaternion.from_array(rot)
  592. , Vector3.from_array(scl)
  593. );
  594. }
  595. else if (msg_type == "sound_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. double range = (double) msg["range"];
  603. double volume = (double) msg["volume"];
  604. bool loop = (bool) msg["loop"];
  605. _level.on_sound_spawned(Guid.parse(id)
  606. , name
  607. , Vector3.from_array(pos)
  608. , Quaternion.from_array(rot)
  609. , Vector3.from_array(scl)
  610. , range
  611. , volume
  612. , loop
  613. );
  614. }
  615. else if (msg_type == "move_objects")
  616. {
  617. Hashtable ids = (Hashtable)msg["ids"];
  618. Hashtable new_positions = (Hashtable)msg["new_positions"];
  619. Hashtable new_rotations = (Hashtable)msg["new_rotations"];
  620. Hashtable new_scales = (Hashtable)msg["new_scales"];
  621. ArrayList<string> keys = new ArrayList<string>.wrap(ids.keys.to_array());
  622. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  623. Guid[] n_ids = new Guid[keys.size];
  624. Vector3[] n_positions = new Vector3[keys.size];
  625. Quaternion[] n_rotations = new Quaternion[keys.size];
  626. Vector3[] n_scales = new Vector3[keys.size];
  627. for (int i = 0; i < keys.size; ++i)
  628. {
  629. string k = keys[i];
  630. n_ids[i] = Guid.parse((string)ids[k]);
  631. n_positions[i] = Vector3.from_array((ArrayList<Value?>)(new_positions[k]));
  632. n_rotations[i] = Quaternion.from_array((ArrayList<Value?>)new_rotations[k]);
  633. n_scales[i] = Vector3.from_array((ArrayList<Value?>)new_scales[k]);
  634. }
  635. _level.on_move_objects(n_ids, n_positions, n_rotations, n_scales);
  636. }
  637. else if (msg_type == "selection")
  638. {
  639. Hashtable objects = (Hashtable)msg["objects"];
  640. ArrayList<string> keys = new ArrayList<string>.wrap(objects.keys.to_array());
  641. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  642. Guid[] ids = new Guid[keys.size];
  643. for (int i = 0; i < keys.size; ++i)
  644. {
  645. string k = keys[i];
  646. ids[i] = Guid.parse((string)objects[k]);
  647. }
  648. _level.on_selection(ids);
  649. }
  650. else if (msg_type == "error")
  651. {
  652. loge((string)msg["message"]);
  653. }
  654. else
  655. {
  656. loge("Unknown message type: " + msg_type);
  657. }
  658. // Receive next message
  659. client.receive_async();
  660. }
  661. private void send_state()
  662. {
  663. StringBuilder sb = new StringBuilder();
  664. sb.append(LevelEditorApi.set_grid_size(_grid_size));
  665. sb.append(LevelEditorApi.set_rotation_snap(_rotation_snap));
  666. sb.append(LevelEditorApi.enable_show_grid(_show_grid));
  667. sb.append(LevelEditorApi.enable_snap_to_grid(_snap_to_grid));
  668. sb.append(LevelEditorApi.enable_debug_render_world(_debug_render_world));
  669. sb.append(LevelEditorApi.enable_debug_physics_world(_debug_physics_world));
  670. sb.append(LevelEditorApi.set_tool_type(_tool_type));
  671. sb.append(LevelEditorApi.set_snap_mode(_snap_mode));
  672. sb.append(LevelEditorApi.set_reference_system(_reference_system));
  673. _editor.send_script(sb.str);
  674. }
  675. private bool on_button_press(EventButton ev)
  676. {
  677. return true;
  678. }
  679. private bool on_button_release(EventButton ev)
  680. {
  681. return true;
  682. }
  683. Gtk.Widget starting_compiler_label()
  684. {
  685. return new Gtk.Label("Compiling resources, please wait...");
  686. }
  687. Gtk.Widget compiler_crashed_label()
  688. {
  689. Gtk.Label label = new Gtk.Label(null);
  690. label.set_markup("Data Compiler disconnected.\rTry to <a href=\"restart\">restart</a> compiler to continue.");
  691. label.activate_link.connect(() => {
  692. restart_compiler();
  693. return true;
  694. });
  695. return label;
  696. }
  697. Gtk.Widget compiler_failed_compilation_label()
  698. {
  699. Gtk.Label label = new Gtk.Label(null);
  700. label.set_markup("Data compilation failed.\rFix errors and <a href=\"restart\">restart</a> compiler to continue.");
  701. label.activate_link.connect(() => {
  702. restart_compiler();
  703. return true;
  704. });
  705. return label;
  706. }
  707. private void restart_compiler()
  708. {
  709. stop_compiler();
  710. _project.reset();
  711. _project_slide.show_widget(starting_compiler_label());
  712. _editor_slide.show_widget(starting_compiler_label());
  713. _inspector_slide.show_widget(starting_compiler_label());
  714. string args[] =
  715. {
  716. ENGINE_EXE,
  717. "--source-dir", _project.source_dir(),
  718. "--data-dir", _project.data_dir(),
  719. "--map-source-dir", "core", _project.toolchain_dir(),
  720. "--server",
  721. "--wait-console",
  722. null
  723. };
  724. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  725. sl.set_cwd(ENGINE_DIR);
  726. try
  727. {
  728. _compiler_process = sl.spawnv(args);
  729. }
  730. catch (Error e)
  731. {
  732. loge(e.message);
  733. }
  734. // It is an error if compiler disconnects after here.
  735. _compiler.disconnected.disconnect(on_compiler_disconnected);
  736. _compiler.disconnected.connect(on_compiler_disconnected_unexpected);
  737. for (int tries = 0; !_compiler.is_connected() && tries < 5; ++tries)
  738. {
  739. _compiler.connect("127.0.0.1", CROWN_DEFAULT_SERVER_PORT);
  740. GLib.Thread.usleep(250*1000);
  741. }
  742. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  743. if (_data_compiler.compile.end(res))
  744. {
  745. restart_editor();
  746. _project_slide.show_widget(_project_browser);
  747. _inspector_slide.show_widget(_inspector_pane);
  748. }
  749. else
  750. {
  751. _project_slide.show_widget(compiler_failed_compilation_label());
  752. _editor_slide.show_widget(compiler_failed_compilation_label());
  753. _inspector_slide.show_widget(compiler_failed_compilation_label());
  754. }
  755. });
  756. }
  757. private void stop_compiler()
  758. {
  759. stop_game();
  760. stop_editor();
  761. if (_compiler != null)
  762. {
  763. // Explicit call to this function should not produce error messages.
  764. _compiler.disconnected.disconnect(on_compiler_disconnected_unexpected);
  765. _compiler.disconnected.connect(on_compiler_disconnected);
  766. _compiler.send(DataCompilerApi.quit());
  767. _compiler.close();
  768. }
  769. if (_compiler_process != null)
  770. {
  771. try
  772. {
  773. _compiler_process.wait();
  774. }
  775. catch (Error e)
  776. {
  777. loge(e.message);
  778. }
  779. }
  780. }
  781. private void start_editor(uint window_xid)
  782. {
  783. if (window_xid == 0)
  784. return;
  785. string args[] =
  786. {
  787. ENGINE_EXE,
  788. "--data-dir", _project.data_dir(),
  789. "--boot-dir", LEVEL_EDITOR_BOOT_DIR,
  790. "--parent-window", window_xid.to_string(),
  791. "--wait-console",
  792. null
  793. };
  794. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  795. sl.set_cwd(ENGINE_DIR);
  796. try
  797. {
  798. _editor_process = sl.spawnv(args);
  799. }
  800. catch (Error e)
  801. {
  802. loge(e.message);
  803. }
  804. // It is an error if editor disconnects after here.
  805. _editor.disconnected.disconnect(on_editor_disconnected);
  806. _editor.disconnected.connect(on_editor_disconnected_unexpected);
  807. for (int tries = 0; !_editor.is_connected() && tries < 10; ++tries)
  808. {
  809. _editor.connect("127.0.0.1", 10001);
  810. GLib.Thread.usleep(500*1000);
  811. }
  812. _level.send_level();
  813. send_state();
  814. _preferences_dialog.apply();
  815. }
  816. private void stop_editor()
  817. {
  818. _resource_chooser.stop_editor();
  819. if (_editor != null)
  820. {
  821. // Explicit call to this function should not produce error messages.
  822. _editor.disconnected.disconnect(on_editor_disconnected_unexpected);
  823. _editor.disconnected.connect(on_editor_disconnected);
  824. _editor.send_script("Device.quit()");
  825. _editor.close();
  826. }
  827. if (_editor_process != null)
  828. {
  829. try
  830. {
  831. _editor_process.wait();
  832. }
  833. catch (Error e)
  834. {
  835. loge(e.message);
  836. }
  837. }
  838. _editor_slide.show_widget(new Gtk.Label("Disconnected."));
  839. }
  840. private void restart_editor()
  841. {
  842. stop_editor();
  843. if (_editor_view != null)
  844. {
  845. _editor_view_overlay.remove(_editor_view);
  846. _editor_view = null;
  847. }
  848. _editor_view = new EditorView(_editor);
  849. _editor_view.realized.connect(on_editor_view_realized);
  850. _editor_view.button_press_event.connect(on_button_press);
  851. _editor_view.button_release_event.connect(on_button_release);
  852. _editor_view_overlay.add(_editor_view);
  853. _editor_slide.show_widget(_editor_view_overlay);
  854. _resource_chooser.restart_editor();
  855. }
  856. private void start_game(StartGame sg)
  857. {
  858. _project.dump_test_level(_database);
  859. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  860. if (_data_compiler.compile.end(res))
  861. {
  862. string args[] =
  863. {
  864. ENGINE_EXE,
  865. "--data-dir", _project.data_dir(),
  866. "--console-port", "12345",
  867. "--wait-console",
  868. "--lua-string", sg == StartGame.TEST ? "TEST=true" : "",
  869. null
  870. };
  871. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  872. sl.set_cwd(ENGINE_DIR);
  873. try
  874. {
  875. _game_process = sl.spawnv(args);
  876. }
  877. catch (Error e)
  878. {
  879. loge(e.message);
  880. }
  881. for (int tries = 0; !_game.is_connected() && tries < 10; ++tries)
  882. {
  883. _game.connect("127.0.0.1", 12345);
  884. GLib.Thread.usleep(500*1000);
  885. }
  886. }
  887. else
  888. {
  889. _toolbar_run.icon_name = "game-run";
  890. }
  891. });
  892. }
  893. private void stop_game()
  894. {
  895. if (_game != null)
  896. {
  897. _game.send_script("Device.quit()");
  898. _game.close();
  899. }
  900. if (_game_process != null)
  901. {
  902. try
  903. {
  904. _game_process.wait();
  905. }
  906. catch (Error e)
  907. {
  908. loge(e.message);
  909. }
  910. }
  911. }
  912. private void deploy_game()
  913. {
  914. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Select destination directory..."
  915. , this.active_window
  916. , FileChooserAction.SELECT_FOLDER
  917. , "Cancel"
  918. , ResponseType.CANCEL
  919. , "Open"
  920. , ResponseType.ACCEPT
  921. );
  922. if (fcd.run() == (int)ResponseType.ACCEPT)
  923. {
  924. GLib.File data_dir = File.new_for_path(fcd.get_filename());
  925. string args[] =
  926. {
  927. ENGINE_EXE,
  928. "--source-dir", _project.source_dir(),
  929. "--map-source-dir", "core", _project.toolchain_dir(),
  930. "--data-dir", data_dir.get_path(),
  931. "--compile",
  932. null
  933. };
  934. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  935. sl.set_cwd(ENGINE_DIR);
  936. try
  937. {
  938. GLib.Subprocess compiler = sl.spawnv(args);
  939. compiler.wait();
  940. if (compiler.get_exit_status() == 0)
  941. {
  942. string game_name = DEPLOY_DEFAULT_NAME;
  943. GLib.File engine_exe_src = File.new_for_path(DEPLOY_EXE);
  944. GLib.File engine_exe_dst = File.new_for_path(Path.build_filename(data_dir.get_path(), game_name + EXE_SUFFIX));
  945. engine_exe_src.copy(engine_exe_dst, FileCopyFlags.OVERWRITE);
  946. #if CROWN_PLATFORM_WINDOWS
  947. string lua51_name = "lua51.dll";
  948. GLib.File lua51_dll_src = File.new_for_path(lua51_name);
  949. GLib.File lua51_dll_dst = File.new_for_path(Path.build_filename(data_dir.get_path(), lua51_name));
  950. lua51_dll_src.copy(lua51_dll_dst, FileCopyFlags.OVERWRITE);
  951. string openal_name = "openal-release.dll";
  952. GLib.File openal_dll_src = File.new_for_path(openal_name);
  953. GLib.File openal_dll_dst = File.new_for_path(Path.build_filename(data_dir.get_path(), openal_name));
  954. openal_dll_src.copy(openal_dll_dst, FileCopyFlags.OVERWRITE);
  955. #endif // CROWN_PLATFORM_WINDOWS
  956. logi("Project deployed to `%s`".printf(data_dir.get_path()));
  957. }
  958. }
  959. catch (Error e)
  960. {
  961. logi("%s".printf(e.message));
  962. logi("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. loge("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. logi("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. loge("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_build_data(GLib.SimpleAction action, GLib.Variant? param)
  1405. {
  1406. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  1407. _data_compiler.compile.end(res);
  1408. });
  1409. }
  1410. private void on_refresh_lua(GLib.SimpleAction action, GLib.Variant? param)
  1411. {
  1412. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  1413. if (_data_compiler.compile.end(res))
  1414. {
  1415. _editor.send(DeviceApi.refresh());
  1416. _game.send(DeviceApi.refresh());
  1417. }
  1418. });
  1419. }
  1420. private void on_snap_to_grid(GLib.SimpleAction action, GLib.Variant? param)
  1421. {
  1422. _snap_to_grid = !action.get_state().get_boolean();
  1423. send_state();
  1424. action.set_state(new GLib.Variant.boolean(_snap_to_grid));
  1425. }
  1426. private void on_debug_render_world(GLib.SimpleAction action, GLib.Variant? param)
  1427. {
  1428. _debug_render_world = !action.get_state().get_boolean();
  1429. send_state();
  1430. action.set_state(new GLib.Variant.boolean(_debug_render_world));
  1431. }
  1432. private void on_debug_physics_world(GLib.SimpleAction action, GLib.Variant? param)
  1433. {
  1434. _debug_physics_world = !action.get_state().get_boolean();
  1435. send_state();
  1436. action.set_state(new GLib.Variant.boolean(_debug_physics_world));
  1437. }
  1438. private void on_run_game(GLib.SimpleAction action, GLib.Variant? param)
  1439. {
  1440. if (_game.is_connected())
  1441. {
  1442. stop_game();
  1443. }
  1444. else
  1445. {
  1446. // Always change icon state regardless of failures
  1447. _toolbar_run.icon_name = "game-stop";
  1448. start_game(action.name == "test-level" ? StartGame.TEST : StartGame.NORMAL);
  1449. }
  1450. }
  1451. private void on_undo(GLib.SimpleAction action, GLib.Variant? param)
  1452. {
  1453. int id = _database.undo();
  1454. if (id != -1)
  1455. _statusbar.set_temporary_message("Undo: " + ActionNames[id]);
  1456. }
  1457. private void on_redo(GLib.SimpleAction action, GLib.Variant? param)
  1458. {
  1459. int id = _database.redo();
  1460. if (id != -1)
  1461. _statusbar.set_temporary_message("Redo: " + ActionNames[id]);
  1462. }
  1463. private void on_duplicate(GLib.SimpleAction action, GLib.Variant? param)
  1464. {
  1465. _level.duplicate_selected_objects();
  1466. }
  1467. private void on_delete(GLib.SimpleAction action, GLib.Variant? param)
  1468. {
  1469. _level.destroy_selected_objects();
  1470. }
  1471. private void on_manual(GLib.SimpleAction action, GLib.Variant? param)
  1472. {
  1473. try
  1474. {
  1475. AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION, null);
  1476. }
  1477. catch (Error e)
  1478. {
  1479. loge(e.message);
  1480. }
  1481. }
  1482. private void on_report_issue(GLib.SimpleAction action, GLib.Variant? param)
  1483. {
  1484. try
  1485. {
  1486. AppInfo.launch_default_for_uri("https://github.com/dbartolini/crown/issues", null);
  1487. }
  1488. catch (Error e)
  1489. {
  1490. loge(e.message);
  1491. }
  1492. }
  1493. private void on_browse_logs(GLib.SimpleAction action, GLib.Variant? param)
  1494. {
  1495. open_directory(_logs_dir.get_path());
  1496. }
  1497. private void on_changelog(GLib.SimpleAction action, GLib.Variant? param)
  1498. {
  1499. try
  1500. {
  1501. AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION + "/changelog.html", null);
  1502. }
  1503. catch (Error e)
  1504. {
  1505. loge(e.message);
  1506. }
  1507. }
  1508. private void on_about(GLib.SimpleAction action, GLib.Variant? param)
  1509. {
  1510. Gtk.AboutDialog dlg = new Gtk.AboutDialog();
  1511. dlg.set_destroy_with_parent(true);
  1512. dlg.set_transient_for(this.active_window);
  1513. dlg.set_modal(true);
  1514. dlg.set_logo_icon_name("pepper");
  1515. dlg.program_name = "Crown Game Engine";
  1516. dlg.version = CROWN_VERSION;
  1517. dlg.website = "https://github.com/dbartolini/crown";
  1518. dlg.copyright = "Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.";
  1519. dlg.license = "Crown Game Engine.\n"
  1520. + "Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.\n"
  1521. + "\n"
  1522. + "This program is free software; you can redistribute it and/or\n"
  1523. + "modify it under the terms of the GNU General Public License\n"
  1524. + "as published by the Free Software Foundation; either version 2\n"
  1525. + "of the License, or (at your option) any later version.\n"
  1526. + "\n"
  1527. + "This program is distributed in the hope that it will be useful,\n"
  1528. + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1529. + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1530. + "GNU General Public License for more details.\n"
  1531. + "\n"
  1532. + "You should have received a copy of the GNU General Public License\n"
  1533. + "along with this program; if not, write to the Free Software\n"
  1534. + "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
  1535. ;
  1536. dlg.run();
  1537. dlg.destroy();
  1538. }
  1539. public void set_autosave_timer(uint minutes)
  1540. {
  1541. if (_save_timer_id > 0)
  1542. GLib.Source.remove(_save_timer_id);
  1543. _save_timer_id = GLib.Timeout.add_seconds(minutes*60, save_timeout);
  1544. }
  1545. }
  1546. // Global paths
  1547. public static GLib.File _config_dir;
  1548. public static GLib.File _logs_dir;
  1549. public static GLib.File _log_file;
  1550. public static GLib.File _settings_file;
  1551. public static GLib.FileStream _log_stream;
  1552. public static ConsoleView _console_view;
  1553. public static void log(string system, string severity, string message)
  1554. {
  1555. GLib.DateTime now = new GLib.DateTime.now_utc();
  1556. string line = "%s.%06d %.4s %s: %s\n".printf(now.format("%H:%M:%S")
  1557. , now.get_microsecond()
  1558. , severity.ascii_up()
  1559. , system
  1560. , message
  1561. );
  1562. if (_log_stream != null)
  1563. {
  1564. _log_stream.puts(line);
  1565. _log_stream.flush();
  1566. }
  1567. if (_console_view != null)
  1568. _console_view.log(severity, line);
  1569. }
  1570. public static void logi(string message)
  1571. {
  1572. log("editor", "info", message);
  1573. }
  1574. public static void logw(string message)
  1575. {
  1576. log("editor", "warning", message);
  1577. }
  1578. public static void loge(string message)
  1579. {
  1580. log("editor", "error", message);
  1581. }
  1582. public void open_directory(string directory)
  1583. {
  1584. #if CROWN_PLATFORM_LINUX
  1585. try
  1586. {
  1587. GLib.AppInfo.launch_default_for_uri("file://" + directory, null);
  1588. }
  1589. catch (Error e)
  1590. {
  1591. loge(e.message);
  1592. }
  1593. #else
  1594. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  1595. try
  1596. {
  1597. sl.spawnv({ "explorer.exe", directory, null });
  1598. }
  1599. catch (Error e)
  1600. {
  1601. loge(e.message);
  1602. }
  1603. #endif
  1604. }
  1605. public static GLib.SubprocessFlags subprocess_flags()
  1606. {
  1607. GLib.SubprocessFlags flags = SubprocessFlags.NONE;
  1608. #if !CROWN_DEBUG
  1609. flags |= SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE;
  1610. #endif
  1611. return flags;
  1612. }
  1613. public static int main(string[] args)
  1614. {
  1615. Intl.setlocale(LocaleCategory.ALL, "C");
  1616. // Global paths
  1617. _config_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_config_dir(), "crown"));
  1618. try { _config_dir.make_directory(); } catch (Error e) { /* Nobody cares */ }
  1619. _logs_dir = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "logs"));
  1620. try { _logs_dir.make_directory(); } catch (Error e) { /* Nobody cares */ }
  1621. _log_file = GLib.File.new_for_path(GLib.Path.build_filename(_logs_dir.get_path(), new GLib.DateTime.now_utc().format("%Y-%m-%d") + ".log"));
  1622. _settings_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "settings.sjson"));
  1623. _log_stream = GLib.FileStream.open(_log_file.get_path(), "a");
  1624. LevelEditorApplication app = new LevelEditorApplication();
  1625. return app.run(args);
  1626. }
  1627. }