♻️ Refactor draw node on container craftsman,

This commit is contained in:
Kolosov Alexandr 2024-03-09 13:25:37 +05:00
parent 3b56c7207b
commit f32c34ff9e
3 changed files with 79 additions and 69 deletions

View File

@ -1,32 +1,55 @@
using TUI.Engine.Nodes.Attributes;
using TUI.Engine.Nodes.Attributes.Alignments;
using TUI.Engine.Nodes.Attributes.Orientations;
using TUI.Engine.Nodes.Attributes.Resizing;
using TUI.Engine.Nodes.Components;
namespace TUI.Engine.Nodes.Containers;
public static class ContainerExtension
{
// public static Position GetNextNodePosition(this INode node,
// Orientation orientation,
// Size allowableSize,
// Position currentChildrenPosition)
// {
// var nodeSize = node.GetSize(allowableSize);
//
// return orientation switch
// {
// Orientation.Horizontal => currentChildrenPosition with
// {
// Left = currentChildrenPosition.Left + nodeSize.Width
// },
// Orientation.Vertical => currentChildrenPosition with
// {
// Top = currentChildrenPosition.Top + nodeSize.Height
// },
// _ => throw new ArgumentOutOfRangeException()
// };
// }
public static Size GetSize(this IContainer container, Resizing resizing)
{
int width = resizing switch
{
// Resizing.Fixed => _fixedWeight,
// Resizing.Hug => GetAllowableSize().Width,
// Resizing.Adaptive => GetAllowableSize().Width,
_ => 0
};
int height = resizing switch
{
// Resizing.Fixed => _fixedHeight,
// Resizing.Hug => GetAllowableSize().Height,
// Resizing.Adaptive => GetAllowableSize().Height,
_ => 0
};
return new Size(width, height);
}
public static Position GetNextNodePosition(this IContainer container, Orientation orientation, Size allowableSize,
Position containerPosition)
{
var nodeSize = container.GetSize(allowableSize);
return GetNextNodePosition(orientation, nodeSize, containerPosition);
}
private static Position GetNextNodePosition(Orientation orientation, Size nodeSize, Position nodePosition) =>
orientation switch
{
Orientation.Horizontal => nodePosition with
{
Left = nodePosition.Left + nodeSize.Width
},
Orientation.Vertical => nodePosition with
{
Top = nodePosition.Top + nodeSize.Height
},
_ => throw new ArgumentOutOfRangeException()
};
public static Size GetSize(this IContainer container, Size allowableSize)
{

View File

@ -19,14 +19,13 @@ public sealed class ContainerCraftsman : CraftsmanBase, IDrawable<IContainer>
public Size Draw(IContainer container, Position sketchPosition, Size allowableSize)
{
var sketchSize = container.GetSize(allowableSize);
var controlNumber = 0;
var nodes = container.GetNodes();
while (controlNumber < container.GetNodes().Count)
while (controlNumber < nodes.Count)
{
var node = container.GetNodes()[controlNumber];
sketchPosition = RenderNode(node, container.Orientation, sketchSize, sketchPosition);
var node = nodes[controlNumber];
sketchPosition = DrawNode(node, container.Orientation, sketchPosition, sketchSize);
controlNumber++;
}
@ -34,55 +33,37 @@ public sealed class ContainerCraftsman : CraftsmanBase, IDrawable<IContainer>
return sketchSize;
}
private Position RenderNode(INode node, Orientation orientation, Size defaultSize, Position position)
private Position DrawNode(INode node, Orientation orientation, Position sketchPosition, Size sketchSize)
{
switch (node)
{
case IContainer container when orientation == Orientation.Horizontal:
Draw(container, position, defaultSize);
return position with
{
Left = position.Left + defaultSize.Width
};
case IContainer container when orientation == Orientation.Vertical:
Draw(container, position, defaultSize);
return position with
{
Top = position.Top + defaultSize.Height
};
case IComponent component when orientation == Orientation.Horizontal:
var componentWidth = _componentCraftsman.Draw(component, position, defaultSize).Width;
return position with
{
Left = position.Left + (defaultSize.Width <= componentWidth ? componentWidth : defaultSize.Width)
};
case IComponent component when orientation == Orientation.Vertical:
var componentHeight = _componentCraftsman.Draw(component, position, defaultSize).Height;
return position with
{
Top = position.Top + (defaultSize.Height <= componentHeight ? componentHeight : defaultSize.Height)
};
case IContainer childContainer:
Draw(childContainer, sketchPosition, sketchSize);
return GetNextNodePosition(orientation, sketchSize, sketchPosition);
case IComponent childComponent:
var componentSize = _componentCraftsman.Draw(childComponent, sketchPosition, sketchSize);
return GetNextNodePosition(orientation, sketchSize, sketchPosition, componentSize);
default:
throw new InvalidCastException();
}
}
}
// private int GetWidth() =>
// ResizingHorizontal switch
// {
// Resizing.Fixed => _fixedWidth,
// Resizing.Hug => GetAllowableSize().Width,
// Resizing.Adaptive => GetAllowableSize().Width,
// _ => 0
// };
//
// private int GetHeight() =>
// ResizingVertical switch
// {
// Resizing.Fixed => _fixedHeight,
// Resizing.Hug => GetAllowableSize().Height,
// Resizing.Adaptive => GetAllowableSize().Height,
// _ => 0
// };
private static Position GetNextNodePosition(
Orientation orientation,
Size defaultSize,
Position position,
Size? componentSize = null)
{
switch (orientation)
{
case Orientation.Horizontal:
var componentWidth = componentSize?.Width.Max(defaultSize.Width) ?? defaultSize.Width;
return position with { Left = position.Left + componentWidth };
case Orientation.Vertical:
var componentHeight = componentSize?.Height.Max(defaultSize.Height) ?? defaultSize.Height;
return position with { Top = position.Top + componentHeight };
default:
throw new InvalidCastException();
}
}
}

View File

@ -0,0 +1,6 @@
namespace TUI.Engine.Rendering;
public static class IntegerExtension
{
public static int Max(this int value, int maxValue) => maxValue <= value ? value : maxValue;
}