1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-10 03:28:43 +02:00

Improved the SquishingGroup algorithm by prioritizing each element's final size

This commit is contained in:
Ell 2023-04-06 17:15:57 +02:00
parent 1a7cb65cf2
commit 2265af3fae
3 changed files with 36 additions and 45 deletions

View file

@ -18,17 +18,17 @@ Additions
- Added the ability to add inverse modifiers to a Keybind
- Added GenericInput collections AllKeys, AllMouseButtons, AllButtons and AllInputs
Improvements
- Increased TextFormatter macro recursion limit to 64
- Allow changing the default values used by default TextFormatter codes
- Allow setting ExternalGestureHandling through the InputHandler constructor
Fixes
- Fixed control characters being included in TextInput
- Fixed TextInputs behaving incorrectly when switching between multiline and single-line modes
- Fixed TextInput drawing characters with the wrong width if a masking character is used
- Fixed a multiline TextInput's cursor not returning to the default position when the last character is removed
Improvements
- Increased TextFormatter macro recursion limit to 64
- Allow changing the default values used by default TextFormatter codes
- Allow setting ExternalGestureHandling through the InputHandler constructor
Removals
- Marked GetDownTime, GetUpTime and GetTimeSincePress in Keybind and Combination as obsolete
@ -36,14 +36,15 @@ Removals
Additions
- Added AutoInlineCenter and AutoInlineBottom anchors
Improvements
- Increased Element area calculation recursion limit to 64
- Improved the SquishingGroup algorithm by prioritizing each element's final size
Fixes
- Fixed images not updating their hidden state properly when the displayed texture changes
- Fixed AutoInline elements overflowing into their parent if it's taller
- Fixed Paragraph and Checkbox not reacting to SquishingGroup sizing properly
Improvements
- Increased Element area calculation recursion limit to 64
## 6.1.0
### MLEM

View file

@ -1,13 +1,13 @@
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Misc;
namespace MLEM.Ui.Elements {
/// <summary>
/// A squishing group is a <see cref="Group"/> whose <see cref="Element.Children"/> automatically get resized so that they do not overlap each other.
/// The order in which elements are squished depends on their <see cref="Element.Priority"/>, where elements with a lower priority will move out of the way of elements with a higher priority.
/// If all elements have the same priority, their addition order (their order in <see cref="Element.Children"/>) determines squish order.
/// A squishing group is a <see cref="Group"/> whose <see cref="Element.Children"/> automatically get resized so that they do not overlap each other. Elements are squished in a way that maximizes the final area that each element retains compared to its original area.
/// The order in which elements are squished depends on their <see cref="Element.Priority"/>, where elements with a lower priority will move out of the way of elements with a higher priority. If all elements have the same priority, their addition order (their order in <see cref="Element.Children"/>) determines squish order.
/// </summary>
public class SquishingGroup : Group {
@ -43,27 +43,17 @@ namespace MLEM.Ui.Elements {
var pos = element.Area.Location;
var size = element.Area.Size;
foreach (var sibling in element.GetSiblings(e => !e.IsHidden)) {
var siblingArea = sibling.Area;
var leftIntersect = siblingArea.Right - pos.X;
var rightIntersect = pos.X + size.X - siblingArea.Left;
var bottomIntersect = siblingArea.Bottom - pos.Y;
var topIntersect = pos.Y + size.Y - siblingArea.Top;
if (leftIntersect > 0 && rightIntersect > 0 && bottomIntersect > 0 && topIntersect > 0) {
if (rightIntersect + leftIntersect < topIntersect + bottomIntersect) {
if (rightIntersect > leftIntersect) {
size.X -= siblingArea.Right - pos.X;
pos.X = Math.Max(pos.X, siblingArea.Right);
} else {
size.X = Math.Min(pos.X + size.X, siblingArea.Left) - pos.X;
}
} else {
if (topIntersect > bottomIntersect) {
size.Y -= siblingArea.Bottom - pos.Y;
pos.Y = Math.Max(pos.Y, siblingArea.Bottom);
} else {
size.Y = Math.Min(pos.Y + size.Y, siblingArea.Top) - pos.Y;
}
}
var sibArea = sibling.Area;
if (pos.X < sibArea.Right && sibArea.Left < pos.X + size.X && pos.Y < sibArea.Bottom && sibArea.Top < pos.Y + size.Y) {
var possible = new[] {
new RectangleF(Math.Max(pos.X, sibArea.Right), pos.Y, size.X - (sibArea.Right - pos.X), size.Y),
new RectangleF(pos.X, pos.Y, Math.Min(pos.X + size.X, sibArea.Left) - pos.X, size.Y),
new RectangleF(pos.X, Math.Max(pos.Y, sibArea.Bottom), size.X, size.Y - (sibArea.Bottom - pos.Y)),
new RectangleF(pos.X, pos.Y, size.X, Math.Min(pos.Y + size.Y, sibArea.Top) - pos.Y)
};
var biggest = possible.OrderByDescending(r => r.Width * r.Height).First();
pos = biggest.Location;
size = biggest.Size;
}
}
if (!pos.Equals(element.Area.Location, Element.Epsilon) || !size.Equals(element.Area.Size, Element.Epsilon)) {

View file

@ -229,22 +229,22 @@ public class GameImpl : MlemGame {
par.OnDrawn = (e, time, batch, a) => batch.DrawRectangle(e.DisplayArea.ToExtended(), Color.Red);
this.UiSystem.Add("Load", loadGroup);*/
/*var spillPanel = new Panel(Anchor.Center, new Vector2(100), Vector2.Zero);
var spillPanel = new Panel(Anchor.Center, new Vector2(100), Vector2.Zero);
var squishingGroup = spillPanel.AddChild(new SquishingGroup(Anchor.TopLeft, Vector2.One));
squishingGroup.AddChild(new Button(Anchor.TopLeft, new Vector2(30), "TL") {
OnUpdated = (e, time) => e.IsHidden = Input.IsKeyDown(Keys.D1),
OnUpdated = (e, time) => e.IsHidden = MlemGame.Input.IsDown(Keys.D1),
Priority = 10
}).SetData("Ref", "TL");
squishingGroup.AddChild(new Button(Anchor.TopRight, new Vector2(30), "TR") {
OnUpdated = (e, time) => e.IsHidden = Input.IsKeyDown(Keys.D2),
OnUpdated = (e, time) => e.IsHidden = MlemGame.Input.IsDown(Keys.D2),
Priority = 20
}).SetData("Ref", "TR");
squishingGroup.AddChild(new Button(Anchor.BottomLeft, new Vector2(30), "BL") {
OnUpdated = (e, time) => e.IsHidden = Input.IsKeyDown(Keys.D3),
squishingGroup.AddChild(new Button(Anchor.BottomLeft, new Vector2(1, 30), "BL") {
OnUpdated = (e, time) => e.IsHidden = MlemGame.Input.IsDown(Keys.D3),
Priority = 30
}).SetData("Ref", "BL");
squishingGroup.AddChild(new Button(Anchor.BottomRight, new Vector2(30), "BR") {
OnUpdated = (e, time) => e.IsHidden = Input.IsKeyDown(Keys.D4),
OnUpdated = (e, time) => e.IsHidden = MlemGame.Input.IsDown(Keys.D4),
Priority = 40
}).SetData("Ref", "BR");
squishingGroup.AddChild(new Button(Anchor.Center, Vector2.Zero, "0") {
@ -256,7 +256,7 @@ public class GameImpl : MlemGame {
e.SetAreaDirty();
}
}).SetData("Ref", "Main");
this.UiSystem.Add("SpillTest", spillPanel);*/
this.UiSystem.Add("SpillTest", spillPanel);
/* var regularFont = spriteFont.Font;
var genericFont = spriteFont;
@ -343,7 +343,7 @@ public class GameImpl : MlemGame {
this.SpriteBatch.End();
};*/
var widthPanel = new Panel(Anchor.Center, Vector2.One, Vector2.Zero, true) {SetWidthBasedOnChildren = true};
/*var widthPanel = new Panel(Anchor.Center, Vector2.One, Vector2.Zero, true) {SetWidthBasedOnChildren = true};
for (var i = 0; i < 5; i++) {
widthPanel.AddChild(new Paragraph(Anchor.AutoCenter, 100000, "Test String " + Math.Pow(10, i), true) {
OnUpdated = (e, _) => {
@ -383,12 +383,12 @@ public class GameImpl : MlemGame {
}
}
};
this.OnDraw += (_, _) => batch.Draw(null, SamplerState.PointClamp, null, null, null, Matrix.CreateScale(3));
this.OnDraw += (_, _) => batch.Draw(null, SamplerState.PointClamp, null, null, null, Matrix.CreateScale(3));*/
Console.WriteLine("Keys: " + string.Join(", ", GenericInput.AllKeys));
/*Console.WriteLine("Keys: " + string.Join(", ", GenericInput.AllKeys));
Console.WriteLine("MouseButtons: " + string.Join(", ", GenericInput.AllMouseButtons));
Console.WriteLine("Buttons: " + string.Join(", ", GenericInput.AllButtons));
Console.WriteLine("Inputs: " + string.Join(", ", GenericInput.AllInputs));
Console.WriteLine("Inputs: " + string.Join(", ", GenericInput.AllInputs));*/
}
protected override void DoUpdate(GameTime gameTime) {
@ -410,8 +410,8 @@ public class GameImpl : MlemGame {
Console.WriteLine("Time since press: " + MlemGame.Input.GetTimeSincePress(Keys.A));#1#
Console.WriteLine("Up time: " + MlemGame.Input.GetUpTime(Keys.A));*/
var combination = new Keybind.Combination(Keys.K, new GenericInput[] {Keys.LeftShift}, GenericInput.AllKeys);
Console.WriteLine($"Mod: {combination.IsModifierDown(this.InputHandler)} Down: {combination.IsDown(this.InputHandler)}");
/*var combination = new Keybind.Combination(Keys.K, new GenericInput[] {Keys.LeftShift}, GenericInput.AllKeys);
Console.WriteLine($"Mod: {combination.IsModifierDown(this.InputHandler)} Down: {combination.IsDown(this.InputHandler)}");*/
}
protected override void DoDraw(GameTime gameTime) {