summaryrefslogtreecommitdiff
path: root/scene
diff options
context:
space:
mode:
authorRindbee <idleman@yeah.net>2022-10-20 11:32:47 +0800
committerRindbee <idleman@yeah.net>2022-10-20 14:04:09 +0800
commit075e79bf04980b4b6b61f15500ee574e3ef4669b (patch)
treebb37ff14a8cf19f702fa10558273a4ffc6a1defb /scene
parenta8c805be2947b211ee8b881d7a8bab7cdc86e170 (diff)
Improve validation when setting `max`/`min`/`page` in `Range`
The incoming value is validated first and then compared. Previously, when the ScrollContainer was scrolled, the ScrollBar of the other axis of the ScrollContainer would emit the changed signal. This commit avoids that.
Diffstat (limited to 'scene')
-rw-r--r--scene/gui/range.cpp21
-rw-r--r--scene/gui/range.h1
2 files changed, 9 insertions, 13 deletions
diff --git a/scene/gui/range.cpp b/scene/gui/range.cpp
index 2d2b3e413d..72b5e95ca9 100644
--- a/scene/gui/range.cpp
+++ b/scene/gui/range.cpp
@@ -64,11 +64,6 @@ void Range::_changed_notify(const char *p_what) {
queue_redraw();
}
-void Range::_validate_values() {
- shared->max = MAX(shared->max, shared->min);
- shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
-}
-
void Range::Shared::emit_changed(const char *p_what) {
for (Range *E : owners) {
Range *r = E;
@@ -111,8 +106,9 @@ void Range::set_min(double p_min) {
}
shared->min = p_min;
+ shared->max = MAX(shared->max, shared->min);
+ shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
set_value(shared->val);
- _validate_values();
shared->emit_changed("min");
@@ -120,13 +116,14 @@ void Range::set_min(double p_min) {
}
void Range::set_max(double p_max) {
- if (shared->max == p_max) {
+ double max_validated = MAX(p_max, shared->min);
+ if (shared->max == max_validated) {
return;
}
- shared->max = p_max;
+ shared->max = max_validated;
+ shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
set_value(shared->val);
- _validate_values();
shared->emit_changed("max");
}
@@ -141,13 +138,13 @@ void Range::set_step(double p_step) {
}
void Range::set_page(double p_page) {
- if (shared->page == p_page) {
+ double page_validated = CLAMP(p_page, 0, shared->max - shared->min);
+ if (shared->page == page_validated) {
return;
}
- shared->page = p_page;
+ shared->page = page_validated;
set_value(shared->val);
- _validate_values();
shared->emit_changed("page");
}
diff --git a/scene/gui/range.h b/scene/gui/range.h
index 19452243cf..d54b6df06f 100644
--- a/scene/gui/range.h
+++ b/scene/gui/range.h
@@ -59,7 +59,6 @@ class Range : public Control {
void _value_changed_notify();
void _changed_notify(const char *p_what = "");
- void _validate_values();
protected:
virtual void _value_changed(double p_value);