From 94a54c336e5031b55832188ddea2618f1e1e56fc Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 29 Mar 2023 20:51:34 +0200 Subject: [PATCH] Added `AutoInlineCenter` and `AutoInlineBottom` anchors --- CHANGELOG.md | 3 ++ MLEM.Ui/Anchor.cs | 28 ++++++++++++++-- MLEM.Ui/Elements/Element.cs | 54 ++++++++++++++----------------- MLEM.Ui/Elements/ElementHelper.cs | 26 ++++++++++++--- 4 files changed, 74 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ead0e4..4584933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ Removals - Marked GetDownTime, GetUpTime and GetTimeSincePress in Keybind and Combination as obsolete ### MLEM.Ui +Additions +- Added `AutoInlineCenter` and `AutoInlineBottom` anchors + Fixes - Fixed images not updating their hidden state properly when the displayed texture changes diff --git a/MLEM.Ui/Anchor.cs b/MLEM.Ui/Anchor.cs index a984357..cd2d6d0 100644 --- a/MLEM.Ui/Anchor.cs +++ b/MLEM.Ui/Anchor.cs @@ -2,7 +2,7 @@ using MLEM.Ui.Elements; namespace MLEM.Ui { /// - /// Represents a location for an to attach to within its parent (or within the screen's viewport if it is the ). + /// Represents a location for an to attach to within its parent (or within the screen's viewport if it is the ). /// public enum Anchor { @@ -60,7 +60,7 @@ namespace MLEM.Ui { AutoRight, /// /// This is an auto-anchoring value. - /// This anchor will cause an element to be placed in the same line as its older sibling, or at the start of the next line if there is no space to the right of its older sibling. + /// This anchor will cause an element to be placed at the top right of its older sibling, or at the start of the next line if there is no space to the right of its older sibling. /// AutoInline, /// @@ -68,7 +68,29 @@ namespace MLEM.Ui { /// This anchor is an overflow-ignoring version of , meaning that the element will never be forced into the next line. /// Note that, when using this property, it is very easy to cause an element to overflow out of its parent container. /// - AutoInlineIgnoreOverflow + AutoInlineIgnoreOverflow, + /// + /// This is an auto-anchoring value. + /// This anchor will cause an element to be placed at the center right of its older sibling, or at the start of the next line if there is no space to the right of its older sibling. + /// + AutoInlineCenter, + /// + /// This is an auto-anchoring value. + /// This anchor is an overflow-ignoring version of , meaning that the element will never be forced into the next line. + /// Note that, when using this property, it is very easy to cause an element to overflow out of its parent container. + /// + AutoInlineCenterIgnoreOverflow, + /// + /// This is an auto-anchoring value. + /// This anchor will cause an element to be placed at the bottom right of its older sibling, or at the start of the next line if there is no space to the right of its older sibling. + /// + AutoInlineBottom, + /// + /// This is an auto-anchoring value. + /// This anchor is an overflow-ignoring version of , meaning that the element will never be forced into the next line. + /// Note that, when using this property, it is very easy to cause an element to overflow out of its parent container. + /// + AutoInlineBottomIgnoreOverflow } } diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index d72d81d..951464f 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -672,8 +672,6 @@ namespace MLEM.Ui.Elements { switch (this.anchor) { case Anchor.TopLeft: case Anchor.AutoLeft: - case Anchor.AutoInline: - case Anchor.AutoInlineIgnoreOverflow: pos.X = parentArea.X + this.ScaledOffset.X; pos.Y = parentArea.Y + this.ScaledOffset.Y; break; @@ -714,36 +712,32 @@ namespace MLEM.Ui.Elements { } if (this.Anchor.IsAuto()) { - Element previousChild; - if (this.Anchor == Anchor.AutoInline || this.Anchor == Anchor.AutoInlineIgnoreOverflow) { - previousChild = this.GetOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach); - } else { - previousChild = this.GetLowestOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach); - } - if (previousChild != null) { - var prevArea = previousChild.GetAreaForAutoAnchors(); - switch (this.Anchor) { - case Anchor.AutoLeft: - case Anchor.AutoCenter: - case Anchor.AutoRight: - pos.Y = prevArea.Bottom + this.ScaledOffset.Y; - break; - case Anchor.AutoInline: - var newX = prevArea.Right + this.ScaledOffset.X; - // with awkward ui scale values, floating point rounding can cause an element that would usually - // be positioned correctly to be pushed into the next line due to a very small deviation - if (newX + newSize.X <= parentArea.Right + Element.Epsilon) { - pos.X = newX; - pos.Y = prevArea.Y + this.ScaledOffset.Y; - } else { - pos.Y = prevArea.Bottom + this.ScaledOffset.Y; + if (this.Anchor.IsInline()) { + var anchorEl = this.GetOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach); + if (anchorEl != null) { + var anchorElArea = anchorEl.GetAreaForAutoAnchors(); + var newX = anchorElArea.Right + this.ScaledOffset.X; + // with awkward ui scale values, floating point rounding can cause an element that would usually + // be positioned correctly to be pushed into the next line due to a very small deviation + if (this.Anchor.IsIgnoreOverflow() || newX + newSize.X <= parentArea.Right + Element.Epsilon) { + pos.X = newX; + pos.Y = anchorElArea.Y + this.ScaledOffset.Y; + if (this.Anchor == Anchor.AutoInlineCenter || this.Anchor == Anchor.AutoInlineCenterIgnoreOverflow) { + pos.Y += (anchorElArea.Height - newSize.Y) / 2; + } else if (this.Anchor == Anchor.AutoInlineBottom || this.Anchor == Anchor.AutoInlineBottomIgnoreOverflow) { + pos.Y += anchorElArea.Height - newSize.Y; } - break; - case Anchor.AutoInlineIgnoreOverflow: - pos.X = prevArea.Right + this.ScaledOffset.X; - pos.Y = prevArea.Y + this.ScaledOffset.Y; - break; + } else { + // all inline anchors act the same when overflowing into the next line + pos.X = parentArea.X + this.ScaledOffset.X; + pos.Y = anchorElArea.Bottom + this.ScaledOffset.Y; + } } + } else { + // non-inline auto anchors keep their x coordinates from the switch above + var anchorEl = this.GetLowestOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach); + if (anchorEl != null) + pos.Y = anchorEl.GetAreaForAutoAnchors().Bottom + this.ScaledOffset.Y; } } diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 3fd55a3..cd7e0a6 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -234,21 +234,39 @@ namespace MLEM.Ui.Elements { } /// - /// Returns whether the given is automatic. The anchors , , , and will return true. + /// Returns whether the given is automatic. The anchors , , , and any anchor that will return true. /// /// The anchor to query. /// Whether the given anchor is automatic. public static bool IsAuto(this Anchor anchor) { - return anchor == Anchor.AutoLeft || anchor == Anchor.AutoCenter || anchor == Anchor.AutoRight || anchor == Anchor.AutoInline || anchor == Anchor.AutoInlineIgnoreOverflow; + return anchor == Anchor.AutoLeft || anchor == Anchor.AutoCenter || anchor == Anchor.AutoRight || anchor.IsInline(); } /// - /// Returns whether the given is left-aligned for the purpose of . The anchors , , , , and will return true. + /// Returns whether the given is inline. The anchors , , , and any anchor that will return true. + /// + /// The anchor to query. + /// Whether the given anchor is inline. + public static bool IsInline(this Anchor anchor) { + return anchor == Anchor.AutoInline || anchor == Anchor.AutoInlineCenter || anchor == Anchor.AutoInlineBottom || anchor.IsIgnoreOverflow(); + } + + /// + /// Returns whether the given ignores overflow. The anchors , , and will return true. + /// + /// The anchor to query. + /// Whether the given anchor ignores overflow. + public static bool IsIgnoreOverflow(this Anchor anchor) { + return anchor == Anchor.AutoInlineIgnoreOverflow || anchor == Anchor.AutoInlineCenterIgnoreOverflow || anchor == Anchor.AutoInlineBottomIgnoreOverflow; + } + + /// + /// Returns whether the given is left-aligned for the purpose of . The anchors , , , , and any anchor that will return true. /// /// The anchor to query. /// Whether the given anchor is left-aligned. public static bool IsLeftAligned(this Anchor anchor) { - return anchor == Anchor.TopLeft || anchor == Anchor.CenterLeft || anchor == Anchor.BottomLeft || anchor == Anchor.AutoLeft || anchor == Anchor.AutoInline || anchor == Anchor.AutoInlineIgnoreOverflow; + return anchor == Anchor.TopLeft || anchor == Anchor.CenterLeft || anchor == Anchor.BottomLeft || anchor == Anchor.AutoLeft || anchor.IsInline(); } ///