1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

shuffle some things around

This commit is contained in:
Ellpeck 2019-08-10 18:41:56 +02:00
parent 53f0858239
commit f96511d17d
7 changed files with 62 additions and 35 deletions

View file

@ -18,12 +18,12 @@ namespace MLEM.Ui.Elements {
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.font = font ?? Paragraph.DefaultFont;
this.Color = color ?? Color.White;
this.IgnoresMouse = true;
}
public override void ForceUpdateArea() {

View file

@ -10,16 +10,12 @@ namespace MLEM.Ui.Elements {
public static NinePatch DefaultHoveredTexture;
public static Color DefaultHoveredColor = Color.LightGray;
public NinePatch Texture;
public NinePatch HoveredTexture;
public Color HoveredColor;
public NinePatch Texture = DefaultTexture;
public NinePatch HoveredTexture = DefaultHoveredTexture;
public Color HoveredColor = DefaultHoveredColor;
public AutoScaledText Text;
public Button(Anchor anchor, Vector2 size, NinePatch texture = null, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) {
this.Texture = texture ?? DefaultTexture;
this.HoveredTexture = hoveredTexture ?? DefaultHoveredTexture;
this.HoveredColor = hoveredColor ?? DefaultHoveredColor;
public Button(Anchor anchor, Vector2 size, string text = null) : base(anchor, size) {
if (text != null) {
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text) {
IgnoresMouse = true

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MLEM.Extensions;
using MLEM.Input;
@ -61,13 +62,17 @@ namespace MLEM.Ui.Elements {
}
public MouseClickCallback OnClicked;
public MouseClickCallback OnMouseDown;
public GenericCallback OnSelected;
public GenericCallback OnDeselected;
public MouseCallback OnMouseEnter;
public MouseCallback OnMouseExit;
public TextInputCallback OnTextInput;
public UiSystem System { get; private set; }
protected InputHandler Input => this.System.InputHandler;
public Element Parent { get; private set; }
public bool IsMouseOver { get; private set; }
public bool IsSelected { get; private set; }
private bool isHidden;
public bool IsHidden {
get => this.isHidden;
@ -106,11 +111,13 @@ namespace MLEM.Ui.Elements {
this.OnMouseEnter += (element, mousePos) => this.IsMouseOver = true;
this.OnMouseExit += (element, mousePos) => this.IsMouseOver = false;
this.OnSelected += element => this.IsSelected = true;
this.OnDeselected += element => this.IsSelected = false;
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)
index = this.children.Count;
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) {
if (this.IsHidden || this.IgnoresMouse)
return null;
@ -310,5 +311,21 @@ namespace MLEM.Ui.Elements {
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);
}
}
}

View file

@ -4,6 +4,7 @@ namespace MLEM.Ui.Elements {
public class Group : Element {
public Group(Anchor anchor, Vector2 size) : base(anchor, size) {
this.IgnoresMouse = true;
}
}

View file

@ -10,6 +10,7 @@ namespace MLEM.Ui.Elements {
public class Paragraph : Element {
public static IGenericFont DefaultFont;
public static float DefaultTextScale = 1;
private string text;
private float lineHeight;
@ -17,7 +18,7 @@ namespace MLEM.Ui.Elements {
private readonly IGenericFont font;
private readonly bool centerText;
public float TextScale;
public float TextScale = DefaultTextScale;
public string Text {
get => this.text;
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.font = font ?? DefaultFont;
this.TextScale = textScale;
this.centerText = centerText;
}

View file

@ -10,9 +10,9 @@ using MLEM.Ui.Elements;
namespace MLEM.Ui {
public class UiSystem {
private readonly GraphicsDevice graphicsDevice;
public readonly GraphicsDevice GraphicsDevice;
private readonly List<RootElement> rootElements = new List<RootElement>();
private readonly InputHandler inputHandler;
public readonly InputHandler InputHandler;
private readonly bool isInputOurs;
private float globalScale;
@ -26,30 +26,35 @@ namespace MLEM.Ui {
}
public Rectangle ScaledViewport {
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());
}
}
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 SelectedElement { get; private set; }
public float DrawAlpha = 1;
public BlendState BlendState;
public SamplerState SamplerState = SamplerState.PointClamp;
public UiSystem(GameWindow window, GraphicsDevice device, InputHandler inputHandler = null) {
this.graphicsDevice = device;
this.inputHandler = inputHandler ?? new InputHandler();
this.GraphicsDevice = device;
this.InputHandler = inputHandler ?? new InputHandler();
this.isInputOurs = inputHandler == null;
window.ClientSizeChanged += (sender, args) => {
foreach (var root in this.rootElements)
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) {
if (this.isInputOurs)
this.inputHandler.Update();
this.InputHandler.Update();
var mousedNow = this.GetMousedElement();
if (mousedNow != this.MousedElement) {
@ -60,11 +65,17 @@ namespace MLEM.Ui {
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) {
if (mousedNow.OnMouseDown != null && this.inputHandler.IsMouseButtonDown(button))
mousedNow.OnMouseDown(mousedNow, this.MousePos, button);
if (mousedNow.OnClicked != null && this.inputHandler.IsMouseButtonPressed(button))
if (this.InputHandler.IsMouseButtonPressed(button))
mousedNow.OnClicked(mousedNow, this.MousePos, button);
}
}

View file

@ -26,12 +26,14 @@ namespace Tests {
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
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;
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});
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) => {
if (button == MouseButton.Left)
image.IsHidden = !image.IsHidden;