From 79e0e1a63ed955f643f4c03ade284fa08dd0fe39 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 2 Jun 2020 23:00:40 +0200 Subject: [PATCH] added percentage sizing based on own data to element --- MLEM.Ui/Elements/Element.cs | 21 ++++++++++++++++++--- Sandbox/GameImpl.cs | 12 +++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 10a7cc1..6da76a7 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -87,9 +87,18 @@ namespace MLEM.Ui.Elements { private Vector2 size; /// - /// The size of this element. - /// If the x or y value of the size is lower than or equal to 1, the size will be seen as a percentage of its parent's size. + /// The size of this element, where X represents the width and Y represents the height. + /// If the x or y value of the size is between 0 and 1, the size will be seen as a percentage of its parent's size rather than as an absolute value. + /// If the x (or y) value of the size is negative, the width (or height) is seen as a percentage of the element's resulting height (or width). + /// If is true, this property's X value is ignored and overridden. If is true, this property's Y value is ignored and overridden. /// + /// + /// The following example combines both types of percentage-based sizing. + /// If this element is inside of a whose width is 20, this element's width will be set to 0.5 * 20 = 10, and its height will be set to 2.5 * 10 = 25. + /// + /// element.Size = new Vector2(0.5F, -2.5F); + /// + /// public Vector2 Size { get => this.size; set { @@ -587,13 +596,19 @@ namespace MLEM.Ui.Elements { /// /// Calculates the actual size that this element should take up, based on the area that its parent encompasses. + /// By default, this is based on the information specified in 's documentation. /// /// This parent's area, or the ui system's viewport if it has no parent /// The actual size of this element, taking into account protected virtual Vector2 CalcActualSize(RectangleF parentArea) { - return new Vector2( + var ret = new Vector2( this.size.X > 1 ? this.ScaledSize.X : parentArea.Width * this.size.X, this.size.Y > 1 ? this.ScaledSize.Y : parentArea.Height * this.size.Y); + if (this.size.X < 0) + ret.X = -this.size.X * ret.Y; + if (this.size.Y < 0) + ret.Y = -this.size.Y * ret.X; + return ret; } /// diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 375d009..f3854a0 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -82,7 +82,7 @@ namespace Sandbox { var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true}; panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10))); panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10))); - this.UiSystem.Add("Panel", panel); + //this.UiSystem.Add("Panel", panel); panel.SetData("TestKey", new Vector2(10, 2)); //Console.WriteLine(panel.GetData("TestKey")); @@ -131,14 +131,14 @@ namespace Sandbox { this.tokenized = formatter.Tokenize(font, strg); this.tokenized.Split(font, 400, 5); - this.OnDraw += (g, time) => { + /*this.OnDraw += (g, time) => { this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp); this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green); font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, 5, SpriteEffects.None, 0); this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 5, 0); this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3); this.SpriteBatch.End(); - }; + };*/ this.OnUpdate += (g, time) => { if (this.InputHandler.IsPressed(Keys.W)) { this.tokenized = formatter.Tokenize(font, strg); @@ -146,6 +146,12 @@ namespace Sandbox { } this.tokenized.Update(time); }; + + var testPanel = new Panel(Anchor.Center, new Vector2(0.5F, 100), Vector2.Zero); + testPanel.AddChild(new Button(Anchor.AutoLeft, new Vector2(0.25F, -1))); + testPanel.AddChild(new Button(Anchor.AutoInline, new Vector2(0.25F, -1.5F))); + testPanel.AddChild(new Button(Anchor.AutoInline, new Vector2(0.5F, -1))); + this.UiSystem.Add("Test", testPanel); } protected override void DoUpdate(GameTime gameTime) {