input_double.vala 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 InputDouble : InputField
  8. {
  9. public bool _inconsistent;
  10. public double _min;
  11. public double _max;
  12. public double _value;
  13. public int _preview_decimals;
  14. public int _edit_decimals;
  15. public Gtk.Entry _entry;
  16. public Gtk.GestureMultiPress _gesture_click;
  17. public Gtk.EventControllerScroll _controller_scroll;
  18. public override void set_inconsistent(bool inconsistent)
  19. {
  20. if (_inconsistent != inconsistent) {
  21. _inconsistent = inconsistent;
  22. if (_inconsistent) {
  23. _entry.text = INCONSISTENT_LABEL;
  24. } else {
  25. set_value_safe(string_to_double(_entry.text, _value));
  26. }
  27. }
  28. }
  29. public override bool is_inconsistent()
  30. {
  31. return _inconsistent;
  32. }
  33. public override GLib.Value union_value()
  34. {
  35. return this.value;
  36. }
  37. public override void set_union_value(GLib.Value v)
  38. {
  39. this.value = (double)v;
  40. }
  41. public double value
  42. {
  43. get
  44. {
  45. return _value;
  46. }
  47. set
  48. {
  49. set_value_safe(value);
  50. }
  51. }
  52. public InputDouble(double val, double min, double max, int preview_decimals = 4, int edit_decimals = 5)
  53. {
  54. _entry = new Gtk.Entry();
  55. _entry.input_purpose = Gtk.InputPurpose.NUMBER;
  56. _entry.set_width_chars(1);
  57. _entry.activate.connect(on_activate);
  58. _entry.focus_in_event.connect(on_focus_in);
  59. _entry.focus_out_event.connect(on_focus_out);
  60. _inconsistent = false;
  61. _min = min;
  62. _max = max;
  63. _preview_decimals = preview_decimals;
  64. _edit_decimals = edit_decimals;
  65. set_value_safe(val);
  66. _gesture_click = new Gtk.GestureMultiPress(_entry);
  67. _gesture_click.pressed.connect(on_button_pressed);
  68. _gesture_click.released.connect(on_button_released);
  69. #if CROWN_GTK3
  70. _entry.scroll_event.connect(() => {
  71. GLib.Signal.stop_emission_by_name(_entry, "scroll-event");
  72. return Gdk.EVENT_PROPAGATE;
  73. });
  74. #else
  75. _controller_scroll = new Gtk.EventControllerScroll(_entry, Gtk.EventControllerScrollFlags.BOTH_AXES);
  76. _controller_scroll.set_propagation_phase(Gtk.PropagationPhase.CAPTURE);
  77. _controller_scroll.scroll.connect(() => {
  78. // Do nothing, just consume the event to stop
  79. // the annoying scroll default behavior.
  80. });
  81. #endif
  82. this.add(_entry);
  83. }
  84. public void on_button_pressed(int n_press, double x, double y)
  85. {
  86. _entry.grab_focus();
  87. }
  88. public void on_button_released(int n_press, double x, double y)
  89. {
  90. uint button = _gesture_click.get_current_button();
  91. if (button == Gdk.BUTTON_PRIMARY && _entry.has_focus) {
  92. if (_inconsistent)
  93. _entry.text = "";
  94. else
  95. _entry.text = print_max_decimals(_value, _edit_decimals);
  96. GLib.Idle.add(() => {
  97. _entry.set_position(-1);
  98. _entry.select_region(0, -1);
  99. return GLib.Source.REMOVE;
  100. });
  101. }
  102. }
  103. public void on_activate()
  104. {
  105. _entry.select_region(0, 0);
  106. _entry.set_position(-1);
  107. if (_entry.text != print_max_decimals(_value, _edit_decimals))
  108. set_value_safe(string_to_double(_entry.text, _value));
  109. else
  110. _entry.text = print_max_decimals(_value, _preview_decimals);
  111. }
  112. public bool on_focus_in(Gdk.EventFocus ev)
  113. {
  114. var app = (LevelEditorApplication)GLib.Application.get_default();
  115. app.entry_any_focus_in(_entry);
  116. if (_inconsistent)
  117. _entry.text = "";
  118. else
  119. _entry.text = print_max_decimals(_value, _edit_decimals);
  120. _entry.set_position(-1);
  121. _entry.select_region(0, -1);
  122. return Gdk.EVENT_PROPAGATE;
  123. }
  124. public bool on_focus_out(Gdk.EventFocus ef)
  125. {
  126. var app = (LevelEditorApplication)GLib.Application.get_default();
  127. app.entry_any_focus_out(_entry);
  128. if (_inconsistent) {
  129. if (_entry.text != "") {
  130. set_value_safe(string_to_double(_entry.text, _value));
  131. } else {
  132. _entry.text = INCONSISTENT_LABEL;
  133. }
  134. } else {
  135. if (_entry.text != print_max_decimals(_value, _edit_decimals))
  136. set_value_safe(string_to_double(_entry.text, _value));
  137. else
  138. _entry.text = print_max_decimals(_value, _preview_decimals);
  139. }
  140. _entry.select_region(0, 0);
  141. return Gdk.EVENT_PROPAGATE;
  142. }
  143. public void set_value_safe(double val)
  144. {
  145. double clamped = val.clamp(_min, _max);
  146. // Convert to text for displaying.
  147. _entry.text = print_max_decimals(clamped, _preview_decimals);
  148. _inconsistent = false;
  149. // Notify value changed.
  150. if (_value != clamped) {
  151. _value = clamped;
  152. value_changed(this);
  153. }
  154. }
  155. /// Returns @a str as double or @a deffault if conversion fails.
  156. public double string_to_double(string str, double deffault)
  157. {
  158. TinyExpr.Variable vars[] =
  159. {
  160. { "x", &_value }
  161. };
  162. int err;
  163. TinyExpr.Expr expr = TinyExpr.compile(str, vars, out err);
  164. return err == 0 ? TinyExpr.eval(expr) : deffault;
  165. }
  166. public void set_min(double min)
  167. {
  168. _min = min;
  169. set_value_safe(_value);
  170. }
  171. public void set_max(double max)
  172. {
  173. _max = max;
  174. set_value_safe(_value);
  175. }
  176. }
  177. } /* namespace Crown */