input_resource.vala 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class InputResource : InputField, Gtk.Box
  8. {
  9. // Data
  10. public string _type;
  11. // Widgets
  12. public InputString _name;
  13. public Gtk.Button _selector;
  14. public Gtk.Button _revealer;
  15. public SelectResourceDialog _dialog;
  16. public void set_inconsistent(bool inconsistent)
  17. {
  18. }
  19. public bool is_inconsistent()
  20. {
  21. return false;
  22. }
  23. public GLib.Value union_value()
  24. {
  25. return this.value;
  26. }
  27. public void set_union_value(GLib.Value v)
  28. {
  29. this.value = (string)v;
  30. }
  31. public string value
  32. {
  33. get
  34. {
  35. return _name.value;
  36. }
  37. set
  38. {
  39. _name.value = value;
  40. }
  41. }
  42. public InputResource(string type, Database db)
  43. {
  44. Object(orientation: Gtk.Orientation.HORIZONTAL, spacing: 0);
  45. // Data
  46. _type = type;
  47. // Widgets
  48. _name = new InputString();
  49. _name.set_editable(false);
  50. _name.hexpand = true;
  51. _name.value_changed.connect(on_name_value_changed);
  52. this.pack_start(_name, true, true);
  53. _revealer = new Gtk.Button.from_icon_name("go-jump-symbolic");
  54. _revealer.clicked.connect(on_revealer_clicked);
  55. this.pack_end(_revealer, false);
  56. _selector = new Gtk.Button.from_icon_name("document-open-symbolic");
  57. _selector.clicked.connect(on_selector_clicked);
  58. this.pack_end(_selector, false);
  59. db._project.file_added.connect(on_file_added_or_changed);
  60. db._project.file_changed.connect(on_file_added_or_changed);
  61. db._project.file_removed.connect(on_file_removed);
  62. }
  63. private void on_selector_clicked()
  64. {
  65. if (_dialog == null) {
  66. _dialog = ((LevelEditorApplication)GLib.Application.get_default()).new_select_resource_dialog(_type);
  67. _dialog.resource_selected.connect(on_select_resource_dialog_resource_selected);
  68. }
  69. _dialog.show_all();
  70. _dialog.present();
  71. }
  72. private void on_select_resource_dialog_resource_selected(string type, string name)
  73. {
  74. _name.value = name;
  75. _dialog.hide();
  76. }
  77. private void on_revealer_clicked()
  78. {
  79. var tuple = new GLib.Variant.tuple({_type, _name.value});
  80. GLib.Application.get_default().activate_action("reveal-resource", tuple);
  81. }
  82. private void on_name_value_changed()
  83. {
  84. value_changed(this);
  85. }
  86. private void on_file_added_or_changed(string type, string name, uint64 size, uint64 mtime)
  87. {
  88. if (type == _type && name == _name.value)
  89. value_changed(this);
  90. }
  91. private void on_file_removed(string type, string name)
  92. {
  93. if (type == _type && name == _name.value)
  94. value_changed(this);
  95. }
  96. }
  97. } /* namespace Crown */