1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-14 18:19:09 +01:00
MLEM/MLEM.Ui/UiSystem.cs

175 lines
6.1 KiB
C#
Raw Normal View History

2019-08-09 18:26:28 +02:00
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
2019-08-09 19:28:48 +02:00
using MLEM.Font;
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;
public Rectangle Viewport { get; private set; }
2019-08-09 18:26:28 +02:00
private readonly List<RootElement> rootElements = new List<RootElement>();
2019-08-10 18:41:56 +02:00
public readonly InputHandler InputHandler;
private readonly bool isInputOurs;
2019-08-09 18:26:28 +02:00
private float globalScale = 1;
2019-08-09 23:43:50 +02:00
public float GlobalScale {
get => this.globalScale;
set {
this.globalScale = value;
foreach (var root in this.rootElements)
root.Element.ForceUpdateArea();
}
}
public Element MousedElement { get; private set; }
2019-08-10 18:41:56 +02:00
public Element SelectedElement { get; private set; }
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) {
root.Element.PropagateUiSystem(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-09 18:26:28 +02:00
2019-08-10 21:37:10 +02:00
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
2019-08-10 18:41:56 +02:00
this.GraphicsDevice = device;
this.InputHandler = inputHandler ?? new InputHandler();
this.isInputOurs = inputHandler == null;
2019-08-10 21:37:10 +02:00
this.style = style;
this.Viewport = device.Viewport.Bounds;
2019-08-09 19:28:48 +02:00
2019-08-09 18:26:28 +02:00
window.ClientSizeChanged += (sender, args) => {
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)
root.Element.PropagateInput(args.Key, args.Character);
};
2019-08-09 18:26:28 +02:00
}
public void Update(GameTime time) {
if (this.isInputOurs)
2019-08-10 18:41:56 +02:00
this.InputHandler.Update();
var mousedNow = this.GetMousedElement();
if (mousedNow != this.MousedElement) {
if (this.MousedElement != null)
this.MousedElement.OnMouseExit?.Invoke(this.MousedElement);
if (mousedNow != null)
mousedNow.OnMouseEnter?.Invoke(mousedNow);
this.MousedElement = mousedNow;
}
2019-08-10 18:41:56 +02:00
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) {
2019-08-10 18:41:56 +02:00
if (this.InputHandler.IsMouseButtonPressed(button))
mousedNow.OnClicked(mousedNow, button);
}
}
2019-08-09 18:26:28 +02:00
foreach (var root in this.rootElements)
root.Element.Update(time);
}
public void DrawEarly(GameTime time, SpriteBatch batch) {
foreach (var root in this.rootElements) {
if (!root.Element.IsHidden)
root.Element.DrawEarly(time, batch, this.DrawAlpha * root.Element.DrawAlpha, this.BlendState, this.SamplerState);
}
}
public void Draw(GameTime time, SpriteBatch batch) {
2019-08-09 19:39:51 +02:00
foreach (var root in this.rootElements) {
if (root.Element.IsHidden)
continue;
batch.Begin(SpriteSortMode.Deferred, this.BlendState, this.SamplerState);
root.Element.Draw(time, batch, this.DrawAlpha * root.Element.DrawAlpha, Point.Zero);
batch.End();
2019-08-09 19:39:51 +02:00
}
2019-08-09 18:26:28 +02:00
}
public RootElement Add(string name, Element root) {
2019-08-09 18:26:28 +02:00
if (this.IndexOf(name) >= 0)
throw new ArgumentException($"There is already a root element with name {name}");
var rootInst = new RootElement(name, root, this);
this.rootElements.Add(rootInst);
root.PropagateRoot(rootInst);
2019-08-10 21:37:10 +02:00
root.PropagateUiSystem(this);
return rootInst;
2019-08-09 18:26:28 +02:00
}
public void Remove(string name) {
var index = this.IndexOf(name);
if (index < 0)
return;
this.rootElements.RemoveAt(index);
}
public RootElement Get(string name) {
2019-08-09 18:26:28 +02:00
var index = this.IndexOf(name);
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);
}
private Element GetMousedElement() {
foreach (var root in this.rootElements) {
var moused = root.Element.GetMousedElement();
if (moused != null)
return moused;
}
return null;
}
2019-08-09 18:26:28 +02:00
}
public class RootElement {
2019-08-09 18:26:28 +02:00
public readonly string Name;
public readonly Element Element;
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-09 18:26:28 +02:00
public RootElement(string name, Element element, UiSystem system) {
2019-08-09 18:26:28 +02:00
this.Name = name;
this.Element = element;
this.System = system;
2019-08-09 18:26:28 +02:00
}
}
}