2019-09-10 23:28:25 +02:00
|
|
|
using System;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using MLEM.Extensions;
|
2022-04-25 15:25:58 +02:00
|
|
|
using MLEM.Graphics;
|
2019-09-10 23:28:25 +02:00
|
|
|
using MLEM.Misc;
|
|
|
|
using MLEM.Textures;
|
|
|
|
using MLEM.Ui.Style;
|
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A progress bar element to use inside of a <see cref="UiSystem"/>.
|
|
|
|
/// A progress bar is an element that fills up a bar based on a given <see cref="currentValue"/> percentage.
|
|
|
|
/// </summary>
|
2019-09-10 23:28:25 +02:00
|
|
|
public class ProgressBar : Element {
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The background texture that this progress bar should render
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<NinePatch> Texture;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The color that this progress bar's <see cref="Texture"/> should render with
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<Color> Color;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The padding that this progress bar's <see cref="ProgressTexture"/> should have.
|
|
|
|
/// The padding is the amount of pixels that the progress texture is away from the borders of the progress bar.
|
|
|
|
/// </summary>
|
2019-11-02 14:53:59 +01:00
|
|
|
public StyleProp<Vector2> ProgressPadding;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The texture that this progress bar's progress should render
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<NinePatch> ProgressTexture;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The color that this progress bar's <see cref="ProgressTexture"/> is rendered with.
|
|
|
|
/// </summary>
|
2019-10-14 21:28:12 +02:00
|
|
|
public StyleProp<Color> ProgressColor;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The direction that this progress bar goes in.
|
|
|
|
/// Note that only <see cref="Direction2Helper.Adjacent"/> directions are supported.
|
|
|
|
/// </summary>
|
2019-09-10 23:28:25 +02:00
|
|
|
public Direction2 Direction;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The maximum value that this progress bar should be able to have.
|
|
|
|
/// </summary>
|
2019-09-10 23:28:25 +02:00
|
|
|
public float MaxValue;
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The current value that this progress bar has.
|
|
|
|
/// This value is always between 0 and <see cref="MaxValue"/>.
|
|
|
|
/// </summary>
|
2019-09-10 23:28:25 +02:00
|
|
|
public float CurrentValue {
|
|
|
|
get => this.currentValue;
|
|
|
|
set => this.currentValue = MathHelper.Clamp(value, 0, this.MaxValue);
|
|
|
|
}
|
|
|
|
|
2021-10-30 15:01:04 +02:00
|
|
|
private float currentValue;
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new progress bar with the given settings
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="anchor">The progress bar's anchor</param>
|
|
|
|
/// <param name="size">The size of the progress bar</param>
|
|
|
|
/// <param name="direction">The direction that the progress bar goes into</param>
|
|
|
|
/// <param name="maxValue">The progress bar's maximum value</param>
|
|
|
|
/// <param name="currentValue">The progress bar's current value</param>
|
|
|
|
/// <exception cref="NotSupportedException">If the provided direction is not <see cref="Direction2Helper.IsAdjacent"/></exception>
|
2019-09-10 23:28:25 +02:00
|
|
|
public ProgressBar(Anchor anchor, Vector2 size, Direction2 direction, float maxValue, float currentValue = 0) : base(anchor, size) {
|
|
|
|
if (!direction.IsAdjacent())
|
|
|
|
throw new NotSupportedException("Progress bars only support Up, Down, Left and Right directions");
|
|
|
|
this.Direction = direction;
|
|
|
|
this.MaxValue = maxValue;
|
|
|
|
this.currentValue = currentValue;
|
2019-09-11 10:51:57 +02:00
|
|
|
this.CanBeSelected = false;
|
2019-09-10 23:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <inheritdoc />
|
2022-04-25 15:25:58 +02:00
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
|
2019-10-14 21:28:12 +02:00
|
|
|
batch.Draw(this.Texture, this.DisplayArea, (Color) this.Color * alpha, this.Scale);
|
2019-09-10 23:28:25 +02:00
|
|
|
|
|
|
|
var percentage = this.CurrentValue / this.MaxValue;
|
2020-02-06 17:36:51 +01:00
|
|
|
var padHor = this.ProgressTexture.HasValue() ? this.ProgressTexture.Value.Padding.Width * this.Scale : 0;
|
|
|
|
var padVer = this.ProgressTexture.HasValue() ? this.ProgressTexture.Value.Padding.Height * this.Scale : 0;
|
2019-11-02 14:53:59 +01:00
|
|
|
var width = percentage * (this.DisplayArea.Width - padHor) + padHor;
|
|
|
|
var height = percentage * (this.DisplayArea.Height - padVer) + padVer;
|
|
|
|
RectangleF progressArea;
|
2019-09-10 23:28:25 +02:00
|
|
|
switch (this.Direction) {
|
|
|
|
case Direction2.Up:
|
2019-11-02 14:53:59 +01:00
|
|
|
progressArea = new RectangleF(this.DisplayArea.X,
|
2019-09-10 23:28:25 +02:00
|
|
|
this.DisplayArea.Y + (this.DisplayArea.Height - height),
|
|
|
|
this.DisplayArea.Width, height);
|
|
|
|
break;
|
|
|
|
case Direction2.Down:
|
2019-11-02 14:53:59 +01:00
|
|
|
progressArea = new RectangleF(this.DisplayArea.Location, new Vector2(this.DisplayArea.Width, height));
|
2019-09-10 23:28:25 +02:00
|
|
|
break;
|
|
|
|
case Direction2.Left:
|
2019-11-02 14:53:59 +01:00
|
|
|
progressArea = new RectangleF(
|
2019-09-10 23:28:25 +02:00
|
|
|
this.DisplayArea.X + (this.DisplayArea.Width - width),
|
|
|
|
this.DisplayArea.Y, width, this.DisplayArea.Height);
|
|
|
|
break;
|
|
|
|
default: // Right
|
2019-11-02 14:53:59 +01:00
|
|
|
progressArea = new RectangleF(this.DisplayArea.Location, new Vector2(width, this.DisplayArea.Height));
|
2019-09-10 23:28:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-11-02 14:53:59 +01:00
|
|
|
var offsetArea = progressArea.Shrink(this.ProgressPadding.Value * this.Scale);
|
2020-02-06 17:36:51 +01:00
|
|
|
if (this.ProgressTexture.HasValue()) {
|
2019-10-14 21:28:12 +02:00
|
|
|
batch.Draw(this.ProgressTexture, offsetArea, (Color) this.ProgressColor * alpha, this.Scale);
|
2019-09-10 23:28:25 +02:00
|
|
|
} else {
|
2019-10-14 21:28:12 +02:00
|
|
|
batch.Draw(batch.GetBlankTexture(), offsetArea, (Color) this.ProgressColor * alpha);
|
2019-09-10 23:28:25 +02:00
|
|
|
}
|
2022-04-25 15:25:58 +02:00
|
|
|
base.Draw(time, batch, alpha, context);
|
2019-09-10 23:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 17:02:24 +02:00
|
|
|
/// <inheritdoc />
|
2019-09-10 23:28:25 +02:00
|
|
|
protected override void InitStyle(UiStyle style) {
|
|
|
|
base.InitStyle(style);
|
2021-12-21 11:54:32 +01:00
|
|
|
this.Texture = this.Texture.OrStyle(style.ProgressBarTexture);
|
|
|
|
this.Color = this.Color.OrStyle(style.ProgressBarColor);
|
|
|
|
this.ProgressPadding = this.ProgressPadding.OrStyle(style.ProgressBarProgressPadding);
|
|
|
|
this.ProgressTexture = this.ProgressTexture.OrStyle(style.ProgressBarProgressTexture);
|
|
|
|
this.ProgressColor = this.ProgressColor.OrStyle(style.ProgressBarProgressColor);
|
2019-09-10 23:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-06-17 18:23:47 +02:00
|
|
|
}
|