From d7112dc534e270e03afbd030205d963a8f41b069 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Wed, 30 Aug 2023 17:59:39 -0500 Subject: [PATCH 1/3] Improve progress bar to add switch between clipping and stretch and add direction. --- Source/Engine/UI/GUI/Common/ProgressBar.cs | 78 ++++++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/ProgressBar.cs b/Source/Engine/UI/GUI/Common/ProgressBar.cs index a742d3b43..ee7d32dad 100644 --- a/Source/Engine/UI/GUI/Common/ProgressBar.cs +++ b/Source/Engine/UI/GUI/Common/ProgressBar.cs @@ -10,6 +10,32 @@ namespace FlaxEngine.GUI /// public class ProgressBar : ContainerControl { + /// + /// The direction to move the progress bar + /// + public enum BarDirection + { + /// + /// Move the bar horizontally to the left. + /// + HorizontalLeft, + + /// + /// Move the bar horizontally to the right. + /// + HorizontalRight, + + /// + /// Move the bar vertically up. + /// + VerticalUp, + + /// + /// Move the bar vertically down. + /// + VerticalDown, + } + /// /// The value. /// @@ -40,6 +66,18 @@ namespace FlaxEngine.GUI /// Gets a value indicating whether use progress value smoothing. /// public bool UseSmoothing => !Mathf.IsZero(SmoothingScale); + + /// + /// If true, the progress bar will clip instead of stretch. + /// + [EditorOrder(42), Tooltip("Whether or not to clip vs stretch the progress bar.")] + public bool ClipBar = false; + + /// + /// The direction to clip or stretch the bar. + /// + [EditorOrder(42), Tooltip("The direction to clip or stretch the bar.")] + public BarDirection Direction = BarDirection.HorizontalLeft; /// /// Gets or sets the minimum value. @@ -168,12 +206,42 @@ namespace FlaxEngine.GUI float progressNormalized = (_current - _minimum) / _maximum; if (progressNormalized > 0.001f) { - var barRect = new Rectangle(0, 0, Width * progressNormalized, Height); - BarMargin.ShrinkRectangle(ref barRect); - if (BarBrush != null) - BarBrush.Draw(barRect, BarColor); + Rectangle barRect = new Rectangle(0, 0, Width * progressNormalized, Height); + switch (Direction) + { + case BarDirection.HorizontalLeft: + break; + case BarDirection.HorizontalRight: + barRect = new Rectangle(Width - Width * progressNormalized, 0, Width * progressNormalized, Height); + break; + case BarDirection.VerticalUp: + barRect = new Rectangle(0, 0, Width, Height * progressNormalized); + break; + case BarDirection.VerticalDown: + barRect = new Rectangle(0, Height - Height * progressNormalized, Width, Height * progressNormalized); + break; + default: break; + } + + if (ClipBar) + { + var rect = new Rectangle(0, 0, Width, Height); + BarMargin.ShrinkRectangle(ref rect); + Render2D.PushClip(ref barRect); + if (BarBrush != null) + BarBrush.Draw(rect, BarColor); + else + Render2D.FillRectangle(rect, BarColor); + Render2D.PopClip(); + } else - Render2D.FillRectangle(barRect, BarColor); + { + BarMargin.ShrinkRectangle(ref barRect); + if (BarBrush != null) + BarBrush.Draw(barRect, BarColor); + else + Render2D.FillRectangle(barRect, BarColor); + } } } } From e4dfda72028fcbb9123021eadd128b1380914c90 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 31 Aug 2023 14:07:17 -0500 Subject: [PATCH 2/3] Add method enum. --- Source/Engine/UI/GUI/Common/ProgressBar.cs | 52 +++++++++++++++------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/ProgressBar.cs b/Source/Engine/UI/GUI/Common/ProgressBar.cs index ee7d32dad..ebeed1fa1 100644 --- a/Source/Engine/UI/GUI/Common/ProgressBar.cs +++ b/Source/Engine/UI/GUI/Common/ProgressBar.cs @@ -10,6 +10,22 @@ namespace FlaxEngine.GUI /// public class ProgressBar : ContainerControl { + /// + /// The method used to effect the bar. + /// + public enum BarMethod + { + /// + /// Stretch the bar. + /// + Stretch, + + /// + /// Clip the bar. + /// + Clip, + } + /// /// The direction to move the progress bar /// @@ -28,12 +44,12 @@ namespace FlaxEngine.GUI /// /// Move the bar vertically up. /// - VerticalUp, + VerticalTop, /// /// Move the bar vertically down. /// - VerticalDown, + VerticalBottom, } /// @@ -66,12 +82,12 @@ namespace FlaxEngine.GUI /// Gets a value indicating whether use progress value smoothing. /// public bool UseSmoothing => !Mathf.IsZero(SmoothingScale); - + /// - /// If true, the progress bar will clip instead of stretch. + /// The method used to effect the bar. /// - [EditorOrder(42), Tooltip("Whether or not to clip vs stretch the progress bar.")] - public bool ClipBar = false; + [EditorOrder(41), Tooltip("The method used to effect the bar.")] + public BarMethod Method = BarMethod.Stretch; /// /// The direction to clip or stretch the bar. @@ -214,17 +230,25 @@ namespace FlaxEngine.GUI case BarDirection.HorizontalRight: barRect = new Rectangle(Width - Width * progressNormalized, 0, Width * progressNormalized, Height); break; - case BarDirection.VerticalUp: + case BarDirection.VerticalTop: barRect = new Rectangle(0, 0, Width, Height * progressNormalized); break; - case BarDirection.VerticalDown: + case BarDirection.VerticalBottom: barRect = new Rectangle(0, Height - Height * progressNormalized, Width, Height * progressNormalized); break; default: break; } - if (ClipBar) + switch (Method) { + case BarMethod.Stretch: + BarMargin.ShrinkRectangle(ref barRect); + if (BarBrush != null) + BarBrush.Draw(barRect, BarColor); + else + Render2D.FillRectangle(barRect, BarColor); + break; + case BarMethod.Clip: var rect = new Rectangle(0, 0, Width, Height); BarMargin.ShrinkRectangle(ref rect); Render2D.PushClip(ref barRect); @@ -233,14 +257,8 @@ namespace FlaxEngine.GUI else Render2D.FillRectangle(rect, BarColor); Render2D.PopClip(); - } - else - { - BarMargin.ShrinkRectangle(ref barRect); - if (BarBrush != null) - BarBrush.Draw(barRect, BarColor); - else - Render2D.FillRectangle(barRect, BarColor); + break; + default: break; } } } From a98458be1fe3e0d91fa62d785699876bb82b91b0 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 31 Aug 2023 14:14:01 -0500 Subject: [PATCH 3/3] Small improvements. --- Source/Engine/UI/GUI/Common/ProgressBar.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/ProgressBar.cs b/Source/Engine/UI/GUI/Common/ProgressBar.cs index ebeed1fa1..760f031a7 100644 --- a/Source/Engine/UI/GUI/Common/ProgressBar.cs +++ b/Source/Engine/UI/GUI/Common/ProgressBar.cs @@ -27,9 +27,9 @@ namespace FlaxEngine.GUI } /// - /// The direction to move the progress bar + /// The origin to move the progress bar to. /// - public enum BarDirection + public enum BarOrigin { /// /// Move the bar horizontally to the left. @@ -90,10 +90,10 @@ namespace FlaxEngine.GUI public BarMethod Method = BarMethod.Stretch; /// - /// The direction to clip or stretch the bar. + /// The origin or where the bar decreases to. /// - [EditorOrder(42), Tooltip("The direction to clip or stretch the bar.")] - public BarDirection Direction = BarDirection.HorizontalLeft; + [EditorOrder(42), Tooltip("The origin or where the bar decreases to.")] + public BarOrigin Origin = BarOrigin.HorizontalLeft; /// /// Gets or sets the minimum value. @@ -223,17 +223,17 @@ namespace FlaxEngine.GUI if (progressNormalized > 0.001f) { Rectangle barRect = new Rectangle(0, 0, Width * progressNormalized, Height); - switch (Direction) + switch (Origin) { - case BarDirection.HorizontalLeft: + case BarOrigin.HorizontalLeft: break; - case BarDirection.HorizontalRight: + case BarOrigin.HorizontalRight: barRect = new Rectangle(Width - Width * progressNormalized, 0, Width * progressNormalized, Height); break; - case BarDirection.VerticalTop: + case BarOrigin.VerticalTop: barRect = new Rectangle(0, 0, Width, Height * progressNormalized); break; - case BarDirection.VerticalBottom: + case BarOrigin.VerticalBottom: barRect = new Rectangle(0, Height - Height * progressNormalized, Width, Height * progressNormalized); break; default: break;