Added spinbox patch to godot

This commit is contained in:
2024-08-31 05:38:46 +03:00
parent 49a88f7c2e
commit 1aa73b4367

View File

@@ -0,0 +1,175 @@
From 9ba74bdb924158dfe1644a6dd9dbf7712b025dd2 Mon Sep 17 00:00:00 2001
From: pattlebass <49322676+pattlebass@users.noreply.github.com>
Date: Mon, 2 Jan 2023 15:15:36 +0200
Subject: [PATCH] Fix Range-derived nodes not redrawing
When using set_value_no_signal(), Range-derived nodes wouldn't redraw.
Also added a dedicated method to SpinBox to update its text.
---
scene/gui/range.cpp | 24 ++++++++++++++++++++++--
scene/gui/range.h | 2 ++
scene/gui/spin_box.cpp | 17 +++++++++++------
scene/gui/spin_box.h | 2 +-
4 files changed, 36 insertions(+), 9 deletions(-)
diff --git a/scene/gui/range.cpp b/scene/gui/range.cpp
index 4d48cf307d..9d3ad6dec8 100644
--- a/scene/gui/range.cpp
+++ b/scene/gui/range.cpp
@@ -76,16 +76,26 @@ void Range::Shared::emit_changed(const char *p_what) {
}
}
+void Range::Shared::redraw_owners() {
+ for (Set<Range *>::Element *E = owners.front(); E; E = E->next()) {
+ Range *r = E->get();
+ if (!r->is_inside_tree()) {
+ continue;
+ }
+ r->update();
+ }
+}
+
void Range::set_value(double p_val) {
double prev_val = shared->val;
- set_value_no_signal(p_val);
+ _set_value_no_signal(p_val);
if (shared->val != prev_val) {
shared->emit_value_changed();
}
}
-void Range::set_value_no_signal(double p_val) {
+void Range::_set_value_no_signal(double p_val) {
if (shared->step > 0) {
p_val = Math::round((p_val - shared->min) / shared->step) * shared->step + shared->min;
}
@@ -108,6 +118,16 @@ void Range::set_value_no_signal(double p_val) {
shared->val = p_val;
}
+
+void Range::set_value_no_signal(double p_val) {
+ double prev_val = shared->val;
+ _set_value_no_signal(p_val);
+
+ if (shared->val != prev_val) {
+ shared->redraw_owners();
+ }
+}
+
void Range::set_min(double p_min) {
shared->min = p_min;
set_value(shared->val);
diff --git a/scene/gui/range.h b/scene/gui/range.h
index f64f2620e1..ea5f882f17 100644
--- a/scene/gui/range.h
+++ b/scene/gui/range.h
@@ -45,6 +45,7 @@ class Range : public Control {
Set<Range *> owners;
void emit_value_changed();
void emit_changed(const char *p_what = "");
+ void redraw_owners();
};
Shared *shared;
@@ -56,6 +57,7 @@ class Range : public Control {
void _value_changed_notify();
void _changed_notify(const char *p_what = "");
+ void _set_value_no_signal(double p_val);
protected:
virtual void _value_changed(double) {}
diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp
index 15561cf172..452b2c0456 100644
--- a/scene/gui/spin_box.cpp
+++ b/scene/gui/spin_box.cpp
@@ -38,7 +38,7 @@ Size2 SpinBox::get_minimum_size() const {
return ms;
}
-void SpinBox::_value_changed(double) {
+void SpinBox::_update_text() {
String value = String::num(get_value(), Math::range_step_decimals(get_step()));
if (!line_edit->has_focus()) {
@@ -66,7 +66,7 @@ void SpinBox::_text_entered(const String &p_string) {
if (value.get_type() != Variant::NIL) {
set_value(value);
}
- _value_changed(0);
+ _update_text();
}
LineEdit *SpinBox::get_line_edit() {
@@ -172,11 +172,15 @@ 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.
+ _update_text();
line_edit->set_cursor_position(col);
}
void SpinBox::_line_edit_focus_exit() {
+ // discontinue because the focus_exit was caused by left-clicking the arrows.
+ if (get_focus_owner() == get_line_edit()) {
+ return;
+ }
// discontinue because the focus_exit was caused by right-click context menu
if (line_edit->get_menu()->is_visible()) {
return;
@@ -196,6 +200,7 @@ inline void SpinBox::_adjust_width_for_icon(const Ref<Texture> &icon) {
void SpinBox::_notification(int p_what) {
if (p_what == NOTIFICATION_DRAW) {
Ref<Texture> updown = get_icon("updown");
+ _update_text();
_adjust_width_for_icon(updown);
@@ -208,7 +213,7 @@ void SpinBox::_notification(int p_what) {
//_value_changed(0);
} else if (p_what == NOTIFICATION_ENTER_TREE) {
_adjust_width_for_icon(get_icon("updown"));
- _value_changed(0);
+ _update_text();
} else if (p_what == NOTIFICATION_EXIT_TREE) {
_release_mouse();
} else if (p_what == NOTIFICATION_THEME_CHANGED) {
@@ -227,7 +232,7 @@ LineEdit::Align SpinBox::get_align() const {
void SpinBox::set_suffix(const String &p_suffix) {
suffix = p_suffix;
- _value_changed(0);
+ _update_text();
}
String SpinBox::get_suffix() const {
@@ -236,7 +241,7 @@ String SpinBox::get_suffix() const {
void SpinBox::set_prefix(const String &p_prefix) {
prefix = p_prefix;
- _value_changed(0);
+ _update_text();
}
String SpinBox::get_prefix() const {
diff --git a/scene/gui/spin_box.h b/scene/gui/spin_box.h
index b308b41647..49101fd90f 100644
--- a/scene/gui/spin_box.h
+++ b/scene/gui/spin_box.h
@@ -46,7 +46,7 @@ class SpinBox : public Range {
void _release_mouse();
void _text_entered(const String &p_string);
- virtual void _value_changed(double);
+ void _update_text();
String prefix;
String suffix;
double custom_arrow_step = 0.0;
--
2.34.1