input_resource.vala 2.7 KB

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