level_editor.vala 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. using Gdk; // Pixbuf
  6. using Gee;
  7. using Gtk;
  8. namespace Crown
  9. {
  10. 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 enum StartGame
  28. {
  29. NORMAL,
  30. TEST
  31. }
  32. public class LevelEditor : Gtk.Window
  33. {
  34. // Constants
  35. const Gtk.ActionEntry[] action_entries =
  36. {
  37. { "menu-file", null, "_File", null, null, null },
  38. { "new-level", null, "New Level", "<ctrl>N", null, on_new_level },
  39. { "open-level", null, "Open Level...", "<ctrl>O", null, on_open_level },
  40. { "open-project", null, "Open Project...", null, null, on_open_project },
  41. { "save", null, "Save", "<ctrl>S", null, on_save },
  42. { "save-as", null, "Save As...", "<shift><ctrl>S", null, on_save_as },
  43. { "import", null, "Import", null, null, null },
  44. { "import-sprites", null, "Sprites...", null, null, on_import_sprites },
  45. { "import-meshes", null, "Meshes...", null, null, on_import_meshes },
  46. { "import-sounds", null, "Sounds...", null, null, on_import_sounds },
  47. { "import-textures", null, "Textures...", null, null, on_import_textures },
  48. { "preferences", null, "Preferences", null, null, on_preferences },
  49. { "deploy", null, "Deploy...", null, null, on_deploy },
  50. { "quit", null, "Quit", "<ctrl>Q", null, on_quit },
  51. { "menu-edit", null, "_Edit", null, null, null },
  52. { "undo", null, "Undo", "<ctrl>Z", null, on_undo },
  53. { "redo", null, "Redo", "<shift><ctrl>Z", null, on_redo },
  54. { "duplicate", null, "Duplicate", "<ctrl>D", null, on_duplicate },
  55. { "delete", null, "Delete", "<ctrl>K", null, on_delete },
  56. { "menu-grid", null, "Grid", null, null, null },
  57. { "grid-custom", null, "Custom", "<ctrl>G", null, on_custom_grid },
  58. { "menu-rotation-snap", null, "Rotation Snap", null, null, null },
  59. { "rotation-snap-custom", null, "Custom", "<ctrl>H", null, on_rotation_snap },
  60. { "menu-create", null, "Create", null, null, null },
  61. { "menu-primitives", null, "Primitives", null, null, null },
  62. { "primitive-cube", null, "Cube", null, null, on_create_primitive },
  63. { "primitive-sphere", null, "Sphere", null, null, on_create_primitive },
  64. { "primitive-cone", null, "Cone", null, null, on_create_primitive },
  65. { "primitive-cylinder", null, "Cylinder", null, null, on_create_primitive },
  66. { "primitive-plane", null, "Plane", null, null, on_create_primitive },
  67. { "camera", null, "Camera", null, null, on_create_primitive },
  68. { "light", null, "Light", null, null, on_create_primitive },
  69. { "sound-source", null, "Sound Source", null, null, on_create_primitive },
  70. { "menu-camera", null, "Camera", null, null, null },
  71. { "camera-view-perspective", null, "Perspective", "KP_5", null, on_camera_view },
  72. { "camera-view-front", null, "View Front", "KP_1", null, on_camera_view },
  73. { "camera-view-back", null, "View Back", "<ctrl>KP_1", null, on_camera_view },
  74. { "camera-view-right", null, "View Right", "KP_3", null, on_camera_view },
  75. { "camera-view-left", null, "View Left", "<ctrl>KP_3", null, on_camera_view },
  76. { "camera-view-top", null, "View Top", "KP_7", null, on_camera_view },
  77. { "camera-view-bottom", null, "View Bottom", "<ctrl>KP_7", null, on_camera_view },
  78. { "menu-engine", null, "En_gine", null, null, null },
  79. { "menu-view", null, "View", null, null, null },
  80. { "resource-browser", null, "Resource Browser", "<ctrl>P", null, on_resource_browser },
  81. { "fullscreen", null, "Fullscreen", "F11", null, on_fullscreen },
  82. { "restart", null, "_Restart", null, null, on_engine_restart },
  83. { "reload-lua", null, "Reload Level Editor", "F7", null, on_reload_lua },
  84. { "menu-run", null, "_Run", null, null, null },
  85. { "test-level", "game-run", "Test Level", "F5", "Test Level", on_run_game },
  86. { "run-game", null, "Run Game", null, null, on_run_game },
  87. { "menu-help", null, "Help", null, null, null },
  88. { "manual", null, "Manual", "F1", null, on_manual },
  89. { "report-issue", null, "Report an Issue", null, null, on_report_issue },
  90. { "open-last-log", null, "Open last.log", null, null, on_open_last_log },
  91. { "about", null, "About", null, null, on_about }
  92. };
  93. const Gtk.RadioActionEntry[] grid_entries =
  94. {
  95. { "grid-0.1", null, "0.1m", null, null, 10 },
  96. { "grid-0.2", null, "0.2m", null, null, 20 },
  97. { "grid-0.5", null, "0.5m", null, null, 50 },
  98. { "grid-1", null, "1m", null, null, 100 },
  99. { "grid-2", null, "2m", null, null, 200 },
  100. { "grid-5", null, "5m", null, null, 500 }
  101. };
  102. const Gtk.RadioActionEntry[] rotation_snap_entries =
  103. {
  104. { "rotation-snap-1", null, "1°", null, null, 1 },
  105. { "rotation-snap-15", null, "15°", null, null, 15 },
  106. { "rotation-snap-30", null, "30°", null, null, 30 },
  107. { "rotation-snap-45", null, "45°", null, null, 45 },
  108. { "rotation-snap-90", null, "90°", null, null, 90 },
  109. { "rotation-snap-180", null, "180°", null, null, 180 }
  110. };
  111. const Gtk.RadioActionEntry[] tool_entries =
  112. {
  113. { "place", "tool-place", "Place", null, "Place", (int)ToolType.PLACE },
  114. { "move", "tool-move", "Move", null, "Move", (int)ToolType.MOVE },
  115. { "rotate", "tool-rotate", "Rotate", null, "Rotate", (int)ToolType.ROTATE },
  116. { "scale", "tool-scale", "Scale", null, "Scale", (int)ToolType.SCALE }
  117. };
  118. const Gtk.RadioActionEntry[] snap_mode_entries =
  119. {
  120. { "snap-relative", "reference-local", "Relative Snap", null, "Relative Snap", (int)SnapMode.RELATIVE },
  121. { "snap-absolute", "reference-world", "Absolute Snap", null, "Absolute Snap", (int)SnapMode.ABSOLUTE }
  122. };
  123. const Gtk.RadioActionEntry[] reference_system_entries =
  124. {
  125. { "reference-system-local", "axis-local", "Local Axis", null, "Local Axis", (int)ReferenceSystem.LOCAL },
  126. { "reference-system-world", "axis-world", "World Axis", null, "World Axis", (int)ReferenceSystem.WORLD }
  127. };
  128. const Gtk.ToggleActionEntry[] snap_to_entries =
  129. {
  130. { "snap-to-grid", "snap-to-grid", "Snap To Grid", "<ctrl>U", "Snap To Grid", on_snap_to_grid, true },
  131. { "grid-show", null, "Show Grid", null, "Show Grid", on_show_grid, true }
  132. };
  133. const Gtk.ToggleActionEntry[] view_entries =
  134. {
  135. { "debug-render-world", null, "Debug Render World", null, null, on_debug_render_world, false },
  136. { "debug-physics-world", null, "Debug Physics World", null, null, on_debug_physics_world, false }
  137. };
  138. // Editor state
  139. private double _grid_size;
  140. private double _rotation_snap;
  141. private bool _show_grid;
  142. private bool _snap_to_grid;
  143. private bool _debug_render_world;
  144. private bool _debug_physics_world;
  145. private ToolType _tool_type;
  146. private SnapMode _snap_mode;
  147. private ReferenceSystem _reference_system;
  148. // Engine connections
  149. private GLib.Subprocess _compiler_process;
  150. private GLib.Subprocess _engine_process;
  151. private GLib.Subprocess _game_process;
  152. private ConsoleClient _compiler;
  153. private ConsoleClient _engine;
  154. private ConsoleClient _game;
  155. // Level data
  156. private Database _database;
  157. private Project _project;
  158. private ProjectStore _project_store;
  159. private Level _level;
  160. private DataCompiler _data_compiler;
  161. // Widgets
  162. private ConsoleView _console_view;
  163. private EngineView _engine_view;
  164. private LevelTreeView _level_treeview;
  165. private LevelLayersTreeView _level_layers_treeview;
  166. private PropertiesView _properties_view;
  167. private PreferencesDialog _preferences_dialog;
  168. private ResourceBrowser _resource_browser;
  169. private ResourceBrowser _resource_selection;
  170. private Gtk.Popover _resource_popover;
  171. private Gtk.Overlay _engine_view_overlay;
  172. private Slide _engine_slide;
  173. private Slide _inspector_slide;
  174. private Gtk.ActionGroup _action_group;
  175. private Gtk.UIManager _ui_manager;
  176. private Gtk.MenuBar _menubar;
  177. private Gtk.Toolbar _toolbar;
  178. private Gtk.Paned _engine_pane;
  179. private Gtk.Notebook _level_tree_view_notebook;
  180. private Gtk.Paned _inspector_pane;
  181. private Gtk.Paned _main_pane;
  182. private Gtk.Box _main_vbox;
  183. private Gtk.FileFilter _file_filter;
  184. private bool _fullscreen;
  185. public LevelEditor(Database database, Project project, Level level, ConsoleClient compiler, ConsoleClient engine, ConsoleClient game)
  186. {
  187. this.title = "Level Editor";
  188. // Editor state
  189. _grid_size = 1.0;
  190. _rotation_snap = 15.0;
  191. _show_grid = true;
  192. _snap_to_grid = true;
  193. _debug_render_world = false;
  194. _debug_physics_world = false;
  195. _tool_type = ToolType.MOVE;
  196. _snap_mode = SnapMode.RELATIVE;
  197. _reference_system = ReferenceSystem.LOCAL;
  198. // Engine connections
  199. _compiler_process = null;
  200. _engine_process = null;
  201. _game_process = null;
  202. _compiler = compiler;
  203. _compiler.connected.connect(on_compiler_connected);
  204. _compiler.disconnected.connect(on_compiler_disconnected);
  205. _compiler.message_received.connect(on_message_received);
  206. _engine = engine;
  207. _engine.connected.connect(on_engine_connected);
  208. _engine.disconnected.connect(on_engine_disconnected);
  209. _engine.message_received.connect(on_message_received);
  210. _game = game;
  211. _game.connected.connect(on_game_connected);
  212. _game.disconnected.connect(on_game_disconnected);
  213. _game.message_received.connect(on_message_received);
  214. _data_compiler = new DataCompiler(_compiler);
  215. _database = database;
  216. _project = project;
  217. _project_store = new ProjectStore(_project);
  218. _level = level;
  219. // Widgets
  220. _resource_browser = new ResourceBrowser(_project, _project_store, true);
  221. _resource_browser.resource_selected.connect(on_resource_browser_resource_selected);
  222. _resource_selection = new ResourceBrowser(_project, _project_store, false);
  223. _console_view = new ConsoleView(_engine, _project);
  224. _level_treeview = new LevelTreeView(_database, _level);
  225. _level_layers_treeview = new LevelLayersTreeView(_database, _level);
  226. _properties_view = new PropertiesView(_level, _project_store);
  227. _engine_slide = new Slide();
  228. _engine_slide.show_widget(new StartingCompiler());
  229. _inspector_slide = new Slide();
  230. _inspector_slide.show_widget(new StartingCompiler());
  231. _action_group = new Gtk.ActionGroup("group");
  232. _action_group.add_actions(action_entries, this);
  233. _action_group.add_radio_actions(grid_entries, (int)(_grid_size*100.0), this.on_grid_changed);
  234. _action_group.add_radio_actions(rotation_snap_entries, (int)_rotation_snap, this.on_rotation_snap_changed);
  235. _action_group.add_radio_actions(tool_entries, (int)_tool_type, on_tool_changed);
  236. _action_group.add_radio_actions(snap_mode_entries, (int)_snap_mode, on_snap_mode_changed);
  237. _action_group.add_radio_actions(reference_system_entries, (int)_reference_system, on_reference_system_changed);
  238. _action_group.add_toggle_actions(snap_to_entries, this);
  239. _action_group.add_toggle_actions(view_entries, this);
  240. _ui_manager = new UIManager();
  241. try
  242. {
  243. _ui_manager.add_ui_from_resource("/org/crown/level_editor/level_editor.xml");
  244. _ui_manager.insert_action_group(_action_group, 0);
  245. add_accel_group(_ui_manager.get_accel_group());
  246. }
  247. catch (Error e)
  248. {
  249. error(e.message);
  250. }
  251. _menubar = _ui_manager.get_widget("/menubar") as Gtk.MenuBar;
  252. _toolbar = _ui_manager.get_widget("/toolbar") as Gtk.Toolbar;
  253. _toolbar.set_icon_size(Gtk.IconSize.SMALL_TOOLBAR);
  254. _toolbar.set_style(Gtk.ToolbarStyle.ICONS);
  255. _toolbar.halign = Gtk.Align.START;
  256. _toolbar.valign = Gtk.Align.START;
  257. _toolbar.orientation = Gtk.Orientation.VERTICAL;
  258. _toolbar.margin_start = 6;
  259. _toolbar.margin_top = 6;
  260. _resource_popover = new Gtk.Popover(_toolbar);
  261. _resource_popover.delete_event.connect(() => { _resource_popover.hide(); return true; });
  262. _resource_popover.modal = true;
  263. _resource_browser.resource_selected.connect(() => { _resource_popover.hide(); });
  264. _resource_popover.add(_resource_browser);
  265. _level_tree_view_notebook = new Notebook();
  266. _level_tree_view_notebook.show_border = false;
  267. _level_tree_view_notebook.append_page(_level_treeview, new Gtk.Image.from_icon_name("level-tree", IconSize.SMALL_TOOLBAR));
  268. _level_tree_view_notebook.append_page(_level_layers_treeview, new Gtk.Image.from_icon_name("level-layers", IconSize.SMALL_TOOLBAR));
  269. _engine_view_overlay = new Gtk.Overlay();
  270. _engine_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  271. _engine_pane.pack1(_engine_slide, true, true);
  272. _engine_pane.pack2(_console_view, true, true);
  273. _inspector_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  274. _inspector_pane.pack1(_level_tree_view_notebook, true, true);
  275. _inspector_pane.pack2(_properties_view, true, true);
  276. _main_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  277. _main_pane.pack1(_engine_pane, true, false);
  278. _main_pane.pack2(_inspector_slide, true, false);
  279. _main_vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  280. _main_vbox.pack_start(_menubar, false, false, 0);
  281. _main_vbox.pack_start(_main_pane, true, true, 0);
  282. _file_filter = new FileFilter();
  283. _file_filter.set_filter_name("Level (*.level)");
  284. _file_filter.add_pattern("*.level");
  285. // Save level once every 5 minutes.
  286. GLib.Timeout.add_seconds(5*3600, save_timeout);
  287. _fullscreen = false;
  288. this.destroy.connect(this.on_destroy);
  289. this.delete_event.connect(this.on_delete_event);
  290. this.key_press_event.connect(this.on_key_press);
  291. this.key_release_event.connect(this.on_key_release);
  292. this.window_state_event.connect(this.on_window_state_event);
  293. this.add(_main_vbox);
  294. this.maximize();
  295. this.show_all();
  296. start_compiler();
  297. // This is supposed to work in pixel units but it doesn't for
  298. // a reason I couldn't find. The values passed, though, seem
  299. // to move pane handles to the correct-ish position.
  300. _main_pane.set_position(300);
  301. _engine_pane.set_position(72);
  302. _inspector_pane.set_position(300);
  303. }
  304. private bool on_key_press(Gdk.EventKey ev)
  305. {
  306. if (ev.keyval == Gdk.Key.Control_L)
  307. _engine.send_script(LevelEditorApi.key_down("ctrl_left"));
  308. else if (ev.keyval == Gdk.Key.Shift_L)
  309. _engine.send_script(LevelEditorApi.key_down("shift_left"));
  310. else if (ev.keyval == Gdk.Key.Alt_L)
  311. _engine.send_script(LevelEditorApi.key_down("alt_left"));
  312. return false;
  313. }
  314. private bool on_key_release(Gdk.EventKey ev)
  315. {
  316. if (ev.keyval == Gdk.Key.Control_L)
  317. _engine.send_script(LevelEditorApi.key_up("ctrl_left"));
  318. else if (ev.keyval == Gdk.Key.Shift_L)
  319. _engine.send_script(LevelEditorApi.key_up("shift_left"));
  320. else if (ev.keyval == Gdk.Key.Alt_L)
  321. _engine.send_script(LevelEditorApi.key_up("alt_left"));
  322. return false;
  323. }
  324. private bool on_window_state_event(EventWindowState ev)
  325. {
  326. _fullscreen = (ev.new_window_state & WindowState.FULLSCREEN) != 0;
  327. return true;
  328. }
  329. private void on_resource_browser_resource_selected(string type, string name)
  330. {
  331. _engine.send_script(LevelEditorApi.set_placeable(type, name));
  332. _action_group.get_action("place").activate();
  333. }
  334. private void on_compiler_connected()
  335. {
  336. _console_view.logi("editor", "Compiler connected");
  337. _compiler.receive_async();
  338. }
  339. private void on_compiler_disconnected()
  340. {
  341. _console_view.logi("editor", "Compiler disconnected");
  342. }
  343. private void on_engine_connected()
  344. {
  345. _console_view.logi("editor", "Engine connected");
  346. _engine.receive_async();
  347. }
  348. private void on_engine_disconnected()
  349. {
  350. _console_view.logi("editor", "Engine disconnected");
  351. }
  352. private void on_game_connected()
  353. {
  354. _console_view.logi("editor", "Game connected");
  355. _game.receive_async();
  356. }
  357. private void on_game_disconnected()
  358. {
  359. _action_group.get_action("test-level").icon_name = "game-run";
  360. _console_view.logi("editor", "Game disconnected");
  361. _project.delete_level_editor_test_level();
  362. }
  363. private void on_message_received(ConsoleClient client, uint8[] json)
  364. {
  365. Hashtable msg = JSON.decode(json) as Hashtable;
  366. string msg_type = msg["type"] as string;
  367. if (msg_type == "message")
  368. {
  369. _console_view.log((string)msg["system"], (string)msg["message"], (string)msg["severity"]);
  370. }
  371. else if (msg_type == "add_file")
  372. {
  373. string path = (string)msg["path"];
  374. _project.add_file(path);
  375. }
  376. else if (msg_type == "remove_file")
  377. {
  378. string path = (string)msg["path"];
  379. _project.remove_file(path);
  380. }
  381. else if (msg_type == "compile")
  382. {
  383. // Guid id = Guid.parse((string)msg["id"]);
  384. if (msg.has_key("start"))
  385. {
  386. // FIXME
  387. }
  388. else if (msg.has_key("success"))
  389. {
  390. _data_compiler.finished((bool)msg["success"]);
  391. }
  392. }
  393. else if (msg_type == "unit_spawned")
  394. {
  395. string id = (string) msg["id"];
  396. string name = (string) msg["name"];
  397. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  398. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  399. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  400. _level.on_unit_spawned(Guid.parse(id)
  401. , name
  402. , Vector3.from_array(pos)
  403. , Quaternion.from_array(rot)
  404. , Vector3.from_array(scl)
  405. );
  406. }
  407. else if (msg_type == "sound_spawned")
  408. {
  409. string id = (string) msg["id"];
  410. string name = (string) msg["name"];
  411. ArrayList<Value?> pos = (ArrayList<Value?>)msg["position"];
  412. ArrayList<Value?> rot = (ArrayList<Value?>)msg["rotation"];
  413. ArrayList<Value?> scl = (ArrayList<Value?>)msg["scale"];
  414. double range = (double) msg["range"];
  415. double volume = (double) msg["volume"];
  416. bool loop = (bool) msg["loop"];
  417. _level.on_sound_spawned(Guid.parse(id)
  418. , name
  419. , Vector3.from_array(pos)
  420. , Quaternion.from_array(rot)
  421. , Vector3.from_array(scl)
  422. , range
  423. , volume
  424. , loop
  425. );
  426. }
  427. else if (msg_type == "move_objects")
  428. {
  429. Hashtable ids = (Hashtable)msg["ids"];
  430. Hashtable new_positions = (Hashtable)msg["new_positions"];
  431. Hashtable new_rotations = (Hashtable)msg["new_rotations"];
  432. Hashtable new_scales = (Hashtable)msg["new_scales"];
  433. ArrayList<string> keys = new ArrayList<string>.wrap(ids.keys.to_array());
  434. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  435. Guid[] n_ids = new Guid[keys.size];
  436. Vector3[] n_positions = new Vector3[keys.size];
  437. Quaternion[] n_rotations = new Quaternion[keys.size];
  438. Vector3[] n_scales = new Vector3[keys.size];
  439. for (int i = 0; i < keys.size; ++i)
  440. {
  441. string k = keys[i];
  442. n_ids[i] = Guid.parse((string)ids[k]);
  443. n_positions[i] = Vector3.from_array((ArrayList<Value?>)(new_positions[k]));
  444. n_rotations[i] = Quaternion.from_array((ArrayList<Value?>)new_rotations[k]);
  445. n_scales[i] = Vector3.from_array((ArrayList<Value?>)new_scales[k]);
  446. }
  447. _level.on_move_objects(n_ids, n_positions, n_rotations, n_scales);
  448. }
  449. else if (msg_type == "selection")
  450. {
  451. Hashtable objects = (Hashtable)msg["objects"];
  452. ArrayList<string> keys = new ArrayList<string>.wrap(objects.keys.to_array());
  453. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  454. Guid[] ids = new Guid[keys.size];
  455. for (int i = 0; i < keys.size; ++i)
  456. {
  457. string k = keys[i];
  458. ids[i] = Guid.parse((string)objects[k]);
  459. }
  460. _level.on_selection(ids);
  461. }
  462. else if (msg_type == "error")
  463. {
  464. _console_view.loge("editor", "Error: " + (string)msg["message"]);
  465. }
  466. else
  467. {
  468. _console_view.loge("editor", "Unknown message type: " + msg_type);
  469. }
  470. // Receive next message
  471. client.receive_async();
  472. }
  473. private void send_state()
  474. {
  475. StringBuilder sb = new StringBuilder();
  476. sb.append(LevelEditorApi.set_grid_size(_grid_size));
  477. sb.append(LevelEditorApi.set_rotation_snap(_rotation_snap));
  478. sb.append(LevelEditorApi.enable_show_grid(_show_grid));
  479. sb.append(LevelEditorApi.enable_snap_to_grid(_snap_to_grid));
  480. sb.append(LevelEditorApi.enable_debug_render_world(_debug_render_world));
  481. sb.append(LevelEditorApi.enable_debug_physics_world(_debug_physics_world));
  482. sb.append(LevelEditorApi.set_tool_type(_tool_type));
  483. sb.append(LevelEditorApi.set_snap_mode(_snap_mode));
  484. sb.append(LevelEditorApi.set_reference_system(_reference_system));
  485. _engine.send_script(sb.str);
  486. }
  487. private bool on_button_press(EventButton ev)
  488. {
  489. // Prevent accelerators to step on camera's toes
  490. remove_accel_group(_ui_manager.get_accel_group());
  491. return true;
  492. }
  493. private bool on_button_release(EventButton ev)
  494. {
  495. add_accel_group(_ui_manager.get_accel_group());
  496. return true;
  497. }
  498. private void start_compiler()
  499. {
  500. string args[] =
  501. {
  502. ENGINE_EXE,
  503. "--source-dir", _project.source_dir(),
  504. "--data-dir", _project.data_dir(),
  505. "--map-source-dir", "core", _project.toolchain_dir(),
  506. "--server",
  507. "--wait-console",
  508. null
  509. };
  510. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  511. sl.set_cwd(ENGINE_DIR);
  512. try
  513. {
  514. _compiler_process = sl.spawnv(args);
  515. }
  516. catch (Error e)
  517. {
  518. _console_view.loge("editor", e.message);
  519. }
  520. for (int tries = 0; !_compiler.is_connected() && tries < 5; ++tries)
  521. {
  522. _compiler.connect("127.0.0.1", CROWN_DEFAULT_SERVER_PORT);
  523. GLib.Thread.usleep(250*1000);
  524. }
  525. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  526. if (_data_compiler.compile.end(res))
  527. {
  528. if (_engine_view != null)
  529. return;
  530. _engine_view = new EngineView(_engine);
  531. _engine_view.realized.connect(on_engine_view_realized);
  532. _engine_view.button_press_event.connect(on_button_press);
  533. _engine_view.button_release_event.connect(on_button_release);
  534. _engine_view_overlay.add(_engine_view);
  535. _engine_view_overlay.add_overlay(_toolbar);
  536. _engine_slide.show_widget(_engine_view_overlay);
  537. _inspector_slide.show_widget(_inspector_pane);
  538. }
  539. });
  540. }
  541. private void stop_compiler()
  542. {
  543. _compiler.send(DataCompilerApi.quit());
  544. _compiler.close();
  545. if (_compiler_process != null)
  546. {
  547. try
  548. {
  549. _compiler_process.wait();
  550. }
  551. catch (Error e)
  552. {
  553. _console_view.loge("editor", e.message);
  554. }
  555. }
  556. }
  557. private void start_engine(uint window_xid)
  558. {
  559. if (window_xid == 0)
  560. return;
  561. string args[] =
  562. {
  563. ENGINE_EXE,
  564. "--data-dir", _project.data_dir(),
  565. "--boot-dir", LEVEL_EDITOR_BOOT_DIR,
  566. "--parent-window", window_xid.to_string(),
  567. "--wait-console",
  568. null
  569. };
  570. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  571. sl.set_cwd(ENGINE_DIR);
  572. try
  573. {
  574. _engine_process = sl.spawnv(args);
  575. }
  576. catch (Error e)
  577. {
  578. _console_view.loge("editor", e.message);
  579. }
  580. for (int tries = 0; !_engine.is_connected() && tries < 10; ++tries)
  581. {
  582. _engine.connect("127.0.0.1", 10001);
  583. GLib.Thread.usleep(500*1000);
  584. }
  585. _level.send_level();
  586. send_state();
  587. }
  588. private void stop_engine()
  589. {
  590. _engine.send_script("Device.quit()");
  591. _engine.close();
  592. if (_engine_process != null)
  593. {
  594. try
  595. {
  596. _engine_process.wait();
  597. }
  598. catch (Error e)
  599. {
  600. _console_view.loge("editor", e.message);
  601. }
  602. }
  603. }
  604. private void restart_engine()
  605. {
  606. stop_engine();
  607. start_engine(_engine_view.window_id);
  608. }
  609. private void start_game(StartGame sg)
  610. {
  611. _database.dump(_project.level_editor_test_level());
  612. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  613. if (_data_compiler.compile.end(res))
  614. {
  615. string args[] =
  616. {
  617. ENGINE_EXE,
  618. "--data-dir", _project.data_dir(),
  619. "--console-port", "12345",
  620. "--wait-console",
  621. "--lua-string", sg == StartGame.TEST ? "TEST=true" : "",
  622. null
  623. };
  624. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  625. sl.set_cwd(ENGINE_DIR);
  626. try
  627. {
  628. _game_process = sl.spawnv(args);
  629. }
  630. catch (Error e)
  631. {
  632. _console_view.loge("editor", e.message);
  633. }
  634. for (int tries = 0; !_game.is_connected() && tries < 10; ++tries)
  635. {
  636. _game.connect("127.0.0.1", 12345);
  637. GLib.Thread.usleep(500*1000);
  638. }
  639. }
  640. });
  641. }
  642. private void stop_game()
  643. {
  644. _game.send_script("Device.quit()");
  645. _game.close();
  646. if (_game_process != null)
  647. {
  648. try
  649. {
  650. _game_process.wait();
  651. }
  652. catch (Error e)
  653. {
  654. _console_view.loge("editor", e.message);
  655. }
  656. }
  657. }
  658. private void deploy_game()
  659. {
  660. FileChooserDialog fcd = new FileChooserDialog("Select destination directory..."
  661. , null
  662. , FileChooserAction.SELECT_FOLDER
  663. , "Cancel"
  664. , ResponseType.CANCEL
  665. , "Open"
  666. , ResponseType.ACCEPT
  667. );
  668. if (fcd.run() == (int)ResponseType.ACCEPT)
  669. {
  670. GLib.File data_dir = File.new_for_path(fcd.get_filename());
  671. string args[] =
  672. {
  673. ENGINE_EXE,
  674. "--source-dir", _project.source_dir(),
  675. "--map-source-dir", "core", _project.toolchain_dir(),
  676. "--data-dir", data_dir.get_path(),
  677. "--compile",
  678. null
  679. };
  680. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  681. sl.set_cwd(ENGINE_DIR);
  682. try
  683. {
  684. GLib.Subprocess compiler = sl.spawnv(args);
  685. compiler.wait();
  686. if (compiler.get_exit_status() == 0)
  687. {
  688. GLib.File engine_exe = File.new_for_path(ENGINE_EXE);
  689. GLib.File engine_exe_dest = File.new_for_path(data_dir.get_path() + "/" + ENGINE_EXE);
  690. engine_exe.copy(engine_exe_dest, FileCopyFlags.OVERWRITE);
  691. _console_view.logi("editor", "Project deployed to `%s`".printf(data_dir.get_path()));
  692. }
  693. }
  694. catch (Error e)
  695. {
  696. _console_view.logi("editor", "%s".printf(e.message));
  697. _console_view.logi("editor", "Failed to deploy project");
  698. }
  699. }
  700. fcd.destroy();
  701. }
  702. private void on_engine_view_realized()
  703. {
  704. start_engine(_engine_view.window_id);
  705. }
  706. private void on_tool_changed(Gtk.Action action)
  707. {
  708. RadioAction ra = (RadioAction)action;
  709. _tool_type = (ToolType)ra.current_value;
  710. send_state();
  711. }
  712. private void on_snap_mode_changed(Gtk.Action action)
  713. {
  714. RadioAction ra = (RadioAction)action;
  715. _snap_mode = (SnapMode)ra.current_value;
  716. send_state();
  717. }
  718. private void on_reference_system_changed(Gtk.Action action)
  719. {
  720. RadioAction ra = (RadioAction)action;
  721. _reference_system = (ReferenceSystem)ra.current_value;
  722. send_state();
  723. }
  724. private void on_grid_changed(Gtk.Action action)
  725. {
  726. RadioAction ra = (RadioAction)action;
  727. _grid_size = (float)ra.current_value/100.0;
  728. send_state();
  729. }
  730. private void on_rotation_snap_changed(Gtk.Action action)
  731. {
  732. RadioAction ra = (RadioAction)action;
  733. _rotation_snap = (float)ra.current_value;
  734. send_state();
  735. }
  736. private void new_level()
  737. {
  738. _level.load_empty_level();
  739. _level.send_level();
  740. }
  741. private void load_level()
  742. {
  743. FileChooserDialog fcd = new FileChooserDialog("Open Level..."
  744. , this
  745. , FileChooserAction.OPEN
  746. , "Cancel"
  747. , ResponseType.CANCEL
  748. , "Open"
  749. , ResponseType.ACCEPT
  750. );
  751. fcd.add_filter(_file_filter);
  752. fcd.set_current_folder(_project.source_dir());
  753. if (fcd.run() == (int)ResponseType.ACCEPT)
  754. {
  755. string filename = fcd.get_filename();
  756. if (filename.has_prefix(_project.source_dir()))
  757. {
  758. if (filename.has_suffix(".level") && filename != _level._filename)
  759. {
  760. _level.load(filename);
  761. _level.send_level();
  762. send_state();
  763. }
  764. }
  765. else
  766. {
  767. _console_view.loge("editor", "Level not loaded: file must be within `%s`".printf(_project.source_dir()));
  768. }
  769. }
  770. fcd.destroy();
  771. }
  772. private void load_project()
  773. {
  774. FileChooserDialog fcd = new FileChooserDialog("Open Project..."
  775. , this
  776. , FileChooserAction.SELECT_FOLDER
  777. , "Cancel"
  778. , ResponseType.CANCEL
  779. , "Open"
  780. , ResponseType.ACCEPT
  781. );
  782. if (fcd.run() == (int)ResponseType.ACCEPT)
  783. {
  784. string filename = fcd.get_filename();
  785. _console_view.logi("editor", "Loading project `%s`...".printf(filename));
  786. _project.load(filename, _project.toolchain_dir());
  787. _level.load_empty_level();
  788. stop_compiler();
  789. start_compiler();
  790. restart_engine();
  791. _resource_browser.restart_engine();
  792. }
  793. fcd.destroy();
  794. }
  795. private bool save_as()
  796. {
  797. bool saved = false;
  798. FileChooserDialog fcd = new FileChooserDialog("Save As..."
  799. , this
  800. , FileChooserAction.SAVE
  801. , "Cancel"
  802. , ResponseType.CANCEL
  803. , "Save"
  804. , ResponseType.ACCEPT
  805. );
  806. fcd.add_filter(_file_filter);
  807. fcd.set_current_folder(_project.source_dir());
  808. if (fcd.run() == (int)ResponseType.ACCEPT)
  809. {
  810. string filename = fcd.get_filename();
  811. if (filename.has_prefix(_project.source_dir()))
  812. {
  813. _level.save(filename.has_suffix(".level") ? filename : filename + ".level");
  814. saved = true;
  815. }
  816. else
  817. {
  818. _console_view.loge("editor", "Level not saved: file must be within `%s`".printf(_project.source_dir()));
  819. }
  820. }
  821. fcd.destroy();
  822. return saved;
  823. }
  824. private bool save()
  825. {
  826. bool saved = false;
  827. if (_level._filename == null)
  828. {
  829. saved = save_as();
  830. }
  831. else
  832. {
  833. _level.save(_level._filename);
  834. saved = true;
  835. }
  836. return saved;
  837. }
  838. private bool save_timeout()
  839. {
  840. if (_level._filename != null)
  841. save();
  842. return true;
  843. }
  844. private void shutdown()
  845. {
  846. if (_resource_browser != null)
  847. _resource_browser.destroy();
  848. if (_preferences_dialog != null)
  849. _preferences_dialog.destroy();
  850. stop_game();
  851. stop_engine();
  852. stop_compiler();
  853. Gtk.main_quit();
  854. }
  855. private void quit()
  856. {
  857. if (!_database.changed())
  858. {
  859. shutdown();
  860. return;
  861. }
  862. Gtk.MessageDialog md = new Gtk.MessageDialog(this
  863. , Gtk.DialogFlags.MODAL
  864. , Gtk.MessageType.WARNING
  865. , Gtk.ButtonsType.NONE
  866. , "File changed, save?"
  867. );
  868. md.add_button("Quit _without Saving", ResponseType.NO);
  869. md.add_button("_Cancel", ResponseType.CANCEL);
  870. md.add_button("_Save", ResponseType.YES);
  871. md.set_default_response(ResponseType.YES);
  872. int rt = md.run();
  873. md.destroy();
  874. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  875. shutdown();
  876. }
  877. private void on_new_level()
  878. {
  879. if (!_database.changed())
  880. {
  881. new_level();
  882. send_state();
  883. return;
  884. }
  885. Gtk.MessageDialog md = new Gtk.MessageDialog(this
  886. , Gtk.DialogFlags.MODAL
  887. , Gtk.MessageType.WARNING
  888. , Gtk.ButtonsType.NONE
  889. , "File changed, save?"
  890. );
  891. md.add_button("New _without Saving", ResponseType.NO);
  892. md.add_button("_Cancel", ResponseType.CANCEL);
  893. md.add_button("_Save", ResponseType.YES);
  894. md.set_default_response(ResponseType.YES);
  895. int rt = md.run();
  896. md.destroy();
  897. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  898. {
  899. new_level();
  900. send_state();
  901. }
  902. }
  903. private void on_open_level(Gtk.Action action)
  904. {
  905. if (!_database.changed())
  906. {
  907. load_level();
  908. return;
  909. }
  910. Gtk.MessageDialog md = new Gtk.MessageDialog(this
  911. , Gtk.DialogFlags.MODAL
  912. , Gtk.MessageType.WARNING
  913. , Gtk.ButtonsType.NONE
  914. , "File changed, save?"
  915. );
  916. md.add_button("Open _without Saving", ResponseType.NO);
  917. md.add_button("_Cancel", ResponseType.CANCEL);
  918. md.add_button("_Save", ResponseType.YES);
  919. md.set_default_response(ResponseType.YES);
  920. int rt = md.run();
  921. md.destroy();
  922. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  923. load_level();
  924. }
  925. private void on_open_project(Gtk.Action action)
  926. {
  927. if (!_database.changed())
  928. {
  929. load_project();
  930. return;
  931. }
  932. Gtk.MessageDialog md = new Gtk.MessageDialog(this
  933. , Gtk.DialogFlags.MODAL
  934. , Gtk.MessageType.WARNING
  935. , Gtk.ButtonsType.NONE
  936. , "File changed, save?"
  937. );
  938. md.add_button("Open _without Saving", ResponseType.NO);
  939. md.add_button("_Cancel", ResponseType.CANCEL);
  940. md.add_button("_Save", ResponseType.YES);
  941. md.set_default_response(ResponseType.YES);
  942. int rt = md.run();
  943. md.destroy();
  944. if (rt == (int)ResponseType.YES && save() || rt == (int)ResponseType.NO)
  945. load_project();
  946. }
  947. private void on_save(Gtk.Action action)
  948. {
  949. save();
  950. }
  951. private void on_save_as(Gtk.Action action)
  952. {
  953. save_as();
  954. }
  955. // If it returns true then filenames and out_dir are valid.
  956. private bool on_import_begin(Gtk.FileFilter ff, ref SList<string> filenames, ref string out_dir)
  957. {
  958. FileChooserDialog fcd = new FileChooserDialog("Import..."
  959. , this
  960. , FileChooserAction.OPEN
  961. , "Cancel"
  962. , ResponseType.CANCEL
  963. , "Open"
  964. , ResponseType.ACCEPT
  965. );
  966. fcd.select_multiple = true;
  967. fcd.add_filter(ff);
  968. if (fcd.run() != (int)ResponseType.ACCEPT)
  969. {
  970. fcd.destroy();
  971. return false;
  972. }
  973. filenames = fcd.get_filenames();
  974. fcd.destroy();
  975. FileChooserDialog dst = new FileChooserDialog("Select destination folder..."
  976. , this
  977. , FileChooserAction.SELECT_FOLDER
  978. , "Cancel"
  979. , ResponseType.CANCEL
  980. , "Select"
  981. , ResponseType.ACCEPT
  982. );
  983. dst.set_current_folder(_project.source_dir());
  984. if (dst.run() != (int)ResponseType.ACCEPT)
  985. {
  986. dst.destroy();
  987. return false;
  988. }
  989. out_dir = dst.get_filename();
  990. dst.destroy();
  991. return true;
  992. }
  993. private void on_import_end()
  994. {
  995. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  996. _data_compiler.compile.end(res);
  997. });
  998. }
  999. private void on_import_sprites(Gtk.Action action)
  1000. {
  1001. Gtk.FileFilter ff = new FileFilter();
  1002. ff.set_filter_name("Sprite (*.png)");
  1003. ff.add_pattern("*.png");
  1004. SList<string> filenames = new SList<string>();
  1005. string out_dir = "";
  1006. if (on_import_begin(ff, ref filenames, ref out_dir))
  1007. {
  1008. _project.import_sprites(filenames, out_dir);
  1009. on_import_end();
  1010. }
  1011. }
  1012. private void on_import_meshes(Gtk.Action action)
  1013. {
  1014. Gtk.FileFilter ff = new FileFilter();
  1015. ff.set_filter_name("Mesh (*.mesh)");
  1016. ff.add_pattern("*.mesh");
  1017. SList<string> filenames = new SList<string>();
  1018. string out_dir = "";
  1019. if (on_import_begin(ff, ref filenames, ref out_dir))
  1020. {
  1021. _project.import_meshes(filenames, out_dir);
  1022. on_import_end();
  1023. }
  1024. }
  1025. private void on_import_sounds(Gtk.Action action)
  1026. {
  1027. Gtk.FileFilter ff = new FileFilter();
  1028. ff.set_filter_name("Sound (*.wav)");
  1029. ff.add_pattern("*.wav");
  1030. SList<string> filenames = new SList<string>();
  1031. string out_dir = "";
  1032. if (on_import_begin(ff, ref filenames, ref out_dir))
  1033. {
  1034. _project.import_sounds(filenames, out_dir);
  1035. on_import_end();
  1036. }
  1037. }
  1038. private void on_import_textures(Gtk.Action action)
  1039. {
  1040. Gtk.FileFilter ff = new FileFilter();
  1041. ff.set_filter_name("Texture (*.png, *.tga, *.dds, *.ktx, *.pvr)");
  1042. ff.add_pattern("*.png");
  1043. ff.add_pattern("*.tga");
  1044. ff.add_pattern("*.dds");
  1045. ff.add_pattern("*.ktx");
  1046. ff.add_pattern("*.pvr");
  1047. SList<string> filenames = new SList<string>();
  1048. string out_dir = "";
  1049. if (on_import_begin(ff, ref filenames, ref out_dir))
  1050. {
  1051. _project.import_textures(filenames, out_dir);
  1052. on_import_end();
  1053. }
  1054. }
  1055. private void on_preferences(Gtk.Action action)
  1056. {
  1057. if (_preferences_dialog == null)
  1058. {
  1059. _preferences_dialog = new PreferencesDialog(_engine);
  1060. _preferences_dialog.set_transient_for(this);
  1061. _preferences_dialog.delete_event.connect(() => { _preferences_dialog.hide(); return true; });
  1062. }
  1063. _preferences_dialog.show_all();
  1064. }
  1065. private void on_deploy(Gtk.Action action)
  1066. {
  1067. deploy_game();
  1068. }
  1069. private void on_quit(Gtk.Action action)
  1070. {
  1071. quit();
  1072. }
  1073. private void on_show_grid(Gtk.Action action)
  1074. {
  1075. ToggleAction ta = (ToggleAction)action;
  1076. _show_grid = ta.active;
  1077. send_state();
  1078. }
  1079. private void on_custom_grid()
  1080. {
  1081. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Grid size"
  1082. , this
  1083. , DialogFlags.MODAL
  1084. , "Cancel"
  1085. , ResponseType.CANCEL
  1086. , "Ok"
  1087. , ResponseType.OK
  1088. , null
  1089. );
  1090. SpinButtonDouble sb = new SpinButtonDouble(_grid_size, 0.1, 1000);
  1091. sb._x.activate.connect(() => { dg.response(ResponseType.OK); });
  1092. dg.get_content_area().add(sb);
  1093. dg.skip_taskbar_hint = true;
  1094. dg.show_all();
  1095. if (dg.run() == (int)ResponseType.OK)
  1096. {
  1097. _grid_size = sb.value;
  1098. send_state();
  1099. }
  1100. dg.destroy();
  1101. }
  1102. private void on_rotation_snap(Gtk.Action action)
  1103. {
  1104. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Rotation snap"
  1105. , this
  1106. , DialogFlags.MODAL
  1107. , "Cancel"
  1108. , ResponseType.CANCEL
  1109. , "Ok"
  1110. , ResponseType.OK
  1111. , null
  1112. );
  1113. SpinButtonDouble sb = new SpinButtonDouble(_rotation_snap, 1.0, 180.0);
  1114. sb._x.activate.connect(() => { dg.response(ResponseType.OK); });
  1115. dg.get_content_area().add(sb);
  1116. dg.skip_taskbar_hint = true;
  1117. dg.show_all();
  1118. if (dg.run() == (int)ResponseType.OK)
  1119. {
  1120. _rotation_snap = sb.value;
  1121. send_state();
  1122. }
  1123. dg.destroy();
  1124. }
  1125. private void on_create_primitive(Gtk.Action action)
  1126. {
  1127. if (action.name == "primitive-cube")
  1128. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cube"));
  1129. else if (action.name == "primitive-sphere")
  1130. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/sphere"));
  1131. else if (action.name == "primitive-cone")
  1132. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cone"));
  1133. else if (action.name == "primitive-cylinder")
  1134. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/cylinder"));
  1135. else if (action.name == "primitive-plane")
  1136. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/primitives/plane"));
  1137. else if (action.name == "camera")
  1138. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/camera"));
  1139. else if (action.name == "light")
  1140. _engine.send_script(LevelEditorApi.set_placeable("unit", "core/units/light"));
  1141. else if (action.name == "sound-source")
  1142. _engine.send_script(LevelEditorApi.set_placeable("sound", ""));
  1143. _action_group.get_action("place").activate();
  1144. }
  1145. private void on_camera_view(Gtk.Action action)
  1146. {
  1147. if (action.name == "camera-view-perspective")
  1148. _engine.send_script("LevelEditor:camera_view_perspective()");
  1149. else if (action.name == "camera-view-front")
  1150. _engine.send_script("LevelEditor:camera_view_front()");
  1151. else if (action.name == "camera-view-back")
  1152. _engine.send_script("LevelEditor:camera_view_back()");
  1153. else if (action.name == "camera-view-right")
  1154. _engine.send_script("LevelEditor:camera_view_right()");
  1155. else if (action.name == "camera-view-left")
  1156. _engine.send_script("LevelEditor:camera_view_left()");
  1157. else if (action.name == "camera-view-top")
  1158. _engine.send_script("LevelEditor:camera_view_top()");
  1159. else if (action.name == "camera-view-bottom")
  1160. _engine.send_script("LevelEditor:camera_view_bottom()");
  1161. }
  1162. private void on_resource_browser(Gtk.Action action)
  1163. {
  1164. _resource_popover.show_all();
  1165. }
  1166. private void on_fullscreen(Gtk.Action action)
  1167. {
  1168. if (_fullscreen)
  1169. this.unfullscreen();
  1170. else
  1171. this.fullscreen();
  1172. }
  1173. private void on_engine_restart(Gtk.Action action)
  1174. {
  1175. restart_engine();
  1176. }
  1177. private void on_reload_lua(Gtk.Action action)
  1178. {
  1179. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  1180. if (_data_compiler.compile.end(res))
  1181. {
  1182. _engine.send(DeviceApi.pause());
  1183. _engine.send(DeviceApi.reload("lua", "core/editors/level_editor/level_editor"));
  1184. _engine.send(DeviceApi.unpause());
  1185. }
  1186. });
  1187. }
  1188. public void on_snap_to_grid(Gtk.Action action)
  1189. {
  1190. ToggleAction ta = (ToggleAction)action;
  1191. _snap_to_grid = ta.active;
  1192. send_state();
  1193. }
  1194. private void on_debug_render_world(Gtk.Action action)
  1195. {
  1196. ToggleAction ta = (ToggleAction)action;
  1197. _debug_render_world = ta.active;
  1198. send_state();
  1199. }
  1200. private void on_debug_physics_world(Gtk.Action action)
  1201. {
  1202. ToggleAction ta = (ToggleAction)action;
  1203. _debug_physics_world = ta.active;
  1204. send_state();
  1205. }
  1206. private void on_run_game(Gtk.Action action)
  1207. {
  1208. if (_game.is_connected())
  1209. {
  1210. stop_game();
  1211. action.icon_name = "game-run";
  1212. }
  1213. else
  1214. {
  1215. start_game(action.name == "test-level" ? StartGame.TEST : StartGame.NORMAL);
  1216. action.icon_name = "game-stop";
  1217. }
  1218. }
  1219. private void on_undo(Gtk.Action action)
  1220. {
  1221. _database.undo();
  1222. }
  1223. private void on_redo(Gtk.Action action)
  1224. {
  1225. _database.redo();
  1226. }
  1227. private void on_duplicate(Gtk.Action action)
  1228. {
  1229. _level.duplicate_selected_objects();
  1230. }
  1231. private void on_delete(Gtk.Action action)
  1232. {
  1233. _level.destroy_selected_objects();
  1234. }
  1235. private void on_manual(Gtk.Action action)
  1236. {
  1237. try
  1238. {
  1239. AppInfo.launch_default_for_uri("https://dbartolini.github.io/crown/html/v" + CROWN_VERSION, null);
  1240. }
  1241. catch (Error e)
  1242. {
  1243. _console_view.loge("editor", e.message);
  1244. }
  1245. }
  1246. private void on_report_issue(Gtk.Action action)
  1247. {
  1248. try
  1249. {
  1250. AppInfo.launch_default_for_uri("https://github.com/dbartolini/crown/issues", null);
  1251. }
  1252. catch (Error e)
  1253. {
  1254. _console_view.loge("editor", e.message);
  1255. }
  1256. }
  1257. private void on_open_last_log(Gtk.Action action)
  1258. {
  1259. File file = File.new_for_path(_project.data_dir() + "/last.log");
  1260. try
  1261. {
  1262. AppInfo.launch_default_for_uri(file.get_uri(), null);
  1263. }
  1264. catch (Error e)
  1265. {
  1266. _console_view.loge("editor", e.message);
  1267. }
  1268. }
  1269. private void on_about(Gtk.Action action)
  1270. {
  1271. Gtk.AboutDialog dlg = new Gtk.AboutDialog();
  1272. dlg.set_destroy_with_parent(true);
  1273. dlg.set_transient_for(this);
  1274. dlg.set_modal(true);
  1275. try
  1276. {
  1277. dlg.set_logo(new Pixbuf.from_resource("/org/crown/ui/icons/128x128/pepper.png"));
  1278. }
  1279. catch (Error e)
  1280. {
  1281. _console_view.loge("editor", e.message);
  1282. }
  1283. dlg.program_name = "Crown Game Engine";
  1284. dlg.version = CROWN_VERSION;
  1285. dlg.website = "https://github.com/dbartolini/crown";
  1286. dlg.copyright = "Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.";
  1287. dlg.license = "Crown Game Engine.\n"
  1288. + "Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.\n"
  1289. + "\n"
  1290. + "This program is free software; you can redistribute it and/or\n"
  1291. + "modify it under the terms of the GNU General Public License\n"
  1292. + "as published by the Free Software Foundation; either version 2\n"
  1293. + "of the License, or (at your option) any later version.\n"
  1294. + "\n"
  1295. + "This program is distributed in the hope that it will be useful,\n"
  1296. + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1297. + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1298. + "GNU General Public License for more details.\n"
  1299. + "\n"
  1300. + "You should have received a copy of the GNU General Public License\n"
  1301. + "along with this program; if not, write to the Free Software\n"
  1302. + "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
  1303. ;
  1304. dlg.run();
  1305. dlg.destroy();
  1306. }
  1307. private void on_destroy()
  1308. {
  1309. Gtk.main_quit();
  1310. }
  1311. private bool on_delete_event()
  1312. {
  1313. quit();
  1314. return true;
  1315. }
  1316. }
  1317. public static GLib.SubprocessFlags subprocess_flags()
  1318. {
  1319. GLib.SubprocessFlags flags = SubprocessFlags.NONE;
  1320. #if !CROWN_DEBUG
  1321. flags |= SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE;
  1322. #endif
  1323. return flags;
  1324. }
  1325. public static int main (string[] args)
  1326. {
  1327. Gtk.init(ref args);
  1328. Intl.setlocale(LocaleCategory.ALL, "C");
  1329. Gtk.Settings.get_default().gtk_theme_name = "Adwaita";
  1330. Gtk.Settings.get_default().gtk_application_prefer_dark_theme = true;
  1331. Gtk.CssProvider provider = new Gtk.CssProvider();
  1332. Gdk.Screen screen = Gdk.Display.get_default().get_default_screen();
  1333. Gtk.StyleContext.add_provider_for_screen(screen, provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
  1334. provider.load_from_resource("/org/crown/ui/theme/style.css");
  1335. Gtk.IconTheme.get_default().add_resource_path("/org/crown/ui/icons/theme");
  1336. bool create_initial_files = false;
  1337. string source_dir = "";
  1338. string level_resource = "";
  1339. string toolchain_dir = "";
  1340. if (args.length > 1)
  1341. {
  1342. if (!GLib.FileUtils.test(args[1], FileTest.EXISTS) || !GLib.FileUtils.test(args[1], FileTest.IS_DIR))
  1343. {
  1344. stdout.printf("Source directory does not exist or it is not a directory\n");
  1345. return -1;
  1346. }
  1347. source_dir = args[1];
  1348. }
  1349. else
  1350. {
  1351. FileChooserDialog fcd = new FileChooserDialog("Select folder where to create new project..."
  1352. , null
  1353. , FileChooserAction.SELECT_FOLDER
  1354. , "Cancel"
  1355. , ResponseType.CANCEL
  1356. , "Select"
  1357. , ResponseType.ACCEPT
  1358. );
  1359. if (fcd.run() != (int)ResponseType.ACCEPT)
  1360. {
  1361. fcd.destroy();
  1362. return -1;
  1363. }
  1364. string dir = fcd.get_filename();
  1365. fcd.destroy();
  1366. if (GLib.FileUtils.test(dir, FileTest.IS_REGULAR))
  1367. {
  1368. stdout.printf("Source directory can't be a regular file\n");
  1369. return -1;
  1370. }
  1371. source_dir = dir;
  1372. create_initial_files = true;
  1373. }
  1374. if (args.length > 2)
  1375. {
  1376. // Validation is done below after the Project object instantiation
  1377. level_resource = args[2];
  1378. }
  1379. if (args.length > 3)
  1380. {
  1381. if (!GLib.FileUtils.test(args[3], FileTest.EXISTS) || !GLib.FileUtils.test(args[3], FileTest.IS_DIR))
  1382. {
  1383. stdout.printf("Toolchain directory does not exist or it is not a directory\n");
  1384. return -1;
  1385. }
  1386. toolchain_dir = args[3];
  1387. }
  1388. else
  1389. {
  1390. bool found = false;
  1391. /// More desirable paths come first
  1392. string toolchain_paths[] =
  1393. {
  1394. "../..",
  1395. "../../../samples"
  1396. };
  1397. for (int i = 0; i < toolchain_paths.length; ++i)
  1398. {
  1399. string path = Path.build_filename(toolchain_paths[i], "core");
  1400. // Try to locate the toolchain directory
  1401. if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR))
  1402. {
  1403. toolchain_dir = toolchain_paths[i];
  1404. found = true;
  1405. break;
  1406. }
  1407. }
  1408. if (!found)
  1409. {
  1410. stdout.printf("Unable to find the toolchain directory\n");
  1411. return -1;
  1412. }
  1413. }
  1414. Project project = new Project();
  1415. project.load(source_dir, toolchain_dir);
  1416. if (create_initial_files)
  1417. project.create_initial_files();
  1418. Database database = new Database();
  1419. ConsoleClient compiler = new ConsoleClient();
  1420. ConsoleClient engine = new ConsoleClient();
  1421. ConsoleClient game = new ConsoleClient();
  1422. Level level = new Level(database, engine, project);
  1423. LevelEditor editor = new LevelEditor(database, project, level, compiler, engine, game);
  1424. if (level_resource != "")
  1425. {
  1426. string level_path = project.source_dir() + "/" + level_resource + ".level";
  1427. if (!GLib.FileUtils.test(level_path, FileTest.EXISTS) || !GLib.FileUtils.test(level_path, FileTest.IS_REGULAR))
  1428. {
  1429. stdout.printf("Level resource '%s' does not exist.\n", level_resource);
  1430. return -1;
  1431. }
  1432. level.load(level_path);
  1433. }
  1434. else
  1435. {
  1436. level.load_empty_level();
  1437. }
  1438. editor.show_all();
  1439. Gtk.main();
  1440. return 0;
  1441. }
  1442. }