mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
added progress bars
This commit is contained in:
parent
7e8da6d838
commit
9e5f324652
6 changed files with 125 additions and 18 deletions
|
@ -8,6 +8,7 @@ using MLEM.Extensions;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
using MLEM.Formatting;
|
using MLEM.Formatting;
|
||||||
using MLEM.Input;
|
using MLEM.Input;
|
||||||
|
using MLEM.Misc;
|
||||||
using MLEM.Startup;
|
using MLEM.Startup;
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
using MLEM.Ui;
|
using MLEM.Ui;
|
||||||
|
@ -173,6 +174,17 @@ namespace Demos {
|
||||||
PositionOffset = new Vector2(0, 1)
|
PositionOffset = new Vector2(0, 1)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
root.AddChild(new VerticalSpace(3));
|
||||||
|
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Progress bars in multiple orientations:"));
|
||||||
|
var bar1 = root.AddChild(new ProgressBar(Anchor.AutoLeft, new Vector2(1, 8), Direction2.Right, 10) {PositionOffset = new Vector2(0, 1)});
|
||||||
|
CoroutineHandler.Start(this.WobbleProgressBar(bar1));
|
||||||
|
var bar2 = root.AddChild(new ProgressBar(Anchor.AutoLeft, new Vector2(1, 8), Direction2.Left, 10) {PositionOffset = new Vector2(0, 1)});
|
||||||
|
CoroutineHandler.Start(this.WobbleProgressBar(bar2));
|
||||||
|
var bar3 = root.AddChild(new ProgressBar(Anchor.AutoLeft, new Vector2(8, 30), Direction2.Down, 10) {PositionOffset = new Vector2(0, 1)});
|
||||||
|
CoroutineHandler.Start(this.WobbleProgressBar(bar3));
|
||||||
|
var bar4 = root.AddChild(new ProgressBar(Anchor.AutoInline, new Vector2(8, 30), Direction2.Up, 10) {PositionOffset = new Vector2(1, 1)});
|
||||||
|
CoroutineHandler.Start(this.WobbleProgressBar(bar4));
|
||||||
|
|
||||||
root.AddChild(new VerticalSpace(3));
|
root.AddChild(new VerticalSpace(3));
|
||||||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "There are also some additional \"components\" which are created as combinations of other components. You can find all of them in the ElementHelper class. Here are some examples:"));
|
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "There are also some additional \"components\" which are created as combinations of other components. You can find all of them in the ElementHelper class. Here are some examples:"));
|
||||||
root.AddChild(ElementHelper.NumberField(Anchor.AutoLeft, new Vector2(1, 10))).PositionOffset = new Vector2(0, 1);
|
root.AddChild(ElementHelper.NumberField(Anchor.AutoLeft, new Vector2(1, 10))).PositionOffset = new Vector2(0, 1);
|
||||||
|
@ -226,6 +238,22 @@ namespace Demos {
|
||||||
group.Transform = Matrix.Identity;
|
group.Transform = Matrix.Identity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IEnumerator<Wait> WobbleProgressBar(ProgressBar bar) {
|
||||||
|
var reducing = false;
|
||||||
|
while (bar.Root != null) {
|
||||||
|
if (reducing) {
|
||||||
|
bar.CurrentValue -= 0.1F;
|
||||||
|
if (bar.CurrentValue <= 0)
|
||||||
|
reducing = false;
|
||||||
|
} else {
|
||||||
|
bar.CurrentValue += 0.1F;
|
||||||
|
if (bar.CurrentValue >= bar.MaxValue)
|
||||||
|
reducing = true;
|
||||||
|
}
|
||||||
|
yield return new WaitSeconds(0.01F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override void Clear() {
|
public override void Clear() {
|
||||||
this.UiSystem.Remove("Test");
|
this.UiSystem.Remove("Test");
|
||||||
this.UiSystem.Remove("TestTooltip");
|
this.UiSystem.Remove("TestTooltip");
|
||||||
|
|
|
@ -66,15 +66,7 @@ namespace MLEM.Ui.Elements {
|
||||||
this.SetAreaDirty();
|
this.SetAreaDirty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Rectangle ChildPaddedArea {
|
public Rectangle ChildPaddedArea => this.UnscrolledArea.Shrink(this.ScaledChildPadding);
|
||||||
get {
|
|
||||||
var padded = this.UnscrolledArea;
|
|
||||||
padded.Location += this.ScaledChildPadding;
|
|
||||||
padded.Width -= this.ScaledChildPadding.X * 2;
|
|
||||||
padded.Height -= this.ScaledChildPadding.Y * 2;
|
|
||||||
return padded;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Point ScaledChildPadding => this.childPadding.Multiply(this.Scale);
|
public Point ScaledChildPadding => this.childPadding.Multiply(this.Scale);
|
||||||
|
|
||||||
public GenericCallback OnPressed;
|
public GenericCallback OnPressed;
|
||||||
|
@ -131,15 +123,7 @@ namespace MLEM.Ui.Elements {
|
||||||
public Rectangle Area => this.UnscrolledArea.OffsetCopy(this.ScaledScrollOffset);
|
public Rectangle Area => this.UnscrolledArea.OffsetCopy(this.ScaledScrollOffset);
|
||||||
public Point ScrollOffset;
|
public Point ScrollOffset;
|
||||||
public Point ScaledScrollOffset => this.ScrollOffset.Multiply(this.Scale);
|
public Point ScaledScrollOffset => this.ScrollOffset.Multiply(this.Scale);
|
||||||
public Rectangle DisplayArea {
|
public Rectangle DisplayArea => this.Area.Shrink(this.ScaledPadding);
|
||||||
get {
|
|
||||||
var padded = this.Area;
|
|
||||||
padded.Location += this.ScaledPadding;
|
|
||||||
padded.Width -= this.ScaledPadding.X * 2;
|
|
||||||
padded.Height -= this.ScaledPadding.Y * 2;
|
|
||||||
return padded;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private int priority;
|
private int priority;
|
||||||
public int Priority {
|
public int Priority {
|
||||||
get => this.priority;
|
get => this.priority;
|
||||||
|
|
78
MLEM.Ui/Elements/ProgressBar.cs
Normal file
78
MLEM.Ui/Elements/ProgressBar.cs
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
using System;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
|
using MLEM.Misc;
|
||||||
|
using MLEM.Textures;
|
||||||
|
using MLEM.Ui.Style;
|
||||||
|
|
||||||
|
namespace MLEM.Ui.Elements {
|
||||||
|
public class ProgressBar : Element {
|
||||||
|
|
||||||
|
public NinePatch Texture;
|
||||||
|
public Color Color;
|
||||||
|
public Point ProgressPadding;
|
||||||
|
public NinePatch ProgressTexture;
|
||||||
|
public Color ProgressColor;
|
||||||
|
|
||||||
|
public Direction2 Direction;
|
||||||
|
public float MaxValue;
|
||||||
|
private float currentValue;
|
||||||
|
public float CurrentValue {
|
||||||
|
get => this.currentValue;
|
||||||
|
set => this.currentValue = MathHelper.Clamp(value, 0, this.MaxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha) {
|
||||||
|
batch.Draw(this.Texture, this.DisplayArea, this.Color * alpha, this.Scale);
|
||||||
|
|
||||||
|
var percentage = this.CurrentValue / this.MaxValue;
|
||||||
|
var width = (percentage * this.DisplayArea.Width).Floor();
|
||||||
|
var height = (percentage * this.DisplayArea.Height).Floor();
|
||||||
|
Rectangle progressArea;
|
||||||
|
switch (this.Direction) {
|
||||||
|
case Direction2.Up:
|
||||||
|
progressArea = new Rectangle(this.DisplayArea.X,
|
||||||
|
this.DisplayArea.Y + (this.DisplayArea.Height - height),
|
||||||
|
this.DisplayArea.Width, height);
|
||||||
|
break;
|
||||||
|
case Direction2.Down:
|
||||||
|
progressArea = new Rectangle(this.DisplayArea.Location, new Point(this.DisplayArea.Width, height));
|
||||||
|
break;
|
||||||
|
case Direction2.Left:
|
||||||
|
progressArea = new Rectangle(
|
||||||
|
this.DisplayArea.X + (this.DisplayArea.Width - width),
|
||||||
|
this.DisplayArea.Y, width, this.DisplayArea.Height);
|
||||||
|
break;
|
||||||
|
default: // Right
|
||||||
|
progressArea = new Rectangle(this.DisplayArea.Location, new Point(width, this.DisplayArea.Height));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var offsetArea = progressArea.Shrink(this.ProgressPadding.Multiply(this.Scale));
|
||||||
|
if (this.ProgressTexture != null) {
|
||||||
|
batch.Draw(this.ProgressTexture, offsetArea, this.ProgressColor * alpha, this.Scale);
|
||||||
|
} else {
|
||||||
|
batch.Draw(batch.GetBlankTexture(), offsetArea, this.ProgressColor * alpha);
|
||||||
|
}
|
||||||
|
base.Draw(time, batch, alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void InitStyle(UiStyle style) {
|
||||||
|
base.InitStyle(style);
|
||||||
|
this.Texture = style.ProgressBarTexture;
|
||||||
|
this.Color = style.ProgressBarColor;
|
||||||
|
this.ProgressPadding = style.ProgressBarProgressPadding;
|
||||||
|
this.ProgressTexture = style.ProgressBarProgressTexture;
|
||||||
|
this.ProgressColor = style.ProgressBarProgressColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,6 +25,11 @@ namespace MLEM.Ui.Style {
|
||||||
public TextureRegion RadioCheckmark;
|
public TextureRegion RadioCheckmark;
|
||||||
public NinePatch TooltipBackground;
|
public NinePatch TooltipBackground;
|
||||||
public Color TooltipBackgroundColor;
|
public Color TooltipBackgroundColor;
|
||||||
|
public NinePatch ProgressBarTexture;
|
||||||
|
public Color ProgressBarColor;
|
||||||
|
public Point ProgressBarProgressPadding;
|
||||||
|
public NinePatch ProgressBarProgressTexture;
|
||||||
|
public Color ProgressBarProgressColor;
|
||||||
public IGenericFont Font;
|
public IGenericFont Font;
|
||||||
public IGenericFont BoldFont;
|
public IGenericFont BoldFont;
|
||||||
public IGenericFont ItalicFont;
|
public IGenericFont ItalicFont;
|
||||||
|
|
|
@ -25,6 +25,10 @@ namespace MLEM.Ui.Style {
|
||||||
this.RadioCheckmark = GenerateTexture(batch, Color.CornflowerBlue).Region;
|
this.RadioCheckmark = GenerateTexture(batch, Color.CornflowerBlue).Region;
|
||||||
this.TooltipBackground = GenerateTexture(batch, Color.DarkGray);
|
this.TooltipBackground = GenerateTexture(batch, Color.DarkGray);
|
||||||
this.TooltipBackgroundColor = new Color(Color.Black, 0.65F);
|
this.TooltipBackgroundColor = new Color(Color.Black, 0.65F);
|
||||||
|
this.ProgressBarTexture = GenerateTexture(batch, Color.RoyalBlue);
|
||||||
|
this.ProgressBarColor = Color.White;
|
||||||
|
this.ProgressBarProgressPadding = new Point(1);
|
||||||
|
this.ProgressBarProgressColor = Color.Red;
|
||||||
this.Font = new EmptyFont();
|
this.Font = new EmptyFont();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,5 +44,13 @@ namespace MLEM.Extensions {
|
||||||
return rect;
|
return rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Rectangle Shrink(this Rectangle rect, Point padding) {
|
||||||
|
rect.X += padding.X;
|
||||||
|
rect.Y += padding.Y;
|
||||||
|
rect.Width -= padding.X * 2;
|
||||||
|
rect.Height -= padding.Y * 2;
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue