2019-08-09 18:26:28 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-08-28 18:27:17 +02:00
|
|
|
using System.Linq;
|
2019-08-09 18:26:28 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using MLEM.Extensions;
|
2019-08-09 19:28:48 +02:00
|
|
|
using MLEM.Font;
|
2019-08-09 22:04:26 +02:00
|
|
|
using MLEM.Input;
|
2019-08-09 18:26:28 +02:00
|
|
|
using MLEM.Ui.Elements;
|
2019-08-10 21:37:10 +02:00
|
|
|
using MLEM.Ui.Style;
|
2019-08-09 18:26:28 +02:00
|
|
|
|
|
|
|
namespace MLEM.Ui {
|
|
|
|
public class UiSystem {
|
|
|
|
|
2019-08-10 18:41:56 +02:00
|
|
|
public readonly GraphicsDevice GraphicsDevice;
|
2019-08-12 19:44:16 +02:00
|
|
|
public Rectangle Viewport { get; private set; }
|
2019-08-09 18:26:28 +02:00
|
|
|
private readonly List<RootElement> rootElements = new List<RootElement>();
|
|
|
|
|
2019-08-23 19:46:36 +02:00
|
|
|
public bool AutoScaleWithScreen;
|
|
|
|
public Point AutoScaleReferenceSize;
|
|
|
|
|
2019-08-11 18:02:21 +02:00
|
|
|
private float globalScale = 1;
|
2019-08-09 23:43:50 +02:00
|
|
|
public float GlobalScale {
|
2019-08-23 19:46:36 +02:00
|
|
|
get {
|
|
|
|
if (!this.AutoScaleWithScreen)
|
|
|
|
return this.globalScale;
|
|
|
|
return Math.Min(this.Viewport.Width / (float) this.AutoScaleReferenceSize.X, this.Viewport.Height / (float) this.AutoScaleReferenceSize.Y) * this.globalScale;
|
|
|
|
}
|
2019-08-09 23:43:50 +02:00
|
|
|
set {
|
|
|
|
this.globalScale = value;
|
|
|
|
foreach (var root in this.rootElements)
|
|
|
|
root.Element.ForceUpdateArea();
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 21:37:10 +02:00
|
|
|
private UiStyle style;
|
|
|
|
public UiStyle Style {
|
|
|
|
get => this.style;
|
|
|
|
set {
|
|
|
|
this.style = value;
|
|
|
|
foreach (var root in this.rootElements) {
|
2019-08-28 18:58:05 +02:00
|
|
|
root.Element.Propagate(e => e.System = this);
|
2019-08-12 14:44:42 +02:00
|
|
|
root.Element.SetAreaDirty();
|
2019-08-10 21:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 13:42:18 +02:00
|
|
|
public float DrawAlpha = 1;
|
2019-08-09 23:43:50 +02:00
|
|
|
public BlendState BlendState;
|
|
|
|
public SamplerState SamplerState = SamplerState.PointClamp;
|
2019-08-28 18:27:17 +02:00
|
|
|
public UiControls Controls;
|
2019-08-25 21:49:27 +02:00
|
|
|
|
2019-08-10 21:37:10 +02:00
|
|
|
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
|
2019-08-28 18:27:17 +02:00
|
|
|
this.Controls = new UiControls(this, inputHandler);
|
2019-08-10 18:41:56 +02:00
|
|
|
this.GraphicsDevice = device;
|
2019-08-10 21:37:10 +02:00
|
|
|
this.style = style;
|
2019-08-12 19:44:16 +02:00
|
|
|
this.Viewport = device.Viewport.Bounds;
|
2019-08-23 19:46:36 +02:00
|
|
|
this.AutoScaleReferenceSize = this.Viewport.Size;
|
2019-08-09 19:28:48 +02:00
|
|
|
|
2019-08-09 18:26:28 +02:00
|
|
|
window.ClientSizeChanged += (sender, args) => {
|
2019-08-12 19:44:16 +02:00
|
|
|
this.Viewport = device.Viewport.Bounds;
|
2019-08-09 18:26:28 +02:00
|
|
|
foreach (var root in this.rootElements)
|
|
|
|
root.Element.ForceUpdateArea();
|
|
|
|
};
|
2019-08-10 18:41:56 +02:00
|
|
|
window.TextInput += (sender, args) => {
|
|
|
|
foreach (var root in this.rootElements)
|
2019-08-28 18:58:05 +02:00
|
|
|
root.Element.Propagate(e => e.OnTextInput?.Invoke(e, args.Key, args.Character));
|
2019-08-10 18:41:56 +02:00
|
|
|
};
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Update(GameTime time) {
|
2019-08-28 18:27:17 +02:00
|
|
|
this.Controls.Update();
|
2019-08-10 18:41:56 +02:00
|
|
|
|
2019-08-09 18:26:28 +02:00
|
|
|
foreach (var root in this.rootElements)
|
|
|
|
root.Element.Update(time);
|
|
|
|
}
|
|
|
|
|
2019-08-15 14:59:15 +02:00
|
|
|
public void DrawEarly(GameTime time, SpriteBatch batch) {
|
2019-08-12 19:44:16 +02:00
|
|
|
foreach (var root in this.rootElements) {
|
|
|
|
if (!root.Element.IsHidden)
|
2019-08-15 14:59:15 +02:00
|
|
|
root.Element.DrawEarly(time, batch, this.DrawAlpha * root.Element.DrawAlpha, this.BlendState, this.SamplerState);
|
2019-08-12 19:44:16 +02:00
|
|
|
}
|
2019-08-15 14:59:15 +02:00
|
|
|
}
|
2019-08-12 19:44:16 +02:00
|
|
|
|
2019-08-15 14:59:15 +02:00
|
|
|
public void Draw(GameTime time, SpriteBatch batch) {
|
2019-08-09 19:39:51 +02:00
|
|
|
foreach (var root in this.rootElements) {
|
2019-08-11 18:02:21 +02:00
|
|
|
if (root.Element.IsHidden)
|
|
|
|
continue;
|
2019-08-11 21:24:09 +02:00
|
|
|
batch.Begin(SpriteSortMode.Deferred, this.BlendState, this.SamplerState);
|
2019-08-12 19:44:16 +02:00
|
|
|
root.Element.Draw(time, batch, this.DrawAlpha * root.Element.DrawAlpha, Point.Zero);
|
2019-08-11 18:02:21 +02:00
|
|
|
batch.End();
|
2019-08-09 19:39:51 +02:00
|
|
|
}
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 22:23:10 +02:00
|
|
|
public RootElement Add(string name, Element element) {
|
|
|
|
var root = new RootElement(name, element, this);
|
2019-08-24 15:00:08 +02:00
|
|
|
return !this.Add(root) ? null : root;
|
2019-08-23 22:23:10 +02:00
|
|
|
}
|
2019-08-09 18:26:28 +02:00
|
|
|
|
2019-08-24 15:00:08 +02:00
|
|
|
internal bool Add(RootElement root, int index = -1) {
|
2019-08-23 22:23:10 +02:00
|
|
|
if (this.IndexOf(root.Name) >= 0)
|
2019-08-24 15:00:08 +02:00
|
|
|
return false;
|
2019-08-23 22:23:10 +02:00
|
|
|
if (index < 0 || index > this.rootElements.Count)
|
|
|
|
index = this.rootElements.Count;
|
|
|
|
this.rootElements.Insert(index, root);
|
2019-08-28 18:58:05 +02:00
|
|
|
root.Element.Propagate(e => {
|
|
|
|
e.Root = root;
|
|
|
|
e.System = this;
|
|
|
|
});
|
2019-08-24 15:00:08 +02:00
|
|
|
return true;
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Remove(string name) {
|
2019-08-23 22:23:10 +02:00
|
|
|
var root = this.Get(name);
|
|
|
|
if (root == null)
|
2019-08-09 18:26:28 +02:00
|
|
|
return;
|
2019-08-23 22:23:10 +02:00
|
|
|
this.rootElements.Remove(root);
|
2019-08-28 18:58:05 +02:00
|
|
|
root.Element.Propagate(e => {
|
|
|
|
e.Root = null;
|
|
|
|
e.System = null;
|
|
|
|
});
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:02:21 +02:00
|
|
|
public RootElement Get(string name) {
|
2019-08-09 18:26:28 +02:00
|
|
|
var index = this.IndexOf(name);
|
2019-08-11 18:02:21 +02:00
|
|
|
return index < 0 ? null : this.rootElements[index];
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private int IndexOf(string name) {
|
|
|
|
return this.rootElements.FindIndex(element => element.Name == name);
|
|
|
|
}
|
|
|
|
|
2019-08-28 18:27:17 +02:00
|
|
|
public IEnumerable<RootElement> GetRootElements() {
|
|
|
|
for (var i = this.rootElements.Count - 1; i >= 0; i--)
|
|
|
|
yield return this.rootElements[i];
|
2019-08-28 18:58:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void Propagate(Action<Element> action) {
|
|
|
|
foreach (var root in this.rootElements)
|
|
|
|
root.Element.Propagate(action);
|
2019-08-09 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:02:21 +02:00
|
|
|
public class RootElement {
|
2019-08-09 18:26:28 +02:00
|
|
|
|
|
|
|
public readonly string Name;
|
|
|
|
public readonly Element Element;
|
2019-08-11 18:02:21 +02:00
|
|
|
public readonly UiSystem System;
|
|
|
|
private float scale = 1;
|
|
|
|
public float Scale {
|
|
|
|
get => this.scale;
|
|
|
|
set {
|
|
|
|
if (this.scale == value)
|
|
|
|
return;
|
|
|
|
this.scale = value;
|
|
|
|
this.Element.ForceUpdateArea();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public float ActualScale => this.System.GlobalScale * this.Scale;
|
2019-08-28 18:27:17 +02:00
|
|
|
public bool CanSelectContent = true;
|
2019-08-09 18:26:28 +02:00
|
|
|
|
2019-08-11 18:02:21 +02:00
|
|
|
public RootElement(string name, Element element, UiSystem system) {
|
2019-08-09 18:26:28 +02:00
|
|
|
this.Name = name;
|
|
|
|
this.Element = element;
|
2019-08-11 18:02:21 +02:00
|
|
|
this.System = system;
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 22:23:10 +02:00
|
|
|
public void MoveToFront() {
|
|
|
|
this.System.Remove(this.Name);
|
|
|
|
this.System.Add(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void MoveToBack() {
|
|
|
|
this.System.Remove(this.Name);
|
|
|
|
this.System.Add(this, 0);
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:26:28 +02:00
|
|
|
}
|
|
|
|
}
|