♻️ 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;
using TUI.Engine.Nodes.Attributes.Alignments; using TUI.Engine.Nodes.Attributes.Alignments;
using TUI.Engine.Nodes.Attributes.Orientations; using TUI.Engine.Nodes.Attributes.Orientations;
using TUI.Engine.Nodes.Attributes.Resizing;
using TUI.Engine.Nodes.Components; using TUI.Engine.Nodes.Components;
namespace TUI.Engine.Nodes.Containers; namespace TUI.Engine.Nodes.Containers;
public static class ContainerExtension public static class ContainerExtension
{ {
// public static Position GetNextNodePosition(this INode node, public static Size GetSize(this IContainer container, Resizing resizing)
// Orientation orientation, {
// Size allowableSize, int width = resizing switch
// Position currentChildrenPosition) {
// { // Resizing.Fixed => _fixedWeight,
// var nodeSize = node.GetSize(allowableSize); // Resizing.Hug => GetAllowableSize().Width,
// // Resizing.Adaptive => GetAllowableSize().Width,
// return orientation switch _ => 0
// { };
// Orientation.Horizontal => currentChildrenPosition with
// { int height = resizing switch
// Left = currentChildrenPosition.Left + nodeSize.Width {
// }, // Resizing.Fixed => _fixedHeight,
// Orientation.Vertical => currentChildrenPosition with // Resizing.Hug => GetAllowableSize().Height,
// { // Resizing.Adaptive => GetAllowableSize().Height,
// Top = currentChildrenPosition.Top + nodeSize.Height _ => 0
// }, };
// _ => throw new ArgumentOutOfRangeException()
// }; 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) 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) public Size Draw(IContainer container, Position sketchPosition, Size allowableSize)
{ {
var sketchSize = container.GetSize(allowableSize); var sketchSize = container.GetSize(allowableSize);
var controlNumber = 0; var controlNumber = 0;
var nodes = container.GetNodes();
while (controlNumber < container.GetNodes().Count) while (controlNumber < nodes.Count)
{ {
var node = container.GetNodes()[controlNumber]; var node = nodes[controlNumber];
sketchPosition = DrawNode(node, container.Orientation, sketchPosition, sketchSize);
sketchPosition = RenderNode(node, container.Orientation, sketchSize, sketchPosition);
controlNumber++; controlNumber++;
} }
@ -34,55 +33,37 @@ public sealed class ContainerCraftsman : CraftsmanBase, IDrawable<IContainer>
return sketchSize; 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) switch (node)
{ {
case IContainer container when orientation == Orientation.Horizontal: case IContainer childContainer:
Draw(container, position, defaultSize); Draw(childContainer, sketchPosition, sketchSize);
return position with return GetNextNodePosition(orientation, sketchSize, sketchPosition);
{ case IComponent childComponent:
Left = position.Left + defaultSize.Width var componentSize = _componentCraftsman.Draw(childComponent, sketchPosition, sketchSize);
}; return GetNextNodePosition(orientation, sketchSize, sketchPosition, componentSize);
case IContainer container when orientation == Orientation.Vertical: default:
Draw(container, position, defaultSize); throw new InvalidCastException();
return position with }
{ }
Top = position.Top + defaultSize.Height
}; private static Position GetNextNodePosition(
case IComponent component when orientation == Orientation.Horizontal: Orientation orientation,
var componentWidth = _componentCraftsman.Draw(component, position, defaultSize).Width; Size defaultSize,
return position with Position position,
{ Size? componentSize = null)
Left = position.Left + (defaultSize.Width <= componentWidth ? componentWidth : defaultSize.Width) {
}; switch (orientation)
case IComponent component when orientation == Orientation.Vertical: {
var componentHeight = _componentCraftsman.Draw(component, position, defaultSize).Height; case Orientation.Horizontal:
return position with var componentWidth = componentSize?.Width.Max(defaultSize.Width) ?? defaultSize.Width;
{ return position with { Left = position.Left + componentWidth };
Top = position.Top + (defaultSize.Height <= componentHeight ? componentHeight : defaultSize.Height) case Orientation.Vertical:
}; var componentHeight = componentSize?.Height.Max(defaultSize.Height) ?? defaultSize.Height;
return position with { Top = position.Top + componentHeight };
default: default:
throw new InvalidCastException(); 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
// };

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;
}