state_machine_editor.vala 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class StateMachineEditor : Gtk.ApplicationWindow
  8. {
  9. public LevelEditorApplication _application;
  10. public DatabaseEditor _database_editor;
  11. public Database _database;
  12. public EditorViewport _editor_viewport;
  13. public RuntimeInstance _runtime;
  14. public ObjectTree _objects_tree;
  15. public ObjectProperties _objects_properties;
  16. public Gtk.Paned _inspector_paned;
  17. public Statusbar _statusbar;
  18. public Gtk.Paned _paned;
  19. public Gtk.Button _cancel;
  20. public Gtk.Button _save;
  21. public Gtk.HeaderBar _header_bar;
  22. public Gtk.Box _box;
  23. public Gtk.Box _events_box;
  24. public Gtk.Box _variables_box;
  25. public Gee.HashMap<string, Gtk.Button> _event_buttons;
  26. public Gee.HashMap<string, Gtk.Box> _variable_sliders;
  27. public InputResource _unit;
  28. public string _state_machine_name;
  29. public Guid _state_machine_id;
  30. public signal void saved();
  31. public StateMachineEditor(LevelEditorApplication application
  32. , Project project
  33. , string boot_dir
  34. , string console_addr
  35. , uint16 console_port
  36. , uint32 undo_redo_size
  37. )
  38. {
  39. Object(application: application);
  40. _application = application;
  41. _database_editor = new DatabaseEditor(project, undo_redo_size);
  42. _database_editor.undo.connect(on_undo);
  43. _database_editor.redo.connect(on_redo);
  44. this.insert_action_group("database", _database_editor._action_group);
  45. _database = _database_editor._database;
  46. _database.objects_created.connect(on_objects_created);
  47. _database.objects_destroyed.connect(on_objects_destroyed);
  48. _database.objects_changed.connect(on_objects_changed);
  49. _objects_tree = new ObjectTree(_database_editor);
  50. _objects_properties = new ObjectProperties(_database_editor);
  51. _database_editor.load_types();
  52. _editor_viewport = new EditorViewport("state_machine_editor"
  53. , _database_editor
  54. , project
  55. , boot_dir
  56. , console_addr
  57. , console_port
  58. , ViewportRenderMode.CONTINUOUS
  59. );
  60. this.insert_action_group("viewport", _editor_viewport._action_group);
  61. _runtime = _editor_viewport._runtime;
  62. _runtime.connected.connect(on_editor_connected);
  63. _runtime.disconnected.connect(on_editor_disconnected);
  64. _runtime.disconnected_unexpected.connect(on_editor_disconnected_unexpected);
  65. _runtime.message_received.connect(on_message_received);
  66. _statusbar = new Statusbar();
  67. _cancel = new Gtk.Button.with_label("Cancel");
  68. _cancel.clicked.connect(() => {
  69. close();
  70. });
  71. _save = new Gtk.Button.with_label("Save & Reload");
  72. _save.get_style_context().add_class("suggested-action");
  73. _save.clicked.connect(save);
  74. _header_bar = new Gtk.HeaderBar();
  75. _header_bar.title = "State Machine Editor";
  76. _header_bar.show_close_button = true;
  77. _header_bar.pack_start(_cancel);
  78. _header_bar.pack_end(_save);
  79. _inspector_paned = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  80. _inspector_paned.pack1(_objects_tree, true, false);
  81. _inspector_paned.pack2(_objects_properties, true, false);
  82. _paned = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  83. _paned.pack1(_editor_viewport, true, false);
  84. _paned.pack2(_inspector_paned, false, false);
  85. this.set_titlebar(_header_bar);
  86. this.set_size_request(1280, 720);
  87. int win_w;
  88. int win_h;
  89. this.get_size(out win_w, out win_h);
  90. _paned.set_position(win_w - 360);
  91. GLib.Menu menu = new GLib.Menu();
  92. GLib.MenuItem mi = null;
  93. mi = new GLib.MenuItem("Edit", null);
  94. mi.set_submenu(make_database_editor_menu());
  95. menu.append_item(mi);
  96. mi = new GLib.MenuItem("Camera", null);
  97. mi.set_submenu(make_camera_view_menu());
  98. menu.append_item(mi);
  99. this.show_menubar = false;
  100. Gtk.MenuBar menubar = new Gtk.MenuBar.from_model(menu);
  101. _box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  102. _box.pack_start(menubar, false);
  103. _box.pack_start(_paned);
  104. _box.pack_start(_statusbar, false);
  105. this.delete_event.connect(on_close_request);
  106. this.add(_box);
  107. _events_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  108. _events_box.halign = Gtk.Align.END;
  109. _events_box.valign = Gtk.Align.START;
  110. _events_box.margin_top = 8;
  111. _events_box.margin_end = 8;
  112. _editor_viewport._overlay.add_overlay(_events_box);
  113. _variables_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  114. _variables_box.halign = Gtk.Align.END;
  115. _variables_box.valign = Gtk.Align.START;
  116. _variables_box.margin_top = 200;
  117. _variables_box.margin_end = 8;
  118. _variables_box.set_size_request(200, -1);
  119. _editor_viewport._overlay.add_overlay(_variables_box);
  120. _event_buttons = new Gee.HashMap<string, Gtk.Button>();
  121. _variable_sliders = new Gee.HashMap<string, Gtk.Box>();
  122. _unit = new InputResource(OBJECT_TYPE_UNIT, _database);
  123. _unit.halign = Gtk.Align.START;
  124. _unit.valign = Gtk.Align.START;
  125. _unit.margin_top = 8;
  126. _unit.margin_start = 8;
  127. _unit.value_changed.connect(on_unit_value_changed);
  128. _editor_viewport._overlay.add_overlay(_unit);
  129. reset();
  130. _editor_viewport.restart_runtime.begin();
  131. }
  132. public void update_window_title()
  133. {
  134. string title = "";
  135. if (_state_machine_name != null) {
  136. if (_database.changed())
  137. title += " • ";
  138. title += (_state_machine_name == LEVEL_EMPTY) ? "untitled" : _state_machine_name;
  139. title += " - ";
  140. }
  141. title += CROWN_EDITOR_NAME;
  142. if (this.title != title)
  143. this.title = title;
  144. }
  145. public void send()
  146. {
  147. if (_unit.value == null)
  148. return;
  149. _runtime.send_script(StateMachineEditorApi.set_unit(_unit.value));
  150. }
  151. public void on_editor_connected(RuntimeInstance ri, string address, int port)
  152. {
  153. send();
  154. }
  155. public void on_editor_disconnected(RuntimeInstance ri)
  156. {
  157. }
  158. public void on_editor_disconnected_unexpected(RuntimeInstance ri)
  159. {
  160. }
  161. public void on_message_received(RuntimeInstance ri, ConsoleClient client, uint8[] json)
  162. {
  163. _application.on_message_received(ri, client, json);
  164. }
  165. public void reset()
  166. {
  167. _database.reset();
  168. _state_machine_name = "";
  169. _state_machine_id = GUID_ZERO;
  170. }
  171. public void save()
  172. {
  173. if (_database.save(_database._project.absolute_path(_state_machine_name) + "." + OBJECT_TYPE_STATE_MACHINE, _state_machine_id) == 0)
  174. saved();
  175. }
  176. public void on_objects_created(Guid?[] object_ids, uint32 flags)
  177. {
  178. Guid last_created = object_ids[object_ids.length - 1];
  179. _objects_tree.set_object(_state_machine_id); // Force update the tree.
  180. _database_editor.selection_set({ last_created }); // Select the root object which must always exits.
  181. create_event_buttons();
  182. create_variable_sliders();
  183. update_window_title();
  184. }
  185. public void on_objects_destroyed(Guid?[] object_ids, uint32 flags = 0)
  186. {
  187. _objects_tree.set_object(_state_machine_id); // Force update the tree.
  188. _database_editor.selection_set({ _state_machine_id }); // Select the root object which must always exits.
  189. create_event_buttons();
  190. create_variable_sliders();
  191. update_window_title();
  192. }
  193. public void on_objects_changed(Guid?[] object_ids, uint32 flags = 0)
  194. {
  195. Guid last_changed = object_ids[object_ids.length - 1];
  196. _objects_tree.set_object(_state_machine_id); // Force update the tree.
  197. _database_editor.selection_set({ last_changed });
  198. create_event_buttons();
  199. create_variable_sliders();
  200. update_window_title();
  201. }
  202. public void do_set_state_machine(string state_machine_name)
  203. {
  204. reset();
  205. string resource_path = ResourceId.path(OBJECT_TYPE_STATE_MACHINE, state_machine_name);
  206. string path = _database._project.absolute_path(resource_path);
  207. if (_database.load_from_path(out _state_machine_id, path, resource_path) != 0)
  208. return;
  209. _state_machine_name = state_machine_name;
  210. _unit.value = state_machine_name;
  211. _objects_tree.set_object(_state_machine_id);
  212. _database_editor.selection_set({ _state_machine_id });
  213. create_event_buttons();
  214. create_variable_sliders();
  215. update_window_title();
  216. send();
  217. }
  218. public void set_state_machine(string state_machine_name)
  219. {
  220. if (state_machine_name == _state_machine_name)
  221. return;
  222. if (!_database.changed()) {
  223. this.do_set_state_machine(state_machine_name);
  224. } else {
  225. Gtk.Dialog dlg = new_resource_changed_dialog(this, _state_machine_name);
  226. dlg.response.connect((response_id) => {
  227. if (response_id == Gtk.ResponseType.NO) {
  228. this.do_set_state_machine(state_machine_name);
  229. } else if (response_id == Gtk.ResponseType.YES) {
  230. this.save();
  231. this.do_set_state_machine(state_machine_name);
  232. }
  233. dlg.destroy();
  234. });
  235. dlg.show_all();
  236. }
  237. }
  238. public void on_undo(int action_id)
  239. {
  240. _statusbar.set_temporary_message("Undo: " + ActionNames[action_id]);
  241. }
  242. public void on_redo(int action_id)
  243. {
  244. _statusbar.set_temporary_message("Redo: " + ActionNames[action_id]);
  245. }
  246. public bool on_close_request(Gdk.EventAny event)
  247. {
  248. this.hide();
  249. return Gdk.EVENT_STOP;
  250. }
  251. public void destroy_event_buttons()
  252. {
  253. foreach (var button in _event_buttons)
  254. button.value.destroy();
  255. _event_buttons.clear();
  256. }
  257. public void create_event_buttons()
  258. {
  259. destroy_event_buttons();
  260. Guid?[] all_states = _database.all_objects_of_type(StringId64(OBJECT_TYPE_STATE_MACHINE_NODE));
  261. Guid?[] all_transitions = _database.all_objects_of_type(StringId64(OBJECT_TYPE_NODE_TRANSITION));
  262. foreach (var state in all_states) {
  263. if (!_database.is_subobject_of(state, _state_machine_id, "states"))
  264. continue;
  265. foreach (var transition in all_transitions) {
  266. if (!_database.is_subobject_of(transition, state, "transitions"))
  267. continue;
  268. string event_name = _database.get_string(transition, "event");
  269. if (!_event_buttons.has_key(event_name))
  270. _event_buttons.set(event_name, create_trigger_event_button(transition, event_name));
  271. }
  272. }
  273. foreach (var button in _event_buttons)
  274. _events_box.pack_start(button.value);
  275. _events_box.show_all();
  276. }
  277. public Gtk.Button create_trigger_event_button(Guid object_id, string event_name)
  278. {
  279. Gtk.Button button = new Gtk.Button.with_label(event_name);
  280. button.clicked.connect(() => {
  281. _runtime.send_script(StateMachineEditorApi.trigger_animation_event(event_name));
  282. });
  283. return button;
  284. }
  285. public void destroy_variable_sliders()
  286. {
  287. foreach (var slider in _variable_sliders)
  288. slider.value.destroy();
  289. _variable_sliders.clear();
  290. }
  291. public void create_variable_sliders()
  292. {
  293. destroy_variable_sliders();
  294. Guid?[] all_variables = _database.all_objects_of_type(StringId64(OBJECT_TYPE_STATE_MACHINE_VARIABLE));
  295. foreach (var variable in all_variables) {
  296. if (!_database.is_subobject_of(variable, _state_machine_id, "variables"))
  297. continue;
  298. string variable_name = _database.get_string(variable, "name");
  299. if (!_variable_sliders.has_key(variable_name)) {
  300. double min_value = _database.get_double(variable, "min");
  301. double max_value = _database.get_double(variable, "max");
  302. double current_value = _database.get_double(variable, "value");
  303. _variable_sliders.set(variable_name, create_variable_slider(variable, variable_name, min_value, max_value, current_value));
  304. }
  305. }
  306. foreach (var slider in _variable_sliders)
  307. _variables_box.pack_start(slider.value);
  308. _variables_box.show_all();
  309. }
  310. public Gtk.Box create_variable_slider(Guid object_id, string variable_name, double min_value, double max_value, double current_value)
  311. {
  312. // Create label.
  313. Gtk.Label label = new Gtk.Label(variable_name);
  314. label.ellipsize = Pango.EllipsizeMode.END;
  315. label.halign = Gtk.Align.START;
  316. label.set_size_request(1, -1);
  317. // Create slider.
  318. Gtk.Adjustment adjustment = new Gtk.Adjustment(current_value, min_value, max_value, 0.01, 0.1, 0.0);
  319. Gtk.Scale scale = new Gtk.Scale(Gtk.Orientation.HORIZONTAL, adjustment);
  320. scale.set_draw_value(true);
  321. scale.set_value_pos(Gtk.PositionType.RIGHT);
  322. scale.set_digits(2);
  323. scale.set_size_request(150, -1);
  324. scale.value_changed.connect(() => {
  325. double value = scale.get_value();
  326. _runtime.send_script(StateMachineEditorApi.set_variable(variable_name, value));
  327. });
  328. Gtk.Box slider_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
  329. slider_box.pack_start(label, false, false, 0);
  330. slider_box.pack_start(scale, true, true, 0);
  331. slider_box.margin_bottom = 4;
  332. slider_box.set_size_request(180, -1);
  333. return slider_box;
  334. }
  335. public void on_unit_value_changed()
  336. {
  337. send();
  338. }
  339. }
  340. } /* namespace Crown */