/
RippleXamlLight.cs
159 lines (121 loc) · 5.06 KB
/
RippleXamlLight.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Numerics;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Continuity.Extensions;
namespace Continuity.Lights
{
public class RippleXamlLight : XamlLight
{
#region Fields
private static readonly string Id = typeof(RippleXamlLight).FullName;
private Compositor _compositor;
private const float OffsetZRatio = 2.0f;
private float _rippleOffsetZ;
private Vector3KeyFrameAnimation _lightRippleOffsetAnimation;
#endregion
#region Properties
public Color Color
{
get => (Color)GetValue(ColorProperty);
set => SetValue(ColorProperty, value);
}
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register(
"Color", typeof(Color), typeof(RippleXamlLight), new PropertyMetadata(Colors.White, (s, e) =>
{
var self = (RippleXamlLight)s;
var newColor = (Color)e.NewValue;
if (self.CompositionLight is SpotLight spotLight)
{
spotLight.InnerConeColor = newColor;
spotLight.OuterConeColor = newColor;
}
}));
#endregion
#region Overrides
protected override void OnConnected(UIElement newElement)
{
_compositor = Window.Current.Compositor;
var spotLight = CreateSpotLight();
CompositionLight = spotLight;
_lightRippleOffsetAnimation = CreateLightRippleOffsetAnimation();
SubscribeToPointerEvents();
AddTargetElement(GetId(), newElement);
SpotLight CreateSpotLight()
{
var light = _compositor.CreateSpotLight();
light.InnerConeColor = light.OuterConeColor = Color;
light.InnerConeAngleInDegrees = 90.0f;
light.OuterConeAngleInDegrees = 0.0f;
//light.LinearAttenuation = 0.2f;
//light.QuadraticAttenuation = 0.1f;
_rippleOffsetZ = CalculateRippleOffsetZOnDesizedSize(newElement);
light.Offset = new Vector3(0.0f, 0.0f, _rippleOffsetZ);
return light;
}
void SubscribeToPointerEvents()
{
if (newElement is FrameworkElement element)
{
element.SizeChanged += OnElementSizeChanged;
}
newElement.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(OnPointerPressed), true);
}
}
protected override void OnDisconnected(UIElement oldElement)
{
UnsubscribeFromPointerEvents();
RemoveTargetElement(GetId(), oldElement);
CompositionLight.Dispose();
_lightRippleOffsetAnimation.Dispose();
void UnsubscribeFromPointerEvents()
{
if (oldElement is FrameworkElement element)
{
element.SizeChanged -= OnElementSizeChanged;
}
oldElement.RemoveHandler(UIElement.PointerPressedEvent, new PointerEventHandler(OnPointerPressed));
}
}
protected override string GetId() => Id;
#endregion
#region Event Handlers
private void OnElementSizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.PreviousSize.Equals(e.NewSize)) return;
_rippleOffsetZ = CalculateRippleOffsetZOnRenderSize((FrameworkElement)sender);
}
private void OnPointerPressed(object sender, PointerRoutedEventArgs e) =>
StartLightRippleOffsetAnimation(e.GetCurrentPoint((UIElement)sender).Position.ToVector2());
#endregion
#region Methods
private Vector3KeyFrameAnimation CreateLightRippleOffsetAnimation()
{
var offsetAnimation = _compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.Duration = TimeSpan.FromMilliseconds(1200);
return offsetAnimation;
}
private void StartLightRippleOffsetAnimation(Vector2 position)
{
var startingPoisition = new Vector3(position, 0.0f);
_lightRippleOffsetAnimation?.InsertKeyFrame(0.0f, startingPoisition);
_lightRippleOffsetAnimation?.InsertKeyFrame(1.0f, new Vector3(position.X, position.Y, _rippleOffsetZ));
CompositionLight?.StartAnimation("Offset", _lightRippleOffsetAnimation);
}
private float CalculateRippleOffsetZOnDesizedSize(UIElement element)
{
var desiredSize = element.GetDesiredSize();
return Math.Max(desiredSize.X, desiredSize.Y) * OffsetZRatio;
}
private float CalculateRippleOffsetZOnRenderSize(UIElement element)
{
var desiredSize = element.RenderSize.ToVector2();
return Math.Max(desiredSize.X, desiredSize.Y) * OffsetZRatio;
}
#endregion
}
}