level_editor.vala 83 KB

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