diff options
-rw-r--r-- | doc/classes/SpinBox.xml | 3 | ||||
-rw-r--r-- | scene/gui/spin_box.cpp | 31 | ||||
-rw-r--r-- | scene/gui/spin_box.h | 6 |
3 files changed, 39 insertions, 1 deletions
diff --git a/doc/classes/SpinBox.xml b/doc/classes/SpinBox.xml index c86742eb46..33d2b472b5 100644 --- a/doc/classes/SpinBox.xml +++ b/doc/classes/SpinBox.xml @@ -55,6 +55,9 @@ <member name="suffix" type="String" setter="set_suffix" getter="get_suffix" default=""""> Adds the specified [code]suffix[/code] string after the numerical value of the [SpinBox]. </member> + <member name="update_on_text_changed" type="bool" setter="set_update_on_text_changed" getter="get_update_on_text_changed" default="false"> + Sets the value of the [Range] for this [SpinBox] when the [LineEdit] text is [i]changed[/i] instead of [i]submitted[/i]. See [signal LineEdit.text_changed] and [signal LineEdit.text_submitted]. + </member> </members> <theme_items> <theme_item name="updown" data_type="icon" type="Texture2D"> diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index 1074d0d8a0..0aec017649 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -68,6 +68,15 @@ void SpinBox::_text_submitted(const String &p_string) { _value_changed(0); } +void SpinBox::_text_changed(const String &p_string) { + int cursor_pos = line_edit->get_caret_column(); + + _text_submitted(p_string); + + // Line edit 'set_text' method resets the cursor position so we need to undo that. + line_edit->set_caret_column(cursor_pos); +} + LineEdit *SpinBox::get_line_edit() { return line_edit; } @@ -244,6 +253,24 @@ String SpinBox::get_prefix() const { return prefix; } +void SpinBox::set_update_on_text_changed(bool p_update) { + if (update_on_text_changed == p_update) { + return; + } + + update_on_text_changed = p_update; + + if (p_update) { + line_edit->connect("text_changed", callable_mp(this, &SpinBox::_text_changed), Vector<Variant>(), CONNECT_DEFERRED); + } else { + line_edit->disconnect("text_changed", callable_mp(this, &SpinBox::_text_changed)); + } +} + +bool SpinBox::get_update_on_text_changed() const { + return update_on_text_changed; +} + void SpinBox::set_editable(bool p_editable) { line_edit->set_editable(p_editable); } @@ -267,11 +294,14 @@ void SpinBox::_bind_methods() { ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix); ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable); ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable); + ClassDB::bind_method(D_METHOD("set_update_on_text_changed"), &SpinBox::set_update_on_text_changed); + ClassDB::bind_method(D_METHOD("get_update_on_text_changed"), &SpinBox::get_update_on_text_changed); ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply); ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit); ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_on_text_changed"), "set_update_on_text_changed", "get_update_on_text_changed"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix"); } @@ -284,7 +314,6 @@ SpinBox::SpinBox() { line_edit->set_mouse_filter(MOUSE_FILTER_PASS); line_edit->set_align(LineEdit::ALIGN_LEFT); - //connect("value_changed",this,"_value_changed"); line_edit->connect("text_submitted", callable_mp(this, &SpinBox::_text_submitted), Vector<Variant>(), CONNECT_DEFERRED); line_edit->connect("focus_exited", callable_mp(this, &SpinBox::_line_edit_focus_exit), Vector<Variant>(), CONNECT_DEFERRED); line_edit->connect("gui_input", callable_mp(this, &SpinBox::_line_edit_input)); diff --git a/scene/gui/spin_box.h b/scene/gui/spin_box.h index 9ec3885f1f..9828b894da 100644 --- a/scene/gui/spin_box.h +++ b/scene/gui/spin_box.h @@ -40,6 +40,7 @@ class SpinBox : public Range { LineEdit *line_edit; int last_w = 0; + bool update_on_text_changed = false; Timer *range_click_timer; void _range_click_timeout(); @@ -47,6 +48,8 @@ class SpinBox : public Range { void _text_submitted(const String &p_string); virtual void _value_changed(double) override; + void _text_changed(const String &p_string); + String prefix; String suffix; @@ -88,6 +91,9 @@ public: void set_prefix(const String &p_prefix); String get_prefix() const; + void set_update_on_text_changed(bool p_update); + bool get_update_on_text_changed() const; + void apply(); SpinBox(); |