Skip to content

Commit

Permalink
Draw slider value to the right of the slider
Browse files Browse the repository at this point in the history
  • Loading branch information
Skippeh committed Jan 30, 2022
1 parent 7353e5c commit f30be2c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions JKMP.Core/UI/MenuFields/SliderField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public string Name
return;

name = value;
textSize = font.MeasureString(value);
nameTextSize = font.MeasureString(value);
valueTextSize = font.MeasureString($" {Value:0.00}");
}
}

Expand Down Expand Up @@ -75,7 +76,8 @@ public float NormalizedValue
/// </summary>
public Action<float>? ValueChanged { get; set; }

private Vector2 textSize;
private Vector2 nameTextSize;
private Vector2 valueTextSize;
private readonly SpriteFont font;

private readonly Sprite sliderLeft, sliderRight, sliderLine, sliderCursor;
Expand Down Expand Up @@ -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)
Expand All @@ -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));
Expand All @@ -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));
}

/// <inheritdoc />
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);
}
}
}

0 comments on commit f30be2c

Please sign in to comment.