Browse Source
Conflicts: src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs src/Windows/Perspex.Direct2D1/Media/PerspexTextRenderer.cspull/111/head
21 changed files with 365 additions and 81 deletions
@ -0,0 +1,47 @@ |
|||
namespace Perspex.Threading |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using System.Threading; |
|||
|
|||
|
|||
/// <summary>
|
|||
/// SynchronizationContext to be used on main thread
|
|||
/// </summary>
|
|||
public class PerspexSynchronizationContext : SynchronizationContext |
|||
{ |
|||
/// <summary>
|
|||
/// Controls if SynchronizationContext should be installed in InstallIfNeeded. Used by Designer.
|
|||
/// </summary>
|
|||
public static bool AutoInstall { get; set; } = true; |
|||
|
|||
/// <summary>
|
|||
/// Installs synchronization context in current thread
|
|||
/// </summary>
|
|||
public static void InstallIfNeeded() |
|||
{ |
|||
if (!AutoInstall || Current is PerspexSynchronizationContext) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
SetSynchronizationContext(new PerspexSynchronizationContext()); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public override void Post(SendOrPostCallback d, object state) |
|||
{ |
|||
Dispatcher.UIThread.Post(() => d(state)); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public override void Send(SendOrPostCallback d, object state) |
|||
{ |
|||
// TODO: Add check for being on the main thread, we should invoke the method immediately in this case
|
|||
Dispatcher.UIThread.InvokeAsync(() => d(state)).Wait(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
namespace Perspex.Media |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
public enum BrushMappingMode |
|||
{ |
|||
Absolute, |
|||
RelativeToBoundingBox |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
namespace Perspex.Media |
|||
{ |
|||
using System.Collections.Generic; |
|||
|
|||
public abstract class GradientBrush : Brush |
|||
{ |
|||
public static readonly PerspexProperty<BrushMappingMode> MappingModeProperty = |
|||
PerspexProperty.Register<GradientBrush, BrushMappingMode>(nameof(MappingMode), BrushMappingMode.RelativeToBoundingBox); |
|||
|
|||
public static readonly PerspexProperty<GradientSpreadMethod> SpreadMethodProperty = |
|||
PerspexProperty.Register<GradientBrush, GradientSpreadMethod>(nameof(SpreadMethod), GradientSpreadMethod.Pad); |
|||
|
|||
public static readonly PerspexProperty<List<GradientStop>> GradientStopsProperty = |
|||
PerspexProperty.Register<GradientBrush, List<GradientStop>>(nameof(Opacity), new List<GradientStop>()); |
|||
|
|||
public GradientBrush() |
|||
{ |
|||
} |
|||
|
|||
public BrushMappingMode MappingMode |
|||
{ |
|||
get { return this.GetValue(MappingModeProperty); } |
|||
set { this.SetValue(MappingModeProperty, value); } |
|||
} |
|||
|
|||
public GradientSpreadMethod SpreadMethod |
|||
{ |
|||
get { return this.GetValue(SpreadMethodProperty); } |
|||
set { this.SetValue(SpreadMethodProperty, value); } |
|||
} |
|||
|
|||
public List<GradientStop> GradientStops |
|||
{ |
|||
get { return this.GetValue(GradientStopsProperty); } |
|||
set { this.SetValue(GradientStopsProperty, value); } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
namespace Perspex.Media |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
public enum GradientSpreadMethod |
|||
{ |
|||
Pad, |
|||
Reflect, |
|||
Repeat |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
namespace Perspex.Media |
|||
{ |
|||
/// <summary>
|
|||
/// GradientStop
|
|||
/// </summary>
|
|||
public sealed class GradientStop |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="GradientStop"/> class.
|
|||
/// </summary>
|
|||
public GradientStop() { } |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="GradientStop"/> class.
|
|||
/// </summary>
|
|||
/// <param name="color">The color</param>
|
|||
/// <param name="offset">The offset</param>
|
|||
public GradientStop(Color color, double offset) |
|||
{ |
|||
this.Color = color; |
|||
this.Offset = offset; |
|||
} |
|||
|
|||
// TODO: Make these dependency properties.
|
|||
|
|||
/// <summary>
|
|||
/// The offset
|
|||
/// </summary>
|
|||
public double Offset { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The color
|
|||
/// </summary>
|
|||
public Color Color { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
namespace Perspex.Media |
|||
{ |
|||
public class LinearGradientBrush : GradientBrush |
|||
{ |
|||
public static readonly PerspexProperty<Point> StartPointProperty = |
|||
PerspexProperty.Register<LinearGradientBrush, Point>(nameof(StartPoint), new Point(0,0)); |
|||
|
|||
public static readonly PerspexProperty<Point> EndPointProperty = |
|||
PerspexProperty.Register<LinearGradientBrush, Point>(nameof(EndPoint), new Point(0, 0)); |
|||
|
|||
public Point StartPoint |
|||
{ |
|||
get { return this.GetValue(StartPointProperty); } |
|||
set { this.SetValue(StartPointProperty, value); } |
|||
} |
|||
|
|||
public Point EndPoint |
|||
{ |
|||
get { return this.GetValue(EndPointProperty); } |
|||
set { this.SetValue(EndPointProperty, value); } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
|
|||
namespace Perspex.Direct2D1.Media |
|||
{ |
|||
public abstract class BrushImpl : IDisposable |
|||
{ |
|||
public SharpDX.Direct2D1.Brush PlatformBrush { get; set; } |
|||
|
|||
public BrushImpl(Perspex.Media.Brush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) |
|||
{ |
|||
} |
|||
|
|||
public virtual void Dispose() |
|||
{ |
|||
if (this.PlatformBrush != null) |
|||
this.PlatformBrush.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
namespace Perspex.Direct2D1.Media |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
public class LinearGradientBrushImpl : BrushImpl |
|||
{ |
|||
public LinearGradientBrushImpl(Perspex.Media.LinearGradientBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) |
|||
: base(brush, target, destinationSize) |
|||
{ |
|||
if (brush != null) |
|||
{ |
|||
var gradientStops = brush.GradientStops.Select(s => new SharpDX.Direct2D1.GradientStop { Color = s.Color.ToDirect2D(), Position = (float)s.Offset }).ToArray(); |
|||
|
|||
Point startPoint = new Point(0, 0); |
|||
Point endPoint = new Point(0, 0); |
|||
|
|||
switch (brush.MappingMode) |
|||
{ |
|||
case Perspex.Media.BrushMappingMode.Absolute: |
|||
// TODO:
|
|||
|
|||
break; |
|||
case Perspex.Media.BrushMappingMode.RelativeToBoundingBox: |
|||
startPoint = new Point(brush.StartPoint.X * destinationSize.Width, brush.StartPoint.Y * destinationSize.Height); |
|||
endPoint = new Point(brush.EndPoint.X * destinationSize.Width, brush.EndPoint.Y * destinationSize.Height); |
|||
|
|||
break; |
|||
} |
|||
|
|||
this.PlatformBrush = new SharpDX.Direct2D1.LinearGradientBrush( |
|||
target, |
|||
new SharpDX.Direct2D1.LinearGradientBrushProperties { StartPoint = startPoint.ToSharpDX(), EndPoint = endPoint.ToSharpDX() }, |
|||
new SharpDX.Direct2D1.BrushProperties { Opacity = (float)brush.Opacity, Transform = target.Transform }, |
|||
new SharpDX.Direct2D1.GradientStopCollection(target, gradientStops, brush.SpreadMethod.ToDirect2D()) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
namespace Perspex.Direct2D1.Media |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
public class SolidColorBrushImpl : BrushImpl |
|||
{ |
|||
public SolidColorBrushImpl(Perspex.Media.SolidColorBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) |
|||
: base(brush, target, destinationSize) |
|||
{ |
|||
this.PlatformBrush = new SharpDX.Direct2D1.SolidColorBrush(target, brush?.Color.ToDirect2D() ?? new SharpDX.Color4()); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue