input_quaternion.vala 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 InputQuaternion : InputField, Gtk.Box
  8. {
  9. public Quaternion _rotation;
  10. public InputDouble _x;
  11. public InputDouble _y;
  12. public InputDouble _z;
  13. public void set_inconsistent(bool inconsistent)
  14. {
  15. }
  16. public bool is_inconsistent()
  17. {
  18. return false;
  19. }
  20. public GLib.Value union_value()
  21. {
  22. return this.value;
  23. }
  24. public void set_union_value(GLib.Value v)
  25. {
  26. this.value = (Quaternion)v;
  27. }
  28. public Quaternion value
  29. {
  30. get
  31. {
  32. return _rotation;
  33. }
  34. set
  35. {
  36. _rotation = value;
  37. // Convert to Euler for displaying.
  38. Vector3 euler = value.to_euler();
  39. _x.value_changed.disconnect(on_value_changed);
  40. _y.value_changed.disconnect(on_value_changed);
  41. _z.value_changed.disconnect(on_value_changed);
  42. _x.value = MathUtils.deg(euler.x);
  43. _y.value = MathUtils.deg(euler.y);
  44. _z.value = MathUtils.deg(euler.z);
  45. _x.value_changed.connect(on_value_changed);
  46. _y.value_changed.connect(on_value_changed);
  47. _z.value_changed.connect(on_value_changed);
  48. value_changed(this);
  49. }
  50. }
  51. public InputQuaternion(int preview_decimals = 2, int edit_decimals = 3)
  52. {
  53. Object(orientation: Gtk.Orientation.HORIZONTAL, spacing: 4);
  54. _rotation = QUATERNION_IDENTITY;
  55. _x = new InputDouble(0.0, -180.0, 180.0, preview_decimals, edit_decimals);
  56. _x.get_style_context().add_class("axis");
  57. _x.get_style_context().add_class("x");
  58. _y = new InputDouble(0.0, -180.0, 180.0, preview_decimals, edit_decimals);
  59. _y.get_style_context().add_class("axis");
  60. _y.get_style_context().add_class("y");
  61. _z = new InputDouble(0.0, -180.0, 180.0, preview_decimals, edit_decimals);
  62. _z.get_style_context().add_class("axis");
  63. _z.get_style_context().add_class("z");
  64. _x.value_changed.connect(on_value_changed);
  65. _y.value_changed.connect(on_value_changed);
  66. _z.value_changed.connect(on_value_changed);
  67. this.pack_start(_x, true);
  68. this.pack_start(_y, true);
  69. this.pack_start(_z, true);
  70. }
  71. private void on_value_changed(InputField p)
  72. {
  73. double x = MathUtils.rad((double)_x.value);
  74. double y = MathUtils.rad((double)_y.value);
  75. double z = MathUtils.rad((double)_z.value);
  76. Quaternion new_rotation = Quaternion.from_euler(x, y, z);
  77. if (_rotation != new_rotation) {
  78. _rotation = new_rotation;
  79. value_changed(this);
  80. }
  81. }
  82. }
  83. } /* namespace Crown */