entry_double.vala 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 EntryDouble : Gtk.Entry, Property
  8. {
  9. public bool _inconsistent;
  10. public double _min;
  11. public double _max;
  12. public double _value;
  13. public bool _stop_emit;
  14. public string _preview_fmt;
  15. public string _edit_fmt;
  16. // Signals
  17. public signal void value_changed();
  18. public void set_inconsistent(bool inconsistent)
  19. {
  20. if (_inconsistent != inconsistent) {
  21. _inconsistent = inconsistent;
  22. _stop_emit = true;
  23. if (_inconsistent) {
  24. this.text = INCONSISTENT_LABEL;
  25. } else {
  26. set_value_safe(string_to_double(this.text, _value));
  27. }
  28. _stop_emit = false;
  29. }
  30. }
  31. public bool is_inconsistent()
  32. {
  33. return _inconsistent;
  34. }
  35. public Value? generic_value()
  36. {
  37. return this.value;
  38. }
  39. public void set_generic_value(Value? val)
  40. {
  41. this.value = (double)val;
  42. }
  43. public double value
  44. {
  45. get
  46. {
  47. return _value;
  48. }
  49. set
  50. {
  51. _stop_emit = true;
  52. set_value_safe(value);
  53. _stop_emit = false;
  54. }
  55. }
  56. public EntryDouble(double val, double min, double max, string preview_fmt = "%.6g", string edit_fmt = "%.17g")
  57. {
  58. this.input_purpose = Gtk.InputPurpose.NUMBER;
  59. this.set_width_chars(1);
  60. this.scroll_event.connect(on_scroll);
  61. this.button_press_event.connect(on_button_press);
  62. this.button_release_event.connect(on_button_release);
  63. this.activate.connect(on_activate);
  64. this.focus_in_event.connect(on_focus_in);
  65. this.focus_out_event.connect(on_focus_out);
  66. _inconsistent = false;
  67. _min = min;
  68. _max = max;
  69. _preview_fmt = preview_fmt;
  70. _edit_fmt = edit_fmt;
  71. _stop_emit = true;
  72. set_value_safe(val);
  73. _stop_emit = false;
  74. }
  75. private bool on_scroll(Gdk.EventScroll ev)
  76. {
  77. GLib.Signal.stop_emission_by_name(this, "scroll-event");
  78. return Gdk.EVENT_PROPAGATE;
  79. }
  80. private bool on_button_press(Gdk.EventButton ev)
  81. {
  82. this.grab_focus();
  83. return Gdk.EVENT_PROPAGATE;
  84. }
  85. private bool on_button_release(Gdk.EventButton ev)
  86. {
  87. if (ev.button == Gdk.BUTTON_PRIMARY && this.has_focus) {
  88. if (_inconsistent)
  89. this.text = "";
  90. else
  91. this.text = _edit_fmt.printf(_value);
  92. this.set_position(-1);
  93. this.select_region(0, -1);
  94. return Gdk.EVENT_STOP;
  95. }
  96. return Gdk.EVENT_PROPAGATE;
  97. }
  98. private void on_activate()
  99. {
  100. this.select_region(0, 0);
  101. this.set_position(-1);
  102. set_value_safe(string_to_double(this.text, _value));
  103. }
  104. private bool on_focus_in(Gdk.EventFocus ev)
  105. {
  106. var app = (LevelEditorApplication)GLib.Application.get_default();
  107. app.entry_any_focus_in(this);
  108. if (_inconsistent)
  109. this.text = "";
  110. else
  111. this.text = _edit_fmt.printf(_value);
  112. this.set_position(-1);
  113. this.select_region(0, -1);
  114. return Gdk.EVENT_PROPAGATE;
  115. }
  116. private bool on_focus_out(Gdk.EventFocus ef)
  117. {
  118. var app = (LevelEditorApplication)GLib.Application.get_default();
  119. app.entry_any_focus_out(this);
  120. if (_inconsistent) {
  121. if (this.text != "") {
  122. set_value_safe(string_to_double(this.text, _value));
  123. } else {
  124. this.text = INCONSISTENT_LABEL;
  125. }
  126. } else {
  127. set_value_safe(string_to_double(this.text, _value));
  128. }
  129. this.select_region(0, 0);
  130. return Gdk.EVENT_PROPAGATE;
  131. }
  132. private void set_value_safe(double val)
  133. {
  134. double clamped = val.clamp(_min, _max);
  135. // Convert to text for displaying.
  136. this.text = _preview_fmt.printf(clamped);
  137. _inconsistent = false;
  138. // Notify value changed.
  139. if (_value != clamped) {
  140. _value = clamped;
  141. if (!_stop_emit)
  142. value_changed();
  143. }
  144. }
  145. /// Returns @a str as double or @a deffault if conversion fails.
  146. private double string_to_double(string str, double deffault)
  147. {
  148. TinyExpr.Variable vars[] =
  149. {
  150. { "x", &_value }
  151. };
  152. int err;
  153. TinyExpr.Expr expr = TinyExpr.compile(str, vars, out err);
  154. return err == 0 ? TinyExpr.eval(expr) : deffault;
  155. }
  156. public void set_min(double min)
  157. {
  158. _min = min;
  159. set_value_safe(_value);
  160. }
  161. public void set_max(double max)
  162. {
  163. _max = max;
  164. set_value_safe(_value);
  165. }
  166. }
  167. } /* namespace Crown */