From 9667848c96649c59026ab6646dd2ab247191fe29 Mon Sep 17 00:00:00 2001 From: ontrigger Date: Mon, 25 Sep 2023 23:28:29 +0300 Subject: [PATCH 1/6] make scrolling duration based and deprecate SmoothingScale --- Source/Engine/UI/GUI/Panels/ScrollBar.cs | 92 +++++++++++++++++++----- 1 file changed, 74 insertions(+), 18 deletions(-) diff --git a/Source/Engine/UI/GUI/Panels/ScrollBar.cs b/Source/Engine/UI/GUI/Panels/ScrollBar.cs index a51a1df40..5dfcf5e53 100644 --- a/Source/Engine/UI/GUI/Panels/ScrollBar.cs +++ b/Source/Engine/UI/GUI/Panels/ScrollBar.cs @@ -23,9 +23,9 @@ namespace FlaxEngine.GUI // Scrolling - private float _clickChange = 20, _scrollChange = 100; + private float _clickChange = 20, _scrollChange = 50; private float _minimum, _maximum = 100; - private float _value, _targetValue; + private float _startValue, _value, _targetValue; private readonly Orientation _orientation; private RootControl.UpdateDelegate _update; @@ -42,6 +42,7 @@ namespace FlaxEngine.GUI // Smoothing private float _thumbOpacity = DefaultMinimumOpacity; + private float _scrollAnimationProgress = 0f; /// /// Gets the orientation. @@ -60,13 +61,35 @@ namespace FlaxEngine.GUI /// /// Gets or sets the value smoothing scale (0 to not use it). + /// [Deprecated on 26.09.2023, expires on 26.09.2025] + /// This property is deprecated, use instead + /// Set this to >= 0 to use legacy behavior /// - public float SmoothingScale { get; set; } = 0.6f; + [Obsolete("Deprecated in 1.7")] + public float SmoothingScale { get; set; } = -1f; + + /// + /// The maximum time it takes to animate from current to target scroll position + /// + public float ScrollAnimationDuration { get; set; } = 0.18f; /// /// Gets a value indicating whether use scroll value smoothing. /// - public bool UseSmoothing => !Mathf.IsZero(SmoothingScale); + public bool UseSmoothing + { + get + { + if (!EnableSmoothing || Mathf.IsZero(SmoothingScale)) { return false; } + + return SmoothingScale > 0 || SmoothingScale < 0 && !Mathf.IsZero(ScrollAnimationDuration); + } + } + + /// + /// Enables scroll smoothing + /// + public bool EnableSmoothing { get; set; } = true; /// /// Gets or sets the minimum value. @@ -112,11 +135,15 @@ namespace FlaxEngine.GUI if (!Mathf.NearEqual(value, _targetValue)) { _targetValue = value; + _startValue = _value; + _scrollAnimationProgress = 0f; // Check if skip smoothing if (!UseSmoothing) { _value = value; + _startValue = value; + _scrollAnimationProgress = 1f; OnValueChanged(); } else @@ -208,7 +235,8 @@ namespace FlaxEngine.GUI { if (!Mathf.NearEqual(_value, _targetValue)) { - _value = _targetValue; + _value = _targetValue = _startValue; + _scrollAnimationProgress = 0f; SetUpdate(ref _update, null); OnValueChanged(); } @@ -274,7 +302,8 @@ namespace FlaxEngine.GUI internal void Reset() { - _value = _targetValue = 0; + _value = _targetValue = _startValue = 0; + _scrollAnimationProgress = 0f; } /// @@ -296,22 +325,49 @@ namespace FlaxEngine.GUI _thumbOpacity = isDeltaSlow ? targetOpacity : Mathf.Lerp(_thumbOpacity, targetOpacity, deltaTime * 10.0f); bool needUpdate = Mathf.Abs(_thumbOpacity - targetOpacity) > 0.001f; - // Ensure scroll bar is visible - if (Visible) + // Ensure scroll bar is visible and smoothing is required + if (Visible && Mathf.Abs(_targetValue - _value) > 0.01f) { - // Value smoothing - if (Mathf.Abs(_targetValue - _value) > 0.01f) + // Interpolate or not if running slow + float value; + if (!isDeltaSlow && UseSmoothing) { - // Interpolate or not if running slow - float value; - if (!isDeltaSlow && UseSmoothing) + // use legacy behavior + if (SmoothingScale >= 0) + { value = Mathf.Lerp(_value, _targetValue, deltaTime * 20.0f * SmoothingScale); + } else - value = _targetValue; - _value = value; - OnValueChanged(); - needUpdate = true; + { + // percentage of scroll from 0 to _scrollChange, ex. 0.5 at _scrollChange / 2 + var minScrollChangeRatio = Mathf.Clamp(Mathf.Abs(_targetValue - _startValue) / _scrollChange, 0, 1); + + // shorten the duration if we scrolled less than _scrollChange + var actualDuration = ScrollAnimationDuration * minScrollChangeRatio; + var step = deltaTime / actualDuration; + + var progress = _scrollAnimationProgress; + progress = Mathf.Clamp(progress + step, 0, 1); + + Debug.Log($"why {progress} {step} {minScrollChangeRatio}"); + + // https://easings.net/#easeInOutQuad + var easedProgress = Mathf.Sin((progress * Mathf.Pi) / 2); + value = Mathf.Lerp(_startValue, _targetValue, easedProgress); + + _scrollAnimationProgress = progress; + } } + else + { + value = _targetValue; + _startValue = _targetValue; + _scrollAnimationProgress = 0f; + } + + _value = value; + OnValueChanged(); + needUpdate = true; } // End updating if all animations are done @@ -381,7 +437,7 @@ namespace FlaxEngine.GUI if (ThumbEnabled) { // Scroll - Value = _value - delta * _scrollChange; + Value = _targetValue - delta * _scrollChange; } return true; } From 6a62dac49b39af711131c7c02b1d4de20a22006c Mon Sep 17 00:00:00 2001 From: ontrigger Date: Mon, 25 Sep 2023 23:29:29 +0300 Subject: [PATCH 2/6] remove usages of SmoothingScale across the codebase --- Source/Editor/Surface/ContextMenu/VisjectCM.cs | 4 +--- Source/Editor/Windows/Search/ContentFinder.cs | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Source/Editor/Surface/ContextMenu/VisjectCM.cs b/Source/Editor/Surface/ContextMenu/VisjectCM.cs index bae62556e..8fb775b6e 100644 --- a/Source/Editor/Surface/ContextMenu/VisjectCM.cs +++ b/Source/Editor/Surface/ContextMenu/VisjectCM.cs @@ -671,9 +671,7 @@ namespace FlaxEditor.Surface.ContextMenu SelectedItem = previousSelectedItem; // Scroll into view (without smoothing) - _panel1.VScrollBar.SmoothingScale = 0; - _panel1.ScrollViewTo(SelectedItem); - _panel1.VScrollBar.SmoothingScale = 1; + _panel1.ScrollViewTo(SelectedItem, true); } return true; } diff --git a/Source/Editor/Windows/Search/ContentFinder.cs b/Source/Editor/Windows/Search/ContentFinder.cs index 05e1d6792..e19d212b8 100644 --- a/Source/Editor/Windows/Search/ContentFinder.cs +++ b/Source/Editor/Windows/Search/ContentFinder.cs @@ -54,9 +54,7 @@ namespace FlaxEditor.Windows.Search _selectedItem.BackgroundColor = Style.Current.BackgroundSelected; if (_matchedItems.Count > VisibleItemCount) { - _resultPanel.VScrollBar.SmoothingScale = 0; - _resultPanel.ScrollViewTo(_selectedItem); - _resultPanel.VScrollBar.SmoothingScale = 1; + _resultPanel.ScrollViewTo(_selectedItem, true); } } } From a18d2d0ebabce8603b14d7469497a311a5b9cd92 Mon Sep 17 00:00:00 2001 From: ontrigger Date: Mon, 25 Sep 2023 23:35:52 +0300 Subject: [PATCH 3/6] remove logging --- Source/Engine/UI/GUI/Panels/ScrollBar.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Source/Engine/UI/GUI/Panels/ScrollBar.cs b/Source/Engine/UI/GUI/Panels/ScrollBar.cs index 5dfcf5e53..b2f1202b3 100644 --- a/Source/Engine/UI/GUI/Panels/ScrollBar.cs +++ b/Source/Engine/UI/GUI/Panels/ScrollBar.cs @@ -349,8 +349,6 @@ namespace FlaxEngine.GUI var progress = _scrollAnimationProgress; progress = Mathf.Clamp(progress + step, 0, 1); - Debug.Log($"why {progress} {step} {minScrollChangeRatio}"); - // https://easings.net/#easeInOutQuad var easedProgress = Mathf.Sin((progress * Mathf.Pi) / 2); value = Mathf.Lerp(_startValue, _targetValue, easedProgress); From 056d8d5b6cb2103faefd4365cbd198fa845ff7f7 Mon Sep 17 00:00:00 2001 From: ontrigger Date: Mon, 25 Sep 2023 23:46:16 +0300 Subject: [PATCH 4/6] don't animate when scrollbar is held --- Source/Engine/UI/GUI/Panels/ScrollBar.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/UI/GUI/Panels/ScrollBar.cs b/Source/Engine/UI/GUI/Panels/ScrollBar.cs index b2f1202b3..c6f70fc2c 100644 --- a/Source/Engine/UI/GUI/Panels/ScrollBar.cs +++ b/Source/Engine/UI/GUI/Panels/ScrollBar.cs @@ -425,7 +425,7 @@ namespace FlaxEngine.GUI float mousePosition = _orientation == Orientation.Vertical ? slidePosition.Y : slidePosition.X; float percentage = (mousePosition - _mouseOffset - _thumbSize / 2) / (TrackSize - _thumbSize); - Value = _minimum + percentage * (_maximum - _minimum); + TargetValue = _minimum + percentage * (_maximum - _minimum); } } From 2d37e59e732f8b3b5388989ef319ec223a9f3c9f Mon Sep 17 00:00:00 2001 From: ontrigger Date: Tue, 26 Sep 2023 00:51:44 +0300 Subject: [PATCH 5/6] completely remove SmoothingScale --- Source/Engine/UI/GUI/Panels/ScrollBar.cs | 49 ++++++------------------ 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/Source/Engine/UI/GUI/Panels/ScrollBar.cs b/Source/Engine/UI/GUI/Panels/ScrollBar.cs index c6f70fc2c..db47976a8 100644 --- a/Source/Engine/UI/GUI/Panels/ScrollBar.cs +++ b/Source/Engine/UI/GUI/Panels/ScrollBar.cs @@ -59,15 +59,6 @@ namespace FlaxEngine.GUI /// public float TrackThickness { get; set; } = 2.0f; - /// - /// Gets or sets the value smoothing scale (0 to not use it). - /// [Deprecated on 26.09.2023, expires on 26.09.2025] - /// This property is deprecated, use instead - /// Set this to >= 0 to use legacy behavior - /// - [Obsolete("Deprecated in 1.7")] - public float SmoothingScale { get; set; } = -1f; - /// /// The maximum time it takes to animate from current to target scroll position /// @@ -76,15 +67,7 @@ namespace FlaxEngine.GUI /// /// Gets a value indicating whether use scroll value smoothing. /// - public bool UseSmoothing - { - get - { - if (!EnableSmoothing || Mathf.IsZero(SmoothingScale)) { return false; } - - return SmoothingScale > 0 || SmoothingScale < 0 && !Mathf.IsZero(ScrollAnimationDuration); - } - } + public bool UseSmoothing => EnableSmoothing && !Mathf.IsZero(ScrollAnimationDuration); /// /// Enables scroll smoothing @@ -332,29 +315,21 @@ namespace FlaxEngine.GUI float value; if (!isDeltaSlow && UseSmoothing) { - // use legacy behavior - if (SmoothingScale >= 0) - { - value = Mathf.Lerp(_value, _targetValue, deltaTime * 20.0f * SmoothingScale); - } - else - { - // percentage of scroll from 0 to _scrollChange, ex. 0.5 at _scrollChange / 2 - var minScrollChangeRatio = Mathf.Clamp(Mathf.Abs(_targetValue - _startValue) / _scrollChange, 0, 1); + // percentage of scroll from 0 to _scrollChange, ex. 0.5 at _scrollChange / 2 + var minScrollChangeRatio = Mathf.Clamp(Mathf.Abs(_targetValue - _startValue) / _scrollChange, 0, 1); - // shorten the duration if we scrolled less than _scrollChange - var actualDuration = ScrollAnimationDuration * minScrollChangeRatio; - var step = deltaTime / actualDuration; + // shorten the duration if we scrolled less than _scrollChange + var actualDuration = ScrollAnimationDuration * minScrollChangeRatio; + var step = deltaTime / actualDuration; - var progress = _scrollAnimationProgress; - progress = Mathf.Clamp(progress + step, 0, 1); + var progress = _scrollAnimationProgress; + progress = Mathf.Clamp(progress + step, 0, 1); - // https://easings.net/#easeInOutQuad - var easedProgress = Mathf.Sin((progress * Mathf.Pi) / 2); - value = Mathf.Lerp(_startValue, _targetValue, easedProgress); + // https://easings.net/#easeInOutQuad + var easedProgress = Mathf.Sin((progress * Mathf.Pi) / 2); + value = Mathf.Lerp(_startValue, _targetValue, easedProgress); - _scrollAnimationProgress = progress; - } + _scrollAnimationProgress = progress; } else { From 249ded3d1fb13d56f89873b63201b8f9b6b02cc5 Mon Sep 17 00:00:00 2001 From: ontrigger Date: Wed, 27 Sep 2023 22:12:21 +0300 Subject: [PATCH 6/6] fix incorrect link --- Source/Engine/UI/GUI/Panels/ScrollBar.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/UI/GUI/Panels/ScrollBar.cs b/Source/Engine/UI/GUI/Panels/ScrollBar.cs index db47976a8..abde37b37 100644 --- a/Source/Engine/UI/GUI/Panels/ScrollBar.cs +++ b/Source/Engine/UI/GUI/Panels/ScrollBar.cs @@ -325,7 +325,7 @@ namespace FlaxEngine.GUI var progress = _scrollAnimationProgress; progress = Mathf.Clamp(progress + step, 0, 1); - // https://easings.net/#easeInOutQuad + // https://easings.net/#easeOutSine var easedProgress = Mathf.Sin((progress * Mathf.Pi) / 2); value = Mathf.Lerp(_startValue, _targetValue, easedProgress);