-
Notifications
You must be signed in to change notification settings - Fork 1
/
CImageButton.xaml.cs
267 lines (235 loc) · 9.71 KB
/
CImageButton.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
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
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinUI3_MediaEngine
{
public sealed partial class CImageButton : UserControl
{
public CImageButton(bool bCheckLongPres = false, bool bPointLight = false)
{
this.InitializeComponent();
m_bCheckLongPress = bCheckLongPres;
m_bPointLight = bPointLight;
if (m_bPointLight)
CreateSpotLight(this);
this.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(CImageButton_PointerPressed), true);
this.AddHandler(UIElement.PointerReleasedEvent, new PointerEventHandler(CImageButton_PointerReleased), true);
this.AddHandler(UIElement.PointerEnteredEvent, new PointerEventHandler(CImageButton_PointerEntered), true);
this.AddHandler(UIElement.PointerExitedEvent, new PointerEventHandler(CImageButton_PointerExited), true);
this.AddHandler(UIElement.PointerCaptureLostEvent, new PointerEventHandler(CImageButton_CaptureLost), true);
this.AddHandler(UIElement.PointerMovedEvent, new PointerEventHandler(CImageButton_PointerMoved), true);
}
public CImageButton()
: this(false, false)
{
}
// Adapted from WinUI 3 Gallery : https://github.com/microsoft/WinUI-Gallery
private SpringVector3NaturalMotionAnimation _springAnimation;
private bool m_bCheckLongPress = false;
private bool _bPressed = false;
private bool _bInside = false;
private Microsoft.UI.Composition.PointLight _spotLight = null;
private bool m_bPointLight = false;
private DispatcherTimer dTimer;
private TimeSpan tsDuration;
private DateTime tsEnd;
public void StartTimer(int nMilliSeconds)
{
dTimer = new DispatcherTimer();
dTimer.Interval = TimeSpan.FromMilliseconds(60);
tsDuration = TimeSpan.FromMilliseconds(nMilliSeconds);
tsEnd = DateTime.UtcNow + tsDuration;
dTimer.Tick += Dt_Tick;
dTimer.Start();
}
public void Dt_Tick(object sender, object e)
{
DateTime dtNow = DateTime.UtcNow;
if (dtNow >= tsEnd)
{
if (dTimer != null)
{
dTimer.Stop();
dTimer = null;
if (FunctionDelegate != null)
FunctionDelegate();
}
}
}
public delegate void DELEGATEPROC();
private DELEGATEPROC FunctionDelegate;
public void SetFunctionLongPress(DELEGATEPROC fct)
{
FunctionDelegate = new DELEGATEPROC(fct);
}
private void CImageButton_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var img = (UserControl)sender;
var properties = e.GetCurrentPoint((UIElement)sender).Properties;
if (properties.IsLeftButtonPressed)
{
_bPressed = true;
bool bCapture = ((UserControl)sender).CapturePointer(e.Pointer);
CreateOrUpdateSpringAnimation((UIElement)img.Parent, 3.0f);
(sender as UIElement).StartAnimation(_springAnimation);
if (m_bCheckLongPress)
StartTimer(5000);
}
}
private void CImageButton_PointerReleased(object sender, PointerRoutedEventArgs e)
{
var img = (UserControl)sender;
_bPressed = false;
CreateOrUpdateSpringAnimation((UIElement)img.Parent, 0.0f);
(sender as UIElement).StartAnimation(_springAnimation);
if (_bInside)
CImageButton_Click(sender, e);
if (m_bCheckLongPress)
{
if (dTimer != null)
{
Console.Beep(6000, 10);
dTimer.Stop();
dTimer = null;
}
}
}
private void CImageButton_PointerEntered(object sender, PointerRoutedEventArgs e)
{
var img = (UserControl)sender;
_bInside = true;
if (_bPressed)
{
CreateOrUpdateSpringAnimation((UIElement)img.Parent, 3.0f);
(sender as UIElement).StartAnimation(_springAnimation);
if (m_bCheckLongPress)
StartTimer(5000);
}
if (m_bPointLight)
{
_spotLight.Intensity = 1.5f;
_spotLight.IsEnabled = true;
}
}
private void CImageButton_PointerExited(object sender, PointerRoutedEventArgs e)
{
var img = (UserControl)sender;
_bInside = false;
if (_bPressed)
{
CreateOrUpdateSpringAnimation((UIElement)img.Parent, 0.0f);
(sender as UIElement).StartAnimation(_springAnimation);
if (m_bCheckLongPress)
{
if (dTimer != null)
{
Console.Beep(6000, 10);
dTimer.Stop();
dTimer = null;
}
}
}
if (m_bPointLight)
{
_spotLight.Offset = new System.Numerics.Vector3((float)this.ActualWidth / 2,
(float)this.ActualHeight / 2,
300);
_spotLight.Intensity = 1.0f;
}
}
private void CImageButton_CaptureLost(object sender, PointerRoutedEventArgs e)
{
var img = (UserControl)sender;
if (_bPressed)
{
CreateOrUpdateSpringAnimation((UIElement)img.Parent, 0.0f);
(sender as UIElement).StartAnimation(_springAnimation);
_bPressed = false;
}
if (m_bPointLight && !_bInside)
{
_spotLight.Offset = new System.Numerics.Vector3((float)this.ActualWidth / 2,
(float)this.ActualHeight / 2,
300);
_spotLight.Intensity = 1.0f;
}
}
private void CImageButton_PointerMoved(object sender, PointerRoutedEventArgs e)
{
//var properties = e.GetCurrentPoint((UIElement)sender).Properties;
var pp = e.GetCurrentPoint((UIElement)sender);
if (m_bPointLight && _bInside)
{
_spotLight.Offset = new System.Numerics.Vector3((float)pp.Position.X,
(float)pp.Position.Y,
100);
}
}
private void CreateOrUpdateSpringAnimation(UIElement element, float finalValue)
{
if (_springAnimation == null)
{
Visual visual = ElementCompositionPreview.GetElementVisual(element);
Compositor compositor = visual.Compositor;
_springAnimation = compositor.CreateSpringVector3Animation();
_springAnimation.Period = TimeSpan.FromMilliseconds(10);
_springAnimation.Target = "Translation";
}
_springAnimation.FinalValue = new System.Numerics.Vector3(finalValue);
}
private void CreateSpotLight(UIElement element)
{
if (_spotLight == null)
{
Visual visual = ElementCompositionPreview.GetElementVisual(element);
Compositor compositor = visual.Compositor;
_spotLight = compositor.CreatePointLight();
_spotLight.CoordinateSpace = visual;
_spotLight.Color = Microsoft.UI.Colors.White;
_spotLight.Offset = new System.Numerics.Vector3((float)((FrameworkElement)element).ActualWidth / 2,
(float)((FrameworkElement)element).ActualHeight / 2,
300);
_spotLight.Targets.Add(visual);
_spotLight.Intensity = 1.0f;
_spotLight.IsEnabled = true;
}
}
public event RoutedEventHandler CImageButtonClicked;
private void CImageButton_Click(object sender, RoutedEventArgs e)
{
CImageButtonClicked?.Invoke(this, new RoutedEventArgs());
}
public void SetSource(string sPath, double nWidth = 0, double nHeight = 0)
{
BitmapImage bitmapImage = new BitmapImage();
Uri uri = new Uri(sPath);
bitmapImage.DecodePixelWidth = (int)((nWidth != 0) ? nWidth : this.ActualWidth);
bitmapImage.DecodePixelHeight = (int)((nHeight != 0) ? nHeight : this.ActualHeight);
bitmapImage.UriSource = uri;
this.Source = bitmapImage;
}
public ImageSource Source
{
get { return (ImageSource)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(ImageSource), typeof(CImageButton), null);
}
}