1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-15 21:58:46 +02:00

added auto-scaled text, buttons and vertical space

This commit is contained in:
Ellpeck 2019-08-09 22:04:26 +02:00
parent 4943a7b6f0
commit ef3726b077
12 changed files with 206 additions and 9 deletions

View 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);
}
}
}

View 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);
}
}
}

View file

@ -3,10 +3,12 @@ using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Input;
namespace MLEM.Ui.Elements {
public abstract class Element {
private readonly List<Element> children = new List<Element>();
private Anchor anchor;
private Vector2 size;
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 Element Parent { get; private set; }
public bool IsMouseOver { get; private set; }
public bool IsHidden;
private readonly List<Element> children = new List<Element>();
public bool IgnoresMouse;
private Rectangle area;
public Rectangle Area {
@ -72,10 +80,13 @@ namespace MLEM.Ui.Elements {
}
private bool areaDirty;
public Element(Anchor anchor, Vector2 size, Point positionOffset) {
public Element(Anchor anchor, Vector2 size) {
this.anchor = anchor;
this.size = size;
this.offset = positionOffset;
this.OnMouseEnter += (element, mousePos) => this.IsMouseOver = true;
this.OnMouseExit += (element, mousePos) => this.IsMouseOver = false;
this.SetDirty();
}
@ -238,8 +249,25 @@ namespace MLEM.Ui.Elements {
public void SetUiSystem(UiSystem system) {
this.System = system;
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);
}
}

View file

@ -7,8 +7,9 @@ namespace MLEM.Ui.Elements {
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.PositionOffset = positionOffset;
this.ChildPadding = new Point(5);
}

View file

@ -26,7 +26,7 @@ namespace MLEM.Ui.Elements {
}
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.font = font;
this.TextScale = textScale;

View 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)) {
}
}
}

View file

@ -4,6 +4,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Input;
using MLEM.Ui.Elements;
namespace MLEM.Ui {
@ -11,6 +12,8 @@ namespace MLEM.Ui {
private readonly GraphicsDevice graphicsDevice;
private readonly List<RootElement> rootElements = new List<RootElement>();
private readonly InputHandler inputHandler;
private readonly bool isInputOurs;
public readonly float GlobalScale;
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());
}
}
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.GlobalScale = scale;
this.DefaultFont = defaultFont;
this.inputHandler = inputHandler ?? new InputHandler();
this.isInputOurs = inputHandler == null;
window.ClientSizeChanged += (sender, args) => {
foreach (var root in this.rootElements)
@ -33,6 +40,27 @@ namespace MLEM.Ui {
}
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)
root.Element.Update(time);
}
@ -77,6 +105,15 @@ namespace MLEM.Ui {
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 {

View file

@ -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));
}
public static Color CopyAlpha(this Color color, Color other) {
return color * (other.A / 255F);
}
}
}

View file

@ -1,10 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using MLEM.Misc;
namespace MLEM.Input {
public class InputHandler {
public static MouseButton[] MouseButtons = EnumHelper.GetValues<MouseButton>().ToArray();
public KeyboardState LastKeyboardState { get; private set; }
public KeyboardState KeyboardState { get; private set; }
private readonly bool handleKeyboard;

13
MLEM/Misc/EnumHelper.cs Normal file
View 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

View file

@ -18,16 +18,26 @@ namespace Tests {
private NinePatch testPatch;
private UiSystem uiSystem;
public GameImpl() {
this.IsMouseVisible = true;
}
protected override void LoadContent() {
base.LoadContent();
this.testTexture = LoadContent<Texture2D>("Textures/Test");
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
// 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);
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);
}