-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderLight.xaml.cs
144 lines (123 loc) · 4.05 KB
/
ShaderLight.xaml.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
using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using HueShift;
namespace xmaslights
{
/// <summary>
/// Interaction logic for ShaderLight.xaml
/// </summary>
public partial class ShaderLight : ILight
{
private static BitmapImage lightOff;
private static BitmapImage lightOn;
private static BitmapImage lightBroken;
private static Random random = new Random();
private static MediaPlayer player;
static ShaderLight()
{
lightOn = new BitmapImage(new Uri("pack://application:,,,/ChristmasLights;Component/Resources/LightYellowSmall.png"));
RenderOptions.SetCachingHint(lightOn, CachingHint.Cache);
lightOn.Freeze();
lightOff = new BitmapImage(new Uri("pack://application:,,,/ChristmasLights;Component/Resources/LightRedSmall.png"));
RenderOptions.SetCachingHint(lightOff, CachingHint.Cache);
lightOff.Freeze();
lightBroken = new BitmapImage(new Uri("pack://application:,,,/ChristmasLights;Component/Resources/LightBrokenSmall.png"));
RenderOptions.SetCachingHint(lightBroken, CachingHint.Cache);
lightBroken.Freeze();
player = new MediaPlayer();
player.Volume = 1;
player.Open(new Uri(@"Resources\glass_break_1.mp3", UriKind.Relative));
player.MediaEnded += delegate { player.Stop(); };
player.Stop();
}
public ShaderLight()
{
InitializeComponent();
InitLight();
}
public double HueShift { get; set; }
private bool _isOn;
private int _clickCounter;
private int _blinkCounter;
private int _lifetime;
private bool _isBroken;
private DispatcherTimer _repairtimer;
public void Switch()
{
_isOn = !_isOn;
Update();
}
public void On()
{
_isOn = true;
Update();
}
public void Off()
{
_isOn = false;
Update();
}
public void Update()
{
_blinkCounter++;
if (!_isBroken)
{
if (_lifetime == _blinkCounter)
{
Break();
return;
}
if (_isOn)
{
lightImage.Source = lightOn;
((ShiftHueEffect)lightImage.Effect).HueShift = HueShift;
}
else
{
lightImage.Source = lightOff;
((ShiftHueEffect)lightImage.Effect).HueShift = HueShift;
}
}
}
public void Rotate(int angle)
{
RenderTransform = new RotateTransform(angle, 10, 20);
}
public bool IsOn()
{
return _isOn;
}
public void Click()
{
if (!_isBroken && ++_clickCounter == 3)
{
Break();
}
}
private void Break()
{
player.Play();
_isBroken = true;
_repairtimer = new DispatcherTimer(new TimeSpan(0,0,20), DispatcherPriority.ApplicationIdle, delegate { Repair(); }, Dispatcher.CurrentDispatcher);
_repairtimer.Start();
lightImage.Source = lightBroken;
}
private void Repair()
{
_repairtimer.Stop();
InitLight();
}
private void InitLight()
{
_isBroken = false;
_clickCounter = 0;
_blinkCounter = 0;
_lifetime = random.Next(3600, 10800);
ShiftHueEffect effect = new ShiftHueEffect();
lightImage.Effect = effect;
lightImage.Source = lightOn;
}
}
}