-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionConfigurationActivity.cs
203 lines (175 loc) · 7.31 KB
/
MotionConfigurationActivity.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
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using System;
using Android.Util;
using Android.Graphics;
using Android.Views;
using Android.Content.PM;
using static Android.Views.View;
using Android.Widget;
using SoftWing.SwSystem;
namespace SoftWing
{
[Activity(Theme = "@style/AppTheme", ScreenOrientation = ScreenOrientation.Landscape, LaunchMode = LaunchMode.SingleTask)]
public class MotionConfigurationActivity : AppCompatActivity, IOnTouchListener
{
private const String TAG = "MotionConfigurationActivity";
private const int SURFACE_RADIUS_OUTER = 120;
private const int SURFACE_RADIUS_INNER = 100;
private const int STROKE_WIDTH_OUTER = 50;
private const int STROKE_WIDTH_INNER = 30;
private ISurfaceHolder surfaceHolder = null;
private Paint surfacePaintOuter = new Paint(PaintFlags.AntiAlias);
private Paint surfacePaintInner = new Paint(PaintFlags.AntiAlias);
private MotionDescription motion = MotionDescription.InvalidMotion();
public static Android.Net.Uri BackgroundImageUri = null;
public static ControlId control;
public static MotionType motionType = MotionType.Invalid;
protected override void OnCreate(Bundle savedInstanceState)
{
Log.Debug(TAG, "OnCreate()");
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
RequestWindowFeature(WindowFeatures.NoTitle);
var uiOptions = SystemUiFlags.HideNavigation |
SystemUiFlags.LayoutHideNavigation |
SystemUiFlags.LayoutFullscreen |
SystemUiFlags.Fullscreen |
SystemUiFlags.LayoutStable |
SystemUiFlags.ImmersiveSticky;
#pragma warning disable CS0618 // Type or member is obsolete
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
#pragma warning restore CS0618 // Type or member is obsolete
surfacePaintOuter.StrokeWidth = STROKE_WIDTH_OUTER;
surfacePaintOuter.Color = Color.Black;
surfacePaintInner.StrokeWidth = STROKE_WIDTH_INNER;
surfacePaintInner.Color = Color.White;
SetContentView(Resource.Layout.motion_configuration);
}
protected override void OnStart()
{
base.OnStart();
var motionSurface = FindViewById<ImageView>(Resource.Id.motionConfigurationImage);
motionSurface.SetImageURI(BackgroundImageUri);
ConfigureMotionDrawSurface();
if (multiplePointsRequired())
{
PromptUserForSwipeBegin();
}
else
{
PromptUserForTap();
}
}
private bool multiplePointsRequired()
{
return (motionType != MotionType.Tap) || SwSettings.IsAnalogControl(control);
}
private void PromptUserForSwipeBegin()
{
Log.Debug(TAG, "PromptUserForSwipeBegin()");
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
var alert = dialog.Create();
alert.SetTitle("Select Touch Origin");
var message = "Tap the origin point for the touch action.\nThis could be the center of a joystick, or the beginning of a swpie motion\n";
alert.SetMessage(message);
alert.SetButton("Continue", (c, ev) => { });
alert.Show();
}
private void PromptUserForSwipeEnd()
{
Log.Debug(TAG, "PromptUserForSwipeEnd()");
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
var alert = dialog.Create();
alert.SetTitle("Select Touch End");
var message = "Tap the end point for the touch action.\nThis could be the outer edge of a joystick, or the end of a swpie motion\n";
alert.SetMessage(message);
alert.SetButton("Continue", (c, ev) => { });
alert.Show();
}
private void PromptUserForTap()
{
Log.Debug(TAG, "PromptUserForTap()");
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
var alert = dialog.Create();
alert.SetTitle("Select Touch Point");
var message = "Tap the point on the screen where you want the tap action to occur\n";
alert.SetMessage(message);
alert.SetButton("Continue", (c, ev) => { });
alert.Show();
}
private void ConfigureMotionDrawSurface()
{
var motionSurface = FindViewById<SurfaceView>(Resource.Id.motionConfigurationSurface);
motionSurface.SetOnTouchListener(this);
motionSurface.SetZOrderOnTop(true);
surfaceHolder = motionSurface.Holder;
surfaceHolder.SetFormat(Format.Transparent);
}
private void MovePointMarker(MotionEvent e)
{
var canvas = surfaceHolder.LockCanvas();
canvas.DrawColor(0, BlendMode.Clear);
if (motion.beginX > -1)
{
canvas.DrawLine(motion.beginX, motion.beginY, e.GetX(), e.GetY(), surfacePaintOuter);
canvas.DrawLine(motion.beginX, motion.beginY, e.GetX(), e.GetY(), surfacePaintInner);
canvas.DrawCircle(motion.beginX, motion.beginY, SURFACE_RADIUS_OUTER / 2, surfacePaintOuter);
canvas.DrawCircle(motion.beginX, motion.beginY, SURFACE_RADIUS_INNER / 2, surfacePaintInner);
}
if (e.Action != MotionEventActions.Up)
{
canvas.DrawCircle(e.GetX(), e.GetY(), SURFACE_RADIUS_OUTER, surfacePaintOuter);
canvas.DrawCircle(e.GetX(), e.GetY(), SURFACE_RADIUS_INNER, surfacePaintInner);
}
surfaceHolder.UnlockCanvasAndPost(canvas);
}
private void CommitMotionAndFinish()
{
SwSettings.SetControlMotion(control, motion);
Finish();
}
private void EndPointSetting(MotionEvent e)
{
motion.type = motionType;
if (motion.beginX == -1)
{
motion.beginX = e.GetX();
motion.beginY = e.GetY();
if (multiplePointsRequired())
{
PromptUserForSwipeEnd();
}
else
{
motion.endX = motion.beginX;
motion.endY = motion.beginY;
CommitMotionAndFinish();
}
}
else
{
motion.endX = e.GetX();
motion.endY = e.GetY();
CommitMotionAndFinish();
}
}
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Up:
Log.Info(TAG, "OnTouch - Up");
EndPointSetting(e);
break;
default:
Log.Info(TAG, "OnTouch - Other");
break;
}
MovePointMarker(e);
return true;
}
}
}