mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
added auto-scaled text, buttons and vertical space
This commit is contained in:
parent
4943a7b6f0
commit
ef3726b077
12 changed files with 206 additions and 9 deletions
50
MLEM.Ui/Elements/AutoScaledText.cs
Normal file
50
MLEM.Ui/Elements/AutoScaledText.cs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
using System;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
|
using MLEM.Font;
|
||||||
|
|
||||||
|
namespace MLEM.Ui.Elements {
|
||||||
|
public class AutoScaledText : Element {
|
||||||
|
|
||||||
|
private readonly IGenericFont font;
|
||||||
|
private readonly bool centered;
|
||||||
|
private float scale;
|
||||||
|
private string text;
|
||||||
|
|
||||||
|
public IGenericFont Font => this.font ?? this.System.DefaultFont;
|
||||||
|
public string Text {
|
||||||
|
get => this.text;
|
||||||
|
set {
|
||||||
|
this.text = value;
|
||||||
|
this.SetDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Color Color;
|
||||||
|
|
||||||
|
public AutoScaledText(Anchor anchor, Vector2 size, string text, bool centered = false, Color? color = null, IGenericFont font = null) : base(anchor, size) {
|
||||||
|
this.Text = text;
|
||||||
|
this.centered = centered;
|
||||||
|
this.font = font;
|
||||||
|
this.Color = color ?? Color.White;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ForceUpdateArea() {
|
||||||
|
base.ForceUpdateArea();
|
||||||
|
|
||||||
|
this.scale = 0;
|
||||||
|
Vector2 measure;
|
||||||
|
do {
|
||||||
|
this.scale += 0.1F;
|
||||||
|
measure = this.Font.MeasureString(this.Text) * this.scale;
|
||||||
|
} while (measure.X <= this.DisplayArea.Size.X && measure.Y <= this.DisplayArea.Size.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Draw(GameTime time, SpriteBatch batch, Color color) {
|
||||||
|
var pos = this.DisplayArea.Location.ToVector2() + this.DisplayArea.Size.ToVector2() / 2;
|
||||||
|
this.Font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color.CopyAlpha(color), true, true);
|
||||||
|
base.Draw(time, batch, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
39
MLEM.Ui/Elements/Button.cs
Normal file
39
MLEM.Ui/Elements/Button.cs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
|
using MLEM.Textures;
|
||||||
|
|
||||||
|
namespace MLEM.Ui.Elements {
|
||||||
|
public class Button : Element {
|
||||||
|
|
||||||
|
public NinePatch Texture;
|
||||||
|
public NinePatch HoveredTexture;
|
||||||
|
public Color HoveredColor;
|
||||||
|
public AutoScaledText Text;
|
||||||
|
|
||||||
|
public Button(Anchor anchor, Vector2 size, NinePatch texture, string text = null, NinePatch hoveredTexture = null, Color? hoveredColor = null) : base(anchor, size) {
|
||||||
|
this.Texture = texture;
|
||||||
|
this.HoveredTexture = hoveredTexture;
|
||||||
|
this.HoveredColor = hoveredColor ?? Color.White;
|
||||||
|
|
||||||
|
if (text != null) {
|
||||||
|
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text, true) {
|
||||||
|
IgnoresMouse = true
|
||||||
|
};
|
||||||
|
this.AddChild(this.Text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Draw(GameTime time, SpriteBatch batch, Color color) {
|
||||||
|
var tex = this.Texture;
|
||||||
|
if (this.IsMouseOver) {
|
||||||
|
if (this.HoveredTexture != null)
|
||||||
|
tex = this.HoveredTexture;
|
||||||
|
color = this.HoveredColor.CopyAlpha(color);
|
||||||
|
}
|
||||||
|
batch.Draw(tex, this.DisplayArea, color);
|
||||||
|
base.Draw(time, batch, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,10 +3,12 @@ using System.Collections.Generic;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
|
using MLEM.Input;
|
||||||
|
|
||||||
namespace MLEM.Ui.Elements {
|
namespace MLEM.Ui.Elements {
|
||||||
public abstract class Element {
|
public abstract class Element {
|
||||||
|
|
||||||
|
private readonly List<Element> children = new List<Element>();
|
||||||
private Anchor anchor;
|
private Anchor anchor;
|
||||||
private Vector2 size;
|
private Vector2 size;
|
||||||
private Point offset;
|
private Point offset;
|
||||||
|
@ -48,10 +50,16 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MouseClickCallback OnClicked;
|
||||||
|
public MouseClickCallback OnMouseDown;
|
||||||
|
public MouseCallback OnMouseEnter;
|
||||||
|
public MouseCallback OnMouseExit;
|
||||||
|
|
||||||
public UiSystem System { get; private set; }
|
public UiSystem System { get; private set; }
|
||||||
public Element Parent { get; private set; }
|
public Element Parent { get; private set; }
|
||||||
|
public bool IsMouseOver { get; private set; }
|
||||||
public bool IsHidden;
|
public bool IsHidden;
|
||||||
private readonly List<Element> children = new List<Element>();
|
public bool IgnoresMouse;
|
||||||
|
|
||||||
private Rectangle area;
|
private Rectangle area;
|
||||||
public Rectangle Area {
|
public Rectangle Area {
|
||||||
|
@ -72,10 +80,13 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
private bool areaDirty;
|
private bool areaDirty;
|
||||||
|
|
||||||
public Element(Anchor anchor, Vector2 size, Point positionOffset) {
|
public Element(Anchor anchor, Vector2 size) {
|
||||||
this.anchor = anchor;
|
this.anchor = anchor;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
this.offset = positionOffset;
|
|
||||||
|
this.OnMouseEnter += (element, mousePos) => this.IsMouseOver = true;
|
||||||
|
this.OnMouseExit += (element, mousePos) => this.IsMouseOver = false;
|
||||||
|
|
||||||
this.SetDirty();
|
this.SetDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,8 +249,25 @@ namespace MLEM.Ui.Elements {
|
||||||
public void SetUiSystem(UiSystem system) {
|
public void SetUiSystem(UiSystem system) {
|
||||||
this.System = system;
|
this.System = system;
|
||||||
foreach (var child in this.children)
|
foreach (var child in this.children)
|
||||||
child.System = system;
|
child.SetUiSystem(system);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Element GetMousedElement(Vector2 mousePos) {
|
||||||
|
if (this.IsHidden || this.IgnoresMouse)
|
||||||
|
return null;
|
||||||
|
if (!this.Area.Contains(mousePos))
|
||||||
|
return null;
|
||||||
|
foreach (var child in this.children) {
|
||||||
|
var element = child.GetMousedElement(mousePos);
|
||||||
|
if (element != null)
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public delegate void MouseClickCallback(Element element, Vector2 mousePos, MouseButton button);
|
||||||
|
|
||||||
|
public delegate void MouseCallback(Element element, Vector2 mousePos);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,8 +7,9 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
private readonly NinePatch texture;
|
private readonly NinePatch texture;
|
||||||
|
|
||||||
public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture) : base(anchor, size, positionOffset) {
|
public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture) : base(anchor, size) {
|
||||||
this.texture = texture;
|
this.texture = texture;
|
||||||
|
this.PositionOffset = positionOffset;
|
||||||
this.ChildPadding = new Point(5);
|
this.ChildPadding = new Point(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
public IGenericFont Font => this.font ?? this.System.DefaultFont;
|
public IGenericFont Font => this.font ?? this.System.DefaultFont;
|
||||||
|
|
||||||
public Paragraph(Anchor anchor, float width, Point positionOffset, string text, float textScale = 1, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0), positionOffset) {
|
public Paragraph(Anchor anchor, float width, string text, float textScale = 1, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
this.font = font;
|
this.font = font;
|
||||||
this.TextScale = textScale;
|
this.TextScale = textScale;
|
||||||
|
|
10
MLEM.Ui/Elements/VerticalSpace.cs
Normal file
10
MLEM.Ui/Elements/VerticalSpace.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
|
||||||
|
namespace MLEM.Ui.Elements {
|
||||||
|
public class VerticalSpace : Element {
|
||||||
|
|
||||||
|
public VerticalSpace(int height) : base(Anchor.AutoCenter, new Vector2(1, height)) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
|
using MLEM.Input;
|
||||||
using MLEM.Ui.Elements;
|
using MLEM.Ui.Elements;
|
||||||
|
|
||||||
namespace MLEM.Ui {
|
namespace MLEM.Ui {
|
||||||
|
@ -11,6 +12,8 @@ namespace MLEM.Ui {
|
||||||
|
|
||||||
private readonly GraphicsDevice graphicsDevice;
|
private readonly GraphicsDevice graphicsDevice;
|
||||||
private readonly List<RootElement> rootElements = new List<RootElement>();
|
private readonly List<RootElement> rootElements = new List<RootElement>();
|
||||||
|
private readonly InputHandler inputHandler;
|
||||||
|
private readonly bool isInputOurs;
|
||||||
|
|
||||||
public readonly float GlobalScale;
|
public readonly float GlobalScale;
|
||||||
public readonly IGenericFont DefaultFont;
|
public readonly IGenericFont DefaultFont;
|
||||||
|
@ -20,11 +23,15 @@ namespace MLEM.Ui {
|
||||||
return new Rectangle(bounds.X, bounds.Y, (bounds.Width / this.GlobalScale).Floor(), (bounds.Height / this.GlobalScale).Floor());
|
return new Rectangle(bounds.X, bounds.Y, (bounds.Width / this.GlobalScale).Floor(), (bounds.Height / this.GlobalScale).Floor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public Vector2 MousePos => this.inputHandler.MousePosition.ToVector2() / this.GlobalScale;
|
||||||
|
public Element MousedElement { get; private set; }
|
||||||
|
|
||||||
public UiSystem(GameWindow window, GraphicsDevice device, float scale, IGenericFont defaultFont) {
|
public UiSystem(GameWindow window, GraphicsDevice device, float scale, IGenericFont defaultFont, InputHandler inputHandler = null) {
|
||||||
this.graphicsDevice = device;
|
this.graphicsDevice = device;
|
||||||
this.GlobalScale = scale;
|
this.GlobalScale = scale;
|
||||||
this.DefaultFont = defaultFont;
|
this.DefaultFont = defaultFont;
|
||||||
|
this.inputHandler = inputHandler ?? new InputHandler();
|
||||||
|
this.isInputOurs = inputHandler == null;
|
||||||
|
|
||||||
window.ClientSizeChanged += (sender, args) => {
|
window.ClientSizeChanged += (sender, args) => {
|
||||||
foreach (var root in this.rootElements)
|
foreach (var root in this.rootElements)
|
||||||
|
@ -33,6 +40,27 @@ namespace MLEM.Ui {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(GameTime time) {
|
public void Update(GameTime time) {
|
||||||
|
if (this.isInputOurs)
|
||||||
|
this.inputHandler.Update();
|
||||||
|
|
||||||
|
var mousedNow = this.GetMousedElement();
|
||||||
|
if (mousedNow != this.MousedElement) {
|
||||||
|
if (this.MousedElement != null)
|
||||||
|
this.MousedElement.OnMouseExit?.Invoke(this.MousedElement, this.MousePos);
|
||||||
|
if (mousedNow != null)
|
||||||
|
mousedNow.OnMouseEnter?.Invoke(mousedNow, this.MousePos);
|
||||||
|
this.MousedElement = mousedNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mousedNow != null && (mousedNow.OnMouseDown != null || mousedNow.OnClicked != null)) {
|
||||||
|
foreach (var button in InputHandler.MouseButtons) {
|
||||||
|
if (mousedNow.OnMouseDown != null && this.inputHandler.IsMouseButtonDown(button))
|
||||||
|
mousedNow.OnMouseDown(mousedNow, this.MousePos, button);
|
||||||
|
if (mousedNow.OnClicked != null && this.inputHandler.IsMouseButtonPressed(button))
|
||||||
|
mousedNow.OnClicked(mousedNow, this.MousePos, button);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var root in this.rootElements)
|
foreach (var root in this.rootElements)
|
||||||
root.Element.Update(time);
|
root.Element.Update(time);
|
||||||
}
|
}
|
||||||
|
@ -77,6 +105,15 @@ namespace MLEM.Ui {
|
||||||
return this.rootElements.FindIndex(element => element.Name == name);
|
return this.rootElements.FindIndex(element => element.Name == name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Element GetMousedElement() {
|
||||||
|
foreach (var root in this.rootElements) {
|
||||||
|
var moused = root.Element.GetMousedElement(this.MousePos);
|
||||||
|
if (moused != null)
|
||||||
|
return moused;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct RootElement {
|
public struct RootElement {
|
||||||
|
|
|
@ -12,5 +12,9 @@ namespace MLEM.Extensions {
|
||||||
return new Color((int) (value >> 16 & 0xFF), (int) (value >> 8 & 0xFF), (int) (value >> 0 & 0xFF), (int) (value >> 24 & 0xFF));
|
return new Color((int) (value >> 16 & 0xFF), (int) (value >> 8 & 0xFF), (int) (value >> 0 & 0xFF), (int) (value >> 24 & 0xFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Color CopyAlpha(this Color color, Color other) {
|
||||||
|
return color * (other.A / 255F);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,15 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Input {
|
namespace MLEM.Input {
|
||||||
public class InputHandler {
|
public class InputHandler {
|
||||||
|
|
||||||
|
public static MouseButton[] MouseButtons = EnumHelper.GetValues<MouseButton>().ToArray();
|
||||||
|
|
||||||
public KeyboardState LastKeyboardState { get; private set; }
|
public KeyboardState LastKeyboardState { get; private set; }
|
||||||
public KeyboardState KeyboardState { get; private set; }
|
public KeyboardState KeyboardState { get; private set; }
|
||||||
private readonly bool handleKeyboard;
|
private readonly bool handleKeyboard;
|
||||||
|
|
13
MLEM/Misc/EnumHelper.cs
Normal file
13
MLEM/Misc/EnumHelper.cs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace MLEM.Misc {
|
||||||
|
public static class EnumHelper {
|
||||||
|
|
||||||
|
public static IEnumerable<T> GetValues<T>() {
|
||||||
|
return Enum.GetValues(typeof(T)).Cast<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 1,003 B After Width: | Height: | Size: 1 KiB |
|
@ -18,16 +18,26 @@ namespace Tests {
|
||||||
private NinePatch testPatch;
|
private NinePatch testPatch;
|
||||||
private UiSystem uiSystem;
|
private UiSystem uiSystem;
|
||||||
|
|
||||||
|
public GameImpl() {
|
||||||
|
this.IsMouseVisible = true;
|
||||||
|
}
|
||||||
|
|
||||||
protected override void LoadContent() {
|
protected override void LoadContent() {
|
||||||
base.LoadContent();
|
base.LoadContent();
|
||||||
this.testTexture = LoadContent<Texture2D>("Textures/Test");
|
this.testTexture = LoadContent<Texture2D>("Textures/Test");
|
||||||
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
|
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
|
||||||
|
|
||||||
// Ui system tests
|
// Ui system tests
|
||||||
this.uiSystem = new UiSystem(this.Window, this.GraphicsDevice, 5, new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")));
|
this.uiSystem = new UiSystem(this.Window, this.GraphicsDevice, 5, new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")), this.InputHandler);
|
||||||
|
|
||||||
var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch);
|
var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch);
|
||||||
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, Point.Zero, "This is a test text that is hopefully long enough to cover at least a few lines, otherwise it would be very sad.", 0.2F));
|
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a test text that is hopefully long enough to cover at least a few lines, otherwise it would be very sad.", 0.2F));
|
||||||
|
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 15), new NinePatch(new TextureRegion(this.testTexture, 24, 8, 16, 16), 4), "Test Button") {
|
||||||
|
OnClicked = (element, pos, button) => {
|
||||||
|
if (button == MouseButton.Left)
|
||||||
|
Console.WriteLine("Clicked");
|
||||||
|
}
|
||||||
|
});
|
||||||
this.uiSystem.Add("Test", root);
|
this.uiSystem.Add("Test", root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue