From f96511d17d597bd9279ac8f443444e0f74a6255f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 10 Aug 2019 18:41:56 +0200 Subject: [PATCH] shuffle some things around --- MLEM.Ui/Elements/AutoScaledText.cs | 6 +++--- MLEM.Ui/Elements/Button.cs | 12 ++++------- MLEM.Ui/Elements/Element.cs | 33 ++++++++++++++++++++++-------- MLEM.Ui/Elements/Group.cs | 1 + MLEM.Ui/Elements/Paragraph.cs | 6 +++--- MLEM.Ui/UiSystem.cs | 33 ++++++++++++++++++++---------- Tests/GameImpl.cs | 6 ++++-- 7 files changed, 62 insertions(+), 35 deletions(-) diff --git a/MLEM.Ui/Elements/AutoScaledText.cs b/MLEM.Ui/Elements/AutoScaledText.cs index 500521e..de247dd 100644 --- a/MLEM.Ui/Elements/AutoScaledText.cs +++ b/MLEM.Ui/Elements/AutoScaledText.cs @@ -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() { diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index b717339..df2b988 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -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 diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 96bcf5f..9cb6c2d 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -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 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); + } + } } \ No newline at end of file diff --git a/MLEM.Ui/Elements/Group.cs b/MLEM.Ui/Elements/Group.cs index 77163b3..470a220 100644 --- a/MLEM.Ui/Elements/Group.cs +++ b/MLEM.Ui/Elements/Group.cs @@ -4,6 +4,7 @@ namespace MLEM.Ui.Elements { public class Group : Element { public Group(Anchor anchor, Vector2 size) : base(anchor, size) { + this.IgnoresMouse = true; } } diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 4b14086..a13fcab 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -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; } diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index b3fd108..608f24f 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -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 rootElements = new List(); - 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); } } diff --git a/Tests/GameImpl.cs b/Tests/GameImpl.cs index 18ba9aa..1300702 100644 --- a/Tests/GameImpl.cs +++ b/Tests/GameImpl.cs @@ -26,12 +26,14 @@ namespace Tests { this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8); Paragraph.DefaultFont = new GenericSpriteFont(LoadContent("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;