property_grid.vala 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 PropertyGrid : Gtk.Grid
  8. {
  9. public Database? _db;
  10. public Guid _id;
  11. public Guid _component_id;
  12. public int _rows;
  13. public double _order;
  14. public bool _visible;
  15. public Gee.HashMap<string, InputField> _widgets;
  16. public Gee.HashMap<InputField, PropertyDefinition?> _definitions;
  17. public PropertyGrid(Database? db = null)
  18. {
  19. this.row_spacing = 4;
  20. this.row_homogeneous = true;
  21. this.column_spacing = 12;
  22. // Data
  23. _db = db;
  24. _id = GUID_ZERO;
  25. _component_id = GUID_ZERO;
  26. _rows = 0;
  27. _order = 0.0;
  28. _visible = true;
  29. _widgets = new Gee.HashMap<string, InputField>();
  30. _definitions = new Gee.HashMap<InputField, PropertyDefinition?>();
  31. }
  32. public PropertyGrid.from_object_type(StringId64 type, Database db)
  33. {
  34. this.row_spacing = 4;
  35. this.row_homogeneous = true;
  36. this.column_spacing = 12;
  37. // Data
  38. _db = db;
  39. _id = GUID_ZERO;
  40. _component_id = GUID_ZERO;
  41. _rows = 0;
  42. _order = db.object_type_info(type).ui_order;
  43. _visible = true;
  44. _widgets = new Gee.HashMap<string, InputField>();
  45. _definitions = new Gee.HashMap<InputField, PropertyDefinition?>();
  46. add_object_type(db.object_definition(type));
  47. }
  48. public Gtk.Widget add_row(string label, Gtk.Widget w)
  49. {
  50. Gtk.Label l = new Gtk.Label(label);
  51. l.width_chars = 13;
  52. l.xalign = 1.0f;
  53. l.yalign = 0.5f;
  54. w.hexpand = true;
  55. this.attach(l, 0, (int)_rows);
  56. this.attach(w, 1, (int)_rows);
  57. ++_rows;
  58. return l;
  59. }
  60. public void add_object_type(PropertyDefinition[] properties)
  61. {
  62. foreach (PropertyDefinition def in properties) {
  63. // Create input field.
  64. InputField? p = null;
  65. switch (def.type) {
  66. case PropertyType.BOOL:
  67. p = new InputBool();
  68. break;
  69. case PropertyType.DOUBLE:
  70. if (def.editor == PropertyEditorType.ANGLE)
  71. p = new InputAngle((double)def.deffault, (double)def.min, (double)def.max);
  72. else
  73. p = new InputDouble((double)def.deffault, (double)def.min, (double)def.max);
  74. break;
  75. case PropertyType.STRING:
  76. if (def.editor == PropertyEditorType.ENUM)
  77. p = new InputEnum((string)def.deffault, def.enum_labels, def.enum_values);
  78. else if (def.editor == PropertyEditorType.RESOURCE)
  79. p = new InputResource(def.resource_type, _db);
  80. else
  81. p = new InputString();
  82. break;
  83. case PropertyType.GUID:
  84. p = new InputString();
  85. break;
  86. case PropertyType.VECTOR3:
  87. if (def.editor == PropertyEditorType.COLOR)
  88. p = new InputColor3();
  89. else
  90. p = new InputVector3((Vector3)def.deffault, (Vector3)def.min, (Vector3)def.max);
  91. break;
  92. case PropertyType.QUATERNION:
  93. p = new InputQuaternion();
  94. break;
  95. case PropertyType.OBJECTS_SET:
  96. continue;
  97. default:
  98. case PropertyType.NULL:
  99. assert(false);
  100. break;
  101. }
  102. p.value_changed.connect(on_property_value_changed);
  103. _widgets[def.name] = p;
  104. _definitions[p] = def;
  105. if (!def.hidden)
  106. add_row(def.label, p);
  107. }
  108. }
  109. public void on_property_value_changed(InputField p)
  110. {
  111. if (p.is_inconsistent())
  112. return;
  113. PropertyDefinition def = _definitions[p];
  114. bool changed = false;
  115. if (def.type == PropertyType.BOOL) {
  116. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  117. Unit u = Unit(_db, _id);
  118. if (u.get_component_property_bool(_component_id, def.name, (bool)def.deffault) != p.union_value()) {
  119. u.set_component_property_bool(_component_id, def.name, (bool)p.union_value());
  120. changed = true;
  121. }
  122. } else {
  123. if (_db.get_property_bool(_id, def.name, (bool)def.deffault) != p.union_value()) {
  124. _db.set_property_bool(_id, def.name, (bool)p.union_value());
  125. changed = true;
  126. }
  127. }
  128. } else if (def.type == PropertyType.DOUBLE) {
  129. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  130. Unit u = Unit(_db, _id);
  131. if (u.get_component_property_double(_component_id, def.name, (double)def.deffault) != p.union_value()) {
  132. u.set_component_property_double(_component_id, def.name, (double)p.union_value());
  133. changed = true;
  134. }
  135. } else {
  136. if (_db.get_property_double(_id, def.name, (double)def.deffault) != p.union_value()) {
  137. _db.set_property_double(_id, def.name, (double)p.union_value());
  138. changed = true;
  139. }
  140. }
  141. } else if (def.type == PropertyType.STRING) {
  142. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  143. Unit u = Unit(_db, _id);
  144. if (u.get_component_property_string(_component_id, def.name, (string)def.deffault) != (string)p.union_value()) {
  145. u.set_component_property_string(_component_id, def.name, (string)p.union_value());
  146. changed = true;
  147. }
  148. } else {
  149. if (_db.get_property_string(_id, def.name, (string)def.deffault) != (string)p.union_value()) {
  150. _db.set_property_string(_id, def.name, (string)p.union_value());
  151. changed = true;
  152. }
  153. }
  154. } else if (def.type == PropertyType.GUID) {
  155. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  156. Unit u = Unit(_db, _id);
  157. if (Guid.equal_func(u.get_component_property_guid(_component_id, def.name, (Guid)def.deffault), Guid.parse((string)p.union_value())) == false) {
  158. u.set_component_property_guid(_component_id, def.name, Guid.parse((string)p.union_value()));
  159. changed = true;
  160. }
  161. } else {
  162. if (Guid.equal_func(_db.get_property_guid(_id, def.name, (Guid)def.deffault), Guid.parse((string)p.union_value())) == false) {
  163. _db.set_property_guid(_id, def.name, Guid.parse((string)p.union_value()));
  164. changed = true;
  165. }
  166. }
  167. } else if (def.type == PropertyType.VECTOR3) {
  168. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  169. Unit u = Unit(_db, _id);
  170. if (Vector3.equal_func(u.get_component_property_vector3(_component_id, def.name, (Vector3)def.deffault), (Vector3)p.union_value()) == false) {
  171. u.set_component_property_vector3(_component_id, def.name, (Vector3)p.union_value());
  172. changed = true;
  173. }
  174. } else {
  175. if (Vector3.equal_func(_db.get_property_vector3(_id, def.name, (Vector3)def.deffault), (Vector3)p.union_value()) == false) {
  176. _db.set_property_vector3(_id, def.name, (Vector3)p.union_value());
  177. changed = true;
  178. }
  179. }
  180. } else if (def.type == PropertyType.QUATERNION) {
  181. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  182. Unit u = Unit(_db, _id);
  183. if (Quaternion.equal_func(u.get_component_property_quaternion(_component_id, def.name, (Quaternion)def.deffault), (Quaternion)p.union_value()) == false) {
  184. u.set_component_property_quaternion(_component_id, def.name, (Quaternion)p.union_value());
  185. changed = true;
  186. }
  187. } else {
  188. if (Quaternion.equal_func(_db.get_property_quaternion(_id, def.name, (Quaternion)def.deffault), (Quaternion)p.union_value()) == false) {
  189. _db.set_property_quaternion(_id, def.name, (Quaternion)p.union_value());
  190. changed = true;
  191. }
  192. }
  193. } else {
  194. loge("Unknown property type");
  195. }
  196. foreach (var e in _definitions) {
  197. PropertyDefinition other_def = e.value;
  198. if (other_def.enum_property == def.name) {
  199. if (other_def.enum_callback != null)
  200. other_def.enum_callback(p, (InputEnum)_widgets[other_def.name], _db._project);
  201. if (other_def.resource_callback != null)
  202. other_def.resource_callback(p, (InputResource)_widgets[other_def.name], _db._project);
  203. }
  204. }
  205. if (changed)
  206. _db.add_restore_point(ActionType.CHANGE_OBJECTS, new Guid?[] { _id });
  207. }
  208. public virtual void update()
  209. {
  210. foreach (var e in _definitions) {
  211. InputField p = e.key;
  212. PropertyDefinition def = e.value;
  213. if (def.type == PropertyType.BOOL) {
  214. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  215. Unit u = Unit(_db, _id);
  216. p.set_union_value(u.get_component_property_bool(_component_id, def.name, (bool)def.deffault));
  217. } else {
  218. p.set_union_value(_db.get_property_bool(_id, def.name, (bool)def.deffault));
  219. }
  220. } else if (def.type == PropertyType.DOUBLE) {
  221. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  222. Unit u = Unit(_db, _id);
  223. p.set_union_value(u.get_component_property_double(_component_id, def.name, (double)def.deffault));
  224. } else {
  225. p.set_union_value(_db.get_property_double(_id, def.name, (double)def.deffault));
  226. }
  227. } else if (def.type == PropertyType.STRING) {
  228. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  229. Unit u = Unit(_db, _id);
  230. p.set_union_value(u.get_component_property_string(_component_id, def.name, (string)def.deffault));
  231. } else {
  232. p.set_union_value(_db.get_property_string(_id, def.name, (string)def.deffault));
  233. }
  234. } else if (def.type == PropertyType.GUID) {
  235. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  236. Unit u = Unit(_db, _id);
  237. p.set_union_value(u.get_component_property_guid(_component_id, def.name, (Guid)def.deffault));
  238. } else {
  239. p.set_union_value(_db.get_property_guid(_id, def.name, (Guid)def.deffault));
  240. }
  241. } else if (def.type == PropertyType.VECTOR3) {
  242. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  243. Unit u = Unit(_db, _id);
  244. p.set_union_value(u.get_component_property_vector3(_component_id, def.name, (Vector3)def.deffault));
  245. } else {
  246. p.set_union_value(_db.get_property_vector3(_id, def.name, (Vector3)def.deffault));
  247. }
  248. } else if (def.type == PropertyType.QUATERNION) {
  249. if (_db.object_type(_id) == OBJECT_TYPE_UNIT) {
  250. Unit u = Unit(_db, _id);
  251. p.set_union_value(u.get_component_property_quaternion(_component_id, def.name, (Quaternion)def.deffault));
  252. } else {
  253. p.set_union_value(_db.get_property_quaternion(_id, def.name, (Quaternion)def.deffault));
  254. }
  255. } else {
  256. loge("Unknown property value type");
  257. }
  258. }
  259. }
  260. }
  261. public class PropertyGridSet : Gtk.Box
  262. {
  263. public Gtk.ListBox _list_box;
  264. public PropertyGridSet()
  265. {
  266. Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0);
  267. _list_box = new Gtk.ListBox();
  268. _list_box.selection_mode = Gtk.SelectionMode.NONE;
  269. _list_box.margin_bottom
  270. = this.margin_end
  271. = this.margin_start
  272. = this.margin_top
  273. = 12
  274. ;
  275. _list_box.set_sort_func(sort_function);
  276. _list_box.set_filter_func(filter_function);
  277. this.pack_start(_list_box);
  278. }
  279. public static int sort_function(Gtk.ListBoxRow row1, Gtk.ListBoxRow row2)
  280. {
  281. Expander e1 = (Expander)row1.get_child();
  282. Expander e2 = (Expander)row2.get_child();
  283. double order = ((PropertyGrid)e1._child)._order - ((PropertyGrid)e2._child)._order;
  284. return (int)order;
  285. }
  286. public static bool filter_function(Gtk.ListBoxRow row)
  287. {
  288. Expander e = (Expander)row.get_child();
  289. return ((PropertyGrid)e._child)._visible;
  290. }
  291. public Expander add_property_grid(PropertyGrid cv, string label)
  292. {
  293. Gtk.Label l = new Gtk.Label(null);
  294. l.set_markup("<b>%s</b>".printf(label));
  295. l.xalign = 0.0f;
  296. l.yalign = 0.5f;
  297. Expander e = new Expander();
  298. e.custom_header = l;
  299. e.expanded = true;
  300. e.add(cv);
  301. Gtk.ListBoxRow row = new Gtk.ListBoxRow();
  302. row.can_focus = false;
  303. row.add(e);
  304. _list_box.add(row);
  305. return e;
  306. }
  307. public Expander add_property_grid_optional(PropertyGrid cv, string label, InputBool InputBool)
  308. {
  309. Gtk.Label l = new Gtk.Label(null);
  310. l.set_markup("<b>%s</b>".printf(label));
  311. l.xalign = 0.0f;
  312. l.yalign = 0.5f;
  313. Gtk.Box b = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
  314. b.pack_start(InputBool, false, false);
  315. b.pack_start(l, false, false);
  316. Expander e = new Expander();
  317. e.custom_header = b;
  318. e.expanded = true;
  319. e.add(cv);
  320. Gtk.ListBoxRow row = new Gtk.ListBoxRow();
  321. row.can_focus = false;
  322. row.add(e);
  323. _list_box.add(row);
  324. return e;
  325. }
  326. }
  327. } /* namespace Crown */