Skip to content

Commit

Permalink
Merge pull request #2 from timia2109/feature/render-defaults
Browse files Browse the repository at this point in the history
Feature/render defaults
  • Loading branch information
timia2109 authored Mar 22, 2024
2 parents 064f63b + 22f45a9 commit d9d3e2d
Show file tree
Hide file tree
Showing 26 changed files with 473 additions and 219 deletions.
63 changes: 0 additions & 63 deletions Layouting/BorderElement.cs

This file was deleted.

133 changes: 133 additions & 0 deletions Layouting/ElementCollection.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
using System.ComponentModel;
using DisplayUtil.Serializing.Models;
using SkiaSharp;

namespace DisplayUtil.Layouting;

/// <summary>
/// Baseclass for an ElementCollection.
/// Adds support for Borders, Margins and Paddings
/// </summary>
public abstract class ElementCollection : Element
{
/// <summary>
/// Children of this collection
/// </summary>
public List<Element> Children { get; } = [];

/// <summary>
/// Sizes of Borders
/// </summary>
public SiteSize Border { get; set; } = new();

/// <summary>
/// Sizes of Margin
/// </summary>
public SiteSize Margin { get; set; } = new();

/// <summary>
/// Sizes of Padding
/// </summary>
public SiteSize Padding { get; set; } = new();

private readonly Lazy<ContainerSizes> _containerSizes;

protected ElementCollection()
{
_containerSizes = new(CalculateContainerSizes);
}

/// <summary>
/// Adds a new Element to this Collection
/// </summary>
Expand All @@ -19,6 +47,53 @@ public ElementCollection Append(Element element)
return this;
}

protected override SKSize CalculateSize(DrawContext drawContext)
{
var childContext = BuildChildDrawContext(drawContext);
var size = CalculateCollectionSize(childContext);
size.Width += _containerSizes.Value.X;
size.Height += _containerSizes.Value.Y;

return size;
}

private DrawContext BuildChildDrawContext(DrawContext drawContext)
{
var childSize = drawContext.Size;
childSize.Width -= _containerSizes.Value.X;
childSize.Height -= _containerSizes.Value.Y;

var childStartPoint = drawContext.StartPoint;
childStartPoint.X += _containerSizes.Value.StartX;
childStartPoint.Y += _containerSizes.Value.StartY;

return drawContext with
{
Size = childSize,
StartPoint = childStartPoint
};
}

private ContainerSizes CalculateContainerSizes()
{
SiteSize[] siteSizes = [Border, Margin, Padding];
var startX = siteSizes.Aggregate(0, (s, e) => s + e.Left);
var endX = siteSizes.Aggregate(0, (s, e) => s + e.Right);
var startY = siteSizes.Aggregate(0, (s, e) => s + e.Top);
var endY = siteSizes.Aggregate(0, (s, e) => s + e.Bottom);
return new ContainerSizes(startX, startY, endX, endY);
}

public override void Draw(DrawContext drawContext)
{
var childDrawContext = BuildChildDrawContext(drawContext);
DrawCollection(childDrawContext);
DrawBorder(drawContext);
}

protected abstract void DrawCollection(DrawContext context);
protected abstract SKSize CalculateCollectionSize(DrawContext drawContext);

/// <summary>
/// Calculates the sizes of the children
/// </summary>
Expand Down Expand Up @@ -58,4 +133,62 @@ public override void Dispose()
child.Dispose();
}
}

private void DrawBorder(DrawContext drawContext)
{
var size = GetSize(drawContext);

var topStart = new SKPoint(
drawContext.StartPoint.X + Margin.Left,
drawContext.StartPoint.Y + Margin.Top
);

var bottomEnd = new SKPoint(
drawContext.StartPoint.X + size.Width - Margin.Right,
drawContext.StartPoint.Y + size.Height - Margin.Bottom
);

var topEnd = new SKPoint(
bottomEnd.X,
topStart.Y
);

var bottomStart = new SKPoint(
topStart.X,
bottomEnd.Y
);

DrawLine(drawContext, topStart, topEnd, Border.Top);
DrawLine(drawContext, topStart, bottomStart, Border.Left);
DrawLine(drawContext, bottomStart, bottomEnd, Border.Bottom);
DrawLine(drawContext, topEnd, bottomEnd, Border.Right);
}

