From 07866618b7017d68dfb5e43a9cbdb250a5ad06df Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 14 Sep 2014 18:03:55 +0200 Subject: [PATCH] Added opacity. --- Perspex.Direct2D1/Renderer.cs | 15 ++++++++++++++- Perspex/IVisual.cs | 2 ++ Perspex/Layout/Layoutable.cs | 5 ++++- Perspex/Visual.cs | 9 +++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Perspex.Direct2D1/Renderer.cs b/Perspex.Direct2D1/Renderer.cs index 071d1640a0..0e5689afdc 100644 --- a/Perspex.Direct2D1/Renderer.cs +++ b/Perspex.Direct2D1/Renderer.cs @@ -120,8 +120,16 @@ namespace Perspex.Direct2D1 /// The drawing context. private void Render(IVisual visual, DrawingContext context) { - if (visual.IsVisible) + if (visual.IsVisible && visual.Opacity > 0) { + if (visual.Opacity < 1) + { + Layer layer = new Layer(this.renderTarget); + LayerParameters p = new LayerParameters(); + p.Opacity = (float)visual.Opacity; + this.renderTarget.PushLayer(ref p, layer); + } + visual.Render(context); foreach (IVisual child in visual.VisualChildren) @@ -133,6 +141,11 @@ namespace Perspex.Direct2D1 this.Render(child, context); } } + + if (visual.Opacity < 1) + { + this.renderTarget.PopLayer(); + } } } } diff --git a/Perspex/IVisual.cs b/Perspex/IVisual.cs index 209c823e85..1f54d933f2 100644 --- a/Perspex/IVisual.cs +++ b/Perspex/IVisual.cs @@ -18,6 +18,8 @@ namespace Perspex bool IsVisible { get; } + double Opacity { get; } + IEnumerable VisualChildren { get; } IVisual VisualParent { get; set; } diff --git a/Perspex/Layout/Layoutable.cs b/Perspex/Layout/Layoutable.cs index 16642b0fe8..dd3c7fa4e6 100644 --- a/Perspex/Layout/Layoutable.cs +++ b/Perspex/Layout/Layoutable.cs @@ -130,7 +130,10 @@ namespace Perspex.Layout public void Arrange(Rect rect) { - this.ArrangeCore(rect); + if (this.DesiredSize.HasValue) + { + this.ArrangeCore(rect); + } } public void InvalidateMeasure() diff --git a/Perspex/Visual.cs b/Perspex/Visual.cs index 3b5387f4db..d693de7c33 100644 --- a/Perspex/Visual.cs +++ b/Perspex/Visual.cs @@ -19,6 +19,9 @@ namespace Perspex public static readonly PerspexProperty IsVisibleProperty = PerspexProperty.Register("IsVisible", true); + public static readonly PerspexProperty OpacityProperty = + PerspexProperty.Register("Opacity", 1); + private IVisual visualParent; private Rect bounds; @@ -34,6 +37,12 @@ namespace Perspex set { this.SetValue(IsVisibleProperty, value); } } + public double Opacity + { + get { return this.GetValue(OpacityProperty); } + set { this.SetValue(OpacityProperty, value); } + } + Rect IVisual.Bounds { get { return this.bounds; }