mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
shuffle some things around
This commit is contained in:
parent
53f0858239
commit
f96511d17d
7 changed files with 62 additions and 35 deletions
|
@ -18,12 +18,12 @@ namespace MLEM.Ui.Elements {
|
||||||
this.SetDirty();
|
this.SetDirty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Color Color;
|
public Color Color = Color.White;
|
||||||
|
|
||||||
public AutoScaledText(Anchor anchor, Vector2 size, string text, Color? color = null, IGenericFont font = null) : base(anchor, size) {
|
public AutoScaledText(Anchor anchor, Vector2 size, string text, IGenericFont font = null) : base(anchor, size) {
|
||||||
this.Text = text;
|
this.Text = text;
|
||||||
this.font = font ?? Paragraph.DefaultFont;
|
this.font = font ?? Paragraph.DefaultFont;
|
||||||
this.Color = color ?? Color.White;
|
this.IgnoresMouse = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void ForceUpdateArea() {
|
public override void ForceUpdateArea() {
|
||||||
|
|
|
@ -10,16 +10,12 @@ namespace MLEM.Ui.Elements {
|
||||||
public static NinePatch DefaultHoveredTexture;
|
public static NinePatch DefaultHoveredTexture;
|
||||||
public static Color DefaultHoveredColor = Color.LightGray;
|
public static Color DefaultHoveredColor = Color.LightGray;
|
||||||
|
|
||||||
public NinePatch Texture;
|
public NinePatch Texture = DefaultTexture;
|
||||||
public NinePatch HoveredTexture;
|
public NinePatch HoveredTexture = DefaultHoveredTexture;
|
||||||
public Color HoveredColor;
|
public Color HoveredColor = DefaultHoveredColor;
|
||||||
public AutoScaledText Text;
|
public AutoScaledText Text;
|
||||||
|
|
||||||
public Button(Anchor anchor, Vector2 size, NinePatch texture = null, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) {
|
public Button(Anchor anchor, Vector2 size, string text = null) : base(anchor, size) {
|
||||||
this.Texture = texture ?? DefaultTexture;
|
|
||||||
this.HoveredTexture = hoveredTexture ?? DefaultHoveredTexture;
|
|
||||||
this.HoveredColor = hoveredColor ?? DefaultHoveredColor;
|
|
||||||
|
|
||||||
if (text != null) {
|
if (text != null) {
|
||||||
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text) {
|
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text) {
|
||||||
IgnoresMouse = true
|
IgnoresMouse = true
|
||||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using Microsoft.Xna.Framework.Input;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
using MLEM.Input;
|
using MLEM.Input;
|
||||||
|
|
||||||
|
@ -61,13 +62,17 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
|
|
||||||
public MouseClickCallback OnClicked;
|
public MouseClickCallback OnClicked;
|
||||||
public MouseClickCallback OnMouseDown;
|
public GenericCallback OnSelected;
|
||||||
|
public GenericCallback OnDeselected;
|
||||||
public MouseCallback OnMouseEnter;
|
public MouseCallback OnMouseEnter;
|
||||||
public MouseCallback OnMouseExit;
|
public MouseCallback OnMouseExit;
|
||||||
|
public TextInputCallback OnTextInput;
|
||||||
|
|
||||||
public UiSystem System { get; private set; }
|
public UiSystem System { get; private set; }
|
||||||
|
protected InputHandler Input => this.System.InputHandler;
|
||||||
public Element Parent { get; private set; }
|
public Element Parent { get; private set; }
|
||||||
public bool IsMouseOver { get; private set; }
|
public bool IsMouseOver { get; private set; }
|
||||||
|
public bool IsSelected { get; private set; }
|
||||||
private bool isHidden;
|
private bool isHidden;
|
||||||
public bool IsHidden {
|
public bool IsHidden {
|
||||||
get => this.isHidden;
|
get => this.isHidden;
|
||||||
|
@ -106,11 +111,13 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
this.OnMouseEnter += (element, mousePos) => this.IsMouseOver = true;
|
this.OnMouseEnter += (element, mousePos) => this.IsMouseOver = true;
|
||||||
this.OnMouseExit += (element, mousePos) => this.IsMouseOver = false;
|
this.OnMouseExit += (element, mousePos) => this.IsMouseOver = false;
|
||||||
|
this.OnSelected += element => this.IsSelected = true;
|
||||||
|
this.OnDeselected += element => this.IsSelected = false;
|
||||||
|
|
||||||
this.SetDirty();
|
this.SetDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element AddChild(Element element, int index = -1) {
|
public T AddChild<T>(T element, int index = -1) where T : Element {
|
||||||
if (index < 0 || index > this.children.Count)
|
if (index < 0 || index > this.children.Count)
|
||||||
index = this.children.Count;
|
index = this.children.Count;
|
||||||
this.children.Insert(index, element);
|
this.children.Insert(index, element);
|
||||||
|
@ -287,12 +294,6 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetUiSystem(UiSystem system) {
|
|
||||||
this.System = system;
|
|
||||||
foreach (var child in this.children)
|
|
||||||
child.SetUiSystem(system);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Element GetMousedElement(Vector2 mousePos) {
|
public Element GetMousedElement(Vector2 mousePos) {
|
||||||
if (this.IsHidden || this.IgnoresMouse)
|
if (this.IsHidden || this.IgnoresMouse)
|
||||||
return null;
|
return null;
|
||||||
|
@ -310,5 +311,21 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
public delegate void MouseCallback(Element element, Vector2 mousePos);
|
public delegate void MouseCallback(Element element, Vector2 mousePos);
|
||||||
|
|
||||||
|
public delegate void TextInputCallback(Element element, Keys key, char character);
|
||||||
|
|
||||||
|
public delegate void GenericCallback(Element element);
|
||||||
|
|
||||||
|
internal void SetUiSystem(UiSystem system) {
|
||||||
|
this.System = system;
|
||||||
|
foreach (var child in this.children)
|
||||||
|
child.SetUiSystem(system);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void PropagateInput(Keys key, char character) {
|
||||||
|
this.OnTextInput?.Invoke(this, key, character);
|
||||||
|
foreach (var child in this.children)
|
||||||
|
child.PropagateInput(key, character);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,6 +4,7 @@ namespace MLEM.Ui.Elements {
|
||||||
public class Group : Element {
|
public class Group : Element {
|
||||||
|
|
||||||
public Group(Anchor anchor, Vector2 size) : base(anchor, size) {
|
public Group(Anchor anchor, Vector2 size) : base(anchor, size) {
|
||||||
|
this.IgnoresMouse = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ namespace MLEM.Ui.Elements {
|
||||||
public class Paragraph : Element {
|
public class Paragraph : Element {
|
||||||
|
|
||||||
public static IGenericFont DefaultFont;
|
public static IGenericFont DefaultFont;
|
||||||
|
public static float DefaultTextScale = 1;
|
||||||
|
|
||||||
private string text;
|
private string text;
|
||||||
private float lineHeight;
|
private float lineHeight;
|
||||||
|
@ -17,7 +18,7 @@ namespace MLEM.Ui.Elements {
|
||||||
private readonly IGenericFont font;
|
private readonly IGenericFont font;
|
||||||
private readonly bool centerText;
|
private readonly bool centerText;
|
||||||
|
|
||||||
public float TextScale;
|
public float TextScale = DefaultTextScale;
|
||||||
public string Text {
|
public string Text {
|
||||||
get => this.text;
|
get => this.text;
|
||||||
set {
|
set {
|
||||||
|
@ -27,10 +28,9 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Paragraph(Anchor anchor, float width, string text, float textScale = 1, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
|
public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
this.font = font ?? DefaultFont;
|
this.font = font ?? DefaultFont;
|
||||||
this.TextScale = textScale;
|
|
||||||
this.centerText = centerText;
|
this.centerText = centerText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@ using MLEM.Ui.Elements;
|
||||||
namespace MLEM.Ui {
|
namespace MLEM.Ui {
|
||||||
public class UiSystem {
|
public class UiSystem {
|
||||||
|
|
||||||
private readonly GraphicsDevice graphicsDevice;
|
public readonly GraphicsDevice GraphicsDevice;
|
||||||
private readonly List<RootElement> rootElements = new List<RootElement>();
|
private readonly List<RootElement> rootElements = new List<RootElement>();
|
||||||
private readonly InputHandler inputHandler;
|
public readonly InputHandler InputHandler;
|
||||||
private readonly bool isInputOurs;
|
private readonly bool isInputOurs;
|
||||||
|
|
||||||
private float globalScale;
|
private float globalScale;
|
||||||
|
@ -26,30 +26,35 @@ namespace MLEM.Ui {
|
||||||
}
|
}
|
||||||
public Rectangle ScaledViewport {
|
public Rectangle ScaledViewport {
|
||||||
get {
|
get {
|
||||||
var bounds = this.graphicsDevice.Viewport.Bounds;
|
var bounds = this.GraphicsDevice.Viewport.Bounds;
|
||||||
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 Vector2 MousePos => this.InputHandler.MousePosition.ToVector2() / this.globalScale;
|
||||||
public Element MousedElement { get; private set; }
|
public Element MousedElement { get; private set; }
|
||||||
|
public Element SelectedElement { get; private set; }
|
||||||
public float DrawAlpha = 1;
|
public float DrawAlpha = 1;
|
||||||
public BlendState BlendState;
|
public BlendState BlendState;
|
||||||
public SamplerState SamplerState = SamplerState.PointClamp;
|
public SamplerState SamplerState = SamplerState.PointClamp;
|
||||||
|
|
||||||
public UiSystem(GameWindow window, GraphicsDevice device, InputHandler inputHandler = null) {
|
public UiSystem(GameWindow window, GraphicsDevice device, InputHandler inputHandler = null) {
|
||||||
this.graphicsDevice = device;
|
this.GraphicsDevice = device;
|
||||||
this.inputHandler = inputHandler ?? new InputHandler();
|
this.InputHandler = inputHandler ?? new InputHandler();
|
||||||
this.isInputOurs = inputHandler == null;
|
this.isInputOurs = inputHandler == null;
|
||||||
|
|
||||||
window.ClientSizeChanged += (sender, args) => {
|
window.ClientSizeChanged += (sender, args) => {
|
||||||
foreach (var root in this.rootElements)
|
foreach (var root in this.rootElements)
|
||||||
root.Element.ForceUpdateArea();
|
root.Element.ForceUpdateArea();
|
||||||
};
|
};
|
||||||
|
window.TextInput += (sender, args) => {
|
||||||
|
foreach (var root in this.rootElements)
|
||||||
|
root.Element.PropagateInput(args.Key, args.Character);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(GameTime time) {
|
public void Update(GameTime time) {
|
||||||
if (this.isInputOurs)
|
if (this.isInputOurs)
|
||||||
this.inputHandler.Update();
|
this.InputHandler.Update();
|
||||||
|
|
||||||
var mousedNow = this.GetMousedElement();
|
var mousedNow = this.GetMousedElement();
|
||||||
if (mousedNow != this.MousedElement) {
|
if (mousedNow != this.MousedElement) {
|
||||||
|
@ -60,11 +65,17 @@ namespace MLEM.Ui {
|
||||||
this.MousedElement = mousedNow;
|
this.MousedElement = mousedNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mousedNow != null && (mousedNow.OnMouseDown != null || mousedNow.OnClicked != null)) {
|
if (this.SelectedElement != mousedNow && this.InputHandler.IsMouseButtonPressed(MouseButton.Left)) {
|
||||||
|
if (this.SelectedElement != null)
|
||||||
|
this.SelectedElement.OnDeselected?.Invoke(this.SelectedElement);
|
||||||
|
if (mousedNow != null)
|
||||||
|
mousedNow.OnSelected?.Invoke(mousedNow);
|
||||||
|
this.SelectedElement = mousedNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mousedNow?.OnClicked != null) {
|
||||||
foreach (var button in InputHandler.MouseButtons) {
|
foreach (var button in InputHandler.MouseButtons) {
|
||||||
if (mousedNow.OnMouseDown != null && this.inputHandler.IsMouseButtonDown(button))
|
if (this.InputHandler.IsMouseButtonPressed(button))
|
||||||
mousedNow.OnMouseDown(mousedNow, this.MousePos, button);
|
|
||||||
if (mousedNow.OnClicked != null && this.inputHandler.IsMouseButtonPressed(button))
|
|
||||||
mousedNow.OnClicked(mousedNow, this.MousePos, button);
|
mousedNow.OnClicked(mousedNow, this.MousePos, button);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,12 +26,14 @@ namespace Tests {
|
||||||
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);
|
||||||
|
|
||||||
Paragraph.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
|
Paragraph.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
|
||||||
|
Paragraph.DefaultTextScale = 0.2F;
|
||||||
|
Button.DefaultTexture = new NinePatch(new TextureRegion(this.testTexture, 24, 8, 16, 16), 4);
|
||||||
this.UiSystem.GlobalScale = 5;
|
this.UiSystem.GlobalScale = 5;
|
||||||
|
|
||||||
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, "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."));
|
||||||
var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(20, 20), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true});
|
var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(20, 20), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true});
|
||||||
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 15), new NinePatch(new TextureRegion(this.testTexture, 24, 8, 16, 16), 4), "Test Button") {
|
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 15), "Test Button") {
|
||||||
OnClicked = (element, pos, button) => {
|
OnClicked = (element, pos, button) => {
|
||||||
if (button == MouseButton.Left)
|
if (button == MouseButton.Left)
|
||||||
image.IsHidden = !image.IsHidden;
|
image.IsHidden = !image.IsHidden;
|
||||||
|
|
Loading…
Reference in a new issue