From 8c9cd1d3ae15e54318ff1cb4f8e5d32539452454 Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 29 Aug 2022 18:06:25 +0200 Subject: [PATCH] Hide prefix/suffix on SpinBox focus --- scene/gui/spin_box.cpp | 22 +++++++++++++++++----- scene/gui/spin_box.h | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index 5f237f1a836..0fec9399c1e 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -40,12 +40,16 @@ Size2 SpinBox::get_minimum_size() const { void SpinBox::_value_changed(double) { String value = String::num(get_value(), Math::range_step_decimals(get_step())); - if (prefix != "") { - value = prefix + " " + value; - } - if (suffix != "") { - value += " " + suffix; + + if (!line_edit->has_focus()) { + if (prefix != "") { + value = prefix + " " + value; + } + if (suffix != "") { + value += " " + suffix; + } } + line_edit->set_text(value); } @@ -161,6 +165,12 @@ void SpinBox::_gui_input(const Ref &p_event) { } } +void SpinBox::_line_edit_focus_enter() { + int col = line_edit->get_cursor_position(); + _value_changed(0); // Update the LineEdit's text. + line_edit->set_cursor_position(col); +} + void SpinBox::_line_edit_focus_exit() { // discontinue because the focus_exit was caused by right-click context menu if (line_edit->get_menu()->is_visible()) { @@ -253,6 +263,7 @@ void SpinBox::_bind_methods() { 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("apply"), &SpinBox::apply); + ClassDB::bind_method(D_METHOD("_line_edit_focus_enter"), &SpinBox::_line_edit_focus_enter); ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit); ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit); ClassDB::bind_method(D_METHOD("_line_edit_input"), &SpinBox::_line_edit_input); @@ -273,6 +284,7 @@ SpinBox::SpinBox() { line_edit->set_mouse_filter(MOUSE_FILTER_PASS); //connect("value_changed",this,"_value_changed"); line_edit->connect("text_entered", this, "_text_entered", Vector(), CONNECT_DEFERRED); + line_edit->connect("focus_entered", this, "_line_edit_focus_enter", Vector(), CONNECT_DEFERRED); line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector(), CONNECT_DEFERRED); line_edit->connect("gui_input", this, "_line_edit_input"); diff --git a/scene/gui/spin_box.h b/scene/gui/spin_box.h index 2fb3b47f7e1..5f0d4c0d60c 100644 --- a/scene/gui/spin_box.h +++ b/scene/gui/spin_box.h @@ -60,6 +60,7 @@ class SpinBox : public Range { float diff_y = 0; } drag; + void _line_edit_focus_enter(); void _line_edit_focus_exit(); inline void _adjust_width_for_icon(const Ref &icon);