database_editor.vala 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 DatabaseEditor
  8. {
  9. public const GLib.ActionEntry[] actions =
  10. {
  11. { "undo", on_undo, null, null },
  12. { "redo", on_redo, null, null },
  13. { "duplicate", on_duplicate, null, null },
  14. { "delete", on_delete, null, null },
  15. { "add", on_add, "(ss)", null },
  16. { "select_none", on_select_none, null, null },
  17. };
  18. public UndoRedo _undo_redo;
  19. public Database _database;
  20. public Gee.ArrayList<Guid?> _selection;
  21. public GLib.SimpleActionGroup _action_group;
  22. public signal void undo(int action_id);
  23. public signal void redo(int action_id);
  24. public signal void selection_changed();
  25. public DatabaseEditor(Project project, uint32 undo_redo_size)
  26. {
  27. _action_group = new GLib.SimpleActionGroup();
  28. _action_group.add_action_entries(actions, this);
  29. _undo_redo = new UndoRedo(undo_redo_size);
  30. _database = new Database(project, _undo_redo);
  31. _selection = new Gee.ArrayList<Guid?>(Guid.equal_func);
  32. }
  33. public void load_types()
  34. {
  35. create_object_types(_database);
  36. }
  37. public void on_undo(GLib.SimpleAction action, GLib.Variant? param)
  38. {
  39. int id = _database.undo();
  40. if (id != -1)
  41. undo(id);
  42. }
  43. public void on_redo(GLib.SimpleAction action, GLib.Variant? param)
  44. {
  45. int id = _database.redo();
  46. if (id != -1)
  47. redo(id);
  48. }
  49. public void on_duplicate(GLib.SimpleAction action, GLib.Variant? param)
  50. {
  51. if (_selection.size == 0)
  52. return;
  53. Guid?[] ids = _selection.to_array();
  54. Guid?[] new_ids = new Guid?[ids.length];
  55. for (int i = 0; i < new_ids.length; ++i)
  56. new_ids[i] = Guid.new_guid();
  57. for (int i = 0; i < ids.length; ++i)
  58. _database.duplicate_and_add_to_set(ids[i], new_ids[i]);
  59. _database.add_restore_point((int)ActionType.CREATE_OBJECTS, new_ids);
  60. selection_set(new_ids);
  61. }
  62. public void on_delete(GLib.SimpleAction action, GLib.Variant? param)
  63. {
  64. if (_selection.size == 0)
  65. return;
  66. Guid?[] ids = _selection.to_array();
  67. foreach (Guid id in ids) {
  68. _selection.remove(id);
  69. _database.destroy(id);
  70. }
  71. selection_changed();
  72. _database.add_restore_point((int)ActionType.DESTROY_OBJECTS, ids);
  73. }
  74. public void on_add(GLib.SimpleAction action, GLib.Variant? param)
  75. {
  76. Guid object_id = Guid.parse((string)param.get_child_value(0));
  77. string set_name = (string)param.get_child_value(1);
  78. StringId64 object_type = StringId64(_database.object_type(object_id));
  79. unowned PropertyDefinition[] properties = _database.object_definition(object_type);
  80. int i;
  81. for (i = 0; i < properties.length; ++i) {
  82. unowned PropertyDefinition p = properties[i];
  83. if (p.name == set_name)
  84. break;
  85. }
  86. if (i != properties.length) {
  87. string obj_type_name = _database.type_name(properties[i].object_type);
  88. Guid new_obj = Guid.new_guid();
  89. _database.create(new_obj, obj_type_name);
  90. _database.add_to_set(object_id, properties[i].name, new_obj);
  91. _database.add_restore_point((int)ActionType.CREATE_OBJECTS, { new_obj });
  92. }
  93. }
  94. public void on_select_none(GLib.SimpleAction action, GLib.Variant? param)
  95. {
  96. clear_selection();
  97. }
  98. public void clear_selection()
  99. {
  100. _selection.clear();
  101. selection_changed();
  102. }
  103. public void selection_read(Guid?[] ids)
  104. {
  105. _selection.clear();
  106. _selection.add_all_array(ids);
  107. }
  108. public void selection_set(Guid?[] ids)
  109. {
  110. selection_read(ids);
  111. selection_changed();
  112. }
  113. public void send_selection(RuntimeInstance runtime)
  114. {
  115. Guid?[] ids = _selection.to_array();
  116. runtime.send_script(LevelEditorApi.selection_set(ids));
  117. }
  118. }
  119. } /* namespace Crown */