From f30be2c3f02cdb912a21858a642ef675c352387e Mon Sep 17 00:00:00 2001 From: Skipcast Date: Sun, 30 Jan 2022 15:59:47 +0100 Subject: [PATCH] Draw slider value to the right of the slider --- JKMP.Core/UI/MenuFields/SliderField.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/JKMP.Core/UI/MenuFields/SliderField.cs b/JKMP.Core/UI/MenuFields/SliderField.cs index 2694167..62282ae 100644 --- a/JKMP.Core/UI/MenuFields/SliderField.cs +++ b/JKMP.Core/UI/MenuFields/SliderField.cs @@ -30,7 +30,8 @@ public string Name return; name = value; - textSize = font.MeasureString(value); + nameTextSize = font.MeasureString(value); + valueTextSize = font.MeasureString($" {Value:0.00}"); } } @@ -75,7 +76,8 @@ public float NormalizedValue /// public Action? ValueChanged { get; set; } - private Vector2 textSize; + private Vector2 nameTextSize; + private Vector2 valueTextSize; private readonly SpriteFont font; private readonly Sprite sliderLeft, sliderRight, sliderLine, sliderCursor; @@ -121,6 +123,9 @@ protected override BTresult MyRun(TickData tickData) Value -= StepSize; if (padState.right) Value += StepSize; + + // Round the value to 2 fractional digits + Value = (float)Math.Round(value, digits: 2, MidpointRounding.ToEven); // ReSharper disable once CompareOfFloatsByEqualityOperator if (Value != oldValue) @@ -135,8 +140,8 @@ public void Draw(int x, int y, bool selected) // Draw the name Vector2 drawPos = new Vector2(x, y); TextHelper.DrawString(font, Name, drawPos, Color.White, Vector2.Zero); - drawPos.X += textSize.X + TextPadding; - drawPos.Y += (int)(textSize.Y / 2f - sliderLeft.source.Height / 2f); + drawPos.X += nameTextSize.X + TextPadding; + drawPos.Y += (int)(nameTextSize.Y / 2f - sliderLeft.source.Height / 2f); // Draw the slider sliderLine.Draw(new Rectangle((int)(drawPos.X + sliderLeft.source.Width), (int)drawPos.Y, SliderLineWidth, sliderLine.source.Height)); @@ -145,12 +150,18 @@ public void Draw(int x, int y, bool selected) // Draw the cursor sliderCursor.Draw(drawPos.X + sliderLeft.source.Width + (SliderLineWidth * NormalizedValue), drawPos.Y); + + // Draw the value + var valueDrawPos = new Vector2(drawPos.X + SliderLineWidth + sliderLeft.source.Width + sliderRight.source.Width - 2, drawPos.Y); + TextHelper.DrawString(JKContentManager.Font.MenuFontSmall, $" {Value:0.00}", valueDrawPos, Color.White, new Vector2(0, 0.25f)); } /// public Point GetSize() { - return new Point((int)(sliderLeft.source.Width + sliderRight.source.Width + SliderLineWidth + textSize.X + TextPadding), (int)Math.Max(sliderLeft.source.Height, textSize.Y)); + int x = (int)(sliderLeft.source.Width + sliderRight.source.Width + SliderLineWidth + nameTextSize.X + TextPadding + valueTextSize.X); + int y = (int)Math.Max(sliderLeft.source.Height, nameTextSize.Y); + return new Point(x, y); } } } \ No newline at end of file