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

added ui system to startup

This commit is contained in:
Ellpeck 2019-08-09 23:43:50 +02:00
parent 18e634cad8
commit 28fca1cc24
4 changed files with 31 additions and 36 deletions

View file

@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
<ProjectReference Include="..\MLEM\MLEM.csproj" />
</ItemGroup>
</Project>

View file

@ -3,6 +3,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MLEM.Input;
using MLEM.Ui;
using MonoGame.Extended;
namespace MLEM.Startup {
@ -14,6 +15,7 @@ namespace MLEM.Startup {
public readonly GraphicsDeviceManager GraphicsDeviceManager;
public SpriteBatch SpriteBatch { get; protected set; }
public InputHandler InputHandler { get; protected set; }
public UiSystem UiSystem { get; protected set; }
public MlemGame(int windowWidth = 1280, int windowHeight = 720, bool vsync = false, bool allowResizing = true, string contentDir = "Content") {
instance = this;
@ -38,6 +40,7 @@ namespace MLEM.Startup {
protected override void LoadContent() {
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
this.InputHandler = new InputHandler();
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, this.InputHandler);
}
protected override void Initialize() {
@ -48,8 +51,8 @@ namespace MLEM.Startup {
protected override void Update(GameTime gameTime) {
base.Update(gameTime);
if (this.InputHandler != null)
this.InputHandler.Update();
this.InputHandler.Update();
this.UiSystem.Update(gameTime);
CoroutineHandler.Tick(gameTime.GetElapsedSeconds());
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
@ -57,6 +60,7 @@ namespace MLEM.Startup {
protected override void Draw(GameTime gameTime) {
base.Draw(gameTime);
this.UiSystem.Draw(gameTime, this.SpriteBatch);
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
}

View file

@ -15,21 +15,30 @@ namespace MLEM.Ui {
private readonly InputHandler inputHandler;
private readonly bool isInputOurs;
public readonly float GlobalScale;
public readonly IGenericFont DefaultFont;
private float globalScale;
public float GlobalScale {
get => this.globalScale;
set {
this.globalScale = value;
foreach (var root in this.rootElements)
root.Element.ForceUpdateArea();
}
}
public IGenericFont DefaultFont;
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());
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 Color DrawColor = Color.White;
public BlendState BlendState;
public SamplerState SamplerState = SamplerState.PointClamp;
public UiSystem(GameWindow window, GraphicsDevice device, float scale, IGenericFont defaultFont, InputHandler inputHandler = null) {
public UiSystem(GameWindow window, GraphicsDevice device, InputHandler inputHandler = null) {
this.graphicsDevice = device;
this.GlobalScale = scale;
this.DefaultFont = defaultFont;
this.inputHandler = inputHandler ?? new InputHandler();
this.isInputOurs = inputHandler == null;
@ -65,19 +74,17 @@ namespace MLEM.Ui {
root.Element.Update(time);
}
public void Draw(GameTime time, SpriteBatch batch, Color? color = null, BlendState blendState = null, SamplerState samplerState = null) {
var col = color ?? Color.White;
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, transformMatrix: Matrix.CreateScale(this.GlobalScale));
public void Draw(GameTime time, SpriteBatch batch) {
batch.Begin(SpriteSortMode.Deferred, this.BlendState, this.SamplerState, transformMatrix: Matrix.CreateScale(this.globalScale));
foreach (var root in this.rootElements) {
if (!root.Element.IsHidden)
root.Element.Draw(time, batch, col);
root.Element.Draw(time, batch, this.DrawColor);
}
batch.End();
foreach (var root in this.rootElements) {
if (!root.Element.IsHidden)
root.Element.DrawUnbound(time, batch, col, blendState, samplerState);
root.Element.DrawUnbound(time, batch, this.DrawColor, this.BlendState, this.SamplerState);
}
}

View file

@ -16,7 +16,6 @@ namespace Tests {
private Texture2D testTexture;
private NinePatch testPatch;
private UiSystem uiSystem;
public GameImpl() {
this.IsMouseVisible = true;
@ -27,8 +26,8 @@ namespace Tests {
this.testTexture = LoadContent<Texture2D>("Textures/Test");
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
// Ui system tests
this.uiSystem = new UiSystem(this.Window, this.GraphicsDevice, 5, new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")), this.InputHandler);
this.UiSystem.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
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));
@ -39,28 +38,12 @@ namespace Tests {
image.IsHidden = !image.IsHidden;
}
});
this.uiSystem.Add("Test", root);
}
protected override void Update(GameTime gameTime) {
base.Update(gameTime);
// Input tests
if (Input.IsKeyPressed(Keys.A))
Console.WriteLine("A was pressed");
if (Input.IsMouseButtonPressed(MouseButton.Left))
Console.WriteLine("Left was pressed");
if (Input.IsGamepadButtonPressed(0, Buttons.A))
Console.WriteLine("Gamepad A was pressed");
this.uiSystem.Update(gameTime);
this.UiSystem.Add("Test", root);
}
protected override void Draw(GameTime gameTime) {
base.Draw(gameTime);
this.GraphicsDevice.Clear(Color.Black);
this.uiSystem.Draw(gameTime, this.SpriteBatch, samplerState: SamplerState.PointClamp);
base.Draw(gameTime);
}
}