using System.Linq;
using KamiToolKit.Enums;
namespace KamiToolKit.Nodes;
public class VerticalListNode : LayoutListNode {
///
/// Displays items starting from either the bottom or the top of the list
///
public VerticalListAnchor Anchor {
get;
set {
field = value;
RecalculateLayout();
}
}
///
/// Displays items either left aligned or right aligned
///
public VerticalListAlignment Alignment {
get;
set {
field = value;
RecalculateLayout();
}
}
///
/// Resizes this layout node to fit the height of the contained nodes.
///
public bool FitContents { get; set; }
///
/// Resizes nodes that are inserted to be the same width as the content area
///
public bool FitWidth { get; set; }
protected override void OnRecalculateLayout() {
var startY = Anchor switch {
VerticalListAnchor.Top => 0.0f + FirstItemSpacing,
VerticalListAnchor.Bottom => Height,
_ => 0.0f,
};
foreach (var node in NodeList) {
if (!node.IsVisible) continue;
if (Anchor is VerticalListAnchor.Bottom) {
startY -= node.Height + ItemSpacing;
}
node.Y = startY;
if (FitWidth) {
node.Width = Width;
}
else {
switch (Alignment) {
case VerticalListAlignment.Right:
node.X = Width - node.Width;
break;
case VerticalListAlignment.Left:
node.X = 0.0f;
break;
}
}
AdjustNode(node);
if (Anchor is VerticalListAnchor.Top) {
startY += node.Height + ItemSpacing;
}
}
if (FitContents) {
Height = NodeList.Sum(node => node.IsVisible ? node.Height + ItemSpacing : 0.0f) + FirstItemSpacing - ItemSpacing;
}
}
}