private static void DrawLine(DrawContext drawContext, SKPoint startPoint,
SKPoint endPoint, int width)
{
if (width == 0) return;

using var paint = new SKPaint
{
Color = SKColors.Black,
StrokeWidth = width,
Style = SKPaintStyle.Fill
};

drawContext.Canvas.DrawLine(
startPoint, endPoint, paint
);
}

private record ContainerSizes(
int StartX,
int StartY,
int EndX,
int EndY
)
{
public int X => StartX + EndX;
public int Y => StartY + EndY;
}
}
4 changes: 2 additions & 2 deletions Layouting/FlexboxElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public FlexboxElement(int gap = 0, FlexDirection direction = FlexDirection.Horiz
_alignItems = alignItems;
}

public override void Draw(DrawContext drawContext)
protected override void DrawCollection(DrawContext drawContext)
{
var point = drawContext.StartPoint;
var sizes = Children.Select(child => child.GetSize(drawContext)).ToList();
Expand Down Expand Up @@ -76,7 +76,7 @@ public override void Draw(DrawContext drawContext)
}
}

protected override SKSize CalculateSize(DrawContext drawContext)
protected override SKSize CalculateCollectionSize(DrawContext drawContext)
{
var containerSize = _direction == FlexDirection.Horizontal ? drawContext.Size.Width : drawContext.Size.Height;
var lineLength = 0f;
Expand Down
4 changes: 2 additions & 2 deletions Layouting/HBoxElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DisplayUtil.Layouting;
/// <param name="gap">Gap between Items</param>
public class HBoxElement(int gap = 0) : ElementCollection
{
public override void Draw(DrawContext drawContext)
protected override void DrawCollection(DrawContext drawContext)
{
var point = drawContext.StartPoint;
foreach (var child in Children)
Expand All @@ -24,7 +24,7 @@ public override void Draw(DrawContext drawContext)
}
}

protected override SKSize CalculateSize(DrawContext drawContext)
protected override SKSize CalculateCollectionSize(DrawContext drawContext)
{
var childrenSize = GetChildrenSizes(drawContext);
return new SKSize(
Expand Down
39 changes: 0 additions & 39 deletions Layouting/PaddingElement.cs

This file was deleted.

11 changes: 7 additions & 4 deletions Layouting/Padding.cs → Layouting/SiteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ namespace DisplayUtil.Layouting;
/// <param name="Right">Right Padding</param>
/// <param name="Bottom">Bottom Padding</param>
/// <param name="Left">Left Padding</param>
public record struct Padding(
int Top = 0, int Right = 0, int Bottom = 0, int Left = 0
public record struct SiteSize(
int Top = 0,
int Right = 0,
int Bottom = 0,
int Left = 0
)
{
public Padding(int allSizes) : this(allSizes, allSizes, allSizes, allSizes) { }
public SiteSize(int allSizes) : this(allSizes, allSizes, allSizes, allSizes) { }

public Padding(int x, int y) : this(y, x, y, x) { }
public SiteSize(int x, int y) : this(y, x, y, x) { }
}
4 changes: 2 additions & 2 deletions Layouting/VBoxElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DisplayUtil.Layouting;
/// <param name="gap">Gap between Items</param>
public class VBoxElement(int gap = 0) : ElementCollection
{
public override void Draw(DrawContext drawContext)
protected override void DrawCollection(DrawContext drawContext)
{
var point = drawContext.StartPoint;
foreach (var child in Children)
Expand All @@ -24,7 +24,7 @@ public override void Draw(DrawContext drawContext)
}
}

protected override SKSize CalculateSize(DrawContext drawContext)
protected override SKSize CalculateCollectionSize(DrawContext drawContext)
{
var childrenSize = GetChildrenSizes(drawContext);
return new SKSize(
Expand Down
3 changes: 2 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
builder.AddHassSupport();

builder.Services.AddSingleton(FontProvider.Create())
.AddSingleton<XmlLayoutDeserializer>();
.AddSingleton<XmlLayoutDeserializer>()
.AddSingleton<TemplateLoader>();

builder.Services.AddScoped<TemplateRenderer>()
.AddScoped<TemplateContextProvider>();
Expand Down
8 changes: 8 additions & 0 deletions Resources/screens/_icon_data.sbntxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{ func icon_data(sIconName, sText) }}
{{ if (sText | string.strip | string.size ) > 0}}
<HBox Gap="2">
<Icon IconName="{{sIconName}}" />
<Text Content="{{sText}}" />
</HBox>
{{ end }}
{{ end }}
Loading

0 comments on commit d9d3e2d

Please sign in to comment.