| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838 |
- /*
- * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
- * License: https://github.com/dbartolini/crown/blob/master/LICENSE
- */
- using Gdk; // Pixbuf
- using Gee;
- using Gtk;
- namespace Crown
- {
- const int WINDOW_DEFAULT_WIDTH = 1280;
- const int WINDOW_DEFAULT_HEIGHT = 720;
- public class LevelEditorWindow : Gtk.ApplicationWindow
- {
- private const GLib.ActionEntry[] action_entries =
- {
- { "fullscreen", on_fullscreen, null, null }
- };
- public bool _fullscreen;
- public LevelEditorWindow(Gtk.Application app)
- {
- Object(application: app);
- this.add_action_entries(action_entries, this);
- this.title = "Level Editor";
- this.key_press_event.connect(this.on_key_press);
- this.key_release_event.connect(this.on_key_release);
- this.window_state_event.connect(this.on_window_state_event);
- this.show.connect(this.on_show);
- this.delete_event.connect(this.on_delete_event);
- this.set_default_size(WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT);
- this.focus_out_event.connect(this.on_focus_out);
- _fullscreen = false;
- }
- private void on_fullscreen(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (_fullscreen)
- unfullscreen();
- else
- fullscreen();
- }
- private bool on_key_press(Gdk.EventKey ev)
- {
- LevelEditorApplication app = (LevelEditorApplication)application;
- if (ev.keyval == Gdk.Key.Control_L)
- app._editor.send_script(LevelEditorApi.key_down("ctrl_left"));
- else if (ev.keyval == Gdk.Key.Shift_L)
- app._editor.send_script(LevelEditorApi.key_down("shift_left"));
- else if (ev.keyval == Gdk.Key.Alt_L)
- app._editor.send_script(LevelEditorApi.key_down("alt_left"));
- return false;
- }
- private bool on_key_release(Gdk.EventKey ev)
- {
- LevelEditorApplication app = (LevelEditorApplication)application;
- if (ev.keyval == Gdk.Key.Control_L)
- app._editor.send_script(LevelEditorApi.key_up("ctrl_left"));
- else if (ev.keyval == Gdk.Key.Shift_L)
- app._editor.send_script(LevelEditorApi.key_up("shift_left"));
- else if (ev.keyval == Gdk.Key.Alt_L)
- app._editor.send_script(LevelEditorApi.key_up("alt_left"));
- return false;
- }
- private bool on_window_state_event(EventWindowState ev)
- {
- _fullscreen = (ev.new_window_state & WindowState.FULLSCREEN) != 0;
- return true;
- }
- private void on_show()
- {
- this.maximize();
- }
- private bool on_delete_event()
- {
- LevelEditorApplication app = (LevelEditorApplication)application;
- if (app.should_quit())
- {
- app.close_all();
- return false; // Quit application
- }
- return true; // Keep alive
- }
- private bool on_focus_out(Gdk.EventFocus ev)
- {
- LevelEditorApplication app = (LevelEditorApplication)application;
- app._editor.send_script(LevelEditorApi.key_up("ctrl_left"));
- app._editor.send_script(LevelEditorApi.key_up("shift_left"));
- app._editor.send_script(LevelEditorApi.key_up("alt_left"));
- return true;
- }
- }
- public enum ToolType
- {
- PLACE,
- MOVE,
- ROTATE,
- SCALE
- }
- public enum SnapMode
- {
- RELATIVE,
- ABSOLUTE
- }
- public enum ReferenceSystem
- {
- LOCAL,
- WORLD
- }
- public enum StartGame
- {
- NORMAL,
- TEST
- }
- public class LevelEditorApplication : Gtk.Application
- {
- // Constants
- private const GLib.ActionEntry[] action_entries =
- {
- // parameter type
- // name activate() | state
- // | | | |
- { "menu-file", null, null, null },
- { "new-level", on_new_level, null, null },
- { "open-level", on_open_level, "s", null },
- { "open-project", on_open_project, null, null },
- { "save", on_save, null, null },
- { "save-as", on_save_as, null, null },
- { "import", on_import, null, null },
- { "preferences", on_preferences, null, null },
- { "deploy", on_deploy, null, null },
- { "quit", on_quit, null, null },
- { "menu-edit", null, null, null },
- { "undo", on_undo, null, null },
- { "redo", on_redo, null, null },
- { "duplicate", on_duplicate, null, null },
- { "delete", on_delete, null, null },
- { "tool", on_tool_changed, "s", "'move'" },
- { "snap", on_snap_mode_changed, "s", "'relative'" },
- { "reference-system", on_reference_system_changed, "s", "'local'" },
- { "snap-to-grid", on_snap_to_grid, null, "true" },
- { "menu-grid", null, null, null },
- { "grid-show", on_show_grid, null, "true" },
- { "grid-custom", on_custom_grid, null, null },
- { "grid-preset", on_grid_changed, "s", "'1'" },
- { "menu-rotation-snap", null, null, null },
- { "rotation-snap-custom", on_rotation_snap, null, null },
- { "rotation-snap-preset", on_rotation_snap_changed, "s", "'15'" },
- { "menu-create", null, null, null },
- { "menu-primitives", null, null, null },
- { "primitive-cube", on_create_primitive, null, null },
- { "primitive-sphere", on_create_primitive, null, null },
- { "primitive-cone", on_create_primitive, null, null },
- { "primitive-cylinder", on_create_primitive, null, null },
- { "primitive-plane", on_create_primitive, null, null },
- { "camera", on_create_primitive, null, null },
- { "light", on_create_primitive, null, null },
- { "sound-source", on_create_primitive, null, null },
- { "menu-camera", null, null, null },
- { "camera-view", on_camera_view, "s", "'perspective'" },
- { "menu-engine", null, null, null },
- { "restart", on_editor_restart, null, null },
- { "reload-lua", on_refresh_lua, null, null },
- { "menu-view", null, null, null },
- { "resource-chooser", on_resource_chooser, null, null },
- { "project-browser", on_project_browser, null, null },
- { "console", on_console, null, null },
- { "statusbar", on_statusbar, null, null },
- { "inspector", on_inspector, null, null },
- { "menu-run", null, null, null },
- { "test-level", on_run_game, null, null },
- { "run-game", on_run_game, null, null },
- { "menu-help", null, null, null },
- { "manual", on_manual, null, null },
- { "report-issue", on_report_issue, null, null },
- { "open-last-log", on_open_last_log, null, null },
- { "changelog", on_changelog, null, null },
- { "about", on_about, null, null },
- { "debug-render-world", on_debug_render_world, null, "false" },
- { "debug-physics-world", on_debug_physics_world, null, "false" }
- };
- // Command line options
- private string _source_dir = "";
- private string _toolchain_dir = "";
- private string _level_resource = "";
- private bool _create_initial_files = false;
- // Global paths
- private GLib.File _config_dir;
- private GLib.File _settings_file;
- // Editor state
- private double _grid_size;
- private double _rotation_snap;
- private bool _show_grid;
- private bool _snap_to_grid;
- private bool _debug_render_world;
- private bool _debug_physics_world;
- private ToolType _tool_type;
- private SnapMode _snap_mode;
- private ReferenceSystem _reference_system;
- // Engine connections
- private GLib.Subprocess _compiler_process;
- private GLib.Subprocess _editor_process;
- private GLib.Subprocess _game_process;
- private ConsoleClient _compiler;
- public ConsoleClient _editor;
- private ConsoleClient _game;
- // Level data
- private Database _database;
- private Project _project;
- private ProjectStore _project_store;
- private Level _level;
- private DataCompiler _data_compiler;
- // Widgets
- private ConsoleView _console_view;
- private ProjectBrowser _project_browser;
- private EditorView _editor_view;
- private LevelTreeView _level_treeview;
- private LevelLayersTreeView _level_layers_treeview;
- private PropertiesView _properties_view;
- private PreferencesDialog _preferences_dialog;
- private ResourceChooser _resource_chooser;
- private Gtk.Popover _resource_popover;
- private Gtk.Overlay _editor_view_overlay;
- private Slide _project_slide;
- private Slide _editor_slide;
- private Slide _inspector_slide;
- private Gtk.Toolbar _toolbar;
- private Gtk.ToolButton _toolbar_run;
- private Gtk.Notebook _level_tree_view_notebook;
- private Gtk.Paned _editor_pane;
- private Gtk.Paned _content_pane;
- private Gtk.Paned _inspector_pane;
- private Gtk.Paned _main_pane;
- private Statusbar _statusbar;
- private Gtk.Box _main_vbox;
- private Gtk.FileFilter _file_filter;
- private Gtk.ComboBoxText _combo;
- private uint _save_timer_id;
- public LevelEditorApplication()
- {
- Object(application_id: "org.crown.level_editor"
- , flags: GLib.ApplicationFlags.FLAGS_NONE
- );
- }
- protected override void startup()
- {
- base.startup();
- Intl.setlocale(LocaleCategory.ALL, "C");
- Gtk.Settings.get_default().gtk_theme_name = "Adwaita";
- Gtk.Settings.get_default().gtk_application_prefer_dark_theme = true;
- Gtk.CssProvider provider = new Gtk.CssProvider();
- Gdk.Screen screen = Gdk.Display.get_default().get_default_screen();
- Gtk.StyleContext.add_provider_for_screen(screen, provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
- provider.load_from_resource("/org/crown/level_editor/css/style.css");
- this.add_action_entries(action_entries, this);
- if (_source_dir == "")
- {
- Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Select folder where to create new project..."
- , null
- , FileChooserAction.SELECT_FOLDER
- , "Cancel"
- , ResponseType.CANCEL
- , "Select"
- , ResponseType.ACCEPT
- );
- if (fcd.run() != (int)ResponseType.ACCEPT)
- {
- fcd.destroy();
- return;
- }
- string dir = fcd.get_filename();
- fcd.destroy();
- if (GLib.FileUtils.test(dir, FileTest.IS_REGULAR))
- {
- stdout.printf("Source directory can't be a regular file\n");
- return;
- }
- _source_dir = dir;
- _create_initial_files = true;
- }
- _compiler = new ConsoleClient();
- _compiler.connected.connect(on_compiler_connected);
- _compiler.disconnected.connect(on_compiler_disconnected_unexpected);
- _compiler.message_received.connect(on_message_received);
- _data_compiler = new DataCompiler(_compiler);
- _project = new Project(_data_compiler);
- _project.load(_source_dir, _toolchain_dir);
- if (_create_initial_files)
- _project.create_initial_files();
- _database = new Database();
- _editor = new ConsoleClient();
- _editor.connected.connect(on_editor_connected);
- _editor.disconnected.connect(on_editor_disconnected_unexpected);
- _editor.message_received.connect(on_message_received);
- _game = new ConsoleClient();
- _game.connected.connect(on_game_connected);
- _game.disconnected.connect(on_game_disconnected);
- _game.message_received.connect(on_message_received);
- _level = new Level(_database, _editor, _project);
- // Global paths
- _config_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_config_dir(), "crown"));
- _settings_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "settings.sjson"));
- // Editor state
- _grid_size = 1.0;
- _rotation_snap = 15.0;
- _show_grid = true;
- _snap_to_grid = true;
- _debug_render_world = false;
- _debug_physics_world = false;
- _tool_type = ToolType.MOVE;
- _snap_mode = SnapMode.RELATIVE;
- _reference_system = ReferenceSystem.LOCAL;
- // Engine connections
- _compiler_process = null;
- _editor_process = null;
- _game_process = null;
- _project_store = new ProjectStore(_project);
- // Widgets
- _combo = new Gtk.ComboBoxText();
- _combo.append("editor", "Editor");
- _combo.append("game", "Game");
- _combo.set_active_id("editor");
- _console_view = new ConsoleView(_project, _combo);
- _project_browser = new ProjectBrowser(_project, _project_store);
- _level_treeview = new LevelTreeView(_database, _level);
- _level_layers_treeview = new LevelLayersTreeView(_database, _level);
- _properties_view = new PropertiesView(_level, _project_store);
- _project_slide = new Slide();
- _editor_slide = new Slide();
- _inspector_slide = new Slide();
- Gtk.Builder builder = new Gtk.Builder.from_resource("/org/crown/level_editor/ui/toolbar.ui");
- _toolbar = builder.get_object("toolbar") as Gtk.Toolbar;
- _toolbar_run = builder.get_object("run") as Gtk.ToolButton;
- _editor_view_overlay = new Gtk.Overlay();
- _editor_view_overlay.add_overlay(_toolbar);
- _resource_popover = new Gtk.Popover(_toolbar);
- _resource_popover.delete_event.connect(() => { _resource_popover.hide(); return true; });
- _resource_popover.modal = true;
- _preferences_dialog = new PreferencesDialog(this);
- _preferences_dialog.set_transient_for(this.active_window);
- _preferences_dialog.delete_event.connect(() => { _preferences_dialog.hide(); return true; });
- _resource_chooser = new ResourceChooser(_project, _project_store, true);
- _resource_chooser.resource_selected.connect(on_resource_browser_resource_selected);
- _resource_chooser.resource_selected.connect(() => { _resource_popover.hide(); });
- _resource_popover.add(_resource_chooser);
- _level_tree_view_notebook = new Notebook();
- _level_tree_view_notebook.show_border = false;
- _level_tree_view_notebook.append_page(_level_treeview, new Gtk.Image.from_icon_name("level-tree", IconSize.SMALL_TOOLBAR));
- _level_tree_view_notebook.append_page(_level_layers_treeview, new Gtk.Image.from_icon_name("level-layers", IconSize.SMALL_TOOLBAR));
- _editor_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
- _editor_pane.pack1(_project_slide, false, false);
- _editor_pane.pack2(_editor_slide, true, false);
- _editor_pane.set_position(210);
- _content_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
- _content_pane.pack1(_editor_pane, true, false);
- _content_pane.pack2(_console_view, false, false);
- _content_pane.set_position(500);
- _inspector_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
- _inspector_pane.pack1(_level_tree_view_notebook, true, false);
- _inspector_pane.pack2(_properties_view, false, false);
- _inspector_pane.set_position(250);
- _main_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
- _main_pane.pack1(_content_pane, true, false);
- _main_pane.pack2(_inspector_slide, false, false);
- _main_pane.set_position(WINDOW_DEFAULT_WIDTH - 375);
- _statusbar = new Statusbar();
- _main_vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
- _main_vbox.pack_start(_main_pane, true, true, 0);
- _main_vbox.pack_start(_statusbar, false, false, 0);
- _file_filter = new Gtk.FileFilter();
- _file_filter.set_filter_name("Level (*.level)");
- _file_filter.add_pattern("*.level");
- if (_level_resource != "")
- {
- string level_path = Path.build_filename(_project.source_dir(), _level_resource + ".level");
- if (!GLib.FileUtils.test(level_path, FileTest.EXISTS) || !GLib.FileUtils.test(level_path, FileTest.IS_REGULAR))
- {
- stdout.printf("Level resource '%s' does not exist.\n", _level_resource);
- return;
- }
- _level.load(level_path);
- }
- else
- {
- _level.load_empty_level();
- }
- try
- {
- _config_dir.make_directory();
- }
- catch (Error e)
- {
- // Nobody cares
- }
- load_settings();
- restart_compiler();
- }
- public void load_settings()
- {
- Hashtable settings = SJSON.load(_settings_file.get_path());
- _preferences_dialog.load(settings.has_key("preferences") ? (Hashtable)settings["preferences"] : new Hashtable());
- }
- public void save_settings()
- {
- Hashtable preferences = new Hashtable();
- _preferences_dialog.save(preferences);
- Hashtable settings = new Hashtable();
- settings["preferences"] = preferences;
- SJSON.save(settings, _settings_file.get_path());
- }
- protected override void activate()
- {
- if (_source_dir == "")
- return;
- if (this.active_window == null)
- {
- LevelEditorWindow win = new LevelEditorWindow(this);
- win.add(_main_vbox);
- try
- {
- win.icon = IconTheme.get_default().load_icon("pepper", 48, 0);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- this.active_window.show_all();
- }
- protected override bool local_command_line(ref unowned string[] args, out int exit_status)
- {
- if (args.length > 1)
- {
- if (!GLib.FileUtils.test(args[1], FileTest.EXISTS) || !GLib.FileUtils.test(args[1], FileTest.IS_DIR))
- {
- stdout.printf("Source directory does not exist or it is not a directory\n");
- exit_status = 1;
- return true;
- }
- _source_dir = args[1];
- }
- if (args.length > 2)
- {
- // Validation is done below after the Project object instantiation
- _level_resource = args[2];
- }
- if (args.length > 3)
- {
- if (!GLib.FileUtils.test(args[3], FileTest.EXISTS) || !GLib.FileUtils.test(args[3], FileTest.IS_DIR))
- {
- stdout.printf("Toolchain directory does not exist or it is not a directory\n");
- exit_status = 1;
- return true;
- }
- _toolchain_dir = args[3];
- }
- else
- {
- bool found = false;
- /// More desirable paths come first
- string toolchain_paths[] =
- {
- "../..",
- "../../../samples"
- };
- for (int i = 0; i < toolchain_paths.length; ++i)
- {
- string path = Path.build_filename(toolchain_paths[i], "core");
- // Try to locate the toolchain directory
- if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR))
- {
- _toolchain_dir = toolchain_paths[i];
- found = true;
- break;
- }
- }
- if (!found)
- {
- stdout.printf("Unable to find the toolchain directory\n");
- exit_status = 1;
- return true;
- }
- }
- exit_status = 0;
- return false;
- }
- protected override int command_line(ApplicationCommandLine command_line)
- {
- this.activate();
- return 0;
- }
- public ConsoleClient? current_selected_client()
- {
- if (_combo.get_active_id() == "editor")
- return _editor;
- else if (_combo.get_active_id() == "game")
- return _game;
- else
- return null;
- }
- private void on_resource_browser_resource_selected(string type, string name)
- {
- _editor.send_script(LevelEditorApi.set_placeable(type, name));
- activate_action("tool", new GLib.Variant.string("place"));
- }
- private void on_compiler_connected(string address, int port)
- {
- _console_view.logi("editor", "Connected to data_compiler@%s:%d".printf(address, port));
- _compiler.receive_async();
- }
- private void on_compiler_disconnected()
- {
- _console_view.logi("editor", "Disconnected from data_compiler");
- }
- private void on_compiler_disconnected_unexpected()
- {
- on_compiler_disconnected();
- stop_game();
- stop_editor();
- // Reset the callback
- _data_compiler.finished(false);
- _project_slide.show_widget(compiler_crashed_label());
- _editor_slide.show_widget(compiler_crashed_label());
- _inspector_slide.show_widget(compiler_crashed_label());
- }
- private void on_editor_connected(string address, int port)
- {
- _console_view.logi("editor", "Connected to level_editor@%s:%d".printf(address, port));
- _editor.receive_async();
- }
- private void on_editor_disconnected()
- {
- _console_view.logi("editor", "Disconnected from editor");
- }
- private void on_editor_disconnected_unexpected()
- {
- on_editor_disconnected();
- Gtk.Label label = new Gtk.Label(null);
- label.set_markup("Something went wrong.\rTry to <a href=\"restart\">restart</a> this view.");
- label.activate_link.connect(() => {
- activate_action("restart", null);
- return true;
- });
- _editor_slide.show_widget(label);
- }
- private void on_game_connected(string address, int port)
- {
- _console_view.logi("editor", "Connected to game@%s:%d".printf(address, port));
- _game.receive_async();
- }
- private void on_game_disconnected()
- {
- _console_view.logi("editor", "Disconnected from game");
- _toolbar_run.icon_name = "game-run";
- _project.delete_garbage();
- _combo.set_active_id("editor");
- }
- private void on_message_received(ConsoleClient client, uint8[] json)
- {
- Hashtable msg = JSON.decode(json) as Hashtable;
- string msg_type = msg["type"] as string;
- if (msg_type == "message")
- {
- _console_view.log((string)msg["system"], (string)msg["message"], (string)msg["severity"]);
- }
- else if (msg_type == "add_file")
- {
- string path = (string)msg["path"];
- _project.add_file(path);
- }
- else if (msg_type == "remove_file")
- {
- string path = (string)msg["path"];
- _project.remove_file(path);
- }
- else if (msg_type == "add_tree")
- {
- string path = (string)msg["path"];
- _project.add_tree(path);
- }
- else if (msg_type == "remove_tree")
- {
- string path = (string)msg["path"];
- _project.remove_tree(path);
- }
- else if (msg_type == "compile")
- {
- // Guid id = Guid.parse((string)msg["id"]);
- if (msg.has_key("start"))
- {
- // FIXME
- }
- else if (msg.has_key("success"))
- {
- _data_compiler.finished((bool)msg["success"]);
- }
- }
- else if (msg_type == "unit_spawned")
- {
- string id = (string) msg["id"];
- string name = (string) msg["name"];
- ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
- ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
- ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
- _level.on_unit_spawned(Guid.parse(id)
- , name
- , Vector3.from_array(pos)
- , Quaternion.from_array(rot)
- , Vector3.from_array(scl)
- );
- }
- else if (msg_type == "sound_spawned")
- {
- string id = (string) msg["id"];
- string name = (string) msg["name"];
- ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
- ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
- ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
- double range = (double) msg["range"];
- double volume = (double) msg["volume"];
- bool loop = (bool) msg["loop"];
- _level.on_sound_spawned(Guid.parse(id)
- , name
- , Vector3.from_array(pos)
- , Quaternion.from_array(rot)
- , Vector3.from_array(scl)
- , range
- , volume
- , loop
- );
- }
- else if (msg_type == "move_objects")
- {
- Hashtable ids = (Hashtable)msg["ids"];
- Hashtable new_positions = (Hashtable)msg["new_positions"];
- Hashtable new_rotations = (Hashtable)msg["new_rotations"];
- Hashtable new_scales = (Hashtable)msg["new_scales"];
- ArrayList<string> keys = new ArrayList<string>.wrap(ids.keys.to_array());
- keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
- Guid[] n_ids = new Guid[keys.size];
- Vector3[] n_positions = new Vector3[keys.size];
- Quaternion[] n_rotations = new Quaternion[keys.size];
- Vector3[] n_scales = new Vector3[keys.size];
- for (int i = 0; i < keys.size; ++i)
- {
- string k = keys[i];
- n_ids[i] = Guid.parse((string)ids[k]);
- n_positions[i] = Vector3.from_array((ArrayList<Value?>)(new_positions[k]));
- n_rotations[i] = Quaternion.from_array((ArrayList<Value?>)new_rotations[k]);
- n_scales[i] = Vector3.from_array((ArrayList<Value?>)new_scales[k]);
- }
- _level.on_move_objects(n_ids, n_positions, n_rotations, n_scales);
- }
- else if (msg_type == "selection")
- {
- Hashtable objects = (Hashtable)msg["objects"];
- ArrayList<string> keys = new ArrayList<string>.wrap(objects.keys.to_array());
- keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
- Guid[] ids = new Guid[keys.size];
- for (int i = 0; i < keys.size; ++i)
- {
- string k = keys[i];
- ids[i] = Guid.parse((string)objects[k]);
- }
- _level.on_selection(ids);
- }
- else if (msg_type == "error")
- {
- _console_view.loge("editor", "Error: " + (string)msg["message"]);
- }
- else
- {
- _console_view.loge("editor", "Unknown message type: " + msg_type);
- }
- // Receive next message
- client.receive_async();
- }
- private void send_state()
- {
- StringBuilder sb = new StringBuilder();
- sb.append(LevelEditorApi.set_grid_size(_grid_size));
- sb.append(LevelEditorApi.set_rotation_snap(_rotation_snap));
- sb.append(LevelEditorApi.enable_show_grid(_show_grid));
- sb.append(LevelEditorApi.enable_snap_to_grid(_snap_to_grid));
- sb.append(LevelEditorApi.enable_debug_render_world(_debug_render_world));
- sb.append(LevelEditorApi.enable_debug_physics_world(_debug_physics_world));
- sb.append(LevelEditorApi.set_tool_type(_tool_type));
- sb.append(LevelEditorApi.set_snap_mode(_snap_mode));
- sb.append(LevelEditorApi.set_reference_system(_reference_system));
- _editor.send_script(sb.str);
- }
- private bool on_button_press(EventButton ev)
- {
- return true;
- }
- private bool on_button_release(EventButton ev)
- {
- return true;
- }
- Gtk.Widget starting_compiler_label()
- {
- return new Gtk.Label("Compiling resources, please wait...");
- }
- Gtk.Widget compiler_crashed_label()
- {
- Gtk.Label label = new Gtk.Label(null);
- label.set_markup("Data Compiler disconnected.\rTry to <a href=\"restart\">restart</a> compiler to continue.");
- label.activate_link.connect(() => {
- restart_compiler();
- return true;
- });
- return label;
- }
- Gtk.Widget compiler_failed_compilation_label()
- {
- Gtk.Label label = new Gtk.Label(null);
- label.set_markup("Data compilation failed.\rFix errors and <a href=\"restart\">restart</a> compiler to continue.");
- label.activate_link.connect(() => {
- restart_compiler();
- return true;
- });
- return label;
- }
- private void restart_compiler()
- {
- stop_compiler();
- _project.reset();
- _project_slide.show_widget(starting_compiler_label());
- _editor_slide.show_widget(starting_compiler_label());
- _inspector_slide.show_widget(starting_compiler_label());
- string args[] =
- {
- ENGINE_EXE,
- "--source-dir", _project.source_dir(),
- "--data-dir", _project.data_dir(),
- "--map-source-dir", "core", _project.toolchain_dir(),
- "--server",
- "--wait-console",
- null
- };
- GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
- sl.set_cwd(ENGINE_DIR);
- try
- {
- _compiler_process = sl.spawnv(args);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- // It is an error if compiler disconnects after here.
- _compiler.disconnected.disconnect(on_compiler_disconnected);
- _compiler.disconnected.connect(on_compiler_disconnected_unexpected);
- for (int tries = 0; !_compiler.is_connected() && tries < 5; ++tries)
- {
- _compiler.connect("127.0.0.1", CROWN_DEFAULT_SERVER_PORT);
- GLib.Thread.usleep(250*1000);
- }
- _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
- if (_data_compiler.compile.end(res))
- {
- restart_editor();
- _project_slide.show_widget(_project_browser);
- _inspector_slide.show_widget(_inspector_pane);
- }
- else
- {
- _project_slide.show_widget(compiler_failed_compilation_label());
- _editor_slide.show_widget(compiler_failed_compilation_label());
- _inspector_slide.show_widget(compiler_failed_compilation_label());
- }
- });
- }
- private void stop_compiler()
- {
- stop_game();
- stop_editor();
- if (_compiler != null)
- {
- // Explicit call to this function should not produce error messages.
- _compiler.disconnected.disconnect(on_compiler_disconnected_unexpected);
- _compiler.disconnected.connect(on_compiler_disconnected);
- _compiler.send(DataCompilerApi.quit());
- _compiler.close();
- }
- if (_compiler_process != null)
- {
- try
- {
- _compiler_process.wait();
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- }
- private void start_editor(uint window_xid)
- {
- if (window_xid == 0)
- return;
- string args[] =
- {
- ENGINE_EXE,
- "--data-dir", _project.data_dir(),
- "--boot-dir", LEVEL_EDITOR_BOOT_DIR,
- "--parent-window", window_xid.to_string(),
- "--wait-console",
- null
- };
- GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
- sl.set_cwd(ENGINE_DIR);
- try
- {
- _editor_process = sl.spawnv(args);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- // It is an error if editor disconnects after here.
- _editor.disconnected.disconnect(on_editor_disconnected);
- _editor.disconnected.connect(on_editor_disconnected_unexpected);
- for (int tries = 0; !_editor.is_connected() && tries < 10; ++tries)
- {
- _editor.connect("127.0.0.1", 10001);
- GLib.Thread.usleep(500*1000);
- }
- _level.send_level();
- send_state();
- _preferences_dialog.apply();
- }
- private void stop_editor()
- {
- _resource_chooser.stop_editor();
- if (_editor != null)
- {
- // Explicit call to this function should not produce error messages.
- _editor.disconnected.disconnect(on_editor_disconnected_unexpected);
- _editor.disconnected.connect(on_editor_disconnected);
- _editor.send_script("Device.quit()");
- _editor.close();
- }
- if (_editor_process != null)
- {
- try
- {
- _editor_process.wait();
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- _editor_slide.show_widget(new Gtk.Label("Disconnected."));
- }
- private void restart_editor()
- {
- stop_editor();
- if (_editor_view != null)
- {
- _editor_view_overlay.remove(_editor_view);
- _editor_view = null;
- }
- _editor_view = new EditorView(_editor);
- _editor_view.realized.connect(on_editor_view_realized);
- _editor_view.button_press_event.connect(on_button_press);
- _editor_view.button_release_event.connect(on_button_release);
- _editor_view_overlay.add(_editor_view);
- _editor_slide.show_widget(_editor_view_overlay);
- _resource_chooser.restart_editor();
- }
- private void start_game(StartGame sg)
- {
- _project.dump_test_level(_database);
- _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
- if (_data_compiler.compile.end(res))
- {
- string args[] =
- {
- ENGINE_EXE,
- "--data-dir", _project.data_dir(),
- "--console-port", "12345",
- "--wait-console",
- "--lua-string", sg == StartGame.TEST ? "TEST=true" : "",
- null
- };
- GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
- sl.set_cwd(ENGINE_DIR);
- try
- {
- _game_process = sl.spawnv(args);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- for (int tries = 0; !_game.is_connected() && tries < 10; ++tries)
- {
- _game.connect("127.0.0.1", 12345);
- GLib.Thread.usleep(500*1000);
- }
- _combo.set_active_id("game");
- }
- });
- }
- private void stop_game()
- {
- if (_game != null)
- {
- _game.send_script("Device.quit()");
- _game.close();
- }
- if (_game_process != null)
- {
- try
- {
- _game_process.wait();
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- }
- private void deploy_game()
- {
- Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Select destination directory..."
- , this.active_window
- , FileChooserAction.SELECT_FOLDER
- , "Cancel"
- , ResponseType.CANCEL
- , "Open"
- , ResponseType.ACCEPT
- );
- if (fcd.run() == (int)ResponseType.ACCEPT)
- {
- GLib.File data_dir = File.new_for_path(fcd.get_filename());
- string args[] =
- {
- ENGINE_EXE,
- "--source-dir", _project.source_dir(),
- "--map-source-dir", "core", _project.toolchain_dir(),
- "--data-dir", data_dir.get_path(),
- "--compile",
- null
- };
- GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
- sl.set_cwd(ENGINE_DIR);
- try
- {
- GLib.Subprocess compiler = sl.spawnv(args);
- compiler.wait();
- if (compiler.get_exit_status() == 0)
- {
- GLib.File engine_exe = File.new_for_path(ENGINE_EXE);
- GLib.File engine_exe_dest = File.new_for_path(Path.build_filename(data_dir.get_path(), ENGINE_EXE));
- engine_exe.copy(engine_exe_dest, FileCopyFlags.OVERWRITE);
- _console_view.logi("editor", "Project deployed to `%s`".printf(data_dir.get_path()));
- }
- }
- catch (Error e)
- {
- _console_view.logi("editor", "%s".printf(e.message));
- _console_view.logi("editor", "Failed to deploy project");
- }
- }
- fcd.destroy();
- }
- private void on_editor_view_realized()
- {
- start_editor(_editor_view.window_id);
- }
- private void on_tool_changed(GLib.SimpleAction action, GLib.Variant? param)
- {
- string name = param.get_string();
- if (name == "place")
- _tool_type = ToolType.PLACE;
- else if (name == "move")
- _tool_type = ToolType.MOVE;
- else if (name == "rotate")
- _tool_type = ToolType.ROTATE;
- else if (name == "scale")
- _tool_type = ToolType.SCALE;
- send_state();
- action.set_state(param);
- }
- private void on_snap_mode_changed(GLib.SimpleAction action, GLib.Variant? param)
- {
- string name = param.get_string();
- if (name == "relative")
- _snap_mode = SnapMode.RELATIVE;
- else if (name == "absolute")
- _snap_mode = SnapMode.ABSOLUTE;
- send_state();
- action.set_state(param);
- }
- private void on_reference_system_changed(GLib.SimpleAction action, GLib.Variant? param)
- {
- string name = param.get_string();
- if (name == "local")
- _reference_system = ReferenceSystem.LOCAL;
- else if (name == "world")
- _reference_system = ReferenceSystem.WORLD;
- send_state();
- action.set_state(param);
- }
- private void on_grid_changed(GLib.SimpleAction action, GLib.Variant? param)
- {
- _grid_size = float.parse(param.get_string());
- send_state();
- action.set_state(param);
- }
- private void on_rotation_snap_changed(GLib.SimpleAction action, GLib.Variant? param)
- {
- _rotation_snap = float.parse(param.get_string());
- send_state();
- action.set_state(param);
- }
- private void new_level()
- {
- _level.load_empty_level();
- _level.send_level();
- }
- private void load_level(string level)
- {
- string filename = level;
- if (filename == "")
- {
- Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Open Level..."
- , this.active_window
- , FileChooserAction.OPEN
- , "Cancel"
- , ResponseType.CANCEL
- , "Open"
- , ResponseType.ACCEPT
- );
- fcd.add_filter(_file_filter);
- fcd.set_current_folder(_project.source_dir());
- if (fcd.run() == (int)ResponseType.ACCEPT)
- filename = fcd.get_filename();
- fcd.destroy();
- }
- if (filename == "")
- return;
- if (!_project.path_is_within_dir(filename, _project.source_dir()))
- {
- _console_view.loge("editor", "File must be within `%s`".printf(_project.source_dir()));
- return;
- }
- if (filename.has_suffix(".level") && filename != _level._filename)
- {
- _level.load(filename);
- _level.send_level();
- send_state();
- }
- }
- private void load_project()
- {
- Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Open Project..."
- , this.active_window
- , FileChooserAction.SELECT_FOLDER
- , "Cancel"
- , ResponseType.CANCEL
- , "Open"
- , ResponseType.ACCEPT
- );
- int rt = fcd.run();
- if (rt != (int)ResponseType.ACCEPT)
- {
- fcd.destroy();
- return;
- }
- string filename = fcd.get_filename();
- fcd.destroy();
- if (filename == _project.source_dir())
- return;
- _console_view.logi("editor", "Loading `%s`...".printf(filename));
- _project.load(filename, _project.toolchain_dir());
- _level.load_empty_level();
- restart_compiler();
- }
- private bool save_as(string? filename)
- {
- string path;
- if (filename != null)
- {
- path = filename;
- }
- else
- {
- Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Save As..."
- , this.active_window
- , FileChooserAction.SAVE
- , "Cancel"
- , ResponseType.CANCEL
- , "Save"
- , ResponseType.ACCEPT
- );
- fcd.add_filter(_file_filter);
- fcd.set_current_folder(_project.source_dir());
- int rt = fcd.run();
- if (rt != (int)ResponseType.ACCEPT)
- {
- fcd.destroy();
- return false;
- }
- path = fcd.get_filename();
- fcd.destroy();
- }
- if (!_project.path_is_within_dir(path, _project.source_dir()))
- {
- _console_view.loge("editor", "File must be within `%s`".printf(_project.source_dir()));
- return false;
- }
- _level.save(path.has_suffix(".level") ? path : path + ".level");
- _statusbar.set_temporary_message("Saved %s".printf(_level._filename));
- return true;
- }
- private bool save()
- {
- return save_as(_level._filename);
- }
- private bool save_timeout()
- {
- if (_level._filename != null)
- save();
- return true;
- }
- public void close_all()
- {
- save_settings();
- if (_save_timer_id > 0)
- GLib.Source.remove(_save_timer_id);
- if (_resource_chooser != null)
- _resource_chooser.destroy();
- if (_preferences_dialog != null)
- _preferences_dialog.destroy();
- stop_compiler();
- }
- protected override void shutdown()
- {
- base.shutdown();
- }
- // Returns true if the level has been saved or the user decided it
- // should be discarded.
- public bool should_quit()
- {
- if (!_database.changed())
- return true;
- Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
- , Gtk.DialogFlags.MODAL
- , Gtk.MessageType.WARNING
- , Gtk.ButtonsType.NONE
- , "File changed, save?"
- );
- md.add_button("Quit _without Saving", ResponseType.NO);
- md.add_button("_Cancel", ResponseType.CANCEL);
- md.add_button("_Save", ResponseType.YES);
- md.set_default_response(ResponseType.YES);
- int rt = md.run();
- md.destroy();
- if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
- return true;
- return false;
- }
- private void on_new_level(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (!_database.changed())
- {
- new_level();
- send_state();
- return;
- }
- Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
- , Gtk.DialogFlags.MODAL
- , Gtk.MessageType.WARNING
- , Gtk.ButtonsType.NONE
- , "File changed, save?"
- );
- md.add_button("New _without Saving", ResponseType.NO);
- md.add_button("_Cancel", ResponseType.CANCEL);
- md.add_button("_Save", ResponseType.YES);
- md.set_default_response(ResponseType.YES);
- int rt = md.run();
- md.destroy();
- if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
- {
- new_level();
- send_state();
- }
- }
- private void on_open_level(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (!_database.changed())
- {
- load_level(param.get_string());
- return;
- }
- Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
- , Gtk.DialogFlags.MODAL
- , Gtk.MessageType.WARNING
- , Gtk.ButtonsType.NONE
- , "File changed, save?"
- );
- md.add_button("Open _without Saving", ResponseType.NO);
- md.add_button("_Cancel", ResponseType.CANCEL);
- md.add_button("_Save", ResponseType.YES);
- md.set_default_response(ResponseType.YES);
- int rt = md.run();
- md.destroy();
- if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
- load_level(param.get_string());
- }
- private void on_open_project(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (!_database.changed())
- {
- load_project();
- return;
- }
- Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
- , Gtk.DialogFlags.MODAL
- , Gtk.MessageType.WARNING
- , Gtk.ButtonsType.NONE
- , "File changed, save?"
- );
- md.add_button("Open _without Saving", ResponseType.NO);
- md.add_button("_Cancel", ResponseType.CANCEL);
- md.add_button("_Save", ResponseType.YES);
- md.set_default_response(ResponseType.YES);
- int rt = md.run();
- md.destroy();
- if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
- load_project();
- }
- private void on_save(GLib.SimpleAction action, GLib.Variant? param)
- {
- save();
- }
- private void on_save_as(GLib.SimpleAction action, GLib.Variant? param)
- {
- save_as(null);
- }
- private void on_import(GLib.SimpleAction action, GLib.Variant? param)
- {
- _project.import(null, this.active_window);
- }
- private void on_preferences(GLib.SimpleAction action, GLib.Variant? param)
- {
- _preferences_dialog.show_all();
- }
- private void on_deploy(GLib.SimpleAction action, GLib.Variant? param)
- {
- deploy_game();
- }
- private void on_quit(GLib.SimpleAction action, GLib.Variant? param)
- {
- this.active_window.close();
- }
- private void on_show_grid(GLib.SimpleAction action, GLib.Variant? param)
- {
- _show_grid = !action.get_state().get_boolean();
- send_state();
- action.set_state(new GLib.Variant.boolean(_show_grid));
- }
- private void on_custom_grid()
- {
- Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Grid size"
- , this.active_window
- , DialogFlags.MODAL
- , "Cancel"
- , ResponseType.CANCEL
- , "Ok"
- , ResponseType.OK
- , null
- );
- EntryDouble sb = new EntryDouble(_grid_size, 0.1, 1000);
- sb.activate.connect(() => { dg.response(ResponseType.OK); });
- dg.get_content_area().add(sb);
- dg.skip_taskbar_hint = true;
- dg.show_all();
- if (dg.run() == (int)ResponseType.OK)
- {
- _grid_size = sb.value;
- send_state();
- }
- dg.destroy();
- }
- private void on_rotation_snap(GLib.SimpleAction action, GLib.Variant? param)
- {
- Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Rotation snap"
- , this.active_window
- , DialogFlags.MODAL
- , "Cancel"
- , ResponseType.CANCEL
- , "Ok"
- , ResponseType.OK
- , null
- );
- EntryDouble sb = new EntryDouble(_rotation_snap, 1.0, 180.0);
- sb.activate.connect(() => { dg.response(ResponseType.OK); });
- dg.get_content_area().add(sb);
- dg.skip_taskbar_hint = true;
- dg.show_all();
- if (dg.run() == (int)ResponseType.OK)
- {
- _rotation_snap = sb.value;
- send_state();
- }
- dg.destroy();
- }
- private void on_create_primitive(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (action.name == "primitive-cube")
- _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cube"));
- else if (action.name == "primitive-sphere")
- _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/sphere"));
- else if (action.name == "primitive-cone")
- _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cone"));
- else if (action.name == "primitive-cylinder")
- _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cylinder"));
- else if (action.name == "primitive-plane")
- _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/plane"));
- else if (action.name == "camera")
- _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/camera"));
- else if (action.name == "light")
- _editor.send_script(LevelEditorApi.set_placeable("unit", "core/units/light"));
- else if (action.name == "sound-source")
- _editor.send_script(LevelEditorApi.set_placeable("sound", ""));
- activate_action("tool", new GLib.Variant.string("place"));
- }
- private void on_camera_view(GLib.SimpleAction action, GLib.Variant? param)
- {
- string name = param.get_string();
- if (name == "perspective")
- _editor.send_script("LevelEditor:camera_view_perspective()");
- else if (name == "front")
- _editor.send_script("LevelEditor:camera_view_front()");
- else if (name == "back")
- _editor.send_script("LevelEditor:camera_view_back()");
- else if (name == "right")
- _editor.send_script("LevelEditor:camera_view_right()");
- else if (name == "left")
- _editor.send_script("LevelEditor:camera_view_left()");
- else if (name == "top")
- _editor.send_script("LevelEditor:camera_view_top()");
- else if (name == "bottom")
- _editor.send_script("LevelEditor:camera_view_bottom()");
- action.set_state(param);
- }
- private void on_resource_chooser(GLib.SimpleAction action, GLib.Variant? param)
- {
- _resource_popover.show_all();
- }
- private void on_project_browser(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (_project_slide.is_visible())
- {
- _project_slide.hide();
- }
- else
- {
- _project_slide.show_all();
- }
- }
- private void on_console(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (_console_view.is_visible())
- {
- if (_console_view._entry.has_focus)
- _console_view.hide();
- else
- _console_view._entry.grab_focus();
- }
- else
- {
- _console_view.show_all();
- }
- }
- private void on_statusbar(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (_statusbar.is_visible())
- {
- _statusbar.hide();
- }
- else
- {
- _statusbar.show_all();
- }
- }
- private void on_inspector(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (_inspector_slide.is_visible())
- {
- _inspector_slide.hide();
- }
- else
- {
- _inspector_slide.show_all();
- }
- }
- private void on_editor_restart(GLib.SimpleAction action, GLib.Variant? param)
- {
- restart_editor();
- }
- private void on_refresh_lua(GLib.SimpleAction action, GLib.Variant? param)
- {
- _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
- if (_data_compiler.compile.end(res))
- {
- _editor.send(DeviceApi.refresh());
- _game.send(DeviceApi.refresh());
- }
- });
- }
- private void on_snap_to_grid(GLib.SimpleAction action, GLib.Variant? param)
- {
- _snap_to_grid = !action.get_state().get_boolean();
- send_state();
- action.set_state(new GLib.Variant.boolean(_snap_to_grid));
- }
- private void on_debug_render_world(GLib.SimpleAction action, GLib.Variant? param)
- {
- _debug_render_world = !action.get_state().get_boolean();
- send_state();
- action.set_state(new GLib.Variant.boolean(_debug_render_world));
- }
- private void on_debug_physics_world(GLib.SimpleAction action, GLib.Variant? param)
- {
- _debug_physics_world = !action.get_state().get_boolean();
- send_state();
- action.set_state(new GLib.Variant.boolean(_debug_physics_world));
- }
- private void on_run_game(GLib.SimpleAction action, GLib.Variant? param)
- {
- if (_game.is_connected())
- {
- stop_game();
- _toolbar_run.icon_name = "game-run";
- }
- else
- {
- start_game(action.name == "test-level" ? StartGame.TEST : StartGame.NORMAL);
- _toolbar_run.icon_name = "game-stop";
- }
- }
- private void on_undo(GLib.SimpleAction action, GLib.Variant? param)
- {
- int id = _database.undo();
- if (id != -1)
- _statusbar.set_temporary_message("Undo: " + ActionNames[id]);
- }
- private void on_redo(GLib.SimpleAction action, GLib.Variant? param)
- {
- int id = _database.redo();
- if (id != -1)
- _statusbar.set_temporary_message("Redo: " + ActionNames[id]);
- }
- private void on_duplicate(GLib.SimpleAction action, GLib.Variant? param)
- {
- _level.duplicate_selected_objects();
- }
- private void on_delete(GLib.SimpleAction action, GLib.Variant? param)
- {
- _level.destroy_selected_objects();
- }
- private void on_manual(GLib.SimpleAction action, GLib.Variant? param)
- {
- try
- {
- AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION, null);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- private void on_report_issue(GLib.SimpleAction action, GLib.Variant? param)
- {
- try
- {
- AppInfo.launch_default_for_uri("https://github.com/dbartolini/crown/issues", null);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- private void on_open_last_log(GLib.SimpleAction action, GLib.Variant? param)
- {
- File file = File.new_for_path(_project.data_dir() + "/last.log");
- try
- {
- AppInfo.launch_default_for_uri(file.get_uri(), null);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- private void on_changelog(GLib.SimpleAction action, GLib.Variant? param)
- {
- try
- {
- AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION + "/changelog.html", null);
- }
- catch (Error e)
- {
- _console_view.loge("editor", e.message);
- }
- }
- private void on_about(GLib.SimpleAction action, GLib.Variant? param)
- {
- Gtk.AboutDialog dlg = new Gtk.AboutDialog();
- dlg.set_destroy_with_parent(true);
- dlg.set_transient_for(this.active_window);
- dlg.set_modal(true);
- dlg.set_logo_icon_name("pepper");
- dlg.program_name = "Crown Game Engine";
- dlg.version = CROWN_VERSION;
- dlg.website = "https://github.com/dbartolini/crown";
- dlg.copyright = "Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.";
- dlg.license = "Crown Game Engine.\n"
- + "Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.\n"
- + "\n"
- + "This program is free software; you can redistribute it and/or\n"
- + "modify it under the terms of the GNU General Public License\n"
- + "as published by the Free Software Foundation; either version 2\n"
- + "of the License, or (at your option) any later version.\n"
- + "\n"
- + "This program is distributed in the hope that it will be useful,\n"
- + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
- + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
- + "GNU General Public License for more details.\n"
- + "\n"
- + "You should have received a copy of the GNU General Public License\n"
- + "along with this program; if not, write to the Free Software\n"
- + "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
- ;
- dlg.run();
- dlg.destroy();
- }
- public void set_autosave_timer(uint minutes)
- {
- if (_save_timer_id > 0)
- GLib.Source.remove(_save_timer_id);
- _save_timer_id = GLib.Timeout.add_seconds(minutes*60, save_timeout);
- }
- }
- public static GLib.SubprocessFlags subprocess_flags()
- {
- GLib.SubprocessFlags flags = SubprocessFlags.NONE;
- #if !CROWN_DEBUG
- flags |= SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE;
- #endif
- return flags;
- }
- public static int main(string[] args)
- {
- LevelEditorApplication app = new LevelEditorApplication();
- return app.run(args);
- }
- }
|