level_tree_view.vala 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gee;
  6. using Gtk;
  7. namespace Crown
  8. {
  9. public class LevelTreeView : Gtk.Box
  10. {
  11. private enum ItemType
  12. {
  13. FOLDER,
  14. CAMERA,
  15. LIGHT,
  16. SOUND,
  17. UNIT
  18. }
  19. private enum Column
  20. {
  21. TYPE,
  22. GUID,
  23. NAME,
  24. COUNT
  25. }
  26. public enum SortMode
  27. {
  28. NAME_AZ,
  29. NAME_ZA,
  30. TYPE_AZ,
  31. TYPE_ZA,
  32. COUNT;
  33. public string to_label()
  34. {
  35. switch (this) {
  36. case NAME_AZ:
  37. return "Name A-Z";
  38. case NAME_ZA:
  39. return "Name Z-A";
  40. case TYPE_AZ:
  41. return "Type A-Z";
  42. case TYPE_ZA:
  43. return "Type Z-A";
  44. default:
  45. return "Unknown";
  46. }
  47. }
  48. }
  49. // Data
  50. private Level _level;
  51. private Database _db;
  52. // Widgets
  53. private EntrySearch _filter_entry;
  54. private Gtk.TreeStore _tree_store;
  55. private Gtk.TreeModelFilter _tree_filter;
  56. private Gtk.TreeModelSort _tree_sort;
  57. private Gtk.TreeView _tree_view;
  58. private Gtk.TreeSelection _tree_selection;
  59. private Gtk.ScrolledWindow _scrolled_window;
  60. private SortMode _sort_mode;
  61. private Gtk.Box _sort_items_box;
  62. private Gtk.Popover _sort_items_popover;
  63. private Gtk.MenuButton _sort_items;
  64. public LevelTreeView(Database db, Level level)
  65. {
  66. Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0);
  67. // Data
  68. _level = level;
  69. _level.selection_changed.connect(on_level_selection_changed);
  70. _level.object_editor_name_changed.connect(on_object_editor_name_changed);
  71. _db = db;
  72. _db.key_changed.connect(on_database_key_changed);
  73. // Widgets
  74. _filter_entry = new EntrySearch();
  75. _filter_entry.set_placeholder_text("Search...");
  76. _filter_entry.search_changed.connect(on_filter_entry_text_changed);
  77. _tree_store = new Gtk.TreeStore(Column.COUNT
  78. , typeof(int) // Column.TYPE
  79. , typeof(Guid) // Column.GUID
  80. , typeof(string) // Column.NAME
  81. );
  82. _tree_filter = new Gtk.TreeModelFilter(_tree_store, null);
  83. _tree_filter.set_visible_func((model, iter) => {
  84. _tree_view.expand_all();
  85. Value type;
  86. Value name;
  87. model.get_value(iter, Column.TYPE, out type);
  88. model.get_value(iter, Column.NAME, out name);
  89. if ((int)type == ItemType.FOLDER)
  90. return true;
  91. string name_str = (string)name;
  92. string filter_text = _filter_entry.text.down();
  93. return name_str != null
  94. && (filter_text == "" || name_str.down().index_of(filter_text) > -1)
  95. ;
  96. });
  97. _tree_sort = new Gtk.TreeModelSort.with_model(_tree_filter);
  98. _tree_sort.set_default_sort_func((model, iter_a, iter_b) => {
  99. Value type_a;
  100. Value type_b;
  101. model.get_value(iter_a, Column.TYPE, out type_a);
  102. model.get_value(iter_b, Column.TYPE, out type_b);
  103. if ((int)type_a == ItemType.FOLDER || (int)type_b == ItemType.FOLDER)
  104. return -1;
  105. switch (_sort_mode) {
  106. case SortMode.NAME_AZ:
  107. case SortMode.NAME_ZA: {
  108. Value name_a;
  109. Value name_b;
  110. model.get_value(iter_a, Column.NAME, out name_a);
  111. model.get_value(iter_b, Column.NAME, out name_b);
  112. int cmp = strcmp((string)name_a, (string)name_b);
  113. return _sort_mode == SortMode.NAME_AZ ? cmp : -cmp;
  114. }
  115. case SortMode.TYPE_AZ:
  116. case SortMode.TYPE_ZA: {
  117. int cmp = (int)type_a - (int)type_b;
  118. return _sort_mode == SortMode.TYPE_AZ ? cmp : -cmp;
  119. }
  120. default:
  121. return 0;
  122. }
  123. });
  124. Gtk.TreeViewColumn column = new Gtk.TreeViewColumn();
  125. Gtk.CellRendererPixbuf cell_pixbuf = new Gtk.CellRendererPixbuf();
  126. Gtk.CellRendererText cell_text = new Gtk.CellRendererText();
  127. column.pack_start(cell_pixbuf, false);
  128. column.pack_start(cell_text, true);
  129. column.set_cell_data_func(cell_pixbuf, (cell_layout, cell, model, iter) => {
  130. Value type;
  131. model.get_value(iter, LevelTreeView.Column.TYPE, out type);
  132. if ((int)type == LevelTreeView.ItemType.FOLDER)
  133. cell.set_property("icon-name", "browser-folder-symbolic");
  134. else if ((int)type == LevelTreeView.ItemType.UNIT)
  135. cell.set_property("icon-name", "level-object-unit");
  136. else if ((int)type == LevelTreeView.ItemType.SOUND)
  137. cell.set_property("icon-name", "level-object-sound");
  138. else if ((int)type == LevelTreeView.ItemType.LIGHT)
  139. cell.set_property("icon-name", "level-object-light");
  140. else if ((int)type == LevelTreeView.ItemType.CAMERA)
  141. cell.set_property("icon-name", "level-object-camera");
  142. else
  143. cell.set_property("icon-name", "level-object-unknown");
  144. });
  145. column.set_cell_data_func(cell_text, (cell_layout, cell, model, iter) => {
  146. Value name;
  147. model.get_value(iter, LevelTreeView.Column.NAME, out name);
  148. cell.set_property("text", (string)name);
  149. });
  150. _tree_view = new Gtk.TreeView();
  151. _tree_view.append_column(column);
  152. #if 0
  153. // For debugging.
  154. _tree_view.insert_column_with_attributes(-1
  155. , "Guids"
  156. , new gtk.CellRendererText()
  157. , "text"
  158. , Column.GUID
  159. , null
  160. );
  161. #endif
  162. _tree_view.headers_clickable = false;
  163. _tree_view.headers_visible = false;
  164. _tree_view.model = _tree_sort;
  165. _tree_view.button_press_event.connect(on_button_pressed);
  166. _tree_selection = _tree_view.get_selection();
  167. _tree_selection.set_mode(Gtk.SelectionMode.MULTIPLE);
  168. _tree_selection.changed.connect(on_tree_selection_changed);
  169. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  170. _scrolled_window.add(_tree_view);
  171. // Setup sort menu button popover.
  172. _sort_items_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  173. Gtk.RadioButton? button = null;
  174. for (int i = 0; i < SortMode.COUNT; ++i)
  175. button = add_sort_item(button, (SortMode)i);
  176. _sort_items_box.show_all();
  177. _sort_items_popover = new Gtk.Popover(null);
  178. _sort_items_popover.add(_sort_items_box);
  179. _sort_items = new Gtk.MenuButton();
  180. _sort_items.add(new Gtk.Image.from_icon_name("list-sort", Gtk.IconSize.SMALL_TOOLBAR));
  181. _sort_items.get_style_context().add_class("flat");
  182. _sort_items.can_focus = false;
  183. _sort_items.set_popover(_sort_items_popover);
  184. var tree_control = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  185. tree_control.pack_start(_filter_entry, true, true);
  186. tree_control.pack_end(_sort_items, false, false);
  187. this.pack_start(tree_control, false, true, 0);
  188. this.pack_start(_scrolled_window, true, true, 0);
  189. }
  190. private bool on_button_pressed(Gdk.EventButton ev)
  191. {
  192. if (ev.button == Gdk.BUTTON_SECONDARY) {
  193. Gtk.TreePath path;
  194. Gtk.TreeViewColumn column;
  195. if (_tree_view.get_path_at_pos((int)ev.x, (int)ev.y, out path, out column, null, null)) {
  196. if (!_tree_selection.path_is_selected(path)) {
  197. _tree_selection.unselect_all();
  198. _tree_selection.select_path(path);
  199. }
  200. } else { // Clicked on empty space.
  201. return Gdk.EVENT_PROPAGATE;
  202. }
  203. Gtk.Menu menu = new Gtk.Menu();
  204. Gtk.MenuItem mi;
  205. mi = new Gtk.MenuItem.with_label("Rename...");
  206. mi.activate.connect(() => {
  207. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("New Name"
  208. , (Gtk.Window)this.get_toplevel()
  209. , DialogFlags.MODAL
  210. , "Cancel"
  211. , ResponseType.CANCEL
  212. , "Ok"
  213. , ResponseType.OK
  214. , null
  215. );
  216. EntryText sb = new EntryText();
  217. _tree_selection.selected_foreach((model, path, iter) => {
  218. Value name;
  219. model.get_value(iter, Column.NAME, out name);
  220. sb.value = (string)name;
  221. return;
  222. });
  223. sb.activate.connect(() => { dg.response(ResponseType.OK); });
  224. dg.get_content_area().add(sb);
  225. dg.skip_taskbar_hint = true;
  226. dg.show_all();
  227. if (dg.run() == (int)ResponseType.OK) {
  228. string cur_name = "";
  229. string new_name = "";
  230. Guid object_id = GUID_ZERO;
  231. _tree_selection.selected_foreach((model, path, iter) => {
  232. Value type;
  233. model.get_value(iter, Column.TYPE, out type);
  234. if ((int)type == ItemType.FOLDER)
  235. return;
  236. Value name;
  237. model.get_value(iter, Column.NAME, out name);
  238. cur_name = (string)name;
  239. Value guid;
  240. model.get_value(iter, Column.GUID, out guid);
  241. object_id = (Guid)guid;
  242. new_name = sb.text.strip();
  243. });
  244. if (new_name != "" && new_name != cur_name)
  245. _level.object_set_editor_name(object_id, new_name);
  246. }
  247. dg.destroy();
  248. });
  249. if (_tree_selection.count_selected_rows() == 1)
  250. menu.add(mi);
  251. mi = new Gtk.MenuItem.with_label("Duplicate");
  252. mi.activate.connect(() => {
  253. GLib.Application.get_default().activate_action("duplicate", null);
  254. });
  255. menu.add(mi);
  256. mi = new Gtk.MenuItem.with_label("Delete");
  257. mi.activate.connect(() => {
  258. GLib.Application.get_default().activate_action("delete", null);
  259. });
  260. menu.add(mi);
  261. menu.show_all();
  262. menu.popup_at_pointer(ev);
  263. return Gdk.EVENT_STOP;
  264. }
  265. return Gdk.EVENT_PROPAGATE;
  266. }
  267. private void on_tree_selection_changed()
  268. {
  269. _level.selection_changed.disconnect(on_level_selection_changed);
  270. Gee.ArrayList<Guid?> ids = new Gee.ArrayList<Guid?>();
  271. _tree_selection.selected_foreach((model, path, iter) => {
  272. Value type;
  273. model.get_value(iter, Column.TYPE, out type);
  274. if ((int)type == ItemType.FOLDER)
  275. return;
  276. Value id;
  277. model.get_value(iter, Column.GUID, out id);
  278. ids.add((Guid)id);
  279. });
  280. _level.selection_set(ids.to_array());
  281. _level.selection_changed.connect(on_level_selection_changed);
  282. }
  283. private void on_level_selection_changed(Gee.ArrayList<Guid?> selection)
  284. {
  285. _tree_selection.changed.disconnect(on_tree_selection_changed);
  286. _tree_selection.unselect_all();
  287. Gtk.TreePath? last_selected = null;
  288. _tree_sort.foreach ((model, path, iter) => {
  289. Value type;
  290. model.get_value(iter, Column.TYPE, out type);
  291. if ((int)type == ItemType.FOLDER)
  292. return false;
  293. Value id;
  294. model.get_value(iter, Column.GUID, out id);
  295. foreach (Guid? guid in selection) {
  296. if ((Guid)id == guid) {
  297. _tree_selection.select_iter(iter);
  298. last_selected = path;
  299. return false;
  300. }
  301. }
  302. return false;
  303. });
  304. if (last_selected != null)
  305. _tree_view.scroll_to_cell(last_selected, null, false, 0.0f, 0.0f);
  306. _tree_selection.changed.connect(on_tree_selection_changed);
  307. }
  308. private void on_object_editor_name_changed(Guid object_id, string name)
  309. {
  310. _tree_sort.foreach ((model, path, iter) => {
  311. Value type;
  312. model.get_value(iter, Column.TYPE, out type);
  313. if ((int)type == ItemType.FOLDER)
  314. return false;
  315. Value guid;
  316. model.get_value(iter, Column.GUID, out guid);
  317. Guid guid_model = (Guid)guid;
  318. if (guid_model == object_id) {
  319. Gtk.TreeIter iter_filter;
  320. Gtk.TreeIter iter_model;
  321. _tree_sort.convert_iter_to_child_iter(out iter_filter, iter);
  322. _tree_filter.convert_iter_to_child_iter(out iter_model, iter_filter);
  323. _tree_store.set(iter_model
  324. , Column.NAME
  325. , name
  326. , -1
  327. );
  328. return true;
  329. }
  330. return false;
  331. });
  332. }
  333. private ItemType item_type(Unit u)
  334. {
  335. if (u.is_light())
  336. return ItemType.LIGHT;
  337. else if (u.is_camera())
  338. return ItemType.CAMERA;
  339. else
  340. return ItemType.UNIT;
  341. }
  342. private void on_database_key_changed(Guid id, string key)
  343. {
  344. if (id != _level._id)
  345. return;
  346. if (key != "units" && key != "sounds")
  347. return;
  348. _tree_selection.changed.disconnect(on_tree_selection_changed);
  349. _tree_view.model = null;
  350. _tree_store.clear();
  351. Gtk.TreeIter units_iter;
  352. _tree_store.insert_with_values(out units_iter
  353. , null
  354. , -1
  355. , Column.TYPE
  356. , ItemType.FOLDER
  357. , Column.GUID
  358. , GUID_ZERO
  359. , Column.NAME
  360. , "Units"
  361. , -1
  362. );
  363. Gtk.TreeIter sounds_iter;
  364. _tree_store.insert_with_values(out sounds_iter
  365. , null
  366. , -1
  367. , Column.TYPE
  368. , ItemType.FOLDER
  369. , Column.GUID
  370. , GUID_ZERO
  371. , Column.NAME
  372. , "Sounds"
  373. , -1
  374. );
  375. HashSet<Guid?> units = _db.get_property_set(_level._id, "units", new HashSet<Guid?>());
  376. HashSet<Guid?> sounds = _db.get_property_set(_level._id, "sounds", new HashSet<Guid?>());
  377. foreach (Guid unit_id in units) {
  378. Unit u = Unit(_level._db, unit_id);
  379. Gtk.TreeIter iter;
  380. _tree_store.insert_with_values(out iter
  381. , units_iter
  382. , -1
  383. , Column.TYPE
  384. , item_type(u)
  385. , Column.GUID
  386. , unit_id
  387. , Column.NAME
  388. , _level.object_editor_name(unit_id)
  389. , -1
  390. );
  391. }
  392. foreach (Guid sound in sounds) {
  393. Gtk.TreeIter iter;
  394. _tree_store.insert_with_values(out iter
  395. , sounds_iter
  396. , -1
  397. , Column.TYPE
  398. , ItemType.SOUND
  399. , Column.GUID
  400. , sound
  401. , Column.NAME
  402. , _level.object_editor_name(sound)
  403. , -1
  404. );
  405. }
  406. _tree_view.model = _tree_sort;
  407. _tree_view.expand_all();
  408. _tree_selection.changed.connect(on_tree_selection_changed);
  409. }
  410. private void on_filter_entry_text_changed()
  411. {
  412. _tree_selection.changed.disconnect(on_tree_selection_changed);
  413. _tree_filter.refilter();
  414. _tree_selection.changed.connect(on_tree_selection_changed);
  415. }
  416. private Gtk.RadioButton add_sort_item(Gtk.RadioButton? group, SortMode mode)
  417. {
  418. var button = new Gtk.RadioButton.with_label_from_widget(group, mode.to_label());
  419. button.toggled.connect(() => {
  420. _sort_mode = mode;
  421. _tree_filter.refilter();
  422. _sort_items_popover.popdown();
  423. });
  424. _sort_items_box.pack_start(button, false, false);
  425. return button;
  426. }
  427. }
  428. } /* namespace Crown */