Skip to content

Commit

Permalink
fix: Correct unintended drawing results
Browse files Browse the repository at this point in the history
Correct unintended drawing results when using DrawableDecorator if Drawable.Bounds and Node.Bounds do not match
  • Loading branch information
yuto-trd committed Sep 28, 2024
1 parent 8ee65ee commit e29667e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/Beutl.Engine/Graphics/DrawableDecorator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel;
using Beutl.Graphics.Effects;
using Beutl.Media;
using Beutl.Media.Immutable;

namespace Beutl.Graphics;

Expand Down Expand Up @@ -95,7 +96,16 @@ protected override void OnDraw(ICanvas canvas)
private Matrix GetTransformMatrix(Rect coreBounds)
{
Vector origin = TransformOrigin.ToPixels(coreBounds.Size);
Matrix offset = Matrix.CreateTranslation(origin) * Matrix.CreateTranslation(coreBounds.Position);
Matrix offset = Matrix.CreateTranslation(origin);
if (Child?.AlignmentX != AlignmentX.Left)
{
offset *= Matrix.CreateTranslation(coreBounds.Left, 0);
}

if (Child?.AlignmentY != AlignmentY.Top)
{
offset *= Matrix.CreateTranslation(0, coreBounds.Top);
}

if (Transform is { IsEnabled: true })
{
Expand Down
12 changes: 1 addition & 11 deletions src/Beutl.Engine/Graphics/Rendering/DrawableNode.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
using Beutl.Media;
using Beutl.Media.Immutable;

namespace Beutl.Graphics.Rendering;
namespace Beutl.Graphics.Rendering;

public class DrawableNode(Drawable drawable) : ContainerNode
{
public Drawable Drawable { get; private set; } = drawable;

public override void Render(ImmediateCanvas canvas)
{
base.Render(canvas);
//Rect bounds = Bounds.Inflate(5);
//canvas.DrawRectangle(bounds, null, new ImmutablePen(Brushes.White, null, 0, 5));
}

protected override void OnDispose(bool disposing)
{
base.OnDispose(disposing);
Expand Down
42 changes: 41 additions & 1 deletion src/Beutl.Engine/Graphics/Shapes/Shape.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.DataAnnotations;

using Beutl.Animation;
using Beutl.Graphics.Rendering;
using Beutl.Language;
Expand Down Expand Up @@ -196,6 +195,20 @@ internal static Vector CalculateScale(Size requestedSize, Rect shapeBounds, Stre
return new Vector(sx, sy);
}

public override void Measure(Size availableSize)
{
Size size = MeasureCore(availableSize);
var rect = new Rect(size).Translate(CreatedGeometry?.Bounds.Position ?? default);
Matrix transform = GetTransformMatrix(availableSize, size);

if (FilterEffect != null)
{
rect = FilterEffect.TransformBounds(rect);
}

Bounds = rect.TransformToAABB(transform);
}

protected override Size MeasureCore(Size availableSize)
{
Geometry? geometry = GetOrCreateGeometry();
Expand Down Expand Up @@ -248,6 +261,33 @@ protected override void OnDraw(ICanvas canvas)
}
}

public override void Render(ICanvas canvas)
{
if (IsVisible)
{
Size availableSize = canvas.Size.ToSize(1);
Size size = MeasureCore(availableSize);
var rect = new Rect(size).Translate(CreatedGeometry?.Bounds.Position ?? default);
if (FilterEffect != null)
{
rect = FilterEffect.TransformBounds(rect);
}

Matrix transform = GetTransformMatrix(availableSize, size);
Rect transformedBounds = rect.TransformToAABB(transform);
using (canvas.PushBlendMode(BlendMode))
using (canvas.PushTransform(transform))
using (canvas.PushOpacity(Opacity / 100f))
using (FilterEffect == null ? new() : canvas.PushFilterEffect(FilterEffect))
using (OpacityMask == null ? new() : canvas.PushOpacityMask(OpacityMask, new Rect(size)))
{
OnDraw(canvas);
}

Bounds = transformedBounds;
}
}

public override void ApplyAnimations(IClock clock)
{
base.ApplyAnimations(clock);
Expand Down

0 comments on commit e29667e

Please sign in to comment.