// 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 Avalonia.Platform;
namespace Avalonia.Rendering.SceneGraph
{
///
/// A node in the scene graph which represents an image draw.
///
internal class ImageNode : DrawOperation
{
///
/// Initializes a new instance of the class.
///
/// The transform.
/// The image to draw.
/// The draw opacity.
/// The source rect.
/// The destination rect.
public ImageNode(Matrix transform, IBitmapImpl source, double opacity, Rect sourceRect, Rect destRect)
: base(destRect, transform, null)
{
Transform = transform;
Source = source;
Opacity = opacity;
SourceRect = sourceRect;
DestRect = destRect;
}
///
/// Gets the transform with which the node will be drawn.
///
public Matrix Transform { get; }
///
/// Gets the image to draw.
///
public IBitmapImpl Source { get; }
///
/// Gets the draw opacity.
///
public double Opacity { get; }
///
/// Gets the source rect.
///
public Rect SourceRect { get; }
///
/// Gets the destination rect.
///
public Rect DestRect { get; }
///
/// Determines if this draw operation equals another.
///
/// The transform of the other draw operation.
/// The image of the other draw operation.
/// The opacity of the other draw operation.
/// The source rect of the other draw operation.
/// The dest rect 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, IBitmapImpl source, double opacity, Rect sourceRect, Rect destRect)
{
return transform == Transform &&
Equals(source, Source) &&
opacity == Opacity &&
sourceRect == SourceRect &&
destRect == DestRect;
}
///
public override void Render(IDrawingContextImpl context)
{
// TODO: Probably need to introduce some kind of locking mechanism in the case of
// WriteableBitmap.
context.Transform = Transform;
context.DrawImage(Source, Opacity, SourceRect, DestRect);
}
///
public override bool HitTest(Point p) => Bounds.Contains(p);
}
}