Browse Source

Added mutable SolidColorBrush.

As a hack for lack of immutability support in XAML.
pull/464/head
Steven Kirk 10 years ago
parent
commit
e5a439d82a
  1. 35
      src/Perspex.SceneGraph/Media/Mutable/SolidColorBrush.cs
  2. 1
      src/Perspex.SceneGraph/Perspex.SceneGraph.csproj
  3. 3
      src/Perspex.SceneGraph/Properties/AssemblyInfo.cs
  4. 7
      src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs
  5. 13
      src/Windows/Perspex.Direct2D1/Media/SolidColorBrushImpl.cs

35
src/Perspex.SceneGraph/Media/Mutable/SolidColorBrush.cs

@ -0,0 +1,35 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
namespace Perspex.Media.Mutable
{
/// <summary>
/// Fills an area with a solid color.
/// </summary>
/// <remarks>
/// This is a mutable version of the normal immutable <see cref="Perspex.Media.SolidColorBrush"/>
/// for use in XAML. XAML really needs support for immutable data...
/// </remarks>
public class SolidColorBrush : Brush
{
public static readonly DirectProperty<SolidColorBrush, Color> ColorProperty =
PerspexProperty.RegisterDirect<SolidColorBrush, Color>(
"Color",
o => o.Color,
(o, v) => o.Color = v);
/// <summary>
/// Gets the color of the brush.
/// </summary>
public Color Color { get; set; }
/// <summary>
/// Returns a string representation of the brush.
/// </summary>
/// <returns>A string representation of the brush.</returns>
public override string ToString()
{
return Color.ToString();
}
}
}

1
src/Perspex.SceneGraph/Perspex.SceneGraph.csproj

@ -73,6 +73,7 @@
<Compile Include="Media\GradientSpreadMethod.cs" />
<Compile Include="Media\GradientStop.cs" />
<Compile Include="Media\LineGeometry.cs" />
<Compile Include="Media\Mutable\SolidColorBrush.cs" />
<Compile Include="Media\PenLineJoin.cs" />
<Compile Include="Media\PolylineGeometry.cs" />
<Compile Include="Media\RadialGradientBrush.cs" />

3
src/Perspex.SceneGraph/Properties/AssemblyInfo.cs

@ -5,4 +5,5 @@ using System.Reflection;
using Perspex.Metadata;
[assembly: AssemblyTitle("Perspex.SceneGraph")]
[assembly: XmlnsDefinition("https://github.com/perspex", "Perspex.Media")]
[assembly: XmlnsDefinition("https://github.com/perspex", "Perspex.Media")]
[assembly: XmlnsDefinition("https://github.com/perspex/mutable", "Perspex.Media.Mutable")]

7
src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs

@ -294,6 +294,7 @@ namespace Perspex.Direct2D1.Media
public BrushImpl CreateBrush(Perspex.Media.Brush brush, Size destinationSize)
{
var solidColorBrush = brush as Perspex.Media.SolidColorBrush;
var mutableSolidColorBrush = brush as Perspex.Media.Mutable.SolidColorBrush;
var linearGradientBrush = brush as Perspex.Media.LinearGradientBrush;
var radialGradientBrush = brush as Perspex.Media.RadialGradientBrush;
var imageBrush = brush as Perspex.Media.ImageBrush;
@ -303,6 +304,10 @@ namespace Perspex.Direct2D1.Media
{
return new SolidColorBrushImpl(solidColorBrush, _renderTarget);
}
if (mutableSolidColorBrush != null)
{
return new SolidColorBrushImpl(mutableSolidColorBrush, _renderTarget);
}
else if (linearGradientBrush != null)
{
return new LinearGradientBrushImpl(linearGradientBrush, _renderTarget, destinationSize);
@ -321,7 +326,7 @@ namespace Perspex.Direct2D1.Media
}
else
{
return new SolidColorBrushImpl(null, _renderTarget);
return new SolidColorBrushImpl((Perspex.Media.SolidColorBrush)null, _renderTarget);
}
}
}

13
src/Windows/Perspex.Direct2D1/Media/SolidColorBrushImpl.cs

@ -17,5 +17,18 @@ namespace Perspex.Direct2D1.Media
}
);
}
public SolidColorBrushImpl(Perspex.Media.Mutable.SolidColorBrush brush, SharpDX.Direct2D1.RenderTarget target)
{
PlatformBrush = new SharpDX.Direct2D1.SolidColorBrush(
target,
brush?.Color.ToDirect2D() ?? new SharpDX.Mathematics.Interop.RawColor4(),
new SharpDX.Direct2D1.BrushProperties
{
Opacity = brush != null ? (float)brush.Opacity : 1.0f,
Transform = target.Transform
}
);
}
}
}

Loading…
Cancel
Save