added SSCLI 1.0
[windows-sources.git] / sdk / samples / WPFSamples / CompositionTargetRenderingAnimations / csharp / particleeffectexamples / overlayrenderdecorator.cs
blob43706ab2bd104ea2fcf7f2cd3b10de26632cbc30
1 namespace Microsoft.Samples.PerFrameAnimations
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.ComponentModel;
7 using System.Diagnostics;
8 using System.Windows.Threading;
9 using System.Windows.Media;
10 using System.Windows.Markup;
11 using System.Windows;
12 using System.Windows.Controls;
14 /// <summary>
15 /// This class is used internally to delegate it's rendering to the parent OverlayRenderDecorator.
16 /// </summary>
17 internal class OverlayRenderDecoratorOverlayVisual : DrawingVisual
19 private bool _hitTestVisible = false;
21 internal bool IsHitTestVisible
23 get
25 return _hitTestVisible;
27 set
29 _hitTestVisible = value;
33 //dont hit test, these are just overlay graphics
34 protected override GeometryHitTestResult HitTestCore(GeometryHitTestParameters hitTestParameters)
36 if (IsHitTestVisible)
37 return base.HitTestCore(hitTestParameters);
38 else
39 return null;
42 //dont hit test, these are just overlay graphics
43 protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
45 if (IsHitTestVisible)
46 return base.HitTestCore(hitTestParameters);
47 else
48 return null;
54 /// <summary>
55 /// OverlayRenderDecorator provides a simple overlay graphics mechanism similar
56 /// to OnRender called OnOverlayRender
57 /// </summary>
58 [ContentProperty("Child")]
59 public class OverlayRenderDecorator : FrameworkElement
61 //the only logical child
62 private UIElement _child;
63 private VisualCollection _vc;
65 //this will be a visual child, but not a logical child. as the last child, it can 'overlay' graphics
66 // by calling back to us with OnOverlayRender
67 private OverlayRenderDecoratorOverlayVisual _overlayVisual;
70 //-------------------------------------------------------------------
72 // Constructors
74 //-------------------------------------------------------------------
76 #region Constructors
78 /// <summary>
79 /// Default constructor
80 /// </summary>
81 public OverlayRenderDecorator()
82 : base()
84 _overlayVisual = new OverlayRenderDecoratorOverlayVisual();
86 _vc = new VisualCollection(this);
88 //insert the overlay element into the visual tree
89 _vc.Add(_overlayVisual);
92 #endregion
95 //-------------------------------------------------------------------
97 // Public Properties
99 //-------------------------------------------------------------------
101 #region Public Properties
103 /// <summary>
104 /// Enables/Disables hit testing on the overlay visual
105 /// </summary>
106 public bool IsOverlayHitTestVisible
110 if (_overlayVisual != null)
111 return _overlayVisual.IsHitTestVisible;
112 else
113 return false;
117 if (_overlayVisual != null)
118 _overlayVisual.IsHitTestVisible = value;
122 /// <summary>
123 /// The single child of an <see cref="System.Windows.Media.Animation.OverlayRenderDecorator" />
124 /// </summary>
125 [DefaultValue(null)]
126 public virtual UIElement Child
130 return _child;
134 if (_child != value)
136 //need to remove old element from logical tree
137 if (_child != null)
139 OnDetachChild(_child);
140 RemoveLogicalChild(_child);
143 _vc.Clear();
145 if (value != null)
147 //add to visual
148 _vc.Add(value);
149 //add to logical
150 AddLogicalChild(value);
153 //always add the overlay child back into the visual tree if its set
154 if (_overlayVisual != null)
155 _vc.Add(_overlayVisual);
157 //cache the new child
158 _child = value;
160 //notify derived types of the new child
161 if (value != null)
162 OnAttachChild(_child);
164 InvalidateMeasure();
169 /// <summary>
170 /// Returns enumerator to logical children.
171 /// </summary>
172 protected override IEnumerator LogicalChildren
176 if (_child == null)
178 return (IEnumerator)new List<UIElement>().GetEnumerator();
180 List<UIElement> l = new List<UIElement>();
181 l.Add(_child);
182 return (IEnumerator)l.GetEnumerator();
187 #endregion
189 //-------------------------------------------------------------------
191 // Protected Methods
193 //-------------------------------------------------------------------
195 #region Protected Methods
197 /// <summary>
198 /// Updates DesiredSize of the OverlayRenderDecorator. Called by parent UIElement. This is the first pass of layout.
199 /// </summary>
200 /// <remarks>
201 /// OverlayRenderDecorator determines a desired size it needs from the child's sizing properties, margin, and requested size.
202 /// </remarks>
203 /// <param name="constraint">Constraint size is an "upper limit" that the return value should not exceed.</param>
204 /// <returns>The OverlayRenderDecorator's desired size.</returns>
205 protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
207 UIElement child = Child;
208 if (child != null)
210 child.Measure(constraint);
211 return (child.DesiredSize);
213 return (new System.Windows.Size());
216 /// <summary>
217 /// OverlayRenderDecorator computes the position of its single child inside child's Margin and calls Arrange
218 /// on the child.
219 /// </summary>
220 /// <param name="arrangeSize">Size the OverlayRenderDecorator will assume.</param>
221 protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize)
223 UIElement child = Child;
224 if (child != null)
226 child.Arrange(new Rect(arrangeSize));
229 //Our OnRender gets called in Arrange, but
230 // we dont have access to that, so update the
231 // overlay here.
232 if (_overlayVisual != null)
234 using (DrawingContext dc = _overlayVisual.RenderOpen())
236 //delegate to derived types
237 OnOverlayRender(dc);
241 return (arrangeSize);
244 /// <summary>
245 /// render method for overlay graphics.
246 /// </summary>
247 /// <param name="dc"></param>
248 protected virtual void OnOverlayRender(DrawingContext dc)
253 /// <summary>
254 /// gives derives types a simple way to respond to a new child being added
255 /// </summary>
256 /// <param name="child"></param>
257 protected virtual void OnAttachChild(UIElement child)
262 /// <summary>
263 /// gives derives types a simple way to respond to a child being removed
264 /// </summary>
265 /// <param name="child"></param>
266 protected virtual void OnDetachChild(UIElement child)
271 protected override int VisualChildrenCount
273 get {return _vc.Count;}
276 protected override Visual GetVisualChild(int index)
278 return _vc[index];
281 #endregion