From eee7ce635b523ac573f4f820909a51f3bb108de5 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Tue, 29 Jul 2025 15:30:05 -0700 Subject: [PATCH] Fix snapping logic in Range --- doc/classes/Range.xml | 2 +- scene/gui/range.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/classes/Range.xml b/doc/classes/Range.xml index 716587d5464..80265b2475f 100644 --- a/doc/classes/Range.xml +++ b/doc/classes/Range.xml @@ -64,7 +64,7 @@ - If greater than 0, [member value] will always be rounded to a multiple of this property's value. If [member rounded] is also [code]true[/code], [member value] will first be rounded to a multiple of this property's value, then rounded to the nearest integer. + If greater than 0, [member value] will always be rounded to a multiple of this property's value above [member min_value]. For example, if [member min_value] is [code]0.1[/code] and step is 0.2, then [member value] is limited to [code]0.1[/code], [code]0.3[/code], [code]0.5[/code], and so on. If [member rounded] is also [code]true[/code], [member value] will first be rounded to a multiple of this property's value, then rounded to the nearest integer. 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/range.cpp b/scene/gui/range.cpp index 8b69e923c8c..bfce675a04c 100644 --- a/scene/gui/range.cpp +++ b/scene/gui/range.cpp @@ -140,7 +140,9 @@ void Range::_set_value_no_signal(double p_val) { double Range::_calc_value(double p_val, double p_step) const { if (p_step > 0) { - p_val = Math::round((p_val - shared->min) / p_step) * p_step + shared->min; + // TODO: In the future, change `step` to a more suitable type for more robust precise calculations. + // Subtract min to support cases like min = 0.1, step = 0.2, snaps to 0.1, 0.3, 0.5, etc. + p_val = Math::snapped(p_val - shared->min, p_step) + shared->min; } if (_rounded_values) {