level_editor.vala 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  1. /*
  2. * Copyright (c) 2012-2021 Daniele Bartolini et al.
  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. const string LEVEL_EDITOR_WINDOW_TITLE = "Crown Editor";
  13. const string CROWN_ICON_NAME = "crown";
  14. public enum Theme
  15. {
  16. DARK,
  17. LIGHT,
  18. COUNT
  19. }
  20. public class LevelEditorWindow : Gtk.ApplicationWindow
  21. {
  22. private const GLib.ActionEntry[] action_entries =
  23. {
  24. { "fullscreen", on_fullscreen, null, null }
  25. };
  26. public bool _fullscreen;
  27. public LevelEditorWindow(Gtk.Application app)
  28. {
  29. Object(application: app);
  30. this.add_action_entries(action_entries, this);
  31. this.title = LEVEL_EDITOR_WINDOW_TITLE;
  32. this.key_press_event.connect(this.on_key_press);
  33. this.key_release_event.connect(this.on_key_release);
  34. this.window_state_event.connect(this.on_window_state_event);
  35. this.delete_event.connect(this.on_delete_event);
  36. this.focus_out_event.connect(this.on_focus_out);
  37. _fullscreen = false;
  38. }
  39. private void on_fullscreen(GLib.SimpleAction action, GLib.Variant? param)
  40. {
  41. if (_fullscreen)
  42. unfullscreen();
  43. else
  44. fullscreen();
  45. }
  46. private bool on_key_press(Gdk.EventKey ev)
  47. {
  48. LevelEditorApplication app = (LevelEditorApplication)application;
  49. string str = "";
  50. if (ev.keyval == Gdk.Key.Control_L)
  51. str += LevelEditorApi.key_down("ctrl_left");
  52. else if (ev.keyval == Gdk.Key.Shift_L)
  53. str += LevelEditorApi.key_down("shift_left");
  54. else if (ev.keyval == Gdk.Key.Alt_L)
  55. str += LevelEditorApi.key_down("alt_left");
  56. if (str.length != 0)
  57. {
  58. app._editor.send_script(str);
  59. app._editor.send(DeviceApi.frame());
  60. }
  61. return Gdk.EVENT_PROPAGATE;
  62. }
  63. private bool on_key_release(Gdk.EventKey ev)
  64. {
  65. LevelEditorApplication app = (LevelEditorApplication)application;
  66. string str = "";
  67. if (ev.keyval == Gdk.Key.Control_L)
  68. str += LevelEditorApi.key_up("ctrl_left");
  69. else if (ev.keyval == Gdk.Key.Shift_L)
  70. str += LevelEditorApi.key_up("shift_left");
  71. else if (ev.keyval == Gdk.Key.Alt_L)
  72. str += LevelEditorApi.key_up("alt_left");
  73. if (str.length != 0)
  74. {
  75. app._editor.send_script(str);
  76. app._editor.send(DeviceApi.frame());
  77. }
  78. return Gdk.EVENT_PROPAGATE;
  79. }
  80. private bool on_window_state_event(Gdk.EventWindowState ev)
  81. {
  82. _fullscreen = (ev.new_window_state & Gdk.WindowState.FULLSCREEN) != 0;
  83. return Gdk.EVENT_STOP;
  84. }
  85. private bool on_delete_event()
  86. {
  87. LevelEditorApplication app = (LevelEditorApplication)application;
  88. if (app.should_quit())
  89. app.stop_backend_and_quit();
  90. return Gdk.EVENT_STOP; // Keep window alive.
  91. }
  92. private bool on_focus_out(Gdk.EventFocus ev)
  93. {
  94. LevelEditorApplication app = (LevelEditorApplication)application;
  95. app._editor.send_script(LevelEditorApi.key_up("ctrl_left"));
  96. app._editor.send_script(LevelEditorApi.key_up("shift_left"));
  97. app._editor.send_script(LevelEditorApi.key_up("alt_left"));
  98. return Gdk.EVENT_PROPAGATE;
  99. }
  100. }
  101. public enum StartGame
  102. {
  103. NORMAL,
  104. TEST
  105. }
  106. public class LevelEditorApplication : Gtk.Application
  107. {
  108. // Constants
  109. private const GLib.ActionEntry[] action_entries_file =
  110. {
  111. // parameter type
  112. // name activate() | state
  113. // | | | |
  114. { "menu-file", null, null, null },
  115. { "new-level", on_new_level, null, null },
  116. { "open-level", on_open_level, "s", null },
  117. { "new-project", on_new_project, null, null },
  118. { "open-project", on_open_project, null, null },
  119. { "save", on_save, null, null },
  120. { "save-as", on_save_as, null, null },
  121. { "import", on_import, "s", null },
  122. { "preferences", on_preferences, null, null },
  123. { "deploy", on_deploy, null, null },
  124. { "close", on_close, null, null },
  125. { "quit", on_quit, null, null },
  126. { "open-resource", on_open_resource, "s", null }
  127. };
  128. private const GLib.ActionEntry[] action_entries_edit =
  129. {
  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. };
  147. private const GLib.ActionEntry[] action_entries_create =
  148. {
  149. { "menu-create", null, null, null },
  150. { "menu-primitives", null, null, null },
  151. { "primitive-cube", on_create_primitive, null, null },
  152. { "primitive-sphere", on_create_primitive, null, null },
  153. { "primitive-cone", on_create_primitive, null, null },
  154. { "primitive-cylinder", on_create_primitive, null, null },
  155. { "primitive-plane", on_create_primitive, null, null },
  156. { "camera", on_create_primitive, null, null },
  157. { "light", on_create_primitive, null, null },
  158. { "sound-source", on_create_primitive, null, null },
  159. { "unit-empty", on_create_unit, null, null }
  160. };
  161. private const GLib.ActionEntry[] action_entries_camera =
  162. {
  163. { "menu-camera", null, null, null },
  164. { "camera-view", on_camera_view, "s", "'perspective'" }
  165. };
  166. private const GLib.ActionEntry[] action_entries_view =
  167. {
  168. { "menu-view", null, null, null },
  169. { "resource-chooser", on_resource_chooser, null, null },
  170. { "project-browser", on_project_browser, null, null },
  171. { "console", on_console, null, null },
  172. { "statusbar", on_statusbar, null, null },
  173. { "inspector", on_inspector, null, null },
  174. { "debug-render-world", on_debug_render_world, null, "false" },
  175. { "debug-physics-world", on_debug_physics_world, null, "false" }
  176. };
  177. private const GLib.ActionEntry[] action_entries_debug =
  178. {
  179. { "menu-debug", null, null, null },
  180. { "test-level", on_run_game, null, null },
  181. { "run-game", on_run_game, null, null },
  182. { "build-data", on_build_data, null, null },
  183. { "reload-lua", on_refresh_lua, null, null },
  184. { "restart-editor-view", on_restart_editor_view, null, null }
  185. };
  186. private const GLib.ActionEntry[] action_entries_help =
  187. {
  188. { "menu-help", null, null, null },
  189. { "manual", on_manual, null, null },
  190. { "report-issue", on_report_issue, null, null },
  191. { "browse-logs", on_browse_logs, null, null },
  192. { "changelog", on_changelog, null, null },
  193. { "about", on_about, null, null }
  194. };
  195. private const GLib.ActionEntry[] action_entries_project =
  196. {
  197. { "delete-file", on_delete_file, "s", null }
  198. };
  199. // Command line options
  200. private string? _source_dir = null;
  201. private string _level_resource = "";
  202. private User _user;
  203. private Hashtable _settings;
  204. // Editor state
  205. private double _grid_size;
  206. private double _rotation_snap;
  207. private bool _show_grid;
  208. private bool _snap_to_grid;
  209. private bool _debug_render_world;
  210. private bool _debug_physics_world;
  211. private LevelEditorApi.ToolType _tool_type;
  212. private LevelEditorApi.ToolType _tool_type_prev;
  213. private LevelEditorApi.SnapMode _snap_mode;
  214. private LevelEditorApi.ReferenceSystem _reference_system;
  215. // Project state
  216. private string _placeable_type;
  217. private string _placeable_name;
  218. // Accelerators
  219. private string[] _tool_place_accels;
  220. private string[] _tool_move_accels;
  221. private string[] _tool_rotate_accels;
  222. private string[] _tool_scale_accels;
  223. private string[] _delete_accels;
  224. private string[] _camera_view_perspective_accels;
  225. private string[] _camera_view_front_accels;
  226. private string[] _camera_view_back_accels;
  227. private string[] _camera_view_right_accels;
  228. private string[] _camera_view_left_accels;
  229. private string[] _camera_view_top_accels;
  230. private string[] _camera_view_bottom_accels;
  231. // Engine connections
  232. private GLib.Subprocess _compiler_process;
  233. private GLib.Subprocess _editor_process;
  234. private GLib.Subprocess _game_process;
  235. private GLib.SourceFunc _stop_data_compiler_callback = null;
  236. private GLib.SourceFunc _stop_editor_callback = null;
  237. private GLib.SourceFunc _stop_game_callback = null;
  238. private ConsoleClient _compiler;
  239. public ConsoleClient _editor;
  240. private ConsoleClient _game;
  241. // Level data
  242. private Database _database;
  243. private Project _project;
  244. private ProjectStore _project_store;
  245. private Level _level;
  246. private DataCompiler _data_compiler;
  247. // Widgets
  248. private Gtk.CssProvider _css_provider;
  249. private ProjectBrowser _project_browser;
  250. private EditorView _editor_view;
  251. private LevelTreeView _level_treeview;
  252. private LevelLayersTreeView _level_layers_treeview;
  253. private PropertiesView _properties_view;
  254. private PreferencesDialog _preferences_dialog;
  255. private ResourceChooser _resource_chooser;
  256. private Gtk.Popover _resource_popover;
  257. private Gtk.Overlay _editor_view_overlay;
  258. private Gtk.Stack _project_stack;
  259. private Gtk.Label _project_stack_compiling_data_label;
  260. private Gtk.Label _project_stack_connecting_to_data_compiler_label;
  261. private Gtk.Label _project_stack_compiler_crashed_label;
  262. private Gtk.Label _project_stack_compiler_failed_compilation_label;
  263. private Gtk.Stack _editor_stack;
  264. private Gtk.Label _editor_stack_compiling_data_label;
  265. private Gtk.Label _editor_stack_connecting_to_data_compiler_label;
  266. private Gtk.Label _editor_stack_compiler_crashed_label;
  267. private Gtk.Label _editor_stack_compiler_failed_compilation_label;
  268. private Gtk.Label _editor_stack_disconnected_label;
  269. private Gtk.Label _editor_stack_oops_label;
  270. private Gtk.Stack _inspector_stack;
  271. private Gtk.Label _inspector_stack_compiling_data_label;
  272. private Gtk.Label _inspector_stack_connecting_to_data_compiler_label;
  273. private Gtk.Label _inspector_stack_compiler_crashed_label;
  274. private Gtk.Label _inspector_stack_compiler_failed_compilation_label;
  275. private Gtk.Toolbar _toolbar;
  276. private Gtk.ToolButton _toolbar_run;
  277. private Gtk.Notebook _level_tree_view_notebook;
  278. private Gtk.Paned _editor_pane;
  279. private Gtk.Paned _content_pane;
  280. private Gtk.Paned _inspector_pane;
  281. private Gtk.Paned _main_pane;
  282. private Statusbar _statusbar;
  283. private Gtk.Box _main_vbox;
  284. private Gtk.FileFilter _file_filter;
  285. private Gtk.ComboBoxText _combo;
  286. private PanelNewProject _panel_new_project;
  287. private PanelProjectsList _panel_projects_list;
  288. private PanelWelcome _panel_welcome;
  289. private Gtk.Stack _main_stack;
  290. private uint _save_timer_id;
  291. public LevelEditorApplication()
  292. {
  293. Object(application_id: "org.crown.level_editor"
  294. , flags: GLib.ApplicationFlags.FLAGS_NONE
  295. );
  296. }
  297. public Theme theme_name_to_enum(string theme)
  298. {
  299. if (theme == "dark")
  300. return Theme.DARK;
  301. else if (theme == "light")
  302. return Theme.LIGHT;
  303. else
  304. return Theme.COUNT;
  305. }
  306. public void set_theme_from_name(string theme_name)
  307. {
  308. Theme theme = theme_name_to_enum(theme_name);
  309. set_theme(theme);
  310. }
  311. public void set_theme(Theme theme)
  312. {
  313. if (theme == Theme.COUNT)
  314. return;
  315. string css = "/org/crown/level_editor/theme/Adwaita/gtk%s.css".printf(theme == Theme.DARK ? "-dark" : "");
  316. _css_provider.load_from_resource(css);
  317. }
  318. protected override void startup()
  319. {
  320. base.startup();
  321. Intl.setlocale(LocaleCategory.ALL, "C");
  322. _css_provider = new Gtk.CssProvider();
  323. var default_screen = Gdk.Display.get_default().get_default_screen();
  324. Gtk.StyleContext.add_provider_for_screen(default_screen
  325. , _css_provider
  326. , STYLE_PROVIDER_PRIORITY_APPLICATION
  327. );
  328. _settings = SJSON.load_from_path(_settings_file.get_path());
  329. // Set theme.
  330. set_theme(Theme.DARK);
  331. if (_settings.has_key("preferences"))
  332. {
  333. Hashtable preferences = (Hashtable)_settings["preferences"];
  334. if (preferences.has_key("theme"))
  335. set_theme_from_name((string)preferences["theme"]);
  336. }
  337. // HACK: register CrownClamp type within GObject's type system to
  338. // make GtkBuilder able to find it when creating the widget from
  339. // .ui files.
  340. // https://stackoverflow.com/questions/24235937/custom-gtk-widget-with-template-ui
  341. new Clamp().get_type().ensure();
  342. this.add_action_entries(action_entries_file, this);
  343. this.add_action_entries(action_entries_edit, this);
  344. this.add_action_entries(action_entries_create, this);
  345. this.add_action_entries(action_entries_camera, this);
  346. this.add_action_entries(action_entries_view, this);
  347. this.add_action_entries(action_entries_debug, this);
  348. this.add_action_entries(action_entries_help, this);
  349. this.add_action_entries(action_entries_project, this);
  350. _tool_place_accels = this.get_accels_for_action("app.tool::place");
  351. _tool_move_accels = this.get_accels_for_action("app.tool::move");
  352. _tool_rotate_accels = this.get_accels_for_action("app.tool::rotate");
  353. _tool_scale_accels = this.get_accels_for_action("app.tool::scale");
  354. _delete_accels = this.get_accels_for_action("app.delete");
  355. _camera_view_perspective_accels = this.get_accels_for_action("app.camera-view::perspective");
  356. _camera_view_front_accels = this.get_accels_for_action("app.camera-view::front");
  357. _camera_view_back_accels = this.get_accels_for_action("app.camera-view::back");
  358. _camera_view_right_accels = this.get_accels_for_action("app.camera-view::right");
  359. _camera_view_left_accels = this.get_accels_for_action("app.camera-view::left");
  360. _camera_view_top_accels = this.get_accels_for_action("app.camera-view::top");
  361. _camera_view_bottom_accels = this.get_accels_for_action("app.camera-view::bottom");
  362. _compiler = new ConsoleClient();
  363. _compiler.connected.connect(on_data_compiler_connected);
  364. _compiler.message_received.connect(on_message_received);
  365. _data_compiler = new DataCompiler(_compiler);
  366. _database = new Database();
  367. _database.key_changed.connect(() => { update_active_window_title(); });
  368. _project = new Project(_database, _data_compiler);
  369. _project.set_toolchain_dir(_toolchain_dir.get_path());
  370. _project.register_importer("Sprite", { "png" }, SpriteResource.import, 0.0);
  371. _project.register_importer("Mesh", { "mesh" }, MeshResource.import, 1.0);
  372. _project.register_importer("Sound", { "wav" }, SoundResource.import, 2.0);
  373. _project.register_importer("Texture", { "png", "tga", "dds", "ktx", "pvr" }, TextureResource.import, 2.0);
  374. _editor = new ConsoleClient();
  375. _editor.connected.connect(on_editor_connected);
  376. _editor.message_received.connect(on_message_received);
  377. _game = new ConsoleClient();
  378. _game.connected.connect(on_game_connected);
  379. _game.message_received.connect(on_message_received);
  380. _level = new Level(_database, _editor, _project);
  381. // Editor state
  382. _grid_size = 1.0;
  383. _rotation_snap = 15.0;
  384. _show_grid = true;
  385. _snap_to_grid = true;
  386. _debug_render_world = false;
  387. _debug_physics_world = false;
  388. _tool_type = LevelEditorApi.ToolType.MOVE;
  389. _tool_type_prev = _tool_type;
  390. _snap_mode = LevelEditorApi.SnapMode.RELATIVE;
  391. _reference_system = LevelEditorApi.ReferenceSystem.LOCAL;
  392. // Project state
  393. _placeable_type = "";
  394. _placeable_name = "";
  395. // Engine connections
  396. _compiler_process = null;
  397. _editor_process = null;
  398. _game_process = null;
  399. _project_store = new ProjectStore(_project);
  400. // Widgets
  401. _preferences_dialog = new PreferencesDialog(this);
  402. _preferences_dialog.delete_event.connect(() => { _preferences_dialog.hide(); return Gdk.EVENT_STOP; });
  403. _combo = new Gtk.ComboBoxText();
  404. _combo.append("editor", "Editor");
  405. _combo.append("game", "Game");
  406. _combo.set_active_id("editor");
  407. _console_view = new ConsoleView(_project, _combo, _preferences_dialog);
  408. _project_browser = new ProjectBrowser(this, _project, _project_store);
  409. _level_treeview = new LevelTreeView(_database, _level);
  410. _level_layers_treeview = new LevelLayersTreeView(_database, _level);
  411. _properties_view = new PropertiesView(_level, _project_store);
  412. _project_stack = new Gtk.Stack();
  413. _project_stack.add(_project_browser);
  414. _project_stack_compiling_data_label = compiling_data_label();
  415. _project_stack.add(_project_stack_compiling_data_label);
  416. _project_stack_connecting_to_data_compiler_label = connecting_to_data_compiler_label();
  417. _project_stack.add(_project_stack_connecting_to_data_compiler_label);
  418. _project_stack_compiler_crashed_label = compiler_crashed_label();
  419. _project_stack.add(_project_stack_compiler_crashed_label);
  420. _project_stack_compiler_failed_compilation_label = compiler_failed_compilation_label();
  421. _project_stack.add(_project_stack_compiler_failed_compilation_label);
  422. _editor_stack = new Gtk.Stack();
  423. _editor_stack_compiling_data_label = compiling_data_label();
  424. _editor_stack.add(_editor_stack_compiling_data_label);
  425. _editor_stack_connecting_to_data_compiler_label = connecting_to_data_compiler_label();
  426. _editor_stack.add(_editor_stack_connecting_to_data_compiler_label);
  427. _editor_stack_compiler_crashed_label = compiler_crashed_label();
  428. _editor_stack.add(_editor_stack_compiler_crashed_label);
  429. _editor_stack_compiler_failed_compilation_label = compiler_failed_compilation_label();
  430. _editor_stack.add(_editor_stack_compiler_failed_compilation_label);
  431. _editor_stack_disconnected_label = new Gtk.Label("Disconnected.");
  432. _editor_stack.add(_editor_stack_disconnected_label);
  433. _editor_stack_oops_label = new Gtk.Label(null);
  434. _editor_stack_oops_label.set_markup("Something went wrong.\rTry to <a href=\"restart\">restart</a> this view.");
  435. _editor_stack_oops_label.activate_link.connect(() => {
  436. activate_action("restart-editor-view", null);
  437. return true;
  438. });
  439. _editor_stack.add(_editor_stack_oops_label);
  440. _inspector_stack = new Gtk.Stack();
  441. _inspector_stack_compiling_data_label = compiling_data_label();
  442. _inspector_stack.add(_inspector_stack_compiling_data_label);
  443. _inspector_stack_connecting_to_data_compiler_label = connecting_to_data_compiler_label();
  444. _inspector_stack.add(_inspector_stack_connecting_to_data_compiler_label);
  445. _inspector_stack_compiler_crashed_label = compiler_crashed_label();
  446. _inspector_stack.add(_inspector_stack_compiler_crashed_label);
  447. _inspector_stack_compiler_failed_compilation_label = compiler_failed_compilation_label();
  448. _inspector_stack.add(_inspector_stack_compiler_failed_compilation_label);
  449. Gtk.Builder builder = new Gtk.Builder.from_resource("/org/crown/level_editor/ui/toolbar.ui");
  450. _toolbar = builder.get_object("toolbar") as Gtk.Toolbar;
  451. _toolbar_run = builder.get_object("run") as Gtk.ToolButton;
  452. _editor_view_overlay = new Gtk.Overlay();
  453. _editor_view_overlay.add_overlay(_toolbar);
  454. _resource_popover = new Gtk.Popover(_toolbar);
  455. _resource_popover.key_press_event.connect((ev) => {
  456. if (ev.keyval == Gdk.Key.Escape)
  457. {
  458. // Do not transition-animate (i.e. call hide() instead of popdown()).
  459. _resource_popover.hide();
  460. return Gdk.EVENT_STOP;
  461. }
  462. return Gdk.EVENT_PROPAGATE;
  463. });
  464. _resource_popover.button_press_event.connect((ev) => {
  465. // Do not transition-animate (i.e. call hide() instead of popdown()).
  466. // See: https://gitlab.gnome.org/GNOME/gtk/-/blob/3.22.30/gtk/gtkpopover.c
  467. Gtk.Widget child = _resource_popover.get_child();
  468. Gtk.Widget event_widget = Gtk.get_event_widget(ev);
  469. if (child != null && ev.window == event_widget.get_window())
  470. {
  471. Gtk.Allocation child_alloc;
  472. child.get_allocation(out child_alloc);
  473. if ((int)ev.x < child_alloc.x
  474. || (int)ev.x > child_alloc.x + child_alloc.width
  475. || (int)ev.y < child_alloc.y
  476. || (int)ev.y > child_alloc.y + child_alloc.height
  477. )
  478. {
  479. _resource_popover.hide();
  480. }
  481. }
  482. else if (!event_widget.is_ancestor(_resource_popover))
  483. {
  484. _resource_popover.hide();
  485. }
  486. return Gdk.EVENT_PROPAGATE;
  487. });
  488. _resource_popover.events |= Gdk.EventMask.STRUCTURE_MASK; // unmap_event
  489. _resource_popover.unmap_event.connect(() => {
  490. // Redraw the editor view when the popover is not on-screen anymore.
  491. device_frame_delayed(16, _editor);
  492. return Gdk.EVENT_PROPAGATE;
  493. });
  494. _resource_popover.delete_event.connect(() => {
  495. // Do not destroy the widget.
  496. _resource_popover.hide();
  497. return Gdk.EVENT_STOP;
  498. });
  499. _resource_popover.modal = true;
  500. _resource_chooser = new ResourceChooser(_project, _project_store, true);
  501. _resource_chooser.resource_selected.connect(on_resource_browser_resource_selected);
  502. _resource_popover.add(_resource_chooser);
  503. _level_tree_view_notebook = new Notebook();
  504. _level_tree_view_notebook.show_border = false;
  505. _level_tree_view_notebook.append_page(_level_treeview, new Gtk.Image.from_icon_name("level-tree", IconSize.SMALL_TOOLBAR));
  506. _level_tree_view_notebook.append_page(_level_layers_treeview, new Gtk.Image.from_icon_name("level-layers", IconSize.SMALL_TOOLBAR));
  507. _editor_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  508. _editor_pane.pack1(_project_stack, false, false);
  509. _editor_pane.pack2(_editor_stack, true, false);
  510. _content_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  511. _content_pane.pack1(_editor_pane, true, false);
  512. _content_pane.pack2(_console_view, false, false);
  513. _inspector_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  514. _inspector_pane.pack1(_level_tree_view_notebook, true, false);
  515. _inspector_pane.pack2(_properties_view, false, false);
  516. _inspector_stack.add(_inspector_pane);
  517. _main_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  518. _main_pane.pack1(_content_pane, true, false);
  519. _main_pane.pack2(_inspector_stack, false, false);
  520. _statusbar = new Statusbar();
  521. _main_vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  522. _main_vbox.pack_start(_main_pane, true, true, 0);
  523. _main_vbox.pack_start(_statusbar, false, false, 0);
  524. _main_vbox.set_visible(true);
  525. _file_filter = new Gtk.FileFilter();
  526. _file_filter.set_filter_name("Level (*.level)");
  527. _file_filter.add_pattern("*.level");
  528. _user = new User();
  529. _panel_new_project = new PanelNewProject(this, _user, _project);
  530. _panel_new_project.fill_templates_list(_templates_dir.get_path());
  531. _panel_welcome = new PanelWelcome();
  532. _panel_projects_list = new PanelProjectsList(this, _user);
  533. _panel_welcome.pack_start(_panel_projects_list);
  534. _panel_welcome.set_visible(true); // To make Gtk.Stack work...
  535. _main_stack = new Gtk.Stack();
  536. _main_stack.add_named(_panel_welcome, "panel_welcome");
  537. _main_stack.add_named(_panel_new_project, "panel_new_project");
  538. _main_stack.add_named(_main_vbox, "main_vbox");
  539. _preferences_dialog.decode(_settings);
  540. // Delete expired logs
  541. if (_preferences_dialog._log_delete_after_days.value != 0)
  542. {
  543. try
  544. {
  545. FileEnumerator enumerator = _logs_dir.enumerate_children("standard::*"
  546. , FileQueryInfoFlags.NOFOLLOW_SYMLINKS
  547. );
  548. GLib.FileInfo info = null;
  549. while ((info = enumerator.next_file()) != null)
  550. {
  551. if (info.get_file_type() != GLib.FileType.REGULAR)
  552. continue; // Skip anything but regular files
  553. // Parse DateTime from log filename
  554. int year = 1970;
  555. int month = 1;
  556. int day = 1;
  557. if (info.get_name().scanf("%d-%d-%d.log", &year, &month, &day) != 3)
  558. continue; // Skip malformed filenames
  559. GLib.DateTime time_log = new GLib.DateTime.utc(year, month, day, 0, 0, 0.0);
  560. if (time_log == null)
  561. continue; // Skip invalid dates
  562. GLib.DateTime time_now = new GLib.DateTime.now_utc();
  563. if (time_now.difference(time_log) <= GLib.TimeSpan.DAY*_preferences_dialog._log_delete_after_days.value)
  564. continue; // Skip if date is within range
  565. // Delete
  566. GLib.File log_file = _logs_dir.resolve_relative_path(info.get_name());
  567. log_file.delete();
  568. }
  569. }
  570. catch (GLib.Error e)
  571. {
  572. loge(e.message);
  573. }
  574. }
  575. _user.load(_user_file.get_path());
  576. _console_view._entry_history.load(_console_history_file.get_path());
  577. if (_source_dir == null)
  578. {
  579. show_panel("panel_welcome");
  580. }
  581. else
  582. {
  583. show_panel("main_vbox");
  584. restart_backend.begin(_source_dir, _level_resource);
  585. }
  586. }
  587. protected override void activate()
  588. {
  589. if (this.active_window == null)
  590. {
  591. LevelEditorWindow win = new LevelEditorWindow(this);
  592. win.set_default_size(WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT);
  593. win.add(_main_stack);
  594. try
  595. {
  596. win.icon = IconTheme.get_default().load_icon(CROWN_ICON_NAME, 256, 0);
  597. }
  598. catch (Error e)
  599. {
  600. loge(e.message);
  601. }
  602. }
  603. this.active_window.show_all();
  604. this.active_window.maximize();
  605. }
  606. protected override bool local_command_line(ref unowned string[] args, out int exit_status)
  607. {
  608. if (args.length > 1)
  609. {
  610. if (!GLib.FileUtils.test(args[1], FileTest.EXISTS) || !GLib.FileUtils.test(args[1], FileTest.IS_DIR))
  611. {
  612. loge("Source directory does not exist or it is not a directory");
  613. exit_status = 1;
  614. return true;
  615. }
  616. _source_dir = args[1];
  617. }
  618. if (args.length > 2)
  619. {
  620. // Validation is done below after the Project object instantiation
  621. _level_resource = args[2];
  622. }
  623. exit_status = 0;
  624. return false;
  625. }
  626. protected override int command_line(ApplicationCommandLine command_line)
  627. {
  628. this.activate();
  629. return 0;
  630. }
  631. public ConsoleClient? current_selected_client()
  632. {
  633. if (_combo.get_active_id() == "editor")
  634. return _editor;
  635. else if (_combo.get_active_id() == "game")
  636. return _game;
  637. else
  638. return null;
  639. }
  640. private void on_resource_browser_resource_selected(string type, string name)
  641. {
  642. set_placeable(type, name);
  643. activate_action("tool", new GLib.Variant.string("place"));
  644. }
  645. private void on_data_compiler_connected(string address, int port)
  646. {
  647. logi("Connected to data_compiler@%s:%d".printf(address, port));
  648. _compiler.receive_async();
  649. }
  650. private void on_data_compiler_disconnected()
  651. {
  652. logi("Disconnected from data_compiler");
  653. if (_stop_data_compiler_callback != null)
  654. _stop_data_compiler_callback();
  655. }
  656. private async void on_data_compiler_disconnected_unexpected()
  657. {
  658. logw("Disconnected from data_compiler unexpectedly");
  659. int exit_status;
  660. wait_process(out exit_status, _compiler_process);
  661. _compiler_process = null;
  662. yield stop_heads();
  663. // Reset the callback
  664. _data_compiler.finished(false);
  665. _project_stack.set_visible_child(_project_stack_compiler_crashed_label);
  666. _editor_stack.set_visible_child(_editor_stack_compiler_crashed_label);
  667. _inspector_stack.set_visible_child(_inspector_stack_compiler_crashed_label);
  668. }
  669. private void on_editor_connected(string address, int port)
  670. {
  671. logi("Connected to level_editor@%s:%d".printf(address, port));
  672. // Start receiving data from the editor view.
  673. _editor.receive_async();
  674. // Update editor view with current editor state.
  675. _level.send_level();
  676. send_state();
  677. _preferences_dialog.apply();
  678. }
  679. private void on_editor_disconnected()
  680. {
  681. logi("Disconnected from editor");
  682. if (_stop_editor_callback != null)
  683. _stop_editor_callback();
  684. }
  685. private void on_editor_disconnected_unexpected()
  686. {
  687. logw("Disconnected from editor unexpectedly");
  688. int exit_status;
  689. wait_process(out exit_status, _editor_process);
  690. _editor_process = null;
  691. _editor_stack.set_visible_child(_editor_stack_oops_label);
  692. }
  693. private void on_game_connected(string address, int port)
  694. {
  695. logi("Connected to game@%s:%d".printf(address, port));
  696. _game.receive_async();
  697. _combo.set_active_id("game");
  698. }
  699. private void on_game_disconnected()
  700. {
  701. logi("Disconnected from game");
  702. _project.delete_garbage();
  703. _combo.set_active_id("editor");
  704. _toolbar_run.icon_name = "game-run";
  705. if (_stop_game_callback != null)
  706. _stop_game_callback();
  707. }
  708. private void on_game_disconnected_externally()
  709. {
  710. on_game_disconnected();
  711. int exit_status;
  712. wait_process(out exit_status, _game_process);
  713. _game_process = null;
  714. }
  715. private void on_message_received(ConsoleClient client, uint8[] json)
  716. {
  717. Hashtable msg = JSON.decode(json) as Hashtable;
  718. string msg_type = msg["type"] as string;
  719. if (msg_type == "message")
  720. {
  721. log((string)msg["system"], (string)msg["severity"], (string)msg["message"]);
  722. }
  723. else if (msg_type == "add_file")
  724. {
  725. string path = (string)msg["path"];
  726. _project.add_file(path);
  727. }
  728. else if (msg_type == "remove_file")
  729. {
  730. string path = (string)msg["path"];
  731. _project.remove_file(path);
  732. }
  733. else if (msg_type == "add_tree")
  734. {
  735. string path = (string)msg["path"];
  736. _project.add_tree(path);
  737. }
  738. else if (msg_type == "remove_tree")
  739. {
  740. string path = (string)msg["path"];
  741. _project.remove_tree(path);
  742. }
  743. else if (msg_type == "compile")
  744. {
  745. // Guid id = Guid.parse((string)msg["id"]);
  746. if (msg.has_key("start"))
  747. {
  748. // FIXME
  749. }
  750. else if (msg.has_key("success"))
  751. {
  752. _data_compiler.finished((bool)msg["success"]);
  753. }
  754. }
  755. else if (msg_type == "unit_spawned")
  756. {
  757. string id = (string) msg["id"];
  758. string name = (string) msg["name"];
  759. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  760. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  761. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  762. _level.on_unit_spawned(Guid.parse(id)
  763. , name
  764. , Vector3.from_array(pos)
  765. , Quaternion.from_array(rot)
  766. , Vector3.from_array(scl)
  767. );
  768. }
  769. else if (msg_type == "sound_spawned")
  770. {
  771. string id = (string) msg["id"];
  772. string name = (string) msg["name"];
  773. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  774. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  775. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  776. double range = (double) msg["range"];
  777. double volume = (double) msg["volume"];
  778. bool loop = (bool) msg["loop"];
  779. _level.on_sound_spawned(Guid.parse(id)
  780. , name
  781. , Vector3.from_array(pos)
  782. , Quaternion.from_array(rot)
  783. , Vector3.from_array(scl)
  784. , range
  785. , volume
  786. , loop
  787. );
  788. }
  789. else if (msg_type == "move_objects")
  790. {
  791. Hashtable ids = (Hashtable)msg["ids"];
  792. Hashtable new_positions = (Hashtable)msg["new_positions"];
  793. Hashtable new_rotations = (Hashtable)msg["new_rotations"];
  794. Hashtable new_scales = (Hashtable)msg["new_scales"];
  795. ArrayList<string> keys = new ArrayList<string>.wrap(ids.keys.to_array());
  796. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  797. Guid[] n_ids = new Guid[keys.size];
  798. Vector3[] n_positions = new Vector3[keys.size];
  799. Quaternion[] n_rotations = new Quaternion[keys.size];
  800. Vector3[] n_scales = new Vector3[keys.size];
  801. for (int i = 0; i < keys.size; ++i)
  802. {
  803. string k = keys[i];
  804. n_ids[i] = Guid.parse((string)ids[k]);
  805. n_positions[i] = Vector3.from_array((ArrayList<Value?>)(new_positions[k]));
  806. n_rotations[i] = Quaternion.from_array((ArrayList<Value?>)new_rotations[k]);
  807. n_scales[i] = Vector3.from_array((ArrayList<Value?>)new_scales[k]);
  808. }
  809. _level.on_move_objects(n_ids, n_positions, n_rotations, n_scales);
  810. }
  811. else if (msg_type == "selection")
  812. {
  813. Hashtable objects = (Hashtable)msg["objects"];
  814. ArrayList<string> keys = new ArrayList<string>.wrap(objects.keys.to_array());
  815. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  816. Guid[] ids = new Guid[keys.size];
  817. for (int i = 0; i < keys.size; ++i)
  818. {
  819. string k = keys[i];
  820. ids[i] = Guid.parse((string)objects[k]);
  821. }
  822. _level.on_selection(ids);
  823. }
  824. else if (msg_type == "error")
  825. {
  826. loge((string)msg["message"]);
  827. }
  828. else
  829. {
  830. loge("Unknown message type: " + msg_type);
  831. }
  832. // Receive next message
  833. client.receive_async();
  834. }
  835. private void append_editor_state(StringBuilder sb)
  836. {
  837. // This state is common to any project.
  838. sb.append(LevelEditorApi.set_grid_size(_grid_size));
  839. sb.append(LevelEditorApi.set_rotation_snap(_rotation_snap));
  840. sb.append(LevelEditorApi.enable_show_grid(_show_grid));
  841. sb.append(LevelEditorApi.enable_snap_to_grid(_snap_to_grid));
  842. sb.append(LevelEditorApi.enable_debug_render_world(_debug_render_world));
  843. sb.append(LevelEditorApi.enable_debug_physics_world(_debug_physics_world));
  844. sb.append(LevelEditorApi.set_tool_type(_tool_type));
  845. sb.append(LevelEditorApi.set_snap_mode(_snap_mode));
  846. sb.append(LevelEditorApi.set_reference_system(_reference_system));
  847. }
  848. private void append_project_state(StringBuilder sb)
  849. {
  850. // This state is not guaranteed to be applicable to any project.
  851. if (_placeable_type != "")
  852. sb.append(LevelEditorApi.set_placeable(_placeable_type, _placeable_name));
  853. }
  854. private void send_state()
  855. {
  856. StringBuilder sb = new StringBuilder();
  857. append_editor_state(sb);
  858. append_project_state(sb);
  859. _editor.send_script(sb.str);
  860. _editor.send(DeviceApi.frame());
  861. }
  862. private bool on_button_press(Gdk.EventButton ev)
  863. {
  864. return Gdk.EVENT_STOP;
  865. }
  866. private bool on_button_release(Gdk.EventButton ev)
  867. {
  868. return Gdk.EVENT_STOP;
  869. }
  870. Gtk.Label compiling_data_label()
  871. {
  872. return new Gtk.Label("Compiling resources, please wait...");
  873. }
  874. Gtk.Label connecting_to_data_compiler_label()
  875. {
  876. return new Gtk.Label("Connecting to Data Compiler...");
  877. }
  878. Gtk.Label compiler_crashed_label()
  879. {
  880. Gtk.Label label = new Gtk.Label(null);
  881. label.set_markup("Data Compiler disconnected.\rTry to <a href=\"restart\">restart</a> compiler to continue.");
  882. label.activate_link.connect(() => {
  883. restart_backend.begin(_project.source_dir(), _level._name != null ? _level._name : "");
  884. return true;
  885. });
  886. return label;
  887. }
  888. Gtk.Label compiler_failed_compilation_label()
  889. {
  890. Gtk.Label label = new Gtk.Label(null);
  891. label.set_markup("Data compilation failed.\rFix errors and <a href=\"restart\">restart</a> compiler to continue.");
  892. label.activate_link.connect(() => {
  893. restart_backend.begin(_project.source_dir(), _level._name != null ? _level._name : "");
  894. return true;
  895. });
  896. return label;
  897. }
  898. public async void restart_backend(string source_dir, string level_name)
  899. {
  900. string sd = source_dir.dup();
  901. string ln = level_name.dup();
  902. yield stop_backend();
  903. // Reset project state.
  904. _placeable_type = "";
  905. _placeable_name = "";
  906. // Load project and level if any.
  907. logi("Loading project: `%s`...".printf(sd));
  908. _project.load(sd);
  909. // Spawn the data compiler.
  910. string args[] =
  911. {
  912. ENGINE_EXE
  913. , "--source-dir"
  914. , _project.source_dir()
  915. , "--data-dir"
  916. , _project.data_dir()
  917. , "--map-source-dir"
  918. , "core"
  919. , _project.toolchain_dir()
  920. , "--server"
  921. , "--wait-console"
  922. , null
  923. };
  924. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  925. sl.set_cwd(ENGINE_DIR);
  926. try
  927. {
  928. _compiler_process = sl.spawnv(args);
  929. }
  930. catch (Error e)
  931. {
  932. loge(e.message);
  933. }
  934. // It is an error if the data compiler. disconnects after here.
  935. _compiler.disconnected.disconnect(on_data_compiler_disconnected);
  936. _compiler.disconnected.connect(on_data_compiler_disconnected_unexpected);
  937. _project_stack.set_visible_child(_project_stack_connecting_to_data_compiler_label);
  938. _editor_stack.set_visible_child(_editor_stack_connecting_to_data_compiler_label);
  939. _inspector_stack.set_visible_child(_inspector_stack_connecting_to_data_compiler_label);
  940. int tries = yield _compiler.connect_async(DATA_COMPILER_ADDRESS
  941. , DATA_COMPILER_TCP_PORT
  942. , DATA_COMPILER_CONNECTION_TRIES
  943. , DATA_COMPILER_CONNECTION_INTERVAL
  944. );
  945. if (tries == DATA_COMPILER_CONNECTION_TRIES)
  946. {
  947. loge("Cannot connect to data_compiler");
  948. return;
  949. }
  950. _project_stack.set_visible_child(_project_stack_compiling_data_label);
  951. _editor_stack.set_visible_child(_editor_stack_compiling_data_label);
  952. _inspector_stack.set_visible_child(_inspector_stack_compiling_data_label);
  953. // Compile data.
  954. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  955. if (_data_compiler.compile.end(res))
  956. {
  957. load_level(ln);
  958. // If successful, start the level editor.
  959. restart_editor.begin((obj, res) => {
  960. restart_editor.end(res);
  961. _project_stack.set_visible_child(_project_browser);
  962. _inspector_stack.set_visible_child(_inspector_pane);
  963. });
  964. }
  965. else
  966. {
  967. _project_stack.set_visible_child(_project_stack_compiler_failed_compilation_label);
  968. _editor_stack.set_visible_child(_editor_stack_compiler_failed_compilation_label);
  969. _inspector_stack.set_visible_child(_inspector_stack_compiler_failed_compilation_label);
  970. }
  971. });
  972. }
  973. public async void stop_heads()
  974. {
  975. yield stop_game();
  976. yield stop_editor();
  977. }
  978. public async void stop_backend()
  979. {
  980. yield stop_heads();
  981. yield stop_data_compiler();
  982. _level.reset();
  983. _project.reset();
  984. this.active_window.title = LEVEL_EDITOR_WINDOW_TITLE;
  985. }
  986. private async void stop_data_compiler()
  987. {
  988. if (_compiler != null)
  989. {
  990. // Reset "disconnected" signal.
  991. _compiler.disconnected.disconnect(on_data_compiler_disconnected);
  992. _compiler.disconnected.disconnect(on_data_compiler_disconnected_unexpected);
  993. // Explicit call to this function should not produce error messages.
  994. _compiler.disconnected.connect(on_data_compiler_disconnected);
  995. if (_compiler.is_connected())
  996. {
  997. _stop_data_compiler_callback = stop_data_compiler.callback;
  998. _compiler.send(DataCompilerApi.quit());
  999. yield; // Wait for ConsoleClient to disconnect.
  1000. _stop_data_compiler_callback = null;
  1001. }
  1002. }
  1003. int exit_status;
  1004. wait_process(out exit_status, _compiler_process);
  1005. _compiler_process = null;
  1006. }
  1007. private async void start_editor(uint window_xid)
  1008. {
  1009. if (window_xid == 0)
  1010. return;
  1011. // Spawn the level editor.
  1012. string args[] =
  1013. {
  1014. ENGINE_EXE
  1015. , "--data-dir"
  1016. , _project.data_dir()
  1017. , "--boot-dir"
  1018. , LEVEL_EDITOR_BOOT_DIR
  1019. , "--parent-window"
  1020. , window_xid.to_string()
  1021. , "--wait-console"
  1022. , "--pumped"
  1023. , null
  1024. };
  1025. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  1026. sl.set_cwd(ENGINE_DIR);
  1027. try
  1028. {
  1029. _editor_process = sl.spawnv(args);
  1030. }
  1031. catch (Error e)
  1032. {
  1033. loge(e.message);
  1034. }
  1035. // It is an error if the level editor disconnects after here.
  1036. _editor.disconnected.disconnect(on_editor_disconnected);
  1037. _editor.disconnected.connect(on_editor_disconnected_unexpected);
  1038. // Try to connect to the level editor.
  1039. int tries = yield _editor.connect_async(EDITOR_ADDRESS
  1040. , EDITOR_TCP_PORT
  1041. , EDITOR_CONNECTION_TRIES
  1042. , EDITOR_CONNECTION_INTERVAL
  1043. );
  1044. if (tries == EDITOR_CONNECTION_TRIES)
  1045. {
  1046. loge("Cannot connect to level_editor");
  1047. return;
  1048. }
  1049. }
  1050. private async void stop_editor()
  1051. {
  1052. yield _resource_chooser.stop_editor();
  1053. if (_editor != null)
  1054. {
  1055. // Reset "disconnected" signal.
  1056. _editor.disconnected.disconnect(on_editor_disconnected);
  1057. _editor.disconnected.disconnect(on_editor_disconnected_unexpected);
  1058. // Explicit call to this function should not produce error messages.
  1059. _editor.disconnected.connect(on_editor_disconnected);
  1060. if (_editor.is_connected())
  1061. {
  1062. _stop_editor_callback = stop_editor.callback;
  1063. _editor.send_script("Device.quit()");
  1064. yield; // Wait for ConsoleClient to disconnect.
  1065. _stop_editor_callback = null;
  1066. _editor_stack.set_visible_child(_editor_stack_disconnected_label);
  1067. }
  1068. }
  1069. int exit_status;
  1070. wait_process(out exit_status, _editor_process);
  1071. _editor_process = null;
  1072. }
  1073. private async void restart_editor()
  1074. {
  1075. yield stop_editor();
  1076. if (_editor_view != null)
  1077. {
  1078. _editor_view_overlay.remove(_editor_view);
  1079. _editor_stack.remove(_editor_view_overlay);
  1080. _editor_view = null;
  1081. }
  1082. _editor_view = new EditorView(_editor);
  1083. _editor_view.realized.connect(on_editor_view_realized);
  1084. _editor_view.button_press_event.connect(on_button_press);
  1085. _editor_view.button_release_event.connect(on_button_release);
  1086. _editor_view_overlay.add(_editor_view);
  1087. _editor_view_overlay.show_all();
  1088. _editor_stack.add(_editor_view_overlay);
  1089. _editor_stack.set_visible_child(_editor_view_overlay);
  1090. yield _resource_chooser.restart_editor();
  1091. }
  1092. private async void start_game(StartGame sg)
  1093. {
  1094. // Save test level to file
  1095. _database.dump(_project._level_editor_test_level.get_path(), _level._id);
  1096. // Save temporary package to reference test level
  1097. ArrayList<Value?> level = new ArrayList<Value?>();
  1098. level.add("_level_editor_test");
  1099. Hashtable package = new Hashtable();
  1100. package["level"] = level;
  1101. SJSON.save(package, _project._level_editor_test_package.get_path());
  1102. bool success = yield _data_compiler.compile(_project.data_dir(), _project.platform());
  1103. if (!success)
  1104. {
  1105. _toolbar_run.icon_name = "game-run";
  1106. return;
  1107. }
  1108. // Spawn the game.
  1109. string args[] =
  1110. {
  1111. ENGINE_EXE
  1112. , "--data-dir"
  1113. , _project.data_dir()
  1114. , "--console-port"
  1115. , GAME_TCP_PORT.to_string()
  1116. , "--wait-console"
  1117. , "--lua-string"
  1118. , sg == StartGame.TEST ? "TEST=true" : ""
  1119. , null
  1120. };
  1121. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  1122. sl.set_cwd(ENGINE_DIR);
  1123. try
  1124. {
  1125. _game_process = sl.spawnv(args);
  1126. }
  1127. catch (Error e)
  1128. {
  1129. loge(e.message);
  1130. }
  1131. // Try to connect to the game.
  1132. int tries = yield _game.connect_async(GAME_ADDRESS
  1133. , GAME_TCP_PORT
  1134. , GAME_CONNECTION_TRIES
  1135. , GAME_CONNECTION_INTERVAL
  1136. );
  1137. if (tries == GAME_CONNECTION_TRIES)
  1138. {
  1139. loge("Cannot connect to game");
  1140. return;
  1141. }
  1142. }
  1143. private async void stop_game()
  1144. {
  1145. if (_game != null)
  1146. {
  1147. // Reset "disconnected" signal.
  1148. _game.disconnected.disconnect(on_game_disconnected);
  1149. _game.disconnected.disconnect(on_game_disconnected_externally);
  1150. // Explicit call to this function should not produce error messages.
  1151. _game.disconnected.connect(on_game_disconnected);
  1152. if (_game.is_connected())
  1153. {
  1154. _stop_game_callback = stop_game.callback;
  1155. _game.send_script("Device.quit()");
  1156. yield; // Wait for SocketClient to disconnect.
  1157. _stop_game_callback = null;
  1158. }
  1159. }
  1160. int exit_status;
  1161. wait_process(out exit_status, _game_process);
  1162. _game_process = null;
  1163. }
  1164. private void deploy_game()
  1165. {
  1166. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Select destination directory..."
  1167. , this.active_window
  1168. , FileChooserAction.SELECT_FOLDER
  1169. , "Cancel"
  1170. , ResponseType.CANCEL
  1171. , "Open"
  1172. , ResponseType.ACCEPT
  1173. );
  1174. if (fcd.run() == ResponseType.ACCEPT)
  1175. {
  1176. GLib.File data_dir = File.new_for_path(fcd.get_filename());
  1177. string args[] =
  1178. {
  1179. ENGINE_EXE,
  1180. "--source-dir", _project.source_dir(),
  1181. "--map-source-dir", "core", _project.toolchain_dir(),
  1182. "--data-dir", data_dir.get_path(),
  1183. "--compile",
  1184. null
  1185. };
  1186. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  1187. sl.set_cwd(ENGINE_DIR);
  1188. try
  1189. {
  1190. GLib.Subprocess compiler = sl.spawnv(args);
  1191. compiler.wait();
  1192. if (compiler.get_exit_status() == 0)
  1193. {
  1194. string game_name = DEPLOY_DEFAULT_NAME;
  1195. GLib.File engine_exe_src = File.new_for_path(DEPLOY_EXE);
  1196. GLib.File engine_exe_dst = File.new_for_path(Path.build_filename(data_dir.get_path(), game_name + EXE_SUFFIX));
  1197. engine_exe_src.copy(engine_exe_dst, FileCopyFlags.OVERWRITE);
  1198. #if CROWN_PLATFORM_WINDOWS
  1199. string lua51_name = "lua51.dll";
  1200. GLib.File lua51_dll_src = File.new_for_path(lua51_name);
  1201. GLib.File lua51_dll_dst = File.new_for_path(Path.build_filename(data_dir.get_path(), lua51_name));
  1202. lua51_dll_src.copy(lua51_dll_dst, FileCopyFlags.OVERWRITE);
  1203. string openal_name = "openal-release.dll";
  1204. GLib.File openal_dll_src = File.new_for_path(openal_name);
  1205. GLib.File openal_dll_dst = File.new_for_path(Path.build_filename(data_dir.get_path(), openal_name));
  1206. openal_dll_src.copy(openal_dll_dst, FileCopyFlags.OVERWRITE);
  1207. #endif // CROWN_PLATFORM_WINDOWS
  1208. logi("Project deployed to `%s`".printf(data_dir.get_path()));
  1209. }
  1210. }
  1211. catch (Error e)
  1212. {
  1213. logi("%s".printf(e.message));
  1214. logi("Failed to deploy project");
  1215. }
  1216. }
  1217. fcd.destroy();
  1218. }
  1219. private async void on_editor_view_realized()
  1220. {
  1221. start_editor.begin(_editor_view.window_id);
  1222. }
  1223. private void on_tool_changed(GLib.SimpleAction action, GLib.Variant? param)
  1224. {
  1225. string name = param.get_string();
  1226. if (name == "place")
  1227. {
  1228. // Store previous tool for it to be restored later.
  1229. if (_tool_type != LevelEditorApi.ToolType.PLACE)
  1230. _tool_type_prev = _tool_type;
  1231. _tool_type = LevelEditorApi.ToolType.PLACE;
  1232. }
  1233. else if (name == "move")
  1234. _tool_type = LevelEditorApi.ToolType.MOVE;
  1235. else if (name == "rotate")
  1236. _tool_type = LevelEditorApi.ToolType.ROTATE;
  1237. else if (name == "scale")
  1238. _tool_type = LevelEditorApi.ToolType.SCALE;
  1239. _editor_view.grab_focus();
  1240. send_state();
  1241. action.set_state(param);
  1242. }
  1243. private void on_snap_mode_changed(GLib.SimpleAction action, GLib.Variant? param)
  1244. {
  1245. string name = param.get_string();
  1246. if (name == "relative")
  1247. _snap_mode = LevelEditorApi.SnapMode.RELATIVE;
  1248. else if (name == "absolute")
  1249. _snap_mode = LevelEditorApi.SnapMode.ABSOLUTE;
  1250. send_state();
  1251. action.set_state(param);
  1252. }
  1253. private void on_reference_system_changed(GLib.SimpleAction action, GLib.Variant? param)
  1254. {
  1255. string name = param.get_string();
  1256. if (name == "local")
  1257. _reference_system = LevelEditorApi.ReferenceSystem.LOCAL;
  1258. else if (name == "world")
  1259. _reference_system = LevelEditorApi.ReferenceSystem.WORLD;
  1260. send_state();
  1261. action.set_state(param);
  1262. }
  1263. private void on_grid_changed(GLib.SimpleAction action, GLib.Variant? param)
  1264. {
  1265. _grid_size = float.parse(param.get_string());
  1266. send_state();
  1267. action.set_state(param);
  1268. }
  1269. private void on_rotation_snap_changed(GLib.SimpleAction action, GLib.Variant? param)
  1270. {
  1271. _rotation_snap = float.parse(param.get_string());
  1272. send_state();
  1273. action.set_state(param);
  1274. }
  1275. private void new_level()
  1276. {
  1277. _level.load_from_path(LEVEL_EMPTY);
  1278. _level.send_level();
  1279. }
  1280. private void update_active_window_title()
  1281. {
  1282. string title = "";
  1283. if (_level._name != null)
  1284. {
  1285. title += (_level._name == LEVEL_EMPTY) ? "untitled" : _level._name;
  1286. if (_database.changed())
  1287. title += " • ";
  1288. title += " - ";
  1289. }
  1290. title += LEVEL_EDITOR_WINDOW_TITLE;
  1291. if (this.active_window.title != title)
  1292. this.active_window.title = title;
  1293. }
  1294. private void load_level(string name)
  1295. {
  1296. if (name == _level._name)
  1297. return;
  1298. string resource_name = name != "" ? name : LEVEL_EMPTY;
  1299. if (_level.load_from_path(resource_name) != 0)
  1300. {
  1301. loge("Unable to load level %s".printf(resource_name));
  1302. return;
  1303. }
  1304. if (_editor.is_connected())
  1305. {
  1306. _level.send_level();
  1307. send_state();
  1308. }
  1309. update_active_window_title();
  1310. }
  1311. private bool save_as(string? filename)
  1312. {
  1313. string path = filename;
  1314. if (path == null)
  1315. {
  1316. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Save As..."
  1317. , this.active_window
  1318. , FileChooserAction.SAVE
  1319. , "Cancel"
  1320. , ResponseType.CANCEL
  1321. , "Save"
  1322. , ResponseType.ACCEPT
  1323. );
  1324. fcd.add_filter(_file_filter);
  1325. fcd.set_current_folder(_project.source_dir());
  1326. int rt = ResponseType.CANCEL;
  1327. do
  1328. {
  1329. // Select the file
  1330. rt = fcd.run();
  1331. if (rt != ResponseType.ACCEPT)
  1332. {
  1333. fcd.destroy();
  1334. return false;
  1335. }
  1336. path = fcd.get_filename();
  1337. // Append file extension
  1338. if (!path.has_suffix(".level"))
  1339. path += ".level";
  1340. // Check if the file is within the source directory
  1341. if (!_project.path_is_within_source_dir(path))
  1342. {
  1343. Gtk.MessageDialog md = new Gtk.MessageDialog(fcd
  1344. , DialogFlags.MODAL
  1345. , MessageType.WARNING
  1346. , Gtk.ButtonsType.OK
  1347. , "The file must be within the source directory."
  1348. );
  1349. md.set_default_response(ResponseType.OK);
  1350. md.run();
  1351. md.destroy();
  1352. fcd.set_current_folder(_project.source_dir());
  1353. continue;
  1354. }
  1355. // Check if the file already exists
  1356. rt = ResponseType.YES;
  1357. if (GLib.FileUtils.test(path, FileTest.EXISTS))
  1358. {
  1359. Gtk.MessageDialog md = new Gtk.MessageDialog(fcd
  1360. , DialogFlags.MODAL
  1361. , MessageType.QUESTION
  1362. , Gtk.ButtonsType.YES_NO
  1363. , "A file named `%s` already exists.\nOverwrite?".printf(_project.basename(path))
  1364. );
  1365. md.set_default_response(ResponseType.NO);
  1366. rt = md.run();
  1367. md.destroy();
  1368. }
  1369. }
  1370. while (rt != ResponseType.YES);
  1371. fcd.destroy();
  1372. }
  1373. // Save level
  1374. string resource_filename = _project.absolute_path_to_resource_filename(path);
  1375. string resource_path = _project.resource_filename_to_resource_path(resource_filename);
  1376. string resource_name = _project.resource_path_to_resource_name(resource_path);
  1377. _level.save(resource_name);
  1378. _statusbar.set_temporary_message("Saved %s".printf(_level._path));
  1379. update_active_window_title();
  1380. return true;
  1381. }
  1382. private bool save()
  1383. {
  1384. return save_as(_level._path);
  1385. }
  1386. private bool save_timeout()
  1387. {
  1388. if (_level._path != null)
  1389. save();
  1390. return true;
  1391. }
  1392. protected override void shutdown()
  1393. {
  1394. // Disable auto-save.
  1395. if (_save_timer_id > 0)
  1396. GLib.Source.remove(_save_timer_id);
  1397. // Save editor settings.
  1398. _user.save(_user_file.get_path());
  1399. _preferences_dialog.encode(_settings);
  1400. SJSON.save(_settings, _settings_file.get_path());
  1401. _console_view._entry_history.save(_console_history_file.get_path());
  1402. // Destroy widgets.
  1403. if (_resource_chooser != null)
  1404. _resource_chooser.destroy();
  1405. if (_preferences_dialog != null)
  1406. _preferences_dialog.destroy();
  1407. base.shutdown();
  1408. }
  1409. // Returns true if the level has been saved or the user decided it
  1410. // should be discarded.
  1411. public bool should_quit()
  1412. {
  1413. int rt = ResponseType.YES;
  1414. if (_database.changed())
  1415. rt = run_level_changed_dialog(this.active_window);
  1416. if (!_database.changed() || rt == ResponseType.YES && save() || rt == ResponseType.NO)
  1417. return true;
  1418. return false;
  1419. }
  1420. private void on_new_level(GLib.SimpleAction action, GLib.Variant? param)
  1421. {
  1422. int rt = ResponseType.YES;
  1423. if (_database.changed())
  1424. rt = run_level_changed_dialog(this.active_window);
  1425. if (!_database.changed() || rt == ResponseType.YES && save() || rt == ResponseType.NO)
  1426. {
  1427. new_level();
  1428. send_state();
  1429. }
  1430. }
  1431. private void on_open_level_from_menubar(GLib.SimpleAction action, GLib.Variant? param)
  1432. {
  1433. string path = "";
  1434. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Open Level..."
  1435. , this.active_window
  1436. , FileChooserAction.OPEN
  1437. , "Cancel"
  1438. , ResponseType.CANCEL
  1439. , "Open"
  1440. , ResponseType.ACCEPT
  1441. );
  1442. fcd.add_filter(_file_filter);
  1443. fcd.set_current_folder(_project.source_dir());
  1444. int err = 1;
  1445. int rt = ResponseType.CANCEL;
  1446. do
  1447. {
  1448. // Select the file
  1449. rt = fcd.run();
  1450. if (rt != ResponseType.ACCEPT)
  1451. {
  1452. fcd.destroy();
  1453. return;
  1454. }
  1455. path = fcd.get_filename();
  1456. err = 0;
  1457. // Append file extension
  1458. if (!path.has_suffix(".level"))
  1459. path += ".level";
  1460. if (!_project.path_is_within_source_dir(path))
  1461. {
  1462. Gtk.MessageDialog md = new Gtk.MessageDialog(fcd
  1463. , DialogFlags.MODAL
  1464. , MessageType.WARNING
  1465. , Gtk.ButtonsType.OK
  1466. , "The file must be within the source directory."
  1467. );
  1468. md.set_default_response(ResponseType.OK);
  1469. md.run();
  1470. md.destroy();
  1471. fcd.set_current_folder(_project.source_dir());
  1472. err = 1;
  1473. continue;
  1474. }
  1475. }
  1476. while (err != 0);
  1477. fcd.destroy();
  1478. assert(path != "");
  1479. // Load level
  1480. string resource_filename = _project.absolute_path_to_resource_filename(path);
  1481. string resource_path = _project.resource_filename_to_resource_path(resource_filename);
  1482. string resource_name = _project.resource_path_to_resource_name(resource_path);
  1483. load_level(resource_name);
  1484. }
  1485. private void on_open_level(GLib.SimpleAction action, GLib.Variant? param)
  1486. {
  1487. int rt = ResponseType.YES;
  1488. string level_name = param.get_string();
  1489. if (level_name != "" && level_name == _level._name)
  1490. return;
  1491. if (_database.changed())
  1492. rt = run_level_changed_dialog(this.active_window);
  1493. if (!_database.changed() || rt == ResponseType.YES && save() || rt == ResponseType.NO)
  1494. {
  1495. if (level_name != "")
  1496. load_level(level_name);
  1497. else // Action invoked from menubar File > Open Level...
  1498. on_open_level_from_menubar(action, param);
  1499. }
  1500. }
  1501. private void on_open_project(GLib.SimpleAction action, GLib.Variant? param)
  1502. {
  1503. int rt = ResponseType.YES;
  1504. if (_database.changed())
  1505. rt = run_level_changed_dialog(this.active_window);
  1506. if (!_database.changed() || rt == ResponseType.YES && save() || rt == ResponseType.NO)
  1507. {
  1508. string source_dir;
  1509. rt = run_open_project_dialog(out source_dir, this.active_window);
  1510. if (rt != ResponseType.ACCEPT)
  1511. return;
  1512. if (_project.source_dir() == source_dir)
  1513. return;
  1514. restart_backend.begin(source_dir, LEVEL_NONE);
  1515. }
  1516. }
  1517. private void on_new_project(GLib.SimpleAction action, GLib.Variant? param)
  1518. {
  1519. int rt = ResponseType.YES;
  1520. if (_database.changed())
  1521. rt = run_level_changed_dialog(this.active_window);
  1522. if (!_database.changed() || rt == ResponseType.YES && save() || rt == ResponseType.NO)
  1523. {
  1524. stop_backend.begin((obj, res) => {
  1525. stop_backend.end(res);
  1526. show_panel("panel_new_project");
  1527. });
  1528. }
  1529. }
  1530. private void on_save(GLib.SimpleAction action, GLib.Variant? param)
  1531. {
  1532. save();
  1533. }
  1534. private void on_save_as(GLib.SimpleAction action, GLib.Variant? param)
  1535. {
  1536. save_as(null);
  1537. }
  1538. private void on_import(GLib.SimpleAction action, GLib.Variant? param)
  1539. {
  1540. string destination_dir = param.get_string();
  1541. _project.import(destination_dir != "" ? destination_dir : null, this.active_window);
  1542. }
  1543. private void on_preferences(GLib.SimpleAction action, GLib.Variant? param)
  1544. {
  1545. _preferences_dialog.set_transient_for(this.active_window);
  1546. _preferences_dialog.set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
  1547. _preferences_dialog.show_all();
  1548. }
  1549. private void on_deploy(GLib.SimpleAction action, GLib.Variant? param)
  1550. {
  1551. deploy_game();
  1552. }
  1553. private int run_level_changed_dialog(Gtk.Window? parent)
  1554. {
  1555. Gtk.MessageDialog md = new Gtk.MessageDialog(parent
  1556. , Gtk.DialogFlags.MODAL
  1557. , Gtk.MessageType.WARNING
  1558. , Gtk.ButtonsType.NONE
  1559. , "Save changes to Level before closing?"
  1560. );
  1561. md.add_button("Close _without Saving", ResponseType.NO);
  1562. md.add_button("_Cancel", ResponseType.CANCEL);
  1563. md.add_button("_Save", ResponseType.YES);
  1564. md.set_default_response(ResponseType.YES);
  1565. int rt = md.run();
  1566. md.destroy();
  1567. return rt;
  1568. }
  1569. public int run_open_project_dialog(out string source_dir, Gtk.Window? parent)
  1570. {
  1571. Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Open Project..."
  1572. , parent
  1573. , FileChooserAction.SELECT_FOLDER
  1574. , "Cancel"
  1575. , ResponseType.CANCEL
  1576. , "Open"
  1577. , ResponseType.ACCEPT
  1578. );
  1579. int rt = fcd.run();
  1580. source_dir = fcd.get_filename();
  1581. fcd.destroy();
  1582. return rt;
  1583. }
  1584. private void on_close(GLib.SimpleAction action, GLib.Variant? param)
  1585. {
  1586. int rt = ResponseType.YES;
  1587. if (_database.changed())
  1588. rt = run_level_changed_dialog(this.active_window);
  1589. if (!_database.changed() || rt == ResponseType.YES && save() || rt == ResponseType.NO)
  1590. {
  1591. stop_backend.begin((obj, res) => {
  1592. stop_backend.end(res);
  1593. show_panel("panel_welcome");
  1594. });
  1595. }
  1596. }
  1597. public void stop_backend_and_quit()
  1598. {
  1599. stop_backend.begin((obj, res) => {
  1600. stop_backend.end(res);
  1601. this.quit();
  1602. });
  1603. }
  1604. private void on_quit(GLib.SimpleAction action, GLib.Variant? param)
  1605. {
  1606. if (should_quit())
  1607. stop_backend_and_quit();
  1608. }
  1609. public static bool is_image_file(string path)
  1610. {
  1611. return path.has_suffix(".png")
  1612. || path.has_suffix(".tga")
  1613. ;
  1614. }
  1615. private void on_open_resource(GLib.SimpleAction action, GLib.Variant? param)
  1616. {
  1617. if (param == null)
  1618. return;
  1619. string resource_path = param.get_string();
  1620. string? type = Crown.resource_type(resource_path);
  1621. string? name = Crown.resource_name(type, resource_path);
  1622. if (type == null || name == null)
  1623. return;
  1624. if (type == "level")
  1625. {
  1626. activate_action("open-level", name);
  1627. return;
  1628. }
  1629. GLib.AppInfo? app = null;
  1630. if (type == "lua")
  1631. {
  1632. app = _preferences_dialog._lua_external_tool_button.get_app_info();
  1633. }
  1634. else if (is_image_file(resource_path))
  1635. {
  1636. app = _preferences_dialog._image_external_tool_button.get_app_info();
  1637. }
  1638. try
  1639. {
  1640. GLib.File file = GLib.File.new_for_path(_project.resource_path_to_absolute_path(resource_path));
  1641. if (app == null)
  1642. app = file.query_default_handler();
  1643. GLib.List<GLib.File> files = new GLib.List<GLib.File>();
  1644. files.append(file);
  1645. app.launch(files, null);
  1646. }
  1647. catch (Error e)
  1648. {
  1649. loge(e.message);
  1650. }
  1651. }
  1652. private void on_show_grid(GLib.SimpleAction action, GLib.Variant? param)
  1653. {
  1654. _show_grid = !action.get_state().get_boolean();
  1655. send_state();
  1656. action.set_state(new GLib.Variant.boolean(_show_grid));
  1657. }
  1658. private void on_custom_grid()
  1659. {
  1660. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Grid size"
  1661. , this.active_window
  1662. , DialogFlags.MODAL
  1663. , "Cancel"
  1664. , ResponseType.CANCEL
  1665. , "Ok"
  1666. , ResponseType.OK
  1667. , null
  1668. );
  1669. EntryDouble sb = new EntryDouble(_grid_size, 0.1, 1000);
  1670. sb.activate.connect(() => { dg.response(ResponseType.OK); });
  1671. dg.get_content_area().add(sb);
  1672. dg.skip_taskbar_hint = true;
  1673. dg.show_all();
  1674. if (dg.run() == ResponseType.OK)
  1675. {
  1676. _grid_size = sb.value;
  1677. send_state();
  1678. }
  1679. dg.destroy();
  1680. }
  1681. private void on_rotation_snap(GLib.SimpleAction action, GLib.Variant? param)
  1682. {
  1683. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Rotation snap"
  1684. , this.active_window
  1685. , DialogFlags.MODAL
  1686. , "Cancel"
  1687. , ResponseType.CANCEL
  1688. , "Ok"
  1689. , ResponseType.OK
  1690. , null
  1691. );
  1692. EntryDouble sb = new EntryDouble(_rotation_snap, 1.0, 180.0);
  1693. sb.activate.connect(() => { dg.response(ResponseType.OK); });
  1694. dg.get_content_area().add(sb);
  1695. dg.skip_taskbar_hint = true;
  1696. dg.show_all();
  1697. if (dg.run() == ResponseType.OK)
  1698. {
  1699. _rotation_snap = sb.value;
  1700. send_state();
  1701. }
  1702. dg.destroy();
  1703. }
  1704. private void on_create_primitive(GLib.SimpleAction action, GLib.Variant? param)
  1705. {
  1706. if (action.name == "primitive-cube")
  1707. set_placeable("unit", "core/units/primitives/cube");
  1708. else if (action.name == "primitive-sphere")
  1709. set_placeable("unit", "core/units/primitives/sphere");
  1710. else if (action.name == "primitive-cone")
  1711. set_placeable("unit", "core/units/primitives/cone");
  1712. else if (action.name == "primitive-cylinder")
  1713. set_placeable("unit", "core/units/primitives/cylinder");
  1714. else if (action.name == "primitive-plane")
  1715. set_placeable("unit", "core/units/primitives/plane");
  1716. else if (action.name == "camera")
  1717. set_placeable("unit", "core/units/camera");
  1718. else if (action.name == "light")
  1719. set_placeable("unit", "core/units/light");
  1720. else if (action.name == "sound-source")
  1721. set_placeable("sound", "");
  1722. activate_action("tool", new GLib.Variant.string("place"));
  1723. }
  1724. private void on_create_unit(GLib.SimpleAction action, GLib.Variant? param)
  1725. {
  1726. _level.spawn_empty_unit();
  1727. }
  1728. private void on_camera_view(GLib.SimpleAction action, GLib.Variant? param)
  1729. {
  1730. string name = param.get_string();
  1731. if (name == "perspective")
  1732. _editor.send_script("LevelEditor:camera_view_perspective()");
  1733. else if (name == "front")
  1734. _editor.send_script("LevelEditor:camera_view_front()");
  1735. else if (name == "back")
  1736. _editor.send_script("LevelEditor:camera_view_back()");
  1737. else if (name == "right")
  1738. _editor.send_script("LevelEditor:camera_view_right()");
  1739. else if (name == "left")
  1740. _editor.send_script("LevelEditor:camera_view_left()");
  1741. else if (name == "top")
  1742. _editor.send_script("LevelEditor:camera_view_top()");
  1743. else if (name == "bottom")
  1744. _editor.send_script("LevelEditor:camera_view_bottom()");
  1745. _editor.send(DeviceApi.frame());
  1746. action.set_state(param);
  1747. }
  1748. private void on_resource_chooser(GLib.SimpleAction action, GLib.Variant? param)
  1749. {
  1750. _resource_popover.show_all();
  1751. }
  1752. private void on_project_browser(GLib.SimpleAction action, GLib.Variant? param)
  1753. {
  1754. if (_project_stack.is_visible())
  1755. {
  1756. _project_stack.hide();
  1757. }
  1758. else
  1759. {
  1760. _project_stack.show_all();
  1761. }
  1762. }
  1763. private void on_console(GLib.SimpleAction action, GLib.Variant? param)
  1764. {
  1765. if (_console_view.is_visible())
  1766. {
  1767. if (_console_view._entry.has_focus)
  1768. _console_view.hide();
  1769. else
  1770. _console_view._entry.grab_focus();
  1771. }
  1772. else
  1773. {
  1774. _console_view.show_all();
  1775. }
  1776. }
  1777. private void on_statusbar(GLib.SimpleAction action, GLib.Variant? param)
  1778. {
  1779. if (_statusbar.is_visible())
  1780. {
  1781. _statusbar.hide();
  1782. }
  1783. else
  1784. {
  1785. _statusbar.show_all();
  1786. }
  1787. }
  1788. private void on_inspector(GLib.SimpleAction action, GLib.Variant? param)
  1789. {
  1790. if (_inspector_stack.is_visible())
  1791. {
  1792. _inspector_stack.hide();
  1793. }
  1794. else
  1795. {
  1796. _inspector_stack.show_all();
  1797. }
  1798. }
  1799. private void on_restart_editor_view(GLib.SimpleAction action, GLib.Variant? param)
  1800. {
  1801. restart_editor.begin((obj, res) => {
  1802. restart_editor.end(res);
  1803. });
  1804. }
  1805. private void on_build_data(GLib.SimpleAction action, GLib.Variant? param)
  1806. {
  1807. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  1808. _data_compiler.compile.end(res);
  1809. });
  1810. }
  1811. private void on_refresh_lua(GLib.SimpleAction action, GLib.Variant? param)
  1812. {
  1813. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  1814. if (_data_compiler.compile.end(res))
  1815. {
  1816. _editor.send(DeviceApi.refresh());
  1817. _editor.send(DeviceApi.frame());
  1818. _game.send(DeviceApi.refresh());
  1819. _game.send(DeviceApi.frame());
  1820. }
  1821. });
  1822. }
  1823. private void on_snap_to_grid(GLib.SimpleAction action, GLib.Variant? param)
  1824. {
  1825. _snap_to_grid = !action.get_state().get_boolean();
  1826. send_state();
  1827. action.set_state(new GLib.Variant.boolean(_snap_to_grid));
  1828. }
  1829. private void on_debug_render_world(GLib.SimpleAction action, GLib.Variant? param)
  1830. {
  1831. _debug_render_world = !action.get_state().get_boolean();
  1832. send_state();
  1833. action.set_state(new GLib.Variant.boolean(_debug_render_world));
  1834. }
  1835. private void on_debug_physics_world(GLib.SimpleAction action, GLib.Variant? param)
  1836. {
  1837. _debug_physics_world = !action.get_state().get_boolean();
  1838. send_state();
  1839. action.set_state(new GLib.Variant.boolean(_debug_physics_world));
  1840. }
  1841. private void on_run_game(GLib.SimpleAction action, GLib.Variant? param)
  1842. {
  1843. string icon_name_displayed = _toolbar_run.icon_name;
  1844. stop_game.begin((obj, res) => {
  1845. stop_game.end(res);
  1846. if (icon_name_displayed == "game-run")
  1847. {
  1848. // Always change icon state regardless of failures
  1849. _toolbar_run.icon_name = "game-stop";
  1850. //
  1851. _game.disconnected.disconnect(on_game_disconnected);
  1852. _game.disconnected.connect(on_game_disconnected_externally);
  1853. start_game.begin(action.name == "test-level" ? StartGame.TEST : StartGame.NORMAL);
  1854. }
  1855. });
  1856. }
  1857. private void on_undo(GLib.SimpleAction action, GLib.Variant? param)
  1858. {
  1859. int id = _database.undo();
  1860. if (id != -1)
  1861. _statusbar.set_temporary_message("Undo: " + ActionNames[id]);
  1862. }
  1863. private void on_redo(GLib.SimpleAction action, GLib.Variant? param)
  1864. {
  1865. int id = _database.redo();
  1866. if (id != -1)
  1867. _statusbar.set_temporary_message("Redo: " + ActionNames[id]);
  1868. }
  1869. private void on_duplicate(GLib.SimpleAction action, GLib.Variant? param)
  1870. {
  1871. _level.duplicate_selected_objects();
  1872. }
  1873. private void on_delete(GLib.SimpleAction action, GLib.Variant? param)
  1874. {
  1875. _level.destroy_selected_objects();
  1876. }
  1877. private void on_manual(GLib.SimpleAction action, GLib.Variant? param)
  1878. {
  1879. try
  1880. {
  1881. AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION, null);
  1882. }
  1883. catch (Error e)
  1884. {
  1885. loge(e.message);
  1886. }
  1887. }
  1888. private void on_report_issue(GLib.SimpleAction action, GLib.Variant? param)
  1889. {
  1890. try
  1891. {
  1892. AppInfo.launch_default_for_uri("https://github.com/dbartolini/crown/issues", null);
  1893. }
  1894. catch (Error e)
  1895. {
  1896. loge(e.message);
  1897. }
  1898. }
  1899. private void on_browse_logs(GLib.SimpleAction action, GLib.Variant? param)
  1900. {
  1901. open_directory(_logs_dir.get_path());
  1902. }
  1903. private void on_changelog(GLib.SimpleAction action, GLib.Variant? param)
  1904. {
  1905. try
  1906. {
  1907. AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION + "/changelog.html", null);
  1908. }
  1909. catch (Error e)
  1910. {
  1911. loge(e.message);
  1912. }
  1913. }
  1914. private void on_about(GLib.SimpleAction action, GLib.Variant? param)
  1915. {
  1916. Gtk.AboutDialog dlg = new Gtk.AboutDialog();
  1917. dlg.set_destroy_with_parent(true);
  1918. dlg.set_transient_for(this.active_window);
  1919. dlg.set_modal(true);
  1920. dlg.set_logo_icon_name(CROWN_ICON_NAME);
  1921. dlg.program_name = LEVEL_EDITOR_WINDOW_TITLE;
  1922. dlg.version = CROWN_VERSION;
  1923. dlg.website = "https://github.com/dbartolini/crown";
  1924. dlg.copyright = "Copyright (c) 2012-2021 Daniele Bartolini et al.";
  1925. dlg.license = "Crown Game Engine."
  1926. + "\nCopyright (c) 2012-2021 Daniele Bartolini et al."
  1927. + "\n"
  1928. + "\nPermission is hereby granted, free of charge, to any person"
  1929. + "\nobtaining a copy of this software and associated documentation"
  1930. + "\nfiles (the \"Software\"), to deal in the Software without"
  1931. + "\nrestriction, including without limitation the rights to use,"
  1932. + "\ncopy, modify, merge, publish, distribute, sublicense, and/or sell"
  1933. + "\ncopies of the Software, and to permit persons to whom the"
  1934. + "\nSoftware is furnished to do so, subject to the following"
  1935. + "\nconditions:"
  1936. + "\n"
  1937. + "\nThe above copyright notice and this permission notice shall be"
  1938. + "\nincluded in all copies or substantial portions of the Software."
  1939. + "\n"
  1940. + "\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,"
  1941. + "\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES"
  1942. + "\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND"
  1943. + "\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT"
  1944. + "\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,"
  1945. + "\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING"
  1946. + "\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR"
  1947. + "\nOTHER DEALINGS IN THE SOFTWARE."
  1948. + "\n"
  1949. ;
  1950. dlg.authors = { "Daniele Bartolini"
  1951. , "Simone Boscaratto"
  1952. , "Michele Rossi"
  1953. , "Raphael de Vasconcelos Nascimento"
  1954. };
  1955. dlg.artists = { "Michela Iacchelli - Pepper logo"
  1956. , "Giulia Gazzoli - Crown logo"
  1957. };
  1958. dlg.run();
  1959. dlg.destroy();
  1960. }
  1961. private void on_delete_file(GLib.SimpleAction action, GLib.Variant? param)
  1962. {
  1963. if (param == null)
  1964. return;
  1965. string resource_path = param.get_string();
  1966. string? type = Crown.resource_type(resource_path);
  1967. string? name = Crown.resource_name(type, resource_path);
  1968. if (type == null || name == null)
  1969. return;
  1970. if (name == _level._name)
  1971. {
  1972. int rt = ResponseType.YES;
  1973. if (_database.changed())
  1974. rt = run_level_changed_dialog(this.active_window);
  1975. if (!_database.changed() || rt == ResponseType.YES && save() || rt == ResponseType.NO)
  1976. {
  1977. new_level();
  1978. send_state();
  1979. _project.delete_resource(type, name);
  1980. }
  1981. }
  1982. else
  1983. {
  1984. _project.delete_resource(type, name);
  1985. }
  1986. }
  1987. public void set_autosave_timer(uint minutes)
  1988. {
  1989. if (_save_timer_id > 0)
  1990. GLib.Source.remove(_save_timer_id);
  1991. _save_timer_id = GLib.Timeout.add_seconds(minutes*60, save_timeout);
  1992. }
  1993. public void menu_set_enabled(bool enabled, GLib.ActionEntry[] entries, string[]? whitelist = null)
  1994. {
  1995. for (int ii = 0; ii < entries.length; ++ii)
  1996. {
  1997. string action_name = entries[ii].name;
  1998. int jj = 0;
  1999. if (whitelist != null)
  2000. {
  2001. for (; jj < whitelist.length; ++jj)
  2002. {
  2003. if (action_name == whitelist[jj])
  2004. break;
  2005. }
  2006. }
  2007. if (whitelist == null || whitelist != null && jj == whitelist.length)
  2008. {
  2009. GLib.SimpleAction sa = this.lookup_action(action_name) as GLib.SimpleAction;
  2010. if (sa != null)
  2011. sa.set_enabled(enabled);
  2012. }
  2013. }
  2014. }
  2015. private void set_conflicting_accels(bool on)
  2016. {
  2017. if (on)
  2018. {
  2019. this.set_accels_for_action("app.tool::place", _tool_place_accels);
  2020. this.set_accels_for_action("app.tool::move", _tool_move_accels);
  2021. this.set_accels_for_action("app.tool::rotate", _tool_rotate_accels);
  2022. this.set_accels_for_action("app.tool::scale", _tool_scale_accels);
  2023. this.set_accels_for_action("app.delete", _delete_accels);
  2024. this.set_accels_for_action("app.camera-view::perspective", _camera_view_perspective_accels);
  2025. this.set_accels_for_action("app.camera-view::front", _camera_view_front_accels);
  2026. this.set_accels_for_action("app.camera-view::back", _camera_view_back_accels);
  2027. this.set_accels_for_action("app.camera-view::right", _camera_view_right_accels);
  2028. this.set_accels_for_action("app.camera-view::left", _camera_view_left_accels);
  2029. this.set_accels_for_action("app.camera-view::top", _camera_view_top_accels);
  2030. this.set_accels_for_action("app.camera-view::bottom", _camera_view_bottom_accels);
  2031. }
  2032. else
  2033. {
  2034. this.set_accels_for_action("app.tool::place", {});
  2035. this.set_accels_for_action("app.tool::move", {});
  2036. this.set_accels_for_action("app.tool::rotate", {});
  2037. this.set_accels_for_action("app.tool::scale", {});
  2038. this.set_accels_for_action("app.delete", {});
  2039. this.set_accels_for_action("app.camera-view::perspective", {});
  2040. this.set_accels_for_action("app.camera-view::front", {});
  2041. this.set_accels_for_action("app.camera-view::back", {});
  2042. this.set_accels_for_action("app.camera-view::right", {});
  2043. this.set_accels_for_action("app.camera-view::left", {});
  2044. this.set_accels_for_action("app.camera-view::top", {});
  2045. this.set_accels_for_action("app.camera-view::bottom", {});
  2046. }
  2047. }
  2048. public void entry_any_focus_in(Gtk.Widget widget)
  2049. {
  2050. set_conflicting_accels(false);
  2051. }
  2052. public void entry_any_focus_out(Gtk.Widget widget)
  2053. {
  2054. set_conflicting_accels(true);
  2055. }
  2056. public void show_panel(string name, Gtk.StackTransitionType stt = Gtk.StackTransitionType.NONE)
  2057. {
  2058. _main_stack.set_visible_child_full(name, stt);
  2059. if (name == "main_vbox")
  2060. {
  2061. // FIXME: save/restore last known window state
  2062. int win_w;
  2063. int win_h;
  2064. this.active_window.get_size(out win_w, out win_h);
  2065. _editor_pane.set_position(210);
  2066. _content_pane.set_position(win_h - 250);
  2067. _inspector_pane.set_position(win_h - 600);
  2068. _main_pane.set_position(win_w - 375);
  2069. menu_set_enabled(true, action_entries_file);
  2070. menu_set_enabled(true, action_entries_edit);
  2071. menu_set_enabled(true, action_entries_create);
  2072. menu_set_enabled(true, action_entries_camera);
  2073. menu_set_enabled(true, action_entries_view);
  2074. menu_set_enabled(true, action_entries_debug);
  2075. menu_set_enabled(true, action_entries_help);
  2076. }
  2077. else if (name == "panel_welcome"
  2078. || name == "panel_new_project"
  2079. || name == "panel_projects_list"
  2080. )
  2081. {
  2082. menu_set_enabled(false, action_entries_file, {"new-project", "open-project", "quit"});
  2083. menu_set_enabled(false, action_entries_edit);
  2084. menu_set_enabled(false, action_entries_create);
  2085. menu_set_enabled(false, action_entries_camera);
  2086. menu_set_enabled(false, action_entries_view);
  2087. menu_set_enabled(false, action_entries_debug);
  2088. menu_set_enabled( true, action_entries_help);
  2089. }
  2090. }
  2091. public void set_placeable(string type, string name)
  2092. {
  2093. _placeable_type = type;
  2094. _placeable_name = name;
  2095. _editor.send_script(LevelEditorApi.set_placeable(type, name));
  2096. }
  2097. public void activate_last_tool_before_place()
  2098. {
  2099. const string type_to_name[] =
  2100. {
  2101. "place",
  2102. "move",
  2103. "rotate",
  2104. "scale"
  2105. };
  2106. GLib.static_assert(type_to_name.length == LevelEditorApi.ToolType.COUNT);
  2107. if (_tool_type != LevelEditorApi.ToolType.PLACE)
  2108. return;
  2109. activate_action("tool", new GLib.Variant.string(type_to_name[_tool_type_prev]));
  2110. }
  2111. }
  2112. // Global paths
  2113. public static GLib.File _toolchain_dir;
  2114. public static GLib.File _templates_dir;
  2115. public static GLib.File _config_dir;
  2116. public static GLib.File _logs_dir;
  2117. public static GLib.File _cache_dir;
  2118. public static GLib.File _documents_dir;
  2119. public static GLib.File _log_file;
  2120. public static GLib.File _settings_file;
  2121. public static GLib.File _user_file;
  2122. public static GLib.File _console_history_file;
  2123. public static GLib.FileStream _log_stream;
  2124. public static ConsoleView _console_view;
  2125. public static bool _console_view_valid = false;
  2126. public static void log(string system, string severity, string message)
  2127. {
  2128. GLib.DateTime now = new GLib.DateTime.now_utc();
  2129. int now_us = now.get_microsecond();
  2130. string now_str = now.format("%H:%M:%S");
  2131. if (_log_stream != null)
  2132. {
  2133. string line = "%s.%06d %.4s %s: %s\n".printf(now_str
  2134. , now_us
  2135. , severity.ascii_up()
  2136. , system
  2137. , message
  2138. );
  2139. _log_stream.puts(line);
  2140. _log_stream.flush();
  2141. }
  2142. if (_console_view_valid)
  2143. {
  2144. string line = "%s.%06d %s: %s\n".printf(now_str
  2145. , now_us
  2146. , system
  2147. , message
  2148. );
  2149. _console_view.log(severity, line);
  2150. }
  2151. }
  2152. public static void logi(string message)
  2153. {
  2154. log("editor", "info", message);
  2155. }
  2156. public static void logw(string message)
  2157. {
  2158. log("editor", "warning", message);
  2159. }
  2160. public static void loge(string message)
  2161. {
  2162. log("editor", "error", message);
  2163. }
  2164. public void open_directory(string directory)
  2165. {
  2166. #if CROWN_PLATFORM_LINUX
  2167. try
  2168. {
  2169. GLib.AppInfo.launch_default_for_uri("file://" + directory, null);
  2170. }
  2171. catch (Error e)
  2172. {
  2173. loge(e.message);
  2174. }
  2175. #else
  2176. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  2177. try
  2178. {
  2179. sl.spawnv({ "explorer.exe", directory, null });
  2180. }
  2181. catch (Error e)
  2182. {
  2183. loge(e.message);
  2184. }
  2185. #endif
  2186. }
  2187. public static GLib.SubprocessFlags subprocess_flags()
  2188. {
  2189. GLib.SubprocessFlags flags = SubprocessFlags.NONE;
  2190. #if !CROWN_DEBUG
  2191. flags |= SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE;
  2192. #endif
  2193. return flags;
  2194. }
  2195. public static bool is_directory_empty(string path)
  2196. {
  2197. GLib.File file = GLib.File.new_for_path(path);
  2198. try
  2199. {
  2200. FileEnumerator enumerator = file.enumerate_children("standard::*"
  2201. , FileQueryInfoFlags.NOFOLLOW_SYMLINKS
  2202. );
  2203. return enumerator.next_file() == null;
  2204. }
  2205. catch (GLib.Error e)
  2206. {
  2207. loge(e.message);
  2208. }
  2209. return false;
  2210. }
  2211. /// Waits for @a process to terminate and returns true if success, false
  2212. /// otherwise. If the function succeeds, it also returns the @a process' @a
  2213. /// exit_status.
  2214. public static int wait_process(out int exit_status, GLib.Subprocess? process)
  2215. {
  2216. exit_status = int.MAX;
  2217. if (process == null)
  2218. return 1;
  2219. try
  2220. {
  2221. if (!process.wait())
  2222. return 1;
  2223. if (process.get_if_exited())
  2224. {
  2225. exit_status = process.get_exit_status();
  2226. return 0;
  2227. }
  2228. // Process exited abnormally.
  2229. return 1;
  2230. }
  2231. catch (Error e)
  2232. {
  2233. loge(e.message);
  2234. return 1;
  2235. }
  2236. }
  2237. private void device_frame_delayed(uint delay_ms, ConsoleClient client)
  2238. {
  2239. // FIXME: find a way to time exactly when it is effective to queue a redraw.
  2240. // See: https://blogs.gnome.org/jnelson/2010/10/13/those-realize-map-widget-signals/
  2241. GLib.Timeout.add_full(GLib.Priority.DEFAULT, delay_ms, () => {
  2242. client.send(DeviceApi.frame());
  2243. return false;
  2244. });
  2245. }
  2246. public static int main(string[] args)
  2247. {
  2248. // Global paths
  2249. _config_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_config_dir(), "crown"));
  2250. try { _config_dir.make_directory(); } catch (Error e) { /* Nobody cares */ }
  2251. _logs_dir = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "logs"));
  2252. try { _logs_dir.make_directory(); } catch (Error e) { /* Nobody cares */ }
  2253. _documents_dir = GLib.File.new_for_path(GLib.Environment.get_user_special_dir(GLib.UserDirectory.DOCUMENTS));
  2254. _cache_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_cache_dir(), "crown"));
  2255. try { _cache_dir.make_directory(); } catch (Error e) { /* Nobody cares */ }
  2256. _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"));
  2257. _settings_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "settings.sjson"));
  2258. _user_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "user.sjson"));
  2259. _console_history_file = GLib.File.new_for_path(GLib.Path.build_filename(_cache_dir.get_path(), "console_history.txt"));
  2260. _log_stream = GLib.FileStream.open(_log_file.get_path(), "a");
  2261. // Find toolchain path, more desirable paths come first.
  2262. int ii = 0;
  2263. string toolchain_paths[] =
  2264. {
  2265. ".",
  2266. "../..",
  2267. "../../../samples"
  2268. };
  2269. for (ii = 0; ii < toolchain_paths.length; ++ii)
  2270. {
  2271. string path = Path.build_filename(toolchain_paths[ii], "core");
  2272. if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR))
  2273. {
  2274. _toolchain_dir = File.new_for_path(path).get_parent();
  2275. break;
  2276. }
  2277. }
  2278. if (ii == toolchain_paths.length)
  2279. {
  2280. loge("Unable to find the toolchain directory");
  2281. return 1;
  2282. }
  2283. // Find templates path, more desirable paths come first.
  2284. string templates_path[] =
  2285. {
  2286. ".",
  2287. "../..",
  2288. "../../.."
  2289. };
  2290. for (ii = 0; ii < templates_path.length; ++ii)
  2291. {
  2292. string path = Path.build_filename(templates_path[ii], "samples");
  2293. if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR))
  2294. {
  2295. _templates_dir = File.new_for_path(path);
  2296. break;
  2297. }
  2298. }
  2299. if (ii == templates_path.length)
  2300. {
  2301. loge("Unable to find the templates directory");
  2302. return 1;
  2303. }
  2304. LevelEditorApplication app = new LevelEditorApplication();
  2305. return app.run(args);
  2306. }
  2307. }