From fdae01de101020b3dfec59ff4304a3c4a24805aa Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 31 Oct 2022 23:12:53 +0100 Subject: [PATCH] Add set_value_no_signal() to Range --- doc/classes/Range.xml | 9 ++++++++- scene/gui/base_button.cpp | 9 ++++----- scene/gui/range.cpp | 12 ++++++++++-- scene/gui/range.h | 1 + 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/doc/classes/Range.xml b/doc/classes/Range.xml index 2a40f0a09cd..8f9f0669778 100644 --- a/doc/classes/Range.xml +++ b/doc/classes/Range.xml @@ -9,6 +9,13 @@ + + + + + Sets the [Range]'s current value to the specified [member value], without emitting the [signal value_changed] signal. + + @@ -52,7 +59,7 @@ If greater than 0, [code]value[/code] will always be rounded to a multiple of [code]step[/code]. If [code]rounded[/code] is also [code]true[/code], [code]value[/code] will first be rounded to a multiple of [code]step[/code] then rounded to the nearest integer. - Range's current value. + Range's current value. Changing this property (even via code) will trigger [signal value_changed] signal. Use [method set_value_no_signal] if you want to avoid it. diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index aff1a6805b1..6db6ba3580d 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -211,14 +211,13 @@ bool BaseButton::is_disabled() const { } void BaseButton::set_pressed(bool p_pressed) { - if (!toggle_mode) { - return; - } - if (status.pressed == p_pressed) { + bool prev_pressed = status.pressed; + set_pressed_no_signal(p_pressed); + + if (status.pressed == prev_pressed) { return; } _change_notify("pressed"); - status.pressed = p_pressed; if (p_pressed) { _unpress_group(); diff --git a/scene/gui/range.cpp b/scene/gui/range.cpp index f128aaa42ad..4d48cf307d6 100644 --- a/scene/gui/range.cpp +++ b/scene/gui/range.cpp @@ -77,6 +77,15 @@ void Range::Shared::emit_changed(const char *p_what) { } void Range::set_value(double p_val) { + double prev_val = shared->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) { if (shared->step > 0) { p_val = Math::round((p_val - shared->min) / shared->step) * shared->step + shared->min; } @@ -98,8 +107,6 @@ void Range::set_value(double p_val) { } shared->val = p_val; - - shared->emit_value_changed(); } void Range::set_min(double p_min) { shared->min = p_min; @@ -237,6 +244,7 @@ void Range::_bind_methods() { ClassDB::bind_method(D_METHOD("get_page"), &Range::get_page); ClassDB::bind_method(D_METHOD("get_as_ratio"), &Range::get_as_ratio); ClassDB::bind_method(D_METHOD("set_value", "value"), &Range::set_value); + ClassDB::bind_method(D_METHOD("set_value_no_signal", "value"), &Range::set_value_no_signal); ClassDB::bind_method(D_METHOD("set_min", "minimum"), &Range::set_min); ClassDB::bind_method(D_METHOD("set_max", "maximum"), &Range::set_max); ClassDB::bind_method(D_METHOD("set_step", "step"), &Range::set_step); diff --git a/scene/gui/range.h b/scene/gui/range.h index 7212dc11087..f64f2620e17 100644 --- a/scene/gui/range.h +++ b/scene/gui/range.h @@ -66,6 +66,7 @@ protected: public: void set_value(double p_val); + void set_value_no_signal(double p_val); void set_min(double p_min); void set_max(double p_max); void set_step(double p_step);