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

added percentage sizing based on own data to element

This commit is contained in:
Ellpeck 2020-06-02 23:00:40 +02:00
parent b0309c9707
commit 79e0e1a63e
2 changed files with 27 additions and 6 deletions

View file

@ -87,9 +87,18 @@ namespace MLEM.Ui.Elements {
private Vector2 size;
/// <summary>
/// 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 <see cref="SetWidthBasedOnChildren"/> is true, this property's X value is ignored and overridden. If <see cref="SetHeightBasedOnChildren"/> is true, this property's Y value is ignored and overridden.
/// </summary>
/// <example>
/// The following example combines both types of percentage-based sizing.
/// If this element is inside of a <see cref="Parent"/> whose width is 20, this element's width will be set to <c>0.5 * 20 = 10</c>, and its height will be set to <c>2.5 * 10 = 25</c>.
/// <code>
/// element.Size = new Vector2(0.5F, -2.5F);
/// </code>
/// </example>
public Vector2 Size {
get => this.size;
set {
@ -587,13 +596,19 @@ namespace MLEM.Ui.Elements {
/// <summary>
/// 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 <see cref="Size"/>'s documentation.
/// </summary>
/// <param name="parentArea">This parent's area, or the ui system's viewport if it has no parent</param>
/// <returns>The actual size of this element, taking <see cref="Scale"/> into account</returns>
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;
}
/// <summary>

View file

@ -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<Vector2>("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) {