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
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.SharedUI.Helpers;
namespace Tango.Touch.Components
{
[ContentProperty(nameof(Content))]
[TemplatePart(Name = PART_EllipseName, Type = typeof(Ellipse))]
[TemplatePart(Name = PART_ScaleName, Type = typeof(ScaleTransform))]
public class Ripple : Control
{
private const String PART_ScaleName = "PART_scale";
private const String PART_EllipseName = "PART_ellipse";
private ScaleTransform _scale;
private Ellipse _ellipse;
private bool _isAnimating;
public UIElement Content
{
get { return (UIElement)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(UIElement), typeof(Ripple), new PropertyMetadata(null));
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(Ripple), new PropertyMetadata(new CornerRadius()));
public Brush RippleBrush
{
get { return (Brush)GetValue(RippleBrushProperty); }
set { SetValue(RippleBrushProperty, value); }
}
public static readonly DependencyProperty RippleBrushProperty =
DependencyProperty.Register("RippleBrush", typeof(Brush), typeof(Ripple), new PropertyMetadata(null));
public ScrollViewer SynchedScrollViewer
{
get { return (ScrollViewer)GetValue(SynchedScrollViewerProperty); }
set { SetValue(SynchedScrollViewerProperty, value); }
}
public static readonly DependencyProperty SynchedScrollViewerProperty =
DependencyProperty.Register("SynchedScrollViewer", typeof(ScrollViewer), typeof(Ripple), new PropertyMetadata(null));
static Ripple()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Ripple), new FrameworkPropertyMetadata(typeof(Ripple)));
}
public Ripple()
{
this.Loaded += Ripple_Loaded;
}
private void Ripple_Loaded(object sender, RoutedEventArgs e)
{
if (SynchedScrollViewer != null)
{
SynchedScrollViewer.ScrollChanged += (_, __) => CancelRipple(true);
}
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_scale = GetTemplateChild(PART_ScaleName) as ScaleTransform;
_ellipse = GetTemplateChild(PART_EllipseName) as Ellipse;
FrameworkElement parent = UIHelper.FindAncestor<Button>(this);
if (parent == null)
{
parent = UIHelper.FindAncestor<ListBoxItem>(this);
}
if (parent == null)
{
parent = UIHelper.FindAncestor<FrameworkElement>(this);
}
//parent.PreviewTouchDown += Parent_PreviewTouchDown;
//parent.TouchMove += (_, __) => CancelRipple();
//parent.PreviewTouchUp += Parent_PreviewTouchUp;
//parent.PreviewMouseDown += Parent_PreviewMouseDown;
parent.PreviewTouchDown += Parent_PreviewTouchDown;
//parent.TouchMove += (_, __) => CancelRipple();
//parent.PreviewMouseUp += Parent_PreviewMouseUp;
parent.PreviewTouchUp += Parent_PreviewTouchUp;
}
private void Parent_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
OnRippleUp(new Point(e.GetPosition(this).X, e.GetPosition(this).Y));
}
private void Parent_PreviewTouchUp(object sender, TouchEventArgs e)
{
OnRippleUp(new Point(e.GetTouchPoint(this).Position.X, e.GetTouchPoint(this).Position.Y));
}
private void Parent_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
OnRippleDown(new Point(e.GetPosition(this).X, e.GetPosition(this).Y));
}
private void Parent_PreviewTouchDown(object sender, TouchEventArgs e)
{
OnRippleDown(new Point(e.GetTouchPoint(this).Position.X, e.GetTouchPoint(this).Position.Y));
}
private void CancelRipple(bool immidiate = false)
{
if (_isAnimating)
{
_isAnimating = false;
DoubleAnimation ani = new DoubleAnimation();
ani.To = 0;
ani.Duration = TimeSpan.FromSeconds(immidiate ? 0 : 0.1);
_scale.BeginAnimation(ScaleTransform.ScaleXProperty, ani);
_scale.BeginAnimation(ScaleTransform.ScaleYProperty, ani);
}
}
private void OnRippleUp(Point position)
{
if (!(position.X >= 0 && position.X <= ActualWidth && position.Y >= 0 && position.Y <= ActualHeight))
{
CancelRipple();
}
}
private void OnRippleDown(Point position)
{
_isAnimating = true;
Canvas.SetLeft(_ellipse, position.X - (_ellipse.Width / 2));
Canvas.SetTop(_ellipse, position.Y - (_ellipse.Height / 2));
DoubleAnimation ani = new DoubleAnimation();
ani.From = 0;
ani.To = 5;
ani.AutoReverse = true;
ani.Duration = TimeSpan.FromSeconds(0.5);
ani.AccelerationRatio = 0.2;
ani.DecelerationRatio = 0.2;
_scale.BeginAnimation(ScaleTransform.ScaleXProperty, ani);
_scale.BeginAnimation(ScaleTransform.ScaleYProperty, ani);
}
}
}
|