Skip to content

Commit 064e4a0

Browse files
committed
WIP Touch Joystick Control
1 parent a545a6a commit 064e4a0

File tree

5 files changed

+276
-84
lines changed

5 files changed

+276
-84
lines changed

AttitudeIndicator/MainWindow.xaml

+13-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@
9595
</ToggleButton.ToolTip>
9696
<fa:ImageAwesome Icon="MoonOutline" Height="20"/>
9797
</ToggleButton>
98-
98+
<ComboBox Width="120" Margin="3" DockPanel.Dock="Left"
99+
SelectedItem="{Binding SelectedMode}"
100+
ItemsSource="{Binding ModeList}">
101+
<ComboBox.ItemTemplate>
102+
<DataTemplate>
103+
<TextBlock Text="{Binding Name}"/>
104+
</DataTemplate>
105+
</ComboBox.ItemTemplate>
106+
</ComboBox>
99107
</DockPanel>
100108

101109

@@ -152,9 +160,11 @@
152160
<local:TouchJoyStick DockPanel.Dock="Left"
153161
HorizontalAlignment="Left"
154162
VerticalAlignment="Bottom"
163+
Visibility="{Binding JoyStickVisibility}"
155164
Foreground="{DynamicResource ThemeForeground}"
156-
CommandX="{Binding Rotator.JogX}"
157-
CommandY="{Binding Rotator.JogY}"/>
165+
Command="{Binding Rotator}"
166+
Margin="-40"
167+
/>
158168

159169
</Grid>
160170
</Window>

AttitudeIndicator/TouchJoyStick.xaml

+40-8
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,76 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:AttitudeIndicator"
77
MinHeight="320"
8-
MinWidth="320"
8+
MinWidth="420"
99
mc:Ignorable="d">
1010
<Grid Background="Transparent">
1111
<Grid.Resources>
1212
<GridLength x:Key="BoundingSize">60</GridLength>
1313
</Grid.Resources>
14+
1415
<Grid.RowDefinitions>
1516
<RowDefinition Height="{Binding Source={StaticResource BoundingSize}}" />
1617
<RowDefinition Height="*" />
1718
<RowDefinition Height="{Binding Source={StaticResource BoundingSize}}" />
1819
</Grid.RowDefinitions>
1920
<Grid.ColumnDefinitions>
2021
<ColumnDefinition Width="{Binding Source={StaticResource BoundingSize}}" />
21-
<ColumnDefinition Width="*" />
22+
<ColumnDefinition Width="1*" />
23+
<ColumnDefinition Width="2*" />
2224
<ColumnDefinition Width="{Binding Source={StaticResource BoundingSize}}" />
2325
</Grid.ColumnDefinitions>
2426

27+
<!--Just a test -->
28+
<!--<Canvas Grid.ColumnSpan="2" Grid.RowSpan="2">
29+
<Path Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=UserControl}}"
30+
31+
StrokeThickness="3"
32+
Data="M 10,100 A 90,90 90 0 1 100,10 A 90,90 90 0 1 100,10 ">
33+
</Path>
34+
</Canvas>-->
35+
36+
2537
<Border
26-
x:Name="BasicRect"
38+
x:Name="ZBasicRect"
2739
Grid.Row="1"
2840
Grid.Column="1"
29-
CornerRadius="25"
30-
41+
Margin="15 0 15 0"
42+
CornerRadius="30"
3143
BorderBrush="{Binding Foreground, RelativeSource={RelativeSource AncestorType=UserControl}}"
32-
3344
BorderThickness="3" />
3445

