Hide prefix/suffix on SpinBox focus

This commit is contained in:
kobewi 2022-08-29 18:06:25 +02:00
parent b0ab7ac446
commit 8c9cd1d3ae
2 changed files with 18 additions and 5 deletions

View File

@ -40,12 +40,16 @@ Size2 SpinBox::get_minimum_size() const {
void SpinBox::_value_changed(double) { void SpinBox::_value_changed(double) {
String value = String::num(get_value(), Math::range_step_decimals(get_step())); String value = String::num(get_value(), Math::range_step_decimals(get_step()));
if (prefix != "") {
value = prefix + " " + value; if (!line_edit->has_focus()) {
} if (prefix != "") {
if (suffix != "") { value = prefix + " " + value;
value += " " + suffix; }
if (suffix != "") {
value += " " + suffix;
}
} }
line_edit->set_text(value); line_edit->set_text(value);
} }
@ -161,6 +165,12 @@ void SpinBox::_gui_input(const Ref<InputEvent> &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() { void SpinBox::_line_edit_focus_exit() {
// discontinue because the focus_exit was caused by right-click context menu // discontinue because the focus_exit was caused by right-click context menu
if (line_edit->get_menu()->is_visible()) { 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("set_editable", "editable"), &SpinBox::set_editable);
ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable); ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply); 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("_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("get_line_edit"), &SpinBox::get_line_edit);
ClassDB::bind_method(D_METHOD("_line_edit_input"), &SpinBox::_line_edit_input); 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); line_edit->set_mouse_filter(MOUSE_FILTER_PASS);
//connect("value_changed",this,"_value_changed"); //connect("value_changed",this,"_value_changed");
line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED); line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED);
line_edit->connect("focus_entered", this, "_line_edit_focus_enter", Vector<Variant>(), CONNECT_DEFERRED);
line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED); line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED);
line_edit->connect("gui_input", this, "_line_edit_input"); line_edit->connect("gui_input", this, "_line_edit_input");

View File

@ -60,6 +60,7 @@ class SpinBox : public Range {
float diff_y = 0; float diff_y = 0;
} drag; } drag;
void _line_edit_focus_enter();
void _line_edit_focus_exit(); void _line_edit_focus_exit();
inline void _adjust_width_for_icon(const Ref<Texture> &icon); inline void _adjust_width_for_icon(const Ref<Texture> &icon);