1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Added AutoInlineCenter and AutoInlineBottom anchors

This commit is contained in:
Ell 2023-03-29 20:51:34 +02:00
parent bef670c09b
commit 94a54c336e
4 changed files with 74 additions and 37 deletions

View file

@ -33,6 +33,9 @@ Removals
- Marked GetDownTime, GetUpTime and GetTimeSincePress in Keybind and Combination as obsolete - Marked GetDownTime, GetUpTime and GetTimeSincePress in Keybind and Combination as obsolete
### MLEM.Ui ### MLEM.Ui
Additions
- Added `AutoInlineCenter` and `AutoInlineBottom` anchors
Fixes Fixes
- Fixed images not updating their hidden state properly when the displayed texture changes - Fixed images not updating their hidden state properly when the displayed texture changes

View file

@ -60,7 +60,7 @@ namespace MLEM.Ui {
AutoRight, AutoRight,
/// <summary> /// <summary>
/// This is an auto-anchoring value. /// 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.
/// </summary> /// </summary>
AutoInline, AutoInline,
/// <summary> /// <summary>
@ -68,7 +68,29 @@ namespace MLEM.Ui {
/// This anchor is an overflow-ignoring version of <see cref="AutoInline"/>, meaning that the element will never be forced into the next line. /// This anchor is an overflow-ignoring version of <see cref="AutoInline"/>, 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. /// Note that, when using this property, it is very easy to cause an element to overflow out of its parent container.
/// </summary> /// </summary>
AutoInlineIgnoreOverflow AutoInlineIgnoreOverflow,
/// <summary>
/// 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.
/// </summary>
AutoInlineCenter,
/// <summary>
/// This is an auto-anchoring value.
/// This anchor is an overflow-ignoring version of <see cref="AutoInlineCenter"/>, 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.
/// </summary>
AutoInlineCenterIgnoreOverflow,
/// <summary>
/// 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.
/// </summary>
AutoInlineBottom,
/// <summary>
/// This is an auto-anchoring value.
/// This anchor is an overflow-ignoring version of <see cref="AutoInlineBottom"/>, 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.
/// </summary>
AutoInlineBottomIgnoreOverflow
} }
} }

View file

@ -672,8 +672,6 @@ namespace MLEM.Ui.Elements {
switch (this.anchor) { switch (this.anchor) {
case Anchor.TopLeft: case Anchor.TopLeft:
case Anchor.AutoLeft: case Anchor.AutoLeft:
case Anchor.AutoInline:
case Anchor.AutoInlineIgnoreOverflow:
pos.X = parentArea.X + this.ScaledOffset.X; pos.X = parentArea.X + this.ScaledOffset.X;
pos.Y = parentArea.Y + this.ScaledOffset.Y; pos.Y = parentArea.Y + this.ScaledOffset.Y;
break; break;
@ -714,36 +712,32 @@ namespace MLEM.Ui.Elements {
} }
if (this.Anchor.IsAuto()) { if (this.Anchor.IsAuto()) {
Element previousChild; if (this.Anchor.IsInline()) {
if (this.Anchor == Anchor.AutoInline || this.Anchor == Anchor.AutoInlineIgnoreOverflow) { var anchorEl = this.GetOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach);
previousChild = this.GetOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach); if (anchorEl != null) {
} else { var anchorElArea = anchorEl.GetAreaForAutoAnchors();
previousChild = this.GetLowestOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach); var newX = anchorElArea.Right + this.ScaledOffset.X;
} // with awkward ui scale values, floating point rounding can cause an element that would usually
if (previousChild != null) { // be positioned correctly to be pushed into the next line due to a very small deviation
var prevArea = previousChild.GetAreaForAutoAnchors(); if (this.Anchor.IsIgnoreOverflow() || newX + newSize.X <= parentArea.Right + Element.Epsilon) {
switch (this.Anchor) { pos.X = newX;
case Anchor.AutoLeft: pos.Y = anchorElArea.Y + this.ScaledOffset.Y;
case Anchor.AutoCenter: if (this.Anchor == Anchor.AutoInlineCenter || this.Anchor == Anchor.AutoInlineCenterIgnoreOverflow) {
case Anchor.AutoRight: pos.Y += (anchorElArea.Height - newSize.Y) / 2;
pos.Y = prevArea.Bottom + this.ScaledOffset.Y; } else if (this.Anchor == Anchor.AutoInlineBottom || this.Anchor == Anchor.AutoInlineBottomIgnoreOverflow) {
break; pos.Y += anchorElArea.Height - newSize.Y;
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;
} }
break; } else {
case Anchor.AutoInlineIgnoreOverflow: // all inline anchors act the same when overflowing into the next line
pos.X = prevArea.Right + this.ScaledOffset.X; pos.X = parentArea.X + this.ScaledOffset.X;
pos.Y = prevArea.Y + this.ScaledOffset.Y; pos.Y = anchorElArea.Bottom + this.ScaledOffset.Y;
break; }
} }
} 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;
} }
} }

View file

@ -234,21 +234,39 @@ namespace MLEM.Ui.Elements {
} }
/// <summary> /// <summary>
/// Returns whether the given <see cref="Anchor"/> is automatic. The anchors <see cref="Anchor.AutoLeft"/>, <see cref="Anchor.AutoCenter"/>, <see cref="Anchor.AutoRight"/>, <see cref="Anchor.AutoInline"/> and <see cref="Anchor.AutoInlineIgnoreOverflow"/> will return true. /// Returns whether the given <see cref="Anchor"/> is automatic. The anchors <see cref="Anchor.AutoLeft"/>, <see cref="Anchor.AutoCenter"/>, <see cref="Anchor.AutoRight"/>, and any anchor that <see cref="IsInline"/> will return true.
/// </summary> /// </summary>
/// <param name="anchor">The anchor to query.</param> /// <param name="anchor">The anchor to query.</param>
/// <returns>Whether the given anchor is automatic.</returns> /// <returns>Whether the given anchor is automatic.</returns>
public static bool IsAuto(this Anchor anchor) { 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();
} }
/// <summary> /// <summary>
/// Returns whether the given <see cref="Anchor"/> is left-aligned for the purpose of <see cref="Element.GetRightmostChild"/>. The anchors <see cref="Anchor.TopLeft"/>, <see cref="Anchor.CenterLeft"/>, <see cref="Anchor.BottomLeft"/>, <see cref="Anchor.AutoLeft"/>, <see cref="Anchor.AutoInline"/> and <see cref="Anchor.AutoInlineIgnoreOverflow"/> will return true. /// Returns whether the given <see cref="Anchor"/> is inline. The anchors <see cref="Anchor.AutoInline"/>, <see cref="Anchor.AutoInlineCenter"/>, <see cref="Anchor.AutoInlineBottom"/>, and any anchor that <see cref="IsIgnoreOverflow"/> will return true.
/// </summary>
/// <param name="anchor">The anchor to query.</param>
/// <returns>Whether the given anchor is inline.</returns>
public static bool IsInline(this Anchor anchor) {
return anchor == Anchor.AutoInline || anchor == Anchor.AutoInlineCenter || anchor == Anchor.AutoInlineBottom || anchor.IsIgnoreOverflow();
}
/// <summary>
/// Returns whether the given <see cref="Anchor"/> ignores overflow. The anchors <see cref="Anchor.AutoInlineIgnoreOverflow"/>, <see cref="Anchor.AutoInlineCenterIgnoreOverflow"/>, and <see cref="Anchor.AutoInlineBottomIgnoreOverflow"/> will return true.
/// </summary>
/// <param name="anchor">The anchor to query.</param>
/// <returns>Whether the given anchor ignores overflow.</returns>
public static bool IsIgnoreOverflow(this Anchor anchor) {
return anchor == Anchor.AutoInlineIgnoreOverflow || anchor == Anchor.AutoInlineCenterIgnoreOverflow || anchor == Anchor.AutoInlineBottomIgnoreOverflow;
}
/// <summary>
/// Returns whether the given <see cref="Anchor"/> is left-aligned for the purpose of <see cref="Element.GetRightmostChild"/>. The anchors <see cref="Anchor.TopLeft"/>, <see cref="Anchor.CenterLeft"/>, <see cref="Anchor.BottomLeft"/>, <see cref="Anchor.AutoLeft"/>, and any anchor that <see cref="IsInline"/> will return true.
/// </summary> /// </summary>
/// <param name="anchor">The anchor to query.</param> /// <param name="anchor">The anchor to query.</param>
/// <returns>Whether the given anchor is left-aligned.</returns> /// <returns>Whether the given anchor is left-aligned.</returns>
public static bool IsLeftAligned(this Anchor anchor) { 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();
} }
/// <summary> /// <summary>