3546
<Border
36-
x:Name="Slider"
47+
x:Name="ZSlider"
3748
Grid.Row="1"
3849
Grid.Column="1"
50+
51+
Height="80"
52+
Margin="19 4 19 4"
53+
Background="LightGray"
54+
BorderBrush="{Binding Foreground, RelativeSource={RelativeSource AncestorType=UserControl}}"
55+
BorderThickness="2"
56+
CornerRadius="30"
57+
/>
58+
59+
<Border
60+
x:Name="XYBasicRect"
61+
Grid.Row="1"
62+
Grid.Column="2"
63+
CornerRadius="30"
64+
BorderBrush="{Binding Foreground, RelativeSource={RelativeSource AncestorType=UserControl}}"
65+
BorderThickness="3" />
66+
67+
<Border
68+
x:Name="XYSlider"
69+
Grid.Row="1"
70+
Grid.Column="2"
3971
Width="80"
4072
Height="80"
4173
Margin="4"
4274
Background="LightGray"
4375
BorderBrush="{Binding Foreground, RelativeSource={RelativeSource AncestorType=UserControl}}"
4476
BorderThickness="2"
45-
CornerRadius="25"
77+
CornerRadius="30"
4678
>
4779

4880
<!--CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}}">-->

AttitudeIndicator/TouchJoyStick.xaml.cs

+90-70
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Windows.Media;
1212
using System.Windows.Media.Animation;
1313
using System.Windows.Media.Imaging;
14+
using System.Windows.Media.Media3D;
1415
using System.Windows.Navigation;
1516
using System.Windows.Shapes;
1617

@@ -21,24 +22,43 @@ namespace AttitudeIndicator
2122
/// </summary>
2223
public partial class TouchJoyStick : UserControl
2324
{
25+
26+
27+
private enum InputState { None, DragXY, DragZ}
28+
29+
2430
public TouchJoyStick()
2531
{
2632
InitializeComponent();
2733

28-
BasicRect.Background= NormalBackgroundBrush;
34+
// For XY Slider:
2935

30-
this.Slider.TouchDown += new EventHandler<TouchEventArgs>(TouchableThing_TouchDown);
36+
XYBasicRect.Background= NormalBackgroundBrush;
3137

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;
3547

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;
3656
this.MouseLeave += SliderMouseLeave;
3757
this.MouseUp += SliderMouseLeave;
38-
3958
this.TouchMove += new EventHandler<TouchEventArgs>(TouchableThing_TouchMove);
40-
4159
this.TouchLeave += new EventHandler<TouchEventArgs>(TouchableThing_TouchLeave);
60+
61+
4262
}
4363

4464
ColorAnimation fadein => new ColorAnimation(NormalBackGroundColor, TouchDownBackGroundColor, new Duration(TimeSpan.FromMilliseconds(300)));
@@ -62,61 +82,70 @@ protected override void OnRender(DrawingContext drawingContext)
6282

6383
private void UIElementTransformer()
6484
{
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+
}
6792

68-
TranslateTransform transform = new TranslateTransform(translx, transly);
6993

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);
71105
}
72106

73107

74-
private bool isDragging_;
75-
private bool isDragging
108+
private InputState _dragstate = InputState.None;
109+
110+
private InputState DragState
76111
{
77-
get => isDragging_;
112+
get => _dragstate;
78113
set
79114
{
80-
if (value == isDragging_)
115+
if (value == _dragstate)
81116
return;
82-
isDragging_ = value;
83117

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;
96121

97122
}
98123
}
99124

125+
126+
127+
100128
private void TouchableThing_TouchDown(object sender, TouchEventArgs e)
101129
{
102-
isDragging = true;
103130

104131
}
105132

106133

107134
private void SliderMouseDown(object sender, MouseEventArgs e)
108135
{
109-
isDragging = true;
110136

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);
112141

113142
CalcDeflectionFromPoint(myPoint);
114143

115144
}
116145

117146
private void SliderMouseMove(object sender, MouseEventArgs e)
118147
{
119-
if (!isDragging)
148+
if()
120149
return;
121150

122151
var myPoint = (e as MouseEventArgs).GetPosition(this.BasicRect);
@@ -214,18 +243,42 @@ public double YDeflection
214243
}
215244

216245

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+
217273
public static void DeflectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
218274
{
219275
var obj = (d as TouchJoyStick);
220276
obj.UIElementTransformer();
221277

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));
227279
}
228280

281+
229282
public static readonly DependencyProperty CommandProperty =
230283
DependencyProperty.Register(
231284
"Command",
@@ -240,39 +293,6 @@ public ICommand Command
240293
}
241294

242295

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-
276296
#endregion DependencyProperty
277297
}
278298
}

0 commit comments

Comments
 (0)