-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathMainActivity.cs
268 lines (227 loc) · 10 KB
/
MainActivity.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
268
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Numerics;
using Android.App;
using Silk.NET.Input;
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
using Silk.NET.Windowing.Sdl.Android;
namespace AndroidInputDemo
{
/// <summary>
/// Simple demo on how to use handle user input with Silk.
/// </summary>
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : SilkActivity
{
// Instead of IWindow, we use IView.
// IWindow inherits IView, so you can also use this with your desktop code.
private static IView view;
private static GL Gl;
private static BufferObject<float> Vbo;
private static VertexArrayObject<float, uint> Vao;
private static Shader Shader;
private static int maxPoints = 10000;
private static float[] Vertices = new float[7 * maxPoints];
private static Matrix4x4 projection;
private static int counter = 0;
private static IInputContext input;
private static ITouchDevice currentTouchDevice;
private static Gesture trackedGestures = Gesture.All;
private static DoubleTapBehavior doubleTapBehavior = DoubleTapBehavior.EmitFirstSingleTap;
private static MultiGestureHandling multiGestureHandling = MultiGestureHandling.RecognizeBothGestures;
/// <summary>
/// This is where the application starts.
/// Note that when using net6-android, you do not need to have a main method.
/// </summary>
protected override void OnRun()
{
FileManager.AssetManager = Assets;
var options = ViewOptions.Default;
// We need to tell Silk to use OpenGLES
// Version 3.0 is supported by >90% of devices currently in use.
// https://developer.android.com/about/dashboards#OpenGL
options.API = new GraphicsAPI(ContextAPI.OpenGLES, ContextProfile.Compatability, ContextFlags.Default, new APIVersion(3, 0));
view = Silk.NET.Windowing.Window.GetView(options);
view.Load += OnLoad;
view.Render += OnRender;
view.Closing += OnClose;
view.Run();
view.Dispose();
}
private unsafe static void OnLoad()
{
Gl = GL.GetApi(view);
input = view.CreateInput();
input.Mice[0].MouseMove += MouseMove;
input.Mice[0].MouseDown += MouseDown;
input.Keyboards[0].KeyChar += KeyChar;
if (input.PrimaryTouchDevice != null)
SetupTouchDevice(input.PrimaryTouchDevice);
input.ConnectionChanged += (device, connected) =>
{
if (connected && device is ITouchDevice touchDevice)
SetupTouchDevice(touchDevice);
};
Vbo = new BufferObject<float>(Gl, Vertices, BufferTargetARB.ArrayBuffer);
Vao = new VertexArrayObject<float, uint>(Gl, Vbo, null);
Vao.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, 7, 0);
Vao.VertexAttributePointer(1, 4, VertexAttribPointerType.Float, 7, 3);
Shader = new Shader(Gl, "shader.vert", "shader.frag");
projection = Matrix4x4.CreateOrthographic(
view.Size.X,
-view.Size.Y,
1.0f, 2.0f);
}
private static void GestureRecognizer_Rotate(Vector2 position, float angle)
{
DebugLog($"Rotate gesture at {position} with rotation {angle} degree");
}
private static void GestureRecognizer_Zoom(Vector2 position, Vector2 zoom)
{
DebugLog($"Zoom gesture at {position} with zoom {zoom}");
}
private static void GestureRecognizer_Hold(Vector2 position)
{
Gl.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
DebugLog($"Hold gesture at {position}");
}
private static void GestureRecognizer_Swipe(Vector2 direction)
{
DebugLog($"Swipe gesture with direction {direction}");
}
private static void GestureRecognizer_DoubleTap(Vector2 position)
{
Gl.ClearColor(0.0f, 0.0f, 1.0f, 1.0f);
DebugLog($"Double Tap gesture at {position}");
}
private static void GestureRecognizer_Tap(Vector2 position)
{
Gl.ClearColor(0.0f, 1.0f, 0.0f, 1.0f);
DebugLog($"Tap gesture at {position}");
}
private static void KeyChar(IKeyboard arg1, char arg2)
{
if (arg2 == 'c')
Array.Clear(Vertices);
if (arg2 == 'k')
input.Keyboards[0].EndInput();
if (arg2 == 't')
ToggleGesture(Gesture.Tap);
if (arg2 == 'd')
ToggleGesture(Gesture.DoubleTap);
if (arg2 == 's')
ToggleGesture(Gesture.Swipe);
if (arg2 == 'h')
ToggleGesture(Gesture.Hold);
if (arg2 == 'z')
ToggleGesture(Gesture.Zoom);
if (arg2 == 'r')
ToggleGesture(Gesture.Rotate);
if (arg2 == '0')
SetDoubleTapBehavior(DoubleTapBehavior.WaitForDoubleTapTimeElapse);
if (arg2 == '1')
SetDoubleTapBehavior(DoubleTapBehavior.EmitFirstSingleTap);
if (arg2 == '2')
SetDoubleTapBehavior(DoubleTapBehavior.EmitBothSingleTaps);
if (arg2 == '7')
SetMultiGestureHandling(MultiGestureHandling.RecognizeBothGestures);
if (arg2 == '8')
SetMultiGestureHandling(MultiGestureHandling.PrioritizeZoomGesture);
if (arg2 == '9')
SetMultiGestureHandling(MultiGestureHandling.PrioritizeRotateGesture);
}
private static void ToggleGesture(Gesture gesture)
{
if (trackedGestures.HasFlag(gesture))
trackedGestures &= ~gesture;
else
trackedGestures |= gesture;
if (currentTouchDevice?.GestureRecognizer != null)
currentTouchDevice.GestureRecognizer.TrackedGestures = trackedGestures;
}
private static void SetDoubleTapBehavior(DoubleTapBehavior doubleTapBehavior)
{
MainActivity.doubleTapBehavior = doubleTapBehavior;
if (currentTouchDevice?.GestureRecognizer != null)
currentTouchDevice.GestureRecognizer.DoubleTapBehavior = doubleTapBehavior;
}
private static void SetMultiGestureHandling(MultiGestureHandling multiGestureHandling)
{
MainActivity.multiGestureHandling = multiGestureHandling;
if (currentTouchDevice?.GestureRecognizer != null)
currentTouchDevice.GestureRecognizer.MultiGestureHandling = multiGestureHandling;
}
private static void MouseDown(IMouse arg1, MouseButton arg2)
{
// If touched upper 1/3 of the screen
if(arg1.Position.Y < view.Size.Y / 3)
{
// This has to be called to register input, even though keyboard can be opened via other means.
input.Keyboards[0].BeginInput();
}
}
private static void MouseMove(IMouse arg1, Vector2 arg2)
{
// Coordinates need to be transformed from display to be between -1...1
Vector2 point = Vector2.Transform(arg2, projection) - new Vector2(1, -1);
Debug.WriteLine(point);
Vertices[counter++] = point.X; // x
Vertices[counter++] = point.Y; // y
Vertices[counter++] = 0; // z
Vertices[counter++] = point.Y; // R
Vertices[counter++] = point.X; // G
Vertices[counter++] = 1; // B
Vertices[counter++] = 1; // A
if (counter >= maxPoints) counter = 0;
}
private static unsafe void OnRender(double obj)
{
Gl.Clear((uint) ClearBufferMask.ColorBufferBit);
Gl.Enable(GLEnum.ProgramPointSize);
Vbo.UpdateBuffer(0, Vertices);
Vao.Bind();
Shader.Use();
Gl.DrawArrays(GLEnum.Points, 0, (uint)Vertices.Length);
}
private static void OnClose()
{
Vbo.Dispose();
Vao.Dispose();
Shader.Dispose();
}
private static void SetupTouchDevice(ITouchDevice touchDevice)
{
if (currentTouchDevice == touchDevice)
return;
TouchGestureRecognizer gestureRecognizer;
if (currentTouchDevice != null)
{
gestureRecognizer = currentTouchDevice.GestureRecognizer;
gestureRecognizer.Tap -= GestureRecognizer_Tap;
gestureRecognizer.DoubleTap -= GestureRecognizer_DoubleTap;
gestureRecognizer.Swipe -= GestureRecognizer_Swipe;
gestureRecognizer.Hold -= GestureRecognizer_Hold;
gestureRecognizer.Zoom -= GestureRecognizer_Zoom;
gestureRecognizer.Rotate -= GestureRecognizer_Rotate;
}
currentTouchDevice = touchDevice;
gestureRecognizer = currentTouchDevice.GestureRecognizer;
gestureRecognizer.TrackedGestures = trackedGestures;
gestureRecognizer.DoubleTapBehavior = doubleTapBehavior;
gestureRecognizer.MultiGestureHandling = multiGestureHandling;
gestureRecognizer.Tap += GestureRecognizer_Tap;
gestureRecognizer.DoubleTap += GestureRecognizer_DoubleTap;
gestureRecognizer.Swipe += GestureRecognizer_Swipe;
gestureRecognizer.Hold += GestureRecognizer_Hold;
gestureRecognizer.Zoom += GestureRecognizer_Zoom;
gestureRecognizer.Rotate += GestureRecognizer_Rotate;
}
private static void DebugLog(string message)
{
Android.Util.Log.Debug(nameof(AndroidInputDemo), message);
}
}
}