Browse Source

Added the platform independent interface and the Direct2D1 implementation of opacity masks.

pull/553/head
Jeremy Koritzinsky 11 years ago
parent
commit
5e0f12235f
  1. 16
      src/Avalonia.SceneGraph/Media/DrawingContext.cs
  2. 4
      src/Avalonia.SceneGraph/Media/IDrawingContext.cs
  3. 1
      src/Avalonia.SceneGraph/Rendering/RendererMixin.cs
  4. 16
      src/Avalonia.SceneGraph/Visual.cs
  5. 5
      src/Avalonia.SceneGraph/VisualTree/IVisual.cs
  6. 20
      src/Windows/Avalonia.Direct2D1/Media/DrawingContext.cs

16
src/Avalonia.SceneGraph/Media/DrawingContext.cs

@ -128,7 +128,8 @@ namespace Avalonia.Media
Opacity, Opacity,
Clip, Clip,
MatrixContainer, MatrixContainer,
GeometryClip GeometryClip,
OpacityMask
} }
public PushedState(DrawingContext context, PushedStateType type, Matrix matrix = default(Matrix)) public PushedState(DrawingContext context, PushedStateType type, Matrix matrix = default(Matrix))
@ -156,6 +157,8 @@ namespace Avalonia.Media
_context._impl.PopOpacity(); _context._impl.PopOpacity();
else if (_type == PushedStateType.GeometryClip) else if (_type == PushedStateType.GeometryClip)
_context._impl.PopGeometryClip(); _context._impl.PopGeometryClip();
else if (_type == PushedStateType.OpacityMask)
_context._impl.PopOpacityMask();
else if (_type == PushedStateType.MatrixContainer) else if (_type == PushedStateType.MatrixContainer)
{ {
var cont = _context._transformContainers.Pop(); var cont = _context._transformContainers.Pop();
@ -201,6 +204,17 @@ namespace Avalonia.Media
return new PushedState(this, PushedState.PushedStateType.Opacity); return new PushedState(this, PushedState.PushedStateType.Opacity);
} }
/// <summary>
/// Pushes an opacity mask.
/// </summary>
/// <param name="mask">The opacity mask.</param>
/// <returns>A disposable to undo the opacity mask.</returns>
public PushedState PushOpacityMask(IBrush mask, Rect bounds)
{
_impl.PushOpacityMask(mask, bounds);
return new PushedState(this, PushedState.PushedStateType.OpacityMask);
}
/// <summary> /// <summary>
/// Pushes a matrix transformation. /// Pushes a matrix transformation.
/// </summary> /// </summary>

4
src/Avalonia.SceneGraph/Media/IDrawingContext.cs

@ -81,6 +81,10 @@ namespace Avalonia.Media
void PopOpacity(); void PopOpacity();
void PushOpacityMask(IBrush mask, Rect bounds);
void PopOpacityMask();
/// <summary> /// <summary>
/// Pushes a clip geometry. /// Pushes a clip geometry.
/// </summary> /// </summary>

1
src/Avalonia.SceneGraph/Rendering/RendererMixin.cs

@ -120,6 +120,7 @@ namespace Avalonia.Rendering
using (context.PushOpacity(opacity)) using (context.PushOpacity(opacity))
using (clipToBounds ? context.PushClip(bounds) : default(DrawingContext.PushedState)) using (clipToBounds ? context.PushClip(bounds) : default(DrawingContext.PushedState))
using (visual.Clip != null ? context.PushGeometryClip(visual.Clip) : default(DrawingContext.PushedState)) using (visual.Clip != null ? context.PushGeometryClip(visual.Clip) : default(DrawingContext.PushedState))
using (visual.OpacityMask != null ? context.PushOpacityMask(visual.OpacityMask, clipToBounds ? bounds : clipRect) : default(DrawingContext.PushedState))
using (context.PushTransformContainer()) using (context.PushTransformContainer())
{ {
visual.Render(context); visual.Render(context);

16
src/Avalonia.SceneGraph/Visual.cs

@ -57,6 +57,12 @@ namespace Avalonia
public static readonly StyledProperty<double> OpacityProperty = public static readonly StyledProperty<double> OpacityProperty =
AvaloniaProperty.Register<Visual, double>(nameof(Opacity), 1); AvaloniaProperty.Register<Visual, double>(nameof(Opacity), 1);
/// <summary>
/// Defines the <see cref="OpacityMask"/> property.
/// </summary>
public static readonly StyledProperty<IBrush> OpacityMaskProperty =
AvaloniaProperty.Register<Visual, IBrush>(nameof(OpacityMask));
/// <summary> /// <summary>
/// Defines the <see cref="RenderTransform"/> property. /// Defines the <see cref="RenderTransform"/> property.
/// </summary> /// </summary>
@ -168,6 +174,16 @@ namespace Avalonia
set { SetValue(OpacityProperty, value); } set { SetValue(OpacityProperty, value); }
} }
/// <summary>
/// Gets the opacity mask of the scene graph node.
/// </summary>
public IBrush OpacityMask
{
get { return GetValue(OpacityMaskProperty); }
set { SetValue(OpacityMaskProperty, value); }
}
/// <summary> /// <summary>
/// Gets the render transform of the scene graph node. /// Gets the render transform of the scene graph node.
/// </summary> /// </summary>

5
src/Avalonia.SceneGraph/VisualTree/IVisual.cs

@ -66,6 +66,11 @@ namespace Avalonia.VisualTree
/// </summary> /// </summary>
double Opacity { get; set; } double Opacity { get; set; }
/// <summary>
/// Gets or sets the opacity mask of the scene graph node.
/// </summary>
IBrush OpacityMask { get; set; }
/// <summary> /// <summary>
/// Gets or sets the render transform of the scene graph node. /// Gets or sets the render transform of the scene graph node.
/// </summary> /// </summary>

20
src/Windows/Avalonia.Direct2D1/Media/DrawingContext.cs

@ -350,5 +350,25 @@ namespace Avalonia.Direct2D1.Media
{ {
PopLayer(); PopLayer();
} }
public void PushOpacityMask(IBrush mask, Rect bounds)
{
var parameters = new LayerParameters
{
ContentBounds = PrimitiveExtensions.RectangleInfinite,
MaskTransform = PrimitiveExtensions.Matrix3x2Identity,
Opacity = 1,
OpacityBrush = CreateBrush(mask, bounds.Size).PlatformBrush
};
var layer = _layerPool.Count != 0 ? _layerPool.Pop() : new Layer(_renderTarget);
_renderTarget.PushLayer(ref parameters, layer);
_layers.Push(layer);
}
public void PopOpacityMask()
{
PopLayer();
}
} }
} }

Loading…
Cancel
Save