/
FireworkEffect.cs
335 lines (280 loc) · 11.8 KB
/
FireworkEffect.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// // Copyright (c) Microsoft. All rights reserved.
// // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace PerFrameAnimation
{
//uielement
//visual operations
//animation effect stuff
//LayoutOverrideDecorator
public class FireworkEffect : OverlayRenderDecorator
{
private readonly Vector _gravity = new Vector(0.0, 10.0);
private readonly List<Particle> _particles = new List<Particle>();
private Point _lastMousePosition = new Point(0, 0);
private double _secondsUntilDrop;
private ParticleEffectsTimeTracker _timeTracker;
protected Random Random = new Random();
protected override void OnAttachChild(UIElement child)
{
CompositionTarget.Rendering += OnFrameCallback;
child.PreviewMouseLeftButtonUp += OnMouseLeftButtonUp;
child.PreviewMouseMove += OnMouseMove;
_timeTracker = new ParticleEffectsTimeTracker();
}
protected override void OnDetachChild(UIElement child)
{
CompositionTarget.Rendering -= OnFrameCallback;
child.PreviewMouseLeftButtonUp -= OnMouseLeftButtonUp;
child.PreviewMouseMove -= OnMouseMove;
_timeTracker = null;
}
protected void OnFrameCallback(object sender, EventArgs e)
{
UpdateParticles(_timeTracker.Update());
}
protected virtual void UpdateParticles(double deltatime)
{
//drop particles from mouse position
if (MouseDropsParticles && IsMouseOver)
{
_secondsUntilDrop -= deltatime;
if (_secondsUntilDrop < 0.0)
{
AddRandomBurst(_lastMousePosition - new Vector(Radius/2.0, Radius/2.0), 1);
_secondsUntilDrop = MouseDropDelay + (Random.NextDouble()*2.0 - 1.0)*MouseDropDelayVariation;
}
}
if (_particles.Count > 0)
InvalidateVisual();
//update all particles
for (var i = 0; i < _particles.Count;)
{
//_particles[i]
var p = _particles[i];
if (GravitateToMouse)
{
p.Velocity += (_lastMousePosition - p.Location)*GravitateScale;
}
else
{
p.Velocity += _gravity;
}
p.Location += p.Velocity*deltatime;
if (BounceOffContainer)
{
var radius = p.Diameter/2.0;
if (p.Location.X - radius < 0.0)
{
p.Location.X = radius;
p.Velocity.X *= -0.9;
}
else if (p.Location.X > ActualWidth - radius)
{
p.Location.X = ActualWidth - radius;
p.Velocity.X *= -0.9;
}
if (p.Location.Y - radius < 0.0)
{
p.Location.Y = radius;
p.Velocity.Y *= -0.9;
}
else if (p.Location.Y > ActualHeight - radius)
{
p.Location.Y = ActualHeight - radius;
p.Velocity.Y *= -0.9;
}
}
//only increment counter for live particles
if (_timeTracker.ElapsedTime > p.DeathTime)
_particles.RemoveAt(i);
else
i++;
}
}
protected override void OnOverlayRender(DrawingContext drawingContext)
{
foreach (var p in _particles)
{
//figure out where in the particles life we are
var particlelife = (_timeTracker.ElapsedTime - p.LifeTime).TotalSeconds/
(p.DeathTime - p.LifeTime).TotalSeconds;
var currentcolor = Color.Multiply(p.StartColor, (float) (1.0 - particlelife)) +
Color.Multiply(p.EndColor, (float) particlelife);
Brush brush = new RadialGradientBrush(currentcolor,
Color.FromArgb(0, currentcolor.R, currentcolor.G, currentcolor.B));
var rect =
new RectangleGeometry(
new Rect(new Point(p.Location.X - p.Diameter/2.0, p.Location.Y - p.Diameter/2.0),
new Size(p.Diameter, p.Diameter)));
drawingContext.DrawGeometry(brush, null, rect);
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
_lastMousePosition = e.GetPosition(this);
}
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
AddRandomBurst(e.GetPosition(this));
}
public void AddRandomBurst()
{
var point = new Point(Random.NextDouble()*ActualWidth, Random.NextDouble()*ActualHeight);
AddRandomBurst(point, ClickBurstSize);
}
public void AddRandomBurst(Point location)
{
AddRandomBurst(location, ClickBurstSize);
}
public void AddRandomBurst(Point location, int count)
{
for (var i = 0; i < count; i++)
{
var p = new Particle();
var radius = Radius + (Random.NextDouble()*2.0 - 1.0)*RadiusVariation;
var lifetime = Random.NextDouble()*3.0 + 1.0;
p.Location = location;
p.Velocity = new Vector(Random.NextDouble()*200.0 - 100.0, Random.NextDouble()*-200 + 100.0);
p.LifeTime = _timeTracker.ElapsedTime;
p.DeathTime = _timeTracker.ElapsedTime + TimeSpan.FromSeconds(lifetime);
p.Diameter = 2.0*radius;
var startColor = StartColor;
var startColorVariation = StartColorVariation;
var endColor = EndColor;
var endColorVariation = EndColorVariation;
var startRandColor = Color.FromScRgb(startColorVariation.ScA*(float) (Random.NextDouble()*2.0 - 1.0),
startColorVariation.ScR*(float) (Random.NextDouble()*2.0 - 1.0),
startColorVariation.ScG*(float) (Random.NextDouble()*2.0 - 1.0),
startColorVariation.ScB*(float) (Random.NextDouble()*2.0 - 1.0));
var endRandColor = Color.FromScRgb(endColorVariation.ScA*(float) (Random.NextDouble()*2.0 - 1.0),
endColorVariation.ScR*(float) (Random.NextDouble()*2.0 - 1.0),
endColorVariation.ScG*(float) (Random.NextDouble()*2.0 - 1.0),
endColorVariation.ScB*(float) (Random.NextDouble()*2.0 - 1.0));
p.StartColor = startColor + startRandColor;
p.EndColor = endColor + endRandColor;
_particles.Add(p);
}
}
#region Dependency Properties
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register(
"RadiusBase",
typeof (double),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(15.0)
);
public static readonly DependencyProperty RadiusVariationProperty =
DependencyProperty.Register(
"RadiusVariation",
typeof (double),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(5.0)
);
public static readonly DependencyProperty StartColorProperty =
DependencyProperty.Register(
"StartColor",
typeof (Color),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(Color.FromArgb(245, 200, 50, 20))
);
public static readonly DependencyProperty EndColorProperty =
DependencyProperty.Register(
"EndColor",
typeof (Color),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(Color.FromArgb(100, 200, 255, 20))
);
public static readonly DependencyProperty StartColorVariationProperty =
DependencyProperty.Register(
"StartColorVariation",
typeof (Color),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(Color.FromArgb(10, 55, 50, 20))
);
public static readonly DependencyProperty EndColorVariationProperty =
DependencyProperty.Register(
"EndColorVariation",
typeof (Color),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(Color.FromArgb(50, 20, 100, 20))
);
public static readonly DependencyProperty MouseDropDelayProperty =
DependencyProperty.Register(
"MouseDropDelay",
typeof (double),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(0.1)
);
public static readonly DependencyProperty MouseDropDelayVariationProperty =
DependencyProperty.Register(
"MouseDropDelayVariation",
typeof (double),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(0.05)
);
public static readonly DependencyProperty ClickBurstSizeProperty =
DependencyProperty.Register(
"ClickBurstSize",
typeof (int),
typeof (FireworkEffect),
new FrameworkPropertyMetadata(30)
);
#endregion
#region Properties
public double Radius
{
get { return (double) GetValue(RadiusProperty); }
set { SetValue(RadiusProperty, value); }
}
public double RadiusVariation
{
get { return (double) GetValue(RadiusVariationProperty); }
set { SetValue(RadiusVariationProperty, value); }
}
public Color StartColor
{
get { return (Color) GetValue(StartColorProperty); }
set { SetValue(StartColorProperty, value); }
}
public Color EndColor
{
get { return (Color) GetValue(EndColorProperty); }
set { SetValue(EndColorProperty, value); }
}
public Color StartColorVariation
{
get { return (Color) GetValue(StartColorVariationProperty); }
set { SetValue(StartColorVariationProperty, value); }
}
public Color EndColorVariation
{
get { return (Color) GetValue(EndColorVariationProperty); }
set { SetValue(EndColorVariationProperty, value); }
}
public bool BounceOffContainer { get; set; } = false;
public bool GravitateToMouse { get; set; } = false;
public double GravitateScale { get; set; } = 0.1;
public bool MouseDropsParticles { get; set; } = false;
public double MouseDropDelay
{
get { return (double) GetValue(MouseDropDelayProperty); }
set { SetValue(MouseDropDelayProperty, value); }
}
public double MouseDropDelayVariation
{
get { return (double) GetValue(MouseDropDelayVariationProperty); }
set { SetValue(MouseDropDelayVariationProperty, value); }
}
public int ClickBurstSize
{
get { return (int) GetValue(ClickBurstSizeProperty); }
set { SetValue(ClickBurstSizeProperty, value); }
}
#endregion
}
}