From cb7abbbbefeff19668a0e8d23b8df46409cf198e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 2 Nov 2019 14:21:42 +0100 Subject: [PATCH] fixed some panel issues --- MLEM.Ui/Elements/Panel.cs | 5 ++-- Sandbox/GameImpl.cs | 54 +++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 14de445..34205c2 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -82,7 +82,8 @@ namespace MLEM.Ui.Elements { if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) { if (this.renderTarget != null) this.renderTarget.Dispose(); - this.renderTarget = new RenderTarget2D(this.System.GraphicsDevice, targetArea.Width, targetArea.Height); + var empty = targetArea.Width <= 0 || targetArea.Height <= 0; + this.renderTarget = empty ? null : new RenderTarget2D(this.System.GraphicsDevice, targetArea.Width, targetArea.Height); } } } @@ -97,6 +98,7 @@ namespace MLEM.Ui.Elements { } public override void Update(GameTime time) { + base.Update(time); if (this.relevantChildrenDirty) { this.relevantChildrenDirty = false; @@ -115,7 +117,6 @@ namespace MLEM.Ui.Elements { } } } - base.Update(time); } protected override List GetRelevantChildren() { diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 9916e04..a50e016 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; +using Coroutine; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; using MLEM.Cameras; using MLEM.Extended.Extensions; using MLEM.Extended.Tiled; @@ -16,11 +19,6 @@ using MonoGame.Extended.Tiled; namespace Sandbox { public class GameImpl : MlemGame { - private Camera camera; - private TiledMap map; - private IndividualTiledMapRenderer mapRenderer; - private ProgressBar progress; - public GameImpl() { this.IsMouseVisible = true; } @@ -28,15 +26,6 @@ namespace Sandbox { protected override void LoadContent() { base.LoadContent(); - this.map = LoadContent("Tiled/Map"); - this.mapRenderer = new IndividualTiledMapRenderer(this.map); - - this.camera = new Camera(this.GraphicsDevice) { - AutoScaleWithScreen = true, - Scale = 2, - LookingPosition = new Vector2(25, 25) * this.map.GetTileSize() - }; - var tex = LoadContent("Textures/Test"); this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) { Font = new GenericSpriteFont(LoadContent("Fonts/TestFont")), @@ -48,27 +37,36 @@ namespace Sandbox { this.UiSystem.AutoScaleWithScreen = true; this.UiSystem.GlobalScale = 5; - var root = new Panel(Anchor.Center, new Vector2(50, 50), Vector2.Zero); - this.UiSystem.Add("Root", root); - var group = root.AddChild(new CustomDrawGroup(Anchor.AutoLeft, new Vector2(1, 10))); - group.AddChild(new Button(Anchor.AutoLeft, Vector2.One, "Test text")); + var screen = new Panel(Anchor.Center, new Vector2(200, 100), Vector2.Zero, false, true, new Point(5, 10)); + screen.IsHidden = false; + screen.OnUpdated += (element, time) => { + if (this.InputHandler.IsKeyPressed(Keys.Escape)) + CoroutineHandler.Start(PlayAnimation(screen)); + }; + this.UiSystem.Add("Screen", screen); - this.progress = new ProgressBar(Anchor.Center, new Vector2(0.8F, 0.5F), Direction2.Down, 1); - this.progress.Texture.Set(new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8)); - this.progress.ProgressTexture.Set(new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4)); - this.UiSystem.Add("Progress", this.progress); + for (var i = 0; i < 100; i++) { + screen.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), i.ToString())); + } } - protected override void Update(GameTime gameTime) { - base.Update(gameTime); - this.progress.CurrentValue = (float) (Math.Sin(gameTime.TotalGameTime.TotalSeconds / 2) + 1) / 2; + private static IEnumerator PlayAnimation(Panel screen) { + var show = screen.IsHidden; + screen.IsHidden = false; + var time = 0; + const float total = 200; + while (time <= total) { + yield return new WaitEvent(CoroutineEvents.Update); + var percent = show ? time / total : 1 - time / total; + screen.Root.Scale = percent; + time++; + } + if (!show) + screen.IsHidden = true; } protected override void DoDraw(GameTime gameTime) { this.GraphicsDevice.Clear(Color.Black); - this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, this.camera.ViewMatrix); - this.mapRenderer.Draw(this.SpriteBatch, this.camera.GetVisibleRectangle()); - this.SpriteBatch.End(); base.DoDraw(gameTime); }