mirror of
https://github.com/dnwSilver/tld.git
synced 2024-11-25 16:42:07 +00:00
♻️ Refactor draw node on container craftsman,
This commit is contained in:
parent
3b56c7207b
commit
f32c34ff9e
@ -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)
|
||||||
{
|
{
|
||||||
|
@ -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:
|
||||||
|
var componentSize = _componentCraftsman.Draw(childComponent, sketchPosition, sketchSize);
|
||||||
|
return GetNextNodePosition(orientation, sketchSize, sketchPosition, componentSize);
|
||||||
|
default:
|
||||||
|
throw new InvalidCastException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Position GetNextNodePosition(
|
||||||
|
Orientation orientation,
|
||||||
|
Size defaultSize,
|
||||||
|
Position position,
|
||||||
|
Size? componentSize = null)
|
||||||
{
|
{
|
||||||
Left = position.Left + defaultSize.Width
|
switch (orientation)
|
||||||
};
|
|
||||||
case IContainer container when orientation == Orientation.Vertical:
|
|
||||||
Draw(container, position, defaultSize);
|
|
||||||
return position with
|
|
||||||
{
|
{
|
||||||
Top = position.Top + defaultSize.Height
|
case Orientation.Horizontal:
|
||||||
};
|
var componentWidth = componentSize?.Width.Max(defaultSize.Width) ?? defaultSize.Width;
|
||||||
case IComponent component when orientation == Orientation.Horizontal:
|
return position with { Left = position.Left + componentWidth };
|
||||||
var componentWidth = _componentCraftsman.Draw(component, position, defaultSize).Width;
|
case Orientation.Vertical:
|
||||||
return position with
|
var componentHeight = componentSize?.Height.Max(defaultSize.Height) ?? defaultSize.Height;
|
||||||
{
|
return position with { Top = position.Top + componentHeight };
|
||||||
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)
|
|
||||||
};
|
|
||||||
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
|
|
||||||
// };
|
|
||||||
|
6
src/TUI/Engine/Rendering/IntegerExtension.cs
Normal file
6
src/TUI/Engine/Rendering/IntegerExtension.cs
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user