level_editor.vala 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE-GPLv2
  4. */
  5. using Gdk; // Pixbuf
  6. using Gee;
  7. using Gtk;
  8. namespace Crown
  9. {
  10. public enum ToolType
  11. {
  12. PLACE,
  13. MOVE,
  14. ROTATE,
  15. SCALE
  16. }
  17. public enum SnapMode
  18. {
  19. RELATIVE,
  20. ABSOLUTE
  21. }
  22. public enum ReferenceSystem
  23. {
  24. LOCAL,
  25. WORLD
  26. }
  27. public class LevelEditor : Gtk.Window
  28. {
  29. private Project _project;
  30. // Editor state
  31. private double _grid_size;
  32. private double _rotation_snap;
  33. private bool _show_grid;
  34. private bool _snap_to_grid;
  35. private bool _debug_render_world;
  36. private bool _debug_physics_world;
  37. private ToolType _tool_type;
  38. private SnapMode _snap_mode;
  39. private ReferenceSystem _reference_system;
  40. // Engine connections
  41. private GLib.Subprocess _compiler_process;
  42. private GLib.Subprocess _engine_process;
  43. private GLib.Subprocess _game_process;
  44. private ConsoleClient _compiler;
  45. private ConsoleClient _engine;
  46. // Level data
  47. private Database _db;
  48. private Level _level;
  49. private string _level_filename;
  50. private ResourceCompiler _resource_compiler;
  51. // Widgets
  52. private StartingCompiler _starting_compiler;
  53. private ConsoleView _console_view;
  54. private EngineView _engine_view;
  55. private LevelTreeView _level_treeview;
  56. private LevelLayersTreeView _level_layers_treeview;
  57. private PropertiesView _properties_view;
  58. /*
  59. private GraphStore _graph_store;
  60. private GraphView _graph_view;
  61. */
  62. private PreferencesDialog _preferences_dialog;
  63. private Gtk.Alignment _alignment_engine;
  64. private Gtk.Alignment _alignment_level_tree_view;
  65. private Gtk.Alignment _alignment_properties_view;
  66. private Gtk.ActionGroup _action_group;
  67. private Gtk.UIManager _ui_manager;
  68. private Gtk.Toolbar _toolbar;
  69. private Gtk.Paned _pane_left;
  70. private Gtk.Paned _pane_right;
  71. private Gtk.Notebook _notebook_left;
  72. private Gtk.Notebook _notebook_right;
  73. private Gtk.Box _vbox;
  74. private Gtk.FileFilter _file_filter;
  75. private ResourceBrowser _resource_browser;
  76. const Gtk.ActionEntry[] action_entries =
  77. {
  78. { "menu-file", null, "_File", null, null, null },
  79. { "new", null, "New", "<ctrl>N", null, on_new },
  80. { "open", null, "Open", "<ctrl>O", null, on_open },
  81. { "save", null, "Save", "<ctrl>S", null, on_save },
  82. { "save-as", null, "Save As...", null, null, on_save_as },
  83. { "import", null, "Import...", null, null, on_import },
  84. { "preferences", null, "Preferences", null, null, on_preferences },
  85. { "quit", null, "Quit", "<ctrl>Q", null, on_quit },
  86. { "menu-edit", null, "_Edit", null, null, null },
  87. { "undo", null, "Undo", "<ctrl>Z", null, on_undo },
  88. { "redo", null, "Redo", "<shift><ctrl>Z", null, on_redo },
  89. { "duplicate", null, "Duplicate", "<ctrl>D", null, on_duplicate },
  90. { "delete", null, "Delete", "<ctrl>K", null, on_delete },
  91. { "menu-grid", null, "Grid", null, null, null },
  92. { "grid-custom", null, "Custom", "G", null, on_custom_grid },
  93. { "menu-rotation-snap", null, "Rotation Snap", null, null, null },
  94. { "rotation-snap-custom", null, "Custom", "H", null, on_rotation_snap },
  95. { "menu-create", null, "Create", null, null, null },
  96. { "menu-primitives", null, "Primitives", null, null, null },
  97. { "primitive-cube", null, "Cube", null, null, on_create_cube },
  98. { "primitive-sphere", null, "Sphere", null, null, on_create_sphere },
  99. { "primitive-cone", null, "Cone", null, null, on_create_cone },
  100. { "primitive-cylinder", null, "Cylinder", null, null, on_create_cylinder },
  101. { "primitive-plane", null, "Plane", null, null, on_create_plane },
  102. { "camera", null, "Camera", null, null, on_create_camera },
  103. { "light", null, "Light", null, null, on_create_light },
  104. { "sound-source", null, "Sound Source", null, null, on_create_sound_source },
  105. { "menu-engine", null, "En_gine", null, null, null },
  106. { "menu-view", null, "View", null, null, null },
  107. { "resource-browser", null, "Resource Browser", "<ctrl>P", null, on_resource_browser },
  108. { "restart", null, "_Restart", null, null, on_engine_restart },
  109. { "reload-lua", null, "Reload Lua", "F7", null, on_reload_lua },
  110. { "menu-run", null, "_Run", null, null, null },
  111. { "game-run", "run", "Run Game", "F5", null, on_run_game },
  112. { "menu-help", null, "Help", null, null, null },
  113. { "manual", null, "Manual", "F1", null, on_manual },
  114. { "report-issue", null, "Report an Issue", null, null, on_report_issue },
  115. { "open-last-log", null, "Open last.log", null, null, on_open_last_log },
  116. { "about", null, "About", null, null, on_about }
  117. };
  118. const Gtk.RadioActionEntry[] grid_entries =
  119. {
  120. { "grid-0.1", null, "0.1m", null, null, 10 },
  121. { "grid-0.2", null, "0.2m", null, null, 20 },
  122. { "grid-0.5", null, "0.5m", null, null, 50 },
  123. { "grid-1", null, "1m", null, null, 100 },
  124. { "grid-2", null, "2m", null, null, 200 },
  125. { "grid-5", null, "5m", null, null, 500 }
  126. };
  127. const RadioActionEntry[] rotation_snap_entries =
  128. {
  129. { "rotation-snap-1", null, "1°", null, null, 1 },
  130. { "rotation-snap-15", null, "15°", null, null, 15 },
  131. { "rotation-snap-30", null, "30°", null, null, 30 },
  132. { "rotation-snap-45", null, "45°", null, null, 45 },
  133. { "rotation-snap-90", null, "90°", null, null, 90 },
  134. { "rotation-snap-180", null, "180°", null, null, 180 }
  135. };
  136. const RadioActionEntry[] tool_entries =
  137. {
  138. { "place", "tool-place", "Place", "Q", "Place", (int)ToolType.PLACE },
  139. { "move", "tool-move", "Move", "W", "Move", (int)ToolType.MOVE },
  140. { "rotate", "tool-rotate", "Rotate", "E", "Rotate", (int)ToolType.ROTATE },
  141. { "scale", "tool-scale", "Scale", "R", "Scale", (int)ToolType.SCALE }
  142. };
  143. const RadioActionEntry[] snap_mode_entries =
  144. {
  145. { "snap-relative", "reference-local", "Relative Snap", null, "Relative Snap", (int)SnapMode.RELATIVE },
  146. { "snap-absolute", "reference-world", "Absolute Snap", null, "Absolute Snap", (int)SnapMode.ABSOLUTE }
  147. };
  148. const RadioActionEntry[] reference_system_entries =
  149. {
  150. { "reference-system-local", "axis-local", "Local Axis", null, "Local Axis", (int)ReferenceSystem.LOCAL },
  151. { "reference-system-world", "axis-world", "World Axis", null, "World Axis", (int)ReferenceSystem.WORLD }
  152. };
  153. const ToggleActionEntry[] snap_to_entries =
  154. {
  155. { "snap-to-grid", "snap-to-grid", "Snap To Grid", "<ctrl>U", "Snap To Grid", on_snap_to_grid, true },
  156. { "grid-show", null, "Show Grid", null, "Show Grid", on_show_grid, true }
  157. };
  158. const ToggleActionEntry[] view_entries =
  159. {
  160. { "debug-render-world", null, "Debug Render World", null, null, on_debug_render_world, false },
  161. { "debug-physics-world", null, "Debug Physics World", null, null, on_debug_physics_world, false }
  162. };
  163. public LevelEditor(Project project)
  164. {
  165. this.title = "Level Editor";
  166. _project = project;
  167. // Editor state
  168. _grid_size = 1.0;
  169. _rotation_snap = 45.0;
  170. _show_grid = true;
  171. _snap_to_grid = true;
  172. _debug_render_world = false;
  173. _debug_physics_world = false;
  174. _tool_type = ToolType.MOVE;
  175. _snap_mode = SnapMode.RELATIVE;
  176. _reference_system = ReferenceSystem.LOCAL;
  177. // Engine connections
  178. _compiler_process = null;
  179. _engine_process = null;
  180. _game_process = null;
  181. _compiler = new ConsoleClient();
  182. _compiler.connected.connect(on_compiler_connected);
  183. _compiler.disconnected.connect(on_compiler_disconnected);
  184. _compiler.message_received.connect(on_message_received);
  185. _engine = new ConsoleClient();
  186. _engine.connected.connect(on_engine_connected);
  187. _engine.disconnected.connect(on_engine_disconnected);
  188. _engine.message_received.connect(on_message_received);
  189. // Level data
  190. _db = new Database();
  191. _level = new Level(_db, _engine, _project.source_dir(), _project.toolchain_dir());
  192. _level_filename = null;
  193. _resource_compiler = new ResourceCompiler(_compiler);
  194. // Widgets
  195. _console_view = new ConsoleView(_engine);
  196. _level_treeview = new LevelTreeView(_db, _level);
  197. _level_layers_treeview = new LevelLayersTreeView(_db, _level);
  198. _properties_view = new PropertiesView(_level);
  199. /*
  200. _graph_store = new GraphStore();
  201. _graph_view = new GraphView(_graph_store);
  202. */
  203. _starting_compiler = new StartingCompiler();
  204. _alignment_engine = new Gtk.Alignment(0, 0, 1, 1);
  205. _alignment_level_tree_view = new Gtk.Alignment(0, 0, 1, 1);
  206. _alignment_properties_view = new Gtk.Alignment(0, 0, 1, 1);
  207. _alignment_engine.add(_starting_compiler);
  208. _alignment_level_tree_view.add(_starting_compiler);
  209. _alignment_properties_view.add(_starting_compiler);
  210. start_compiler();
  211. try
  212. {
  213. Gtk.IconTheme.add_builtin_icon("tool-place", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/tool-place.png"));
  214. Gtk.IconTheme.add_builtin_icon("tool-move", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/tool-move.png"));
  215. Gtk.IconTheme.add_builtin_icon("tool-rotate", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/tool-rotate.png"));
  216. Gtk.IconTheme.add_builtin_icon("tool-scale", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/tool-scale.png"));
  217. Gtk.IconTheme.add_builtin_icon("axis-local", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/axis-local.png"));
  218. Gtk.IconTheme.add_builtin_icon("axis-world", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/axis-world.png"));
  219. Gtk.IconTheme.add_builtin_icon("snap-to-grid", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/snap-to-grid.png"));
  220. Gtk.IconTheme.add_builtin_icon("reference-local", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/reference-local.png"));
  221. Gtk.IconTheme.add_builtin_icon("reference-world", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/reference-world.png"));
  222. Gtk.IconTheme.add_builtin_icon("run", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/run.png"));
  223. Gtk.IconTheme.add_builtin_icon("level-tree", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/level-tree.png"));
  224. Gtk.IconTheme.add_builtin_icon("level-layers", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/level-layers.png"));
  225. Gtk.IconTheme.add_builtin_icon("layer-visible", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/layer-visible.png"));
  226. Gtk.IconTheme.add_builtin_icon("layer-locked", 16, new Pixbuf.from_resource("/org/pepper/icons/theme/layer-locked.png"));
  227. }
  228. catch (Error e)
  229. {
  230. stderr.printf(e.message);
  231. }
  232. _action_group = new Gtk.ActionGroup("group");
  233. _action_group.add_actions(action_entries, this);
  234. _action_group.add_radio_actions(grid_entries, (int)(_grid_size*100.0), this.on_grid_changed);
  235. _action_group.add_radio_actions(rotation_snap_entries, (int)_rotation_snap, this.on_rotation_snap_changed);
  236. _action_group.add_radio_actions(tool_entries, (int)_tool_type, on_tool_changed);
  237. _action_group.add_radio_actions(snap_mode_entries, (int)_snap_mode, on_snap_mode_changed);
  238. _action_group.add_radio_actions(reference_system_entries, (int)_reference_system, on_reference_system_changed);
  239. _action_group.add_toggle_actions(snap_to_entries, this);
  240. _action_group.add_toggle_actions(view_entries, this);
  241. _ui_manager = new UIManager();
  242. try
  243. {
  244. _ui_manager.add_ui_from_resource("/org/pepper/level_editor_menu.xml");
  245. _ui_manager.insert_action_group(_action_group, 0);
  246. add_accel_group(_ui_manager.get_accel_group());
  247. }
  248. catch (Error e)
  249. {
  250. error(e.message);
  251. }
  252. _toolbar = _ui_manager.get_widget("/toolbar") as Toolbar;
  253. _toolbar.set_icon_size(Gtk.IconSize.SMALL_TOOLBAR);
  254. _toolbar.set_style(Gtk.ToolbarStyle.ICONS);
  255. _pane_left = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  256. _pane_left.pack1(_alignment_engine, true, true);
  257. _pane_left.pack2(_console_view, true, true);
  258. Gtk.Box vb = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  259. vb.pack_start(_toolbar, false, false, 0);
  260. vb.pack_start(_pane_left, true, true, 0);
  261. _notebook_right = new Notebook();
  262. _notebook_right.show_border = false;
  263. _notebook_right.append_page(_level_treeview, new Gtk.Image.from_icon_name("level-tree", IconSize.SMALL_TOOLBAR));
  264. _notebook_right.append_page(_level_layers_treeview, new Gtk.Image.from_icon_name("level-layers", IconSize.SMALL_TOOLBAR));
  265. Gtk.Paned rb = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  266. rb.pack1(_notebook_right, true, true);
  267. rb.pack2(_alignment_properties_view, true, true);
  268. _pane_right = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  269. _pane_right.pack1(vb, true, false);
  270. _pane_right.pack2(rb, true, false);
  271. _notebook_left = new Notebook();
  272. _notebook_left.show_border = false;
  273. _notebook_left.append_page(_pane_right, new Gtk.Label("Level"));
  274. /*
  275. _notebook_left.append_page(_graph_view, new Gtk.Label("Nodes"));
  276. */
  277. MenuBar menu = (MenuBar)_ui_manager.get_widget("/menubar");
  278. _vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  279. _vbox.pack_start(menu, false, false, 0);
  280. _vbox.pack_start(_notebook_left, true, true, 0);
  281. _file_filter = new FileFilter();
  282. _file_filter.set_filter_name("Level (*.level)");
  283. _file_filter.add_pattern("*.level");
  284. // Save level once every 5 minutes.
  285. GLib.Timeout.add_seconds(5*3600, save_timeout);
  286. this.destroy.connect(this.on_destroy);
  287. this.delete_event.connect(this.on_delete_event);
  288. this.key_press_event.connect(this.on_key_press);
  289. this.key_release_event.connect(this.on_key_release);
  290. this.add(_vbox);
  291. this.maximize();
  292. this.show_all();
  293. }
  294. private bool on_key_press(Gdk.EventKey ev)
  295. {
  296. if (ev.keyval == Gdk.Key.Control_L)
  297. _engine.send_script(LevelEditorApi.key_down("left_ctrl"));
  298. else if (ev.keyval == Gdk.Key.Shift_L)
  299. _engine.send_script(LevelEditorApi.key_down("left_shift"));
  300. return false;
  301. }
  302. private bool on_key_release(Gdk.EventKey ev)
  303. {
  304. if (ev.keyval == Gdk.Key.Control_L)
  305. _engine.send_script(LevelEditorApi.key_up("left_ctrl"));
  306. else if (ev.keyval == Gdk.Key.Shift_L)
  307. _engine.send_script(LevelEditorApi.key_up("left_shift"));
  308. return false;
  309. }
  310. private void on_resource_browser_resource_selected(string type, string name)
  311. {
  312. _engine.send_script(LevelEditorApi.set_placeable(type, name));
  313. }
  314. private void on_compiler_connected()
  315. {
  316. _console_view.log("Compiler connected.", "info");
  317. _compiler.receive_async();
  318. }
  319. private void on_compiler_disconnected()
  320. {
  321. _console_view.log("Compiler disconnected.", "info");
  322. }
  323. private void on_engine_connected()
  324. {
  325. _console_view.log("Engine connected.", "info");
  326. _engine.receive_async();
  327. }
  328. private void on_engine_disconnected()
  329. {
  330. _console_view.log("Engine disconnected.", "info");
  331. }
  332. private static int stringcmp(ref string a, ref string b)
  333. {
  334. return Posix.strcmp(a, b);
  335. }
  336. private void on_message_received(ConsoleClient client, uint8[] json)
  337. {
  338. try
  339. {
  340. Hashtable msg = JSON.decode(json) as Hashtable;
  341. string msg_type = msg["type"] as string;
  342. if (msg_type == "message")
  343. {
  344. _console_view.log((string)msg["message"], (string)msg["severity"]);
  345. }
  346. else if (msg_type == "compile")
  347. {
  348. Guid id = Guid.parse((string)msg["id"]);
  349. if (msg.has_key("start"))
  350. {
  351. // FIXME
  352. }
  353. else if (msg.has_key("success"))
  354. {
  355. _resource_compiler.finished((bool)msg["success"]);
  356. }
  357. }
  358. else if (msg_type == "unit_spawned")
  359. {
  360. string id = (string) msg["id"];
  361. string name = (string) msg["name"];
  362. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  363. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  364. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  365. _level.on_unit_spawned(Guid.parse(id)
  366. , name
  367. , Vector3.from_array(pos)
  368. , Quaternion.from_array(rot)
  369. , Vector3.from_array(scl)
  370. );
  371. }
  372. else if (msg_type == "sound_spawned")
  373. {
  374. string id = (string) msg["id"];
  375. string name = (string) msg["name"];
  376. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  377. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  378. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  379. double range = (double) msg["range"];
  380. double volume = (double) msg["volume"];
  381. bool loop = (bool) msg["loop"];
  382. _level.on_sound_spawned(Guid.parse(id)
  383. , name
  384. , Vector3.from_array(pos)
  385. , Quaternion.from_array(rot)
  386. , Vector3.from_array(scl)
  387. , range
  388. , volume
  389. , loop
  390. );
  391. }
  392. else if (msg_type == "move_objects")
  393. {
  394. Hashtable ids = (Hashtable)msg["ids"];
  395. Hashtable new_positions = (Hashtable)msg["new_positions"];
  396. Hashtable new_rotations = (Hashtable)msg["new_rotations"];
  397. Hashtable new_scales = (Hashtable)msg["new_scales"];
  398. string[] keys = ids.keys.to_array();
  399. Posix.qsort(keys, keys.length, sizeof(string), (Posix.compar_fn_t)stringcmp);
  400. Guid[] n_ids = new Guid[keys.length];
  401. Vector3[] n_positions = new Vector3[keys.length];
  402. Quaternion[] n_rotations = new Quaternion[keys.length];
  403. Vector3[] n_scales = new Vector3[keys.length];
  404. for (int i = 0; i < keys.length; ++i)
  405. {
  406. string k = keys[i];
  407. n_ids[i] = Guid.parse((string)ids[k]);
  408. n_positions[i] = Vector3.from_array((ArrayList<Value?>)(new_positions[k]));
  409. n_rotations[i] = Quaternion.from_array((ArrayList<Value?>)new_rotations[k]);
  410. n_scales[i] = Vector3.from_array((ArrayList<Value?>)new_scales[k]);
  411. }
  412. _level.on_move_objects(n_ids, n_positions, n_rotations, n_scales);
  413. }
  414. else if (msg_type == "selection")
  415. {
  416. Hashtable objects = (Hashtable)msg["objects"];
  417. string[] keys = objects.keys.to_array();
  418. Posix.qsort(keys, keys.length, sizeof(string), (Posix.compar_fn_t)stringcmp);
  419. Guid[] ids = new Guid[keys.length];
  420. for (int i = 0; i < keys.length; ++i)
  421. {
  422. string k = keys[i];
  423. ids[i] = Guid.parse((string)objects[k]);
  424. }
  425. _level.on_selection(ids);
  426. }
  427. }
  428. catch (Error e)
  429. {
  430. _console_view.log(e.message, "error");
  431. }
  432. // Receive next message
  433. client.receive_async();
  434. }
  435. private void send_state()
  436. {
  437. StringBuilder sb = new StringBuilder();
  438. sb.append(LevelEditorApi.set_grid_size(_grid_size));
  439. sb.append(LevelEditorApi.set_rotation_snap(_rotation_snap));
  440. sb.append(LevelEditorApi.enable_show_grid(_show_grid));
  441. sb.append(LevelEditorApi.enable_snap_to_grid(_snap_to_grid));
  442. sb.append(LevelEditorApi.enable_debug_render_world(_debug_render_world));
  443. sb.append(LevelEditorApi.enable_debug_physics_world(_debug_physics_world));
  444. sb.append(LevelEditorApi.set_tool_type(_tool_type));
  445. sb.append(LevelEditorApi.set_snap_mode(_snap_mode));
  446. sb.append(LevelEditorApi.set_reference_system(_reference_system));
  447. _engine.send_script(sb.str);
  448. }
  449. private bool on_button_press(EventButton ev)
  450. {
  451. // Prevent accelerators to step on camera's toes
  452. remove_accel_group(_ui_manager.get_accel_group());
  453. return true;
  454. }
  455. private bool on_button_release(EventButton ev)
  456. {
  457. add_accel_group(_ui_manager.get_accel_group());
  458. return true;
  459. }
  460. private void start_compiler()
  461. {
  462. string args[] =
  463. {
  464. ENGINE_EXE,
  465. "--source-dir", _project.source_dir(),
  466. "--map-source-dir", "core", _project.toolchain_dir(),
  467. "--server",
  468. null
  469. };
  470. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE);
  471. sl.set_cwd(ENGINE_DIR);
  472. try
  473. {
  474. _compiler_process = sl.spawnv(args);
  475. }
  476. catch (Error e)
  477. {
  478. _console_view.log(e.message, "error");
  479. }
  480. while (!_compiler.is_connected())
  481. {
  482. _compiler.connect("127.0.0.1", CROWN_DEFAULT_SERVER_PORT);
  483. GLib.Thread.usleep(100*1000);
  484. }
  485. _resource_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  486. if (_resource_compiler.compile.end(res))
  487. {
  488. if (_engine_view != null)
  489. return;
  490. _engine_view = new EngineView(_engine);
  491. _engine_view.realized.connect(on_engine_view_realized);
  492. _engine_view.button_press_event.connect(on_button_press);
  493. _engine_view.button_release_event.connect(on_button_release);
  494. _alignment_engine.remove(_alignment_engine.get_child());
  495. _alignment_level_tree_view.remove(_alignment_level_tree_view.get_child());
  496. _alignment_properties_view.remove(_alignment_properties_view.get_child());
  497. _alignment_engine.add(_engine_view);
  498. _alignment_level_tree_view.add(_level_treeview);
  499. _alignment_properties_view.add(_properties_view);
  500. _alignment_engine.show_all();
  501. _alignment_level_tree_view.show_all();
  502. _alignment_properties_view.show_all();
  503. }
  504. });
  505. }
  506. private void stop_compiler()
  507. {
  508. _compiler.close();
  509. if (_compiler_process != null)
  510. _compiler_process.force_exit();
  511. }
  512. private void start_engine(uint window_xid)
  513. {
  514. string args[] =
  515. {
  516. ENGINE_EXE,
  517. "--data-dir", _project.data_dir(),
  518. "--boot-dir", LEVEL_EDITOR_BOOT_DIR,
  519. "--parent-window", window_xid.to_string(),
  520. "--wait-console",
  521. null
  522. };
  523. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE);
  524. sl.set_cwd(ENGINE_DIR);
  525. try
  526. {
  527. _engine_process = sl.spawnv(args);
  528. }
  529. catch (Error e)
  530. {
  531. _console_view.log(e.message, "error");
  532. }
  533. while (!_engine.is_connected())
  534. {
  535. _engine.connect("127.0.0.1", 10001);
  536. GLib.Thread.usleep(100*1000);
  537. }
  538. new_level();
  539. send_state();
  540. }
  541. private void stop_engine()
  542. {
  543. _engine.close();
  544. if (_engine_process != null)
  545. _engine_process.force_exit();
  546. }
  547. private void restart_engine()
  548. {
  549. stop_engine();
  550. start_engine(_engine_view.window_id);
  551. }
  552. private void start_game()
  553. {
  554. if (!save())
  555. return;
  556. _resource_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  557. if (_resource_compiler.compile.end(res))
  558. {
  559. string args[] =
  560. {
  561. ENGINE_EXE,
  562. "--data-dir", _project.data_dir(),
  563. "--console-port", "12345",
  564. null
  565. };
  566. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(SubprocessFlags.NONE);
  567. sl.set_cwd(ENGINE_DIR);
  568. try
  569. {
  570. _game_process = sl.spawnv(args);
  571. }
  572. catch (Error e)
  573. {
  574. _console_view.log(e.message, "error");
  575. }
  576. }
  577. });
  578. }
  579. private void stop_game()
  580. {
  581. if (_game_process != null)
  582. _game_process.force_exit();
  583. }
  584. private void on_engine_view_realized()
  585. {
  586. start_engine(_engine_view.window_id);
  587. }
  588. private void on_tool_changed(Gtk.Action action)
  589. {
  590. RadioAction ra = (RadioAction)action;
  591. _tool_type = (ToolType)ra.current_value;
  592. send_state();
  593. }
  594. private void on_snap_mode_changed(Gtk.Action action)
  595. {
  596. RadioAction ra = (RadioAction)action;
  597. _snap_mode = (SnapMode)ra.current_value;
  598. send_state();
  599. }
  600. private void on_reference_system_changed(Gtk.Action action)
  601. {
  602. RadioAction ra = (RadioAction)action;
  603. _reference_system = (ReferenceSystem)ra.current_value;
  604. send_state();
  605. }
  606. private void on_grid_changed(Gtk.Action action)
  607. {
  608. RadioAction ra = (RadioAction)action;
  609. _grid_size = (float)ra.current_value/100.0;
  610. send_state();
  611. }
  612. private void on_rotation_snap_changed(Gtk.Action action)
  613. {
  614. RadioAction ra = (RadioAction)action;
  615. _rotation_snap = (float)ra.current_value;
  616. send_state();
  617. }
  618. private void new_level()
  619. {
  620. _level_filename = null;
  621. _level.load_empty_level();
  622. }
  623. private void load()
  624. {
  625. FileChooserDialog fcd = new FileChooserDialog("Open..."
  626. , this
  627. , FileChooserAction.OPEN
  628. , "Cancel"
  629. , ResponseType.CANCEL
  630. , "Open"
  631. , ResponseType.ACCEPT
  632. );
  633. fcd.add_filter(_file_filter);
  634. fcd.set_current_folder(_project.source_dir());
  635. if (fcd.run() == (int)ResponseType.ACCEPT)
  636. {
  637. string filename = fcd.get_filename();
  638. if (filename.has_suffix(".level"))
  639. {
  640. _level_filename = filename;
  641. _level.load(_level_filename);
  642. send_state();
  643. }
  644. }
  645. fcd.destroy();
  646. }
  647. private bool save_as()
  648. {
  649. bool saved = false;
  650. FileChooserDialog fcd = new FileChooserDialog("Save As..."
  651. , this
  652. , FileChooserAction.SAVE
  653. , "Cancel"
  654. , ResponseType.CANCEL
  655. , "Save"
  656. , ResponseType.ACCEPT
  657. );
  658. fcd.add_filter(_file_filter);
  659. fcd.set_current_folder(_project.source_dir());
  660. if (fcd.run() == (int)ResponseType.ACCEPT)
  661. {
  662. _level_filename = fcd.get_filename();
  663. _level.save(_level_filename);
  664. saved = true;
  665. }
  666. fcd.destroy();
  667. return saved;
  668. }
  669. private bool save()
  670. {
  671. bool saved = false;
  672. if (_level_filename == null)
  673. {
  674. saved = save_as();
  675. }
  676. else
  677. {
  678. _level.save(_level_filename);
  679. saved = true;
  680. }
  681. return saved;
  682. }
  683. private bool save_timeout()
  684. {
  685. if (_level_filename != null)
  686. save();
  687. return true;
  688. }
  689. private void shutdown()
  690. {
  691. if (_resource_browser != null)
  692. _resource_browser.destroy();
  693. if (_preferences_dialog != null)
  694. _preferences_dialog.destroy();
  695. stop_game();
  696. stop_engine();
  697. stop_compiler();
  698. Gtk.main_quit();
  699. }
  700. private void quit()
  701. {
  702. if (!_db.changed())
  703. {
  704. shutdown();
  705. return;
  706. }
  707. Gtk.MessageDialog md = new Gtk.MessageDialog(this
  708. , Gtk.DialogFlags.MODAL
  709. , Gtk.MessageType.WARNING
  710. , Gtk.ButtonsType.NONE
  711. , "File changed, save?"
  712. );
  713. md.add_button("Quit without Saving", ResponseType.NO);
  714. md.add_button("Cancel", ResponseType.CANCEL);
  715. md.add_button("Save", ResponseType.YES);
  716. md.set_default_response(ResponseType.YES);
  717. int rt = md.run();
  718. md.destroy();
  719. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  720. shutdown();
  721. }
  722. private void on_new()
  723. {
  724. if (!_db.changed())
  725. {
  726. new_level();
  727. send_state();
  728. return;
  729. }
  730. Gtk.MessageDialog md = new Gtk.MessageDialog(this
  731. , Gtk.DialogFlags.MODAL
  732. , Gtk.MessageType.WARNING
  733. , Gtk.ButtonsType.NONE
  734. , "File changed, save?"
  735. );
  736. md.add_button("New without Saving", ResponseType.NO);
  737. md.add_button("Cancel", ResponseType.CANCEL);
  738. md.add_button("Save", ResponseType.YES);
  739. md.set_default_response(ResponseType.YES);
  740. int rt = md.run();
  741. md.destroy();
  742. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  743. {
  744. new_level();
  745. send_state();
  746. }
  747. }
  748. private void on_open(Gtk.Action action)
  749. {
  750. if (!_db.changed())
  751. {
  752. load();
  753. return;
  754. }
  755. Gtk.MessageDialog md = new Gtk.MessageDialog(this
  756. , Gtk.DialogFlags.MODAL
  757. , Gtk.MessageType.WARNING
  758. , Gtk.ButtonsType.NONE
  759. , "File changed, save?"
  760. );
  761. md.add_button("Open without Saving", ResponseType.NO);
  762. md.add_button("Cancel", ResponseType.CANCEL);
  763. md.add_button("Save", ResponseType.YES);
  764. md.set_default_response(ResponseType.YES);
  765. int rt = md.run();
  766. md.destroy();
  767. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  768. load();
  769. }
  770. private void on_save(Gtk.Action action)
  771. {
  772. save();
  773. }
  774. private void on_save_as(Gtk.Action action)
  775. {
  776. save_as();
  777. }
  778. private void on_import(Gtk.Action action)
  779. {
  780. FileChooserDialog fcd = new FileChooserDialog("Import..."
  781. , this
  782. , FileChooserAction.OPEN
  783. , "Cancel"
  784. , ResponseType.CANCEL
  785. , "Open"
  786. , ResponseType.ACCEPT
  787. );
  788. fcd.select_multiple = true;
  789. if (fcd.run() == (int)ResponseType.ACCEPT)
  790. {
  791. // TODO
  792. }
  793. fcd.destroy();
  794. }
  795. private void on_preferences(Gtk.Action action)
  796. {
  797. if (_preferences_dialog == null)
  798. {
  799. _preferences_dialog = new PreferencesDialog(_engine);
  800. _preferences_dialog.set_transient_for(this);
  801. _preferences_dialog.delete_event.connect(() => { _preferences_dialog.hide(); return true; });
  802. }
  803. _preferences_dialog.show_all();
  804. }
  805. private void on_quit(Gtk.Action action)
  806. {
  807. quit();
  808. }
  809. private void on_show_grid(Gtk.Action action)
  810. {
  811. ToggleAction ta = (ToggleAction)action;
  812. _show_grid = ta.active;
  813. send_state();
  814. }
  815. private void on_custom_grid()
  816. {
  817. MessageDialog dg = new MessageDialog(this
  818. , DialogFlags.MODAL
  819. , MessageType.OTHER
  820. , ButtonsType.OK_CANCEL
  821. , "Grid size in meters:"
  822. );
  823. SpinButtonDouble sb = new SpinButtonDouble(_grid_size, 0.1, 1000);
  824. (dg.message_area as Gtk.Box).add(sb);
  825. dg.show_all();
  826. if (dg.run() == (int)ResponseType.OK)
  827. {
  828. _grid_size = sb.value;
  829. send_state();
  830. }
  831. dg.destroy();
  832. }
  833. private void on_rotation_snap(Gtk.Action action)
  834. {
  835. MessageDialog dg = new MessageDialog(this
  836. , DialogFlags.MODAL
  837. , MessageType.OTHER
  838. , ButtonsType.OK_CANCEL
  839. , "Rotation snap in degrees:"
  840. );
  841. SpinButtonDouble sb = new SpinButtonDouble(_rotation_snap, 1.0, 180.0);
  842. (dg.message_area as Gtk.Box).add(sb);
  843. dg.show_all();
  844. if (dg.run() == (int)ResponseType.OK)
  845. {
  846. _rotation_snap = sb.value;
  847. send_state();
  848. }
  849. dg.destroy();
  850. }
  851. private void on_create_cube(Gtk.Action action)
  852. {
  853. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cube"));
  854. }
  855. private void on_create_sphere(Gtk.Action action)
  856. {
  857. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/sphere"));
  858. }
  859. private void on_create_cone(Gtk.Action action)
  860. {
  861. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cone"));
  862. }
  863. private void on_create_cylinder(Gtk.Action action)
  864. {
  865. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cylinder"));
  866. }
  867. private void on_create_plane(Gtk.Action action)
  868. {
  869. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/plane"));
  870. }
  871. private void on_create_camera(Gtk.Action action)
  872. {
  873. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/camera"));
  874. }
  875. private void on_create_light(Gtk.Action action)
  876. {
  877. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/light"));
  878. }
  879. private void on_create_sound_source(Gtk.Action action)
  880. {
  881. _engine.send_script(LevelEditorApi.set_placeable("sound", ""));
  882. }
  883. private void on_resource_browser(Gtk.Action action)
  884. {
  885. if (_resource_browser == null)
  886. {
  887. _resource_browser = new ResourceBrowser(_project);
  888. _resource_browser.relative_to = _toolbar;
  889. _resource_browser.resource_selected.connect(on_resource_browser_resource_selected);
  890. _resource_browser.delete_event.connect(() => { _resource_browser.hide(); return true; });
  891. _resource_browser.modal = true;
  892. }
  893. _resource_browser.show_all();
  894. }
  895. private void on_engine_restart(Gtk.Action action)
  896. {
  897. restart_engine();
  898. }
  899. private void on_reload_lua(Gtk.Action action)
  900. {
  901. _resource_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  902. if (_resource_compiler.compile.end(res))
  903. {
  904. _engine.send(EngineApi.pause());
  905. _engine.send(EngineApi.reload("lua", "core/editors/level_editor/level_editor"));
  906. _engine.send(EngineApi.unpause());
  907. }
  908. });
  909. }
  910. public void on_snap_to_grid(Gtk.Action action)
  911. {
  912. ToggleAction ta = (ToggleAction)action;
  913. _snap_to_grid = ta.active;
  914. send_state();
  915. }
  916. private void on_debug_render_world(Gtk.Action action)
  917. {
  918. ToggleAction ta = (ToggleAction)action;
  919. _debug_render_world = ta.active;
  920. send_state();
  921. }
  922. private void on_debug_physics_world(Gtk.Action action)
  923. {
  924. ToggleAction ta = (ToggleAction)action;
  925. _debug_physics_world = ta.active;
  926. send_state();
  927. }
  928. private void on_run_game(Gtk.Action action)
  929. {
  930. start_game();
  931. }
  932. private void on_undo(Gtk.Action action)
  933. {
  934. _db.undo();
  935. }
  936. private void on_redo(Gtk.Action action)
  937. {
  938. _db.redo();
  939. }
  940. private void on_duplicate(Gtk.Action action)
  941. {
  942. _level.duplicate_selected_objects();
  943. }
  944. private void on_delete(Gtk.Action action)
  945. {
  946. _level.destroy_selected_objects();
  947. }
  948. private void on_manual(Gtk.Action action)
  949. {
  950. try
  951. {
  952. AppInfo.launch_default_for_uri("https://taylor001.github.io/crown/manual.html", null);
  953. }
  954. catch (Error e)
  955. {
  956. _console_view.log(e.message, "error");
  957. }
  958. }
  959. private void on_report_issue(Gtk.Action action)
  960. {
  961. try
  962. {
  963. AppInfo.launch_default_for_uri("https://github.com/taylor001/crown/issues", null);
  964. }
  965. catch (Error e)
  966. {
  967. _console_view.log(e.message, "error");
  968. }
  969. }
  970. private void on_open_last_log(Gtk.Action action)
  971. {
  972. File file = File.new_for_path(_project.data_dir() + "/last.log");
  973. try
  974. {
  975. AppInfo.launch_default_for_uri(file.get_uri(), null);
  976. }
  977. catch (Error e)
  978. {
  979. _console_view.log(e.message, "error");
  980. }
  981. }
  982. private void on_about(Gtk.Action action)
  983. {
  984. Gtk.AboutDialog dlg = new Gtk.AboutDialog();
  985. dlg.set_destroy_with_parent(true);
  986. dlg.set_transient_for(this);
  987. dlg.set_modal(true);
  988. try
  989. {
  990. dlg.set_logo(new Pixbuf.from_resource("/org/pepper/icons/128x128/pepper.png"));
  991. }
  992. catch (Error e)
  993. {
  994. stderr.printf("%s\n", e.message);
  995. }
  996. dlg.program_name = "Crown Game Engine";
  997. dlg.version = CROWN_VERSION;
  998. dlg.website = "https://github.com/taylor001/crown";
  999. dlg.copyright = "Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.";
  1000. dlg.license = "Crown Game Engine.\n"
  1001. + "Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.\n"
  1002. + "\n"
  1003. + "This program is free software; you can redistribute it and/or\n"
  1004. + "modify it under the terms of the GNU General Public License\n"
  1005. + "as published by the Free Software Foundation; either version 2\n"
  1006. + "of the License, or (at your option) any later version.\n"
  1007. + "\n"
  1008. + "This program is distributed in the hope that it will be useful,\n"
  1009. + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1010. + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1011. + "GNU General Public License for more details.\n"
  1012. + "\n"
  1013. + "You should have received a copy of the GNU General Public License\n"
  1014. + "along with this program; if not, write to the Free Software\n"
  1015. + "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
  1016. ;
  1017. dlg.run();
  1018. dlg.destroy();
  1019. }
  1020. private void on_destroy()
  1021. {
  1022. Gtk.main_quit();
  1023. }
  1024. private bool on_delete_event()
  1025. {
  1026. quit();
  1027. return true;
  1028. }
  1029. }
  1030. public static int main (string[] args)
  1031. {
  1032. Gtk.init(ref args);
  1033. Intl.setlocale(LocaleCategory.ALL, "C");
  1034. Gtk.Settings.get_default().gtk_application_prefer_dark_theme = true;
  1035. Gtk.CssProvider provider = new Gtk.CssProvider();
  1036. Gdk.Screen screen = Gdk.Display.get_default().get_default_screen();
  1037. Gtk.StyleContext.add_provider_for_screen(screen, provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
  1038. provider.load_from_resource("/org/pepper/theme/style.css");
  1039. Project project = new Project();
  1040. project.load(args[1], args[2], args[3]);
  1041. var editor = new LevelEditor(project);
  1042. editor.show_all();
  1043. Gtk.main();
  1044. return 0;
  1045. }
  1046. }