mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
added ui system to startup
This commit is contained in:
parent
18e634cad8
commit
28fca1cc24
4 changed files with 31 additions and 36 deletions
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
|
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
|
||||||
|
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
|
||||||
<ProjectReference Include="..\MLEM\MLEM.csproj" />
|
<ProjectReference Include="..\MLEM\MLEM.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -3,6 +3,7 @@ using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
using MLEM.Input;
|
using MLEM.Input;
|
||||||
|
using MLEM.Ui;
|
||||||
using MonoGame.Extended;
|
using MonoGame.Extended;
|
||||||
|
|
||||||
namespace MLEM.Startup {
|
namespace MLEM.Startup {
|
||||||
|
@ -14,6 +15,7 @@ namespace MLEM.Startup {
|
||||||
public readonly GraphicsDeviceManager GraphicsDeviceManager;
|
public readonly GraphicsDeviceManager GraphicsDeviceManager;
|
||||||
public SpriteBatch SpriteBatch { get; protected set; }
|
public SpriteBatch SpriteBatch { get; protected set; }
|
||||||
public InputHandler InputHandler { 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") {
|
public MlemGame(int windowWidth = 1280, int windowHeight = 720, bool vsync = false, bool allowResizing = true, string contentDir = "Content") {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
@ -38,6 +40,7 @@ namespace MLEM.Startup {
|
||||||
protected override void LoadContent() {
|
protected override void LoadContent() {
|
||||||
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
|
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
|
||||||
this.InputHandler = new InputHandler();
|
this.InputHandler = new InputHandler();
|
||||||
|
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, this.InputHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Initialize() {
|
protected override void Initialize() {
|
||||||
|
@ -48,8 +51,8 @@ namespace MLEM.Startup {
|
||||||
protected override void Update(GameTime gameTime) {
|
protected override void Update(GameTime gameTime) {
|
||||||
base.Update(gameTime);
|
base.Update(gameTime);
|
||||||
|
|
||||||
if (this.InputHandler != null)
|
|
||||||
this.InputHandler.Update();
|
this.InputHandler.Update();
|
||||||
|
this.UiSystem.Update(gameTime);
|
||||||
|
|
||||||
CoroutineHandler.Tick(gameTime.GetElapsedSeconds());
|
CoroutineHandler.Tick(gameTime.GetElapsedSeconds());
|
||||||
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
|
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
|
||||||
|
@ -57,6 +60,7 @@ namespace MLEM.Startup {
|
||||||
|
|
||||||
protected override void Draw(GameTime gameTime) {
|
protected override void Draw(GameTime gameTime) {
|
||||||
base.Draw(gameTime);
|
base.Draw(gameTime);
|
||||||
|
this.UiSystem.Draw(gameTime, this.SpriteBatch);
|
||||||
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
|
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,21 +15,30 @@ namespace MLEM.Ui {
|
||||||
private readonly InputHandler inputHandler;
|
private readonly InputHandler inputHandler;
|
||||||
private readonly bool isInputOurs;
|
private readonly bool isInputOurs;
|
||||||
|
|
||||||
public readonly float GlobalScale;
|
private float globalScale;
|
||||||
public readonly IGenericFont DefaultFont;
|
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 {
|
public Rectangle ScaledViewport {
|
||||||
get {
|
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());
|
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 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.graphicsDevice = device;
|
||||||
this.GlobalScale = scale;
|
|
||||||
this.DefaultFont = defaultFont;
|
|
||||||
this.inputHandler = inputHandler ?? new InputHandler();
|
this.inputHandler = inputHandler ?? new InputHandler();
|
||||||
this.isInputOurs = inputHandler == null;
|
this.isInputOurs = inputHandler == null;
|
||||||
|
|
||||||
|
@ -65,19 +74,17 @@ namespace MLEM.Ui {
|
||||||
root.Element.Update(time);
|
root.Element.Update(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(GameTime time, SpriteBatch batch, Color? color = null, BlendState blendState = null, SamplerState samplerState = null) {
|
public void Draw(GameTime time, SpriteBatch batch) {
|
||||||
var col = color ?? Color.White;
|
batch.Begin(SpriteSortMode.Deferred, this.BlendState, this.SamplerState, transformMatrix: Matrix.CreateScale(this.globalScale));
|
||||||
|
|
||||||
batch.Begin(SpriteSortMode.Deferred, blendState, samplerState, transformMatrix: Matrix.CreateScale(this.GlobalScale));
|
|
||||||
foreach (var root in this.rootElements) {
|
foreach (var root in this.rootElements) {
|
||||||
if (!root.Element.IsHidden)
|
if (!root.Element.IsHidden)
|
||||||
root.Element.Draw(time, batch, col);
|
root.Element.Draw(time, batch, this.DrawColor);
|
||||||
}
|
}
|
||||||
batch.End();
|
batch.End();
|
||||||
|
|
||||||
foreach (var root in this.rootElements) {
|
foreach (var root in this.rootElements) {
|
||||||
if (!root.Element.IsHidden)
|
if (!root.Element.IsHidden)
|
||||||
root.Element.DrawUnbound(time, batch, col, blendState, samplerState);
|
root.Element.DrawUnbound(time, batch, this.DrawColor, this.BlendState, this.SamplerState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@ namespace Tests {
|
||||||
|
|
||||||
private Texture2D testTexture;
|
private Texture2D testTexture;
|
||||||
private NinePatch testPatch;
|
private NinePatch testPatch;
|
||||||
private UiSystem uiSystem;
|
|
||||||
|
|
||||||
public GameImpl() {
|
public GameImpl() {
|
||||||
this.IsMouseVisible = true;
|
this.IsMouseVisible = true;
|
||||||
|
@ -27,8 +26,8 @@ namespace Tests {
|
||||||
this.testTexture = LoadContent<Texture2D>("Textures/Test");
|
this.testTexture = LoadContent<Texture2D>("Textures/Test");
|
||||||
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
|
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
|
||||||
|
|
||||||
// Ui system tests
|
this.UiSystem.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
|
||||||
this.uiSystem = new UiSystem(this.Window, this.GraphicsDevice, 5, new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")), this.InputHandler);
|
this.UiSystem.GlobalScale = 5;
|
||||||
|
|
||||||
var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch);
|
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.", 0.2F));
|
||||||
|
@ -39,28 +38,12 @@ namespace Tests {
|
||||||
image.IsHidden = !image.IsHidden;
|
image.IsHidden = !image.IsHidden;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.uiSystem.Add("Test", root);
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Draw(GameTime gameTime) {
|
protected override void Draw(GameTime gameTime) {
|
||||||
base.Draw(gameTime);
|
|
||||||
this.GraphicsDevice.Clear(Color.Black);
|
this.GraphicsDevice.Clear(Color.Black);
|
||||||
|
base.Draw(gameTime);
|
||||||
this.uiSystem.Draw(gameTime, this.SpriteBatch, samplerState: SamplerState.PointClamp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue