11
11
using System . Windows . Media ;
12
12
using System . Windows . Media . Animation ;
13
13
using System . Windows . Media . Imaging ;
14
+ using System . Windows . Media . Media3D ;
14
15
using System . Windows . Navigation ;
15
16
using System . Windows . Shapes ;
16
17
@@ -21,24 +22,43 @@ namespace AttitudeIndicator
21
22
/// </summary>
22
23
public partial class TouchJoyStick : UserControl
23
24
{
25
+
26
+
27
+ private enum InputState { None , DragXY , DragZ }
28
+
29
+
24
30
public TouchJoyStick ( )
25
31
{
26
32
InitializeComponent ( ) ;
27
33
28
- BasicRect . Background = NormalBackgroundBrush ;
34
+ // For XY Slider:
29
35
30
- this . Slider . TouchDown += new EventHandler < TouchEventArgs > ( TouchableThing_TouchDown ) ;
36
+ XYBasicRect . Background = NormalBackgroundBrush ;
31
37
32
- this . Slider . MouseDown += SliderMouseDown ;
33
- this . BasicRect . MouseDown += SliderMouseDown ;
34
- this . MouseMove += SliderMouseMove ;
38
+ this . XYSlider . TouchDown += new EventHandler < TouchEventArgs > ( TouchableThing_TouchDown ) ;
39
+
40
+ this . XYSlider . MouseDown += ( s , e ) => { mousedodou } ;
41
+
42
+ this . XYBasicRect . MouseDown += SliderMouseDown ;
43
+
44
+ // For Z Slider:
45
+
46
+ ZBasicRect . Background = NormalBackgroundBrush ;
35
47
48
+ this . ZSlider . TouchDown += new EventHandler < TouchEventArgs > ( TouchableThing_TouchDown ) ;
49
+ this . ZSlider . MouseDown += SliderMouseDown ;
50
+ this . ZBasicRect . MouseDown += SliderMouseDown ;
51
+
52
+
53
+
54
+ //Both
55
+ this . MouseMove += SliderMouseMove ;
36
56
this . MouseLeave += SliderMouseLeave ;
37
57
this . MouseUp += SliderMouseLeave ;
38
-
39
58
this . TouchMove += new EventHandler < TouchEventArgs > ( TouchableThing_TouchMove ) ;
40
-
41
59
this . TouchLeave += new EventHandler < TouchEventArgs > ( TouchableThing_TouchLeave ) ;
60
+
61
+
42
62
}
43
63
44
64
ColorAnimation fadein => new ColorAnimation ( NormalBackGroundColor , TouchDownBackGroundColor , new Duration ( TimeSpan . FromMilliseconds ( 300 ) ) ) ;
@@ -62,61 +82,70 @@ protected override void OnRender(DrawingContext drawingContext)
62
82
63
83
private void UIElementTransformer ( )
64
84
{
65
- var translx = XDeflection * 0.49 * ( this . BasicRect . ActualWidth - this . Slider . ActualWidth ) ;
66
- var transly = YDeflection * 0.49 * ( this . BasicRect . ActualHeight - this . Slider . ActualHeight ) ;
85
+ var translx = XDeflection * 0.49 * ( this . XYBasicRect . ActualWidth - this . XYSlider . ActualWidth ) ;
86
+ var transly = YDeflection * 0.49 * ( this . XYBasicRect . ActualHeight - this . XYSlider . ActualHeight ) ;
87
+ this . XYSlider . RenderTransform = new TranslateTransform ( translx , transly ) ;
88
+
89
+ var translz = ZDeflection * 0.49 * ( this . ZBasicRect . ActualHeight - this . ZSlider . ActualHeight ) ;
90
+ this . ZSlider . RenderTransform = new TranslateTransform ( 0.0 , translz ) ;
91
+ }
67
92
68
- TranslateTransform transform = new TranslateTransform ( translx , transly ) ;
69
93
70
- this . Slider . RenderTransform = transform ;
94
+ private void StartAnimation ( InputState from , InputState to )
95
+ {
96
+ if ( to == InputState . DragXY )
97
+ ( ( SolidColorBrush ) XYBasicRect . Background ) . BeginAnimation ( SolidColorBrush . ColorProperty , fadein ) ;
98
+ if ( to == InputState . DragZ )
99
+ ( ( SolidColorBrush ) ZBasicRect . Background ) . BeginAnimation ( SolidColorBrush . ColorProperty , fadein ) ;
100
+
101
+ if ( from == InputState . DragXY )
102
+ ( ( SolidColorBrush ) XYBasicRect . Background ) . BeginAnimation ( SolidColorBrush . ColorProperty , fadeout ) ;
103
+ if ( from == InputState . DragZ )
104
+ ( ( SolidColorBrush ) ZBasicRect . Background ) . BeginAnimation ( SolidColorBrush . ColorProperty , fadeout ) ;
71
105
}
72
106
73
107
74
- private bool isDragging_ ;
75
- private bool isDragging
108
+ private InputState _dragstate = InputState . None ;
109
+
110
+ private InputState DragState
76
111
{
77
- get => isDragging_ ;
112
+ get => _dragstate ;
78
113
set
79
114
{
80
- if ( value == isDragging_ )
115
+ if ( value == _dragstate )
81
116
return ;
82
- isDragging_ = value ;
83
117
84
- if ( value )
85
- {
86
- ( ( SolidColorBrush ) BasicRect . Background ) . BeginAnimation ( SolidColorBrush . ColorProperty ,
87
- fadein ) ;
88
- }
89
- else
90
- {
91
- ( ( SolidColorBrush ) BasicRect . Background ) . BeginAnimation ( SolidColorBrush . ColorProperty ,
92
- fadeout ) ;
93
- XDeflection = 0 ;
94
- YDeflection = 0 ;
95
- }
118
+ StartAnimation ( _dragstate , value ) ;
119
+
120
+ _dragstate = value ;
96
121
97
122
}
98
123
}
99
124
125
+
126
+
127
+
100
128
private void TouchableThing_TouchDown ( object sender , TouchEventArgs e )
101
129
{
102
- isDragging = true ;
103
130
104
131
}
105
132
106
133
107
134
private void SliderMouseDown ( object sender , MouseEventArgs e )
108
135
{
109
- isDragging = true ;
110
136
111
- var myPoint = ( e as MouseEventArgs ) . GetPosition ( this . BasicRect ) ;
137
+ var obj = ( sender as Control ) ;
138
+
139
+
140
+ var myPoint = ( e as MouseEventArgs ) . GetPosition ( this . XYBasicRect ) ;
112
141
113
142
CalcDeflectionFromPoint ( myPoint ) ;
114
143
115
144
}
116
145
117
146
private void SliderMouseMove ( object sender , MouseEventArgs e )
118
147
{
119
- if ( ! isDragging )
148
+ if ( )
120
149
return ;
121
150
122
151
var myPoint = ( e as MouseEventArgs ) . GetPosition ( this . BasicRect ) ;
@@ -214,18 +243,42 @@ public double YDeflection
214
243
}
215
244
216
245
246
+ /// <summary>
247
+ /// Deflection in the second direction
248
+ /// </summary>
249
+ public static readonly DependencyProperty ZDeflectionProperty =
250
+ DependencyProperty . Register ( nameof ( ZDeflection ) ,
251
+ typeof ( double ) ,
252
+ typeof ( TouchJoyStick ) ,
253
+ new FrameworkPropertyMetadata ( 0d , new PropertyChangedCallback ( DeflectionChanged ) ) ) ;
254
+
255
+ public double ZDeflection
256
+ {
257
+ get => ( double ) GetValue ( ZDeflectionProperty ) ;
258
+ set
259
+ {
260
+ double deflection ;
261
+ if ( value > 1 )
262
+ deflection = 1 ;
263
+ else if ( value < - 1 )
264
+ deflection = - 1 ;
265
+ else
266
+ deflection = value ;
267
+
268
+ SetValue ( ZDeflectionProperty , deflection ) ;
269
+ }
270
+ }
271
+
272
+
217
273
public static void DeflectionChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
218
274
{
219
275
var obj = ( d as TouchJoyStick ) ;
220
276
obj . UIElementTransformer ( ) ;
221
277
222
- obj . Command ? . Execute ( new Point ( obj . XDeflection , obj . YDeflection ) ) ;
223
-
224
- obj . CommandX ? . Execute ( obj . XDeflection ) ;
225
- obj . CommandY ? . Execute ( obj . YDeflection ) ;
226
- // do nothing for now
278
+ obj . Command ? . Execute ( new Vector3D ( obj . XDeflection , obj . YDeflection , obj . ZDeflection ) ) ;
227
279
}
228
280
281
+
229
282
public static readonly DependencyProperty CommandProperty =
230
283
DependencyProperty . Register (
231
284
"Command" ,
@@ -240,39 +293,6 @@ public ICommand Command
240
293
}
241
294
242
295
243
- //obj.Command?.Execute(obj.Deflection);
244
-
245
-
246
-
247
- public static readonly DependencyProperty CommandXProperty =
248
- DependencyProperty . Register (
249
- nameof ( CommandX ) ,
250
- typeof ( ICommand ) ,
251
- typeof ( TouchJoyStick ) ,
252
- new UIPropertyMetadata ( null ) ) ;
253
-
254
- public ICommand CommandX
255
- {
256
- get { return ( ICommand ) GetValue ( CommandXProperty ) ; }
257
- set { SetValue ( CommandXProperty , value ) ; }
258
- }
259
-
260
-
261
- public static readonly DependencyProperty CommandYProperty =
262
- DependencyProperty . Register (
263
- nameof ( CommandY ) ,
264
- typeof ( ICommand ) ,
265
- typeof ( TouchJoyStick ) ,
266
- new UIPropertyMetadata ( null ) ) ;
267
-
268
- public ICommand CommandY
269
- {
270
- get { return ( ICommand ) GetValue ( CommandYProperty ) ; }
271
- set { SetValue ( CommandYProperty , value ) ; }
272
- }
273
-
274
-
275
-
276
296
#endregion DependencyProperty
277
297
}
278
298
}
0 commit comments