1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-23 17:13:38 +02:00
MLEM/MLEM.Ui/UiSystem.cs

93 lines
3.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;
2019-08-09 18:26:28 +02:00
using MLEM.Ui.Elements;
namespace MLEM.Ui {
public class UiSystem {
private readonly GraphicsDevice graphicsDevice;
private readonly List<RootElement> rootElements = new List<RootElement>();
2019-08-09 19:28:48 +02:00
public readonly float GlobalScale;
public readonly IGenericFont DefaultFont;
2019-08-09 18:26:28 +02:00
public Rectangle ScaledViewport {
get {
var bounds = this.graphicsDevice.Viewport.Bounds;
return new Rectangle(bounds.X, bounds.Y, (bounds.Width / this.GlobalScale).Floor(), (bounds.Height / this.GlobalScale).Floor());
}
}
2019-08-09 19:28:48 +02:00
public UiSystem(GameWindow window, GraphicsDevice device, float scale, IGenericFont defaultFont) {
2019-08-09 18:26:28 +02:00
this.graphicsDevice = device;
this.GlobalScale = scale;
2019-08-09 19:28:48 +02:00
this.DefaultFont = defaultFont;
2019-08-09 18:26:28 +02:00
window.ClientSizeChanged += (sender, args) => {
foreach (var root in this.rootElements)
root.Element.ForceUpdateArea();
};
}
public void Update(GameTime time) {
foreach (var root in this.rootElements)
root.Element.Update(time);
}
2019-08-09 19:28:48 +02:00
public void Draw(GameTime time, SpriteBatch batch, Color? color = null, BlendState blendState = null, SamplerState samplerState = null) {
var col = color ?? Color.White;
2019-08-09 18:26:28 +02:00
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, transformMatrix: Matrix.CreateScale(this.GlobalScale));
2019-08-09 19:39:51 +02:00
foreach (var root in this.rootElements) {
if (!root.Element.IsHidden)
root.Element.Draw(time, batch, col);
}
2019-08-09 18:26:28 +02:00
batch.End();
2019-08-09 19:28:48 +02:00
2019-08-09 19:39:51 +02:00
foreach (var root in this.rootElements) {
if (!root.Element.IsHidden)
root.Element.DrawUnbound(time, batch, col, blendState, samplerState);
}
2019-08-09 18:26:28 +02:00
}
public void Add(string name, Element root) {
if (this.IndexOf(name) >= 0)
throw new ArgumentException($"There is already a root element with name {name}");
this.rootElements.Add(new RootElement(name, root));
2019-08-09 19:28:48 +02:00
root.SetUiSystem(this);
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 Element Get(string name) {
var index = this.IndexOf(name);
return index < 0 ? null : this.rootElements[index].Element;
}
private int IndexOf(string name) {
return this.rootElements.FindIndex(element => element.Name == name);
}
}
public struct RootElement {
public readonly string Name;
public readonly Element Element;
public RootElement(string name, Element element) {
this.Name = name;
this.Element = element;
}
}
}