unit_editor.vala 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 UnitEditor : Gtk.ApplicationWindow
  8. {
  9. public DatabaseEditor _database_editor;
  10. public Database _database;
  11. public EditorViewport _editor_viewport;
  12. public RuntimeInstance _runtime;
  13. public ObjectTree _objects_tree;
  14. public PropertiesView _properties_view;
  15. public Level _level;
  16. public Statusbar _statusbar;
  17. public Gtk.Paned _paned_object;
  18. public Gtk.Paned _paned_inspector;
  19. public Gtk.Button _cancel;
  20. public Gtk.Button _save;
  21. public Gtk.HeaderBar _header_bar;
  22. public Gtk.Box _box;
  23. public string _unit_name;
  24. public Guid _unit_id;
  25. public signal void saved();
  26. public UnitEditor(Gtk.Application application
  27. , Project project
  28. , string boot_dir
  29. , string console_addr
  30. , uint16 console_port
  31. , uint32 undo_redo_size
  32. )
  33. {
  34. Object(application: application);
  35. _database_editor = new DatabaseEditor(project, undo_redo_size);
  36. _database_editor.undo.connect(on_undo);
  37. _database_editor.redo.connect(on_redo);
  38. this.insert_action_group("database", _database_editor._action_group);
  39. _database = _database_editor._database;
  40. _database.objects_created.connect(on_objects_created);
  41. _database.objects_destroyed.connect(on_objects_destroyed);
  42. _database.objects_changed.connect(on_objects_changed);
  43. _database.object_type_added.connect(on_object_type_added);
  44. _objects_tree = new ObjectTree(_database_editor);
  45. _properties_view = new PropertiesView(_database_editor);
  46. _properties_view.register_object_type(OBJECT_TYPE_UNIT, new UnitView(_database));
  47. _database_editor.load_types();
  48. _editor_viewport = new EditorViewport("unit_editor"
  49. , _database_editor
  50. , project
  51. , boot_dir
  52. , console_addr
  53. , console_port
  54. );
  55. this.insert_action_group("viewport", _editor_viewport._action_group);
  56. _runtime = _editor_viewport._runtime;
  57. _runtime.connected.connect(on_editor_connected);
  58. _runtime.disconnected.connect(on_editor_disconnected);
  59. _runtime.disconnected_unexpected.connect(on_editor_disconnected_unexpected);
  60. _level = new Level(_database, _runtime);
  61. _statusbar = new Statusbar();
  62. _cancel = new Gtk.Button.with_label("Cancel");
  63. _cancel.clicked.connect(() => {
  64. close();
  65. });
  66. _save = new Gtk.Button.with_label("Save & Reload");
  67. _save.get_style_context().add_class("suggested-action");
  68. _save.clicked.connect(save);
  69. _header_bar = new Gtk.HeaderBar();
  70. _header_bar.title = "Unit Editor";
  71. _header_bar.show_close_button = true;
  72. _header_bar.pack_start(_cancel);
  73. _header_bar.pack_end(_save);
  74. _paned_inspector = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  75. _paned_inspector.pack1(_editor_viewport, false, false);
  76. _paned_inspector.pack2(_properties_view, false, false);
  77. _paned_object = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  78. _paned_object.pack1(_objects_tree, false, false);
  79. _paned_object.pack2(_paned_inspector, true, false);
  80. this.set_titlebar(_header_bar);
  81. this.set_size_request(1280, 720);
  82. int win_w;
  83. int win_h;
  84. this.get_size(out win_w, out win_h);
  85. _paned_inspector.set_position(530);
  86. _paned_object.set_position(340);
  87. GLib.Menu menu = new GLib.Menu();
  88. GLib.MenuItem mi = null;
  89. mi = new GLib.MenuItem("Edit", null);
  90. mi.set_submenu(make_database_editor_menu());
  91. menu.append_item(mi);
  92. mi = new GLib.MenuItem("Camera", null);
  93. mi.set_submenu(make_camera_view_menu());
  94. menu.append_item(mi);
  95. this.show_menubar = false;
  96. Gtk.MenuBar menubar = new Gtk.MenuBar.from_model(menu);
  97. _box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  98. _box.pack_start(menubar, false);
  99. _box.pack_start(_paned_object);
  100. _box.pack_start(_statusbar, false);
  101. this.delete_event.connect(on_close_request);
  102. this.add(_box);
  103. reset();
  104. _editor_viewport.restart_runtime.begin();
  105. }
  106. public void update_window_title()
  107. {
  108. string title = "";
  109. if (_database.changed())
  110. title += " • ";
  111. title += _unit_name;
  112. title += " - ";
  113. title += CROWN_EDITOR_NAME;
  114. if (this.title != title)
  115. this.title = title;
  116. }
  117. public void send()
  118. {
  119. StringBuilder sb = new StringBuilder();
  120. _level.send_level();
  121. Unit.generate_spawn_unit_commands(sb, { _unit_id, }, _database);
  122. if (sb.len > 0)
  123. _runtime.send_script(sb.str);
  124. _editor_viewport.frame();
  125. sb.erase();
  126. sb.append(LevelEditorApi.frame_objects({ _unit_id }));
  127. if (sb.len > 0)
  128. _runtime.send_script(sb.str);
  129. _editor_viewport.frame();
  130. }
  131. public void on_editor_connected(RuntimeInstance ri, string address, int port)
  132. {
  133. send();
  134. }
  135. public void on_editor_disconnected(RuntimeInstance ri)
  136. {
  137. }
  138. public void on_editor_disconnected_unexpected(RuntimeInstance ri)
  139. {
  140. }
  141. public void reset()
  142. {
  143. _database.reset();
  144. _unit_name = "";
  145. _unit_id = GUID_ZERO;
  146. }
  147. public void save()
  148. {
  149. if (_unit_id == GUID_ZERO)
  150. return;
  151. if (_database.save(_database._project.absolute_path(_unit_name) + "." + OBJECT_TYPE_UNIT, _unit_id) == 0)
  152. saved();
  153. update_window_title();
  154. }
  155. public void on_object_type_added(ObjectTypeInfo info)
  156. {
  157. if ((info.flags & ObjectTypeFlags.UNIT_COMPONENT) != 0) {
  158. Unit.register_component_type(info.name, info.user_data != null ? info.user_data : "");
  159. _properties_view.register_object_type(info.name, null);
  160. } else if (info.name != OBJECT_TYPE_UNIT) { // FIXME
  161. _properties_view.register_object_type(info.name, null);
  162. }
  163. }
  164. public void on_objects_created(Guid?[] object_ids, uint32 flags)
  165. {
  166. if ((flags& ActionTypeFlags.FROM_SERVER) == 0) {
  167. StringBuilder sb = new StringBuilder();
  168. _level.generate_spawn_objects(sb, object_ids);
  169. if (sb.len > 0) {
  170. _runtime.send_script(sb.str);
  171. _editor_viewport.frame();
  172. }
  173. }
  174. Guid last = object_ids[object_ids.length - 1];
  175. _objects_tree.set_object(_unit_id); // Force update the tree.
  176. if (_database.object_type(last) == OBJECT_TYPE_UNIT) {
  177. _database_editor.selection_set({ last }); // Select the objects just created.
  178. _properties_view.set_objects({ last });
  179. } else if ((_database.type_flags(StringId64(_database.object_type(last))) & ObjectTypeFlags.UNIT_COMPONENT) != 0) {
  180. _database_editor.selection_set({ _database.owner(last) });
  181. _properties_view.set_objects({ _database.owner(last) });
  182. }
  183. update_window_title();
  184. }
  185. public void on_objects_destroyed(Guid?[] object_ids, uint32 flags = 0)
  186. {
  187. _objects_tree.set_object(_unit_id); // Force update the tree.
  188. Guid last = object_ids[object_ids.length - 1];
  189. if (_database.object_type(last) == OBJECT_TYPE_UNIT) {
  190. // Select the root object which must always exits.
  191. _database_editor.selection_set({ _unit_id });
  192. _properties_view.set_objects({ _unit_id });
  193. } else if ((_database.type_flags(StringId64(_database.object_type(last))) & ObjectTypeFlags.UNIT_COMPONENT) != 0) {
  194. Guid owner_id = _database.owner(last);
  195. if (_database.is_alive(owner_id)) {
  196. _database_editor.selection_set({ owner_id });
  197. _properties_view.set_objects({ owner_id });
  198. } else {
  199. // Select the root object which must always exits.
  200. _database_editor.selection_set({ _unit_id });
  201. _properties_view.set_objects({ _unit_id });
  202. }
  203. }
  204. update_window_title();
  205. if ((flags& ActionTypeFlags.FROM_SERVER) == 0) {
  206. StringBuilder sb = new StringBuilder();
  207. _level.generate_destroy_objects(sb, object_ids);
  208. if (sb.len > 0) {
  209. _runtime.send_script(sb.str);
  210. _editor_viewport.frame();
  211. }
  212. }
  213. }
  214. public void on_objects_changed(Guid?[] object_ids, uint32 flags = 0)
  215. {
  216. if ((flags& ActionTypeFlags.FROM_SERVER) == 0) {
  217. StringBuilder sb = new StringBuilder();
  218. _level.generate_change_objects(sb, object_ids);
  219. if (sb.len > 0) {
  220. _runtime.send_script(sb.str);
  221. _editor_viewport.frame();
  222. }
  223. }
  224. Guid last_changed = object_ids[object_ids.length - 1];
  225. _objects_tree.set_object(_unit_id); // Force update the tree.
  226. _database_editor.selection_set({ last_changed });
  227. update_window_title();
  228. }
  229. public void do_set_unit(string unit_name)
  230. {
  231. reset();
  232. _level.load(LEVEL_EMPTY);
  233. if (Unit.load_unit(out _unit_id, _database, unit_name) != 0)
  234. return;
  235. _unit_name = unit_name;
  236. Unit unit = Unit(_database, _unit_id);
  237. UndoRedo undo_redo = _database.disable_undo();
  238. unit.set_local_position(VECTOR3_ZERO);
  239. _database.restore_undo(undo_redo);
  240. _objects_tree.set_object(_unit_id);
  241. _database_editor.selection_set({ _unit_id });
  242. update_window_title();
  243. send();
  244. }
  245. public void set_unit(string unit_name)
  246. {
  247. if (_unit_name == unit_name)
  248. return;
  249. if (!_database.changed()) {
  250. this.do_set_unit(unit_name);
  251. } else {
  252. Gtk.Dialog dlg = new_resource_changed_dialog(this, _unit_name);
  253. dlg.response.connect((response_id) => {
  254. if (response_id == Gtk.ResponseType.NO) {
  255. this.do_set_unit(unit_name);
  256. } else if (response_id == Gtk.ResponseType.YES) {
  257. this.save();
  258. this.do_set_unit(unit_name);
  259. }
  260. dlg.destroy();
  261. });
  262. dlg.show_all();
  263. }
  264. }
  265. public void on_undo(int action_id)
  266. {
  267. _statusbar.set_temporary_message("Undo: " + ActionNames[action_id]);
  268. }
  269. public void on_redo(int action_id)
  270. {
  271. _statusbar.set_temporary_message("Redo: " + ActionNames[action_id]);
  272. }
  273. public bool on_close_request(Gdk.EventAny event)
  274. {
  275. this.hide();
  276. return Gdk.EVENT_STOP;
  277. }
  278. }
  279. } /* namespace Crown */