input_resource.vala 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 InputResource : InputField
  8. {
  9. public const string UNSET_RESOURCE = "(None)";
  10. // Data
  11. public string _type;
  12. public bool _name_unset;
  13. // Widgets
  14. public InputString _name;
  15. public Gtk.Button _selector;
  16. public Gtk.Button _revealer;
  17. public SelectResourceDialog _dialog;
  18. public Gtk.Box _box;
  19. public override void set_inconsistent(bool inconsistent)
  20. {
  21. }
  22. public override bool is_inconsistent()
  23. {
  24. return false;
  25. }
  26. public override GLib.Value union_value()
  27. {
  28. return this.value;
  29. }
  30. public override void set_union_value(GLib.Value v)
  31. {
  32. this.value = (string?)v;
  33. }
  34. public string? value
  35. {
  36. get
  37. {
  38. return _name_unset ? null : _name.value;
  39. }
  40. set
  41. {
  42. _name_unset = value == null;
  43. _name.value = value != null ? value : UNSET_RESOURCE;
  44. _revealer.sensitive = value != null;
  45. }
  46. }
  47. public InputResource(string type, Database db)
  48. {
  49. _box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  50. // Data
  51. _type = type;
  52. _name_unset = true;
  53. // Widgets
  54. _name = new InputString();
  55. _name._entry.set_editable(false);
  56. _name.hexpand = true;
  57. _name.value_changed.connect(on_name_value_changed);
  58. _box.pack_start(_name, true, true);
  59. _revealer = new Gtk.Button.from_icon_name("go-jump-symbolic");
  60. _revealer.clicked.connect(on_revealer_clicked);
  61. _revealer.set_tooltip_text("Reveal in the project browser.");
  62. _box.pack_end(_revealer, false);
  63. _selector = new Gtk.Button.from_icon_name("document-open-symbolic");
  64. _selector.set_tooltip_text("Select a resource.");
  65. _selector.clicked.connect(on_selector_clicked);
  66. _box.pack_end(_selector, false);
  67. this.value = null;
  68. db._project.file_added.connect(on_file_added_or_changed);
  69. db._project.file_changed.connect(on_file_added_or_changed);
  70. db._project.file_removed.connect(on_file_removed);
  71. this.add(_box);
  72. }
  73. public void on_selector_clicked()
  74. {
  75. if (_dialog == null) {
  76. _dialog = ((LevelEditorApplication)GLib.Application.get_default()).new_select_resource_dialog(_type);
  77. _dialog.resource_selected.connect(on_select_resource_dialog_resource_selected);
  78. }
  79. _dialog.show_all();
  80. _dialog.present();
  81. }
  82. public void on_select_resource_dialog_resource_selected(string type, string name)
  83. {
  84. this.value = name;
  85. _dialog.hide();
  86. }
  87. public void on_revealer_clicked()
  88. {
  89. var tuple = new GLib.Variant.tuple({_type, this.value});
  90. GLib.Application.get_default().activate_action("reveal-resource", tuple);
  91. }
  92. public void on_name_value_changed()
  93. {
  94. value_changed(this);
  95. }
  96. public void on_file_added_or_changed(string type, string name, uint64 size, uint64 mtime)
  97. {
  98. if (type == _type && name == _name.value)
  99. value_changed(this);
  100. }
  101. public void on_file_removed(string type, string name)
  102. {
  103. if (type == _type && name == _name.value)
  104. value_changed(this);
  105. }
  106. }
  107. } /* namespace Crown */