// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.VisualTree;
namespace Avalonia.Rendering.SceneGraph
{
///
/// A node in the scene graph which represents a rectangle draw.
///
internal class RectangleNode : BrushDrawOperation
{
///
/// Initializes a new instance of the class.
///
/// The transform.
/// The fill brush.
/// The stroke pen.
/// The rectanle to draw.
/// The rectangle corner radius.
/// Child scenes for drawing visual brushes.
public RectangleNode(
Matrix transform,
IBrush brush,
Pen pen,
Rect rect,
float cornerRadius,
IDictionary childScenes = null)
: base(rect, transform, pen)
{
Transform = transform;
Brush = brush?.ToImmutable();
Pen = pen?.ToImmutable();
Rect = rect;
CornerRadius = cornerRadius;
ChildScenes = childScenes;
}
///
/// Gets the transform with which the node will be drawn.
///
public Matrix Transform { get; }
///
/// Gets the fill brush.
///
public IBrush Brush { get; }
///
/// Gets the stroke pen.
///
public Pen Pen { get; }
///
/// Gets the rectangle to draw.
///
public Rect Rect { get; }
///
/// Gets the rectangle corner radius.
///
public float CornerRadius { get; }
///
public override IDictionary ChildScenes { get; }
///
/// Determines if this draw operation equals another.
///
/// The transform of the other draw operation.
/// The fill of the other draw operation.
/// The stroke of the other draw operation.
/// The rectangle of the other draw operation.
/// The rectangle corner radius of the other draw operation.
/// True if the draw operations are the same, otherwise false.
///
/// The properties of the other draw operation are passed in as arguments to prevent
/// allocation of a not-yet-constructed draw operation object.
///
public bool Equals(Matrix transform, IBrush brush, Pen pen, Rect rect, float cornerRadius)
{
return transform == Transform &&
Equals(brush, Brush) &&
pen == Pen &&
rect == Rect &&
cornerRadius == CornerRadius;
}
///
public override void Render(IDrawingContextImpl context)
{
context.Transform = Transform;
if (Brush != null)
{
context.FillRectangle(Brush, Rect, CornerRadius);
}
if (Pen != null)
{
context.DrawRectangle(Pen, Rect, CornerRadius);
}
}
// TODO: This doesn't respect CornerRadius yet.
///
public override bool HitTest(Point p) => Bounds.Contains(p);
}
}