Repository navigation
Expand file tree
/
Copy pathMainActivity.java
More file actions
371 lines (311 loc) · 11.7 KB
/
Copy pathMainActivity.java
File metadata and controls
371 lines (311 loc) · 11.7 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package TARGET_PACKAGE_NAME;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.os.Bundle;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Surface;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager.LayoutParams;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Color;
import android.graphics.Insets;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;
import quad_native.QuadNative;
// note: //% is a special miniquad's pre-processor for plugins
// when there are no plugins - //% whatever will be replaced to an empty string
// before compiling
//% IMPORTS
class QuadSurface
extends
SurfaceView
implements
View.OnTouchListener,
View.OnKeyListener,
SurfaceHolder.Callback {
public QuadSurface(Context context){
super(context);
getHolder().addCallback(this);
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
setOnTouchListener(this);
setOnKeyListener(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("SAPP", "surfaceCreated");
Surface surface = holder.getSurface();
QuadNative.surfaceOnSurfaceCreated(surface);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("SAPP", "surfaceDestroyed");
Surface surface = holder.getSurface();
QuadNative.surfaceOnSurfaceDestroyed(surface);
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format,
int width,
int height) {
Log.i("SAPP", "surfaceChanged");
Surface surface = holder.getSurface();
QuadNative.surfaceOnSurfaceChanged(surface, width, height);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int pointerCount = event.getPointerCount();
int action = event.getActionMasked();
switch(action) {
case MotionEvent.ACTION_MOVE: {
for (int i = 0; i < pointerCount; i++) {
final int id = event.getPointerId(i);
final float x = event.getX(i);
final float y = event.getY(i);
QuadNative.surfaceOnTouch(id, 0, x, y);
}
break;
}
case MotionEvent.ACTION_UP: {
final int id = event.getPointerId(0);
final float x = event.getX(0);
final float y = event.getY(0);
QuadNative.surfaceOnTouch(id, 1, x, y);
break;
}
case MotionEvent.ACTION_DOWN: {
final int id = event.getPointerId(0);
final float x = event.getX(0);
final float y = event.getY(0);
QuadNative.surfaceOnTouch(id, 2, x, y);
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = event.getActionIndex();
final int id = event.getPointerId(pointerIndex);
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);
QuadNative.surfaceOnTouch(id, 1, x, y);
break;
}
case MotionEvent.ACTION_POINTER_DOWN: {
final int pointerIndex = event.getActionIndex();
final int id = event.getPointerId(pointerIndex);
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);
QuadNative.surfaceOnTouch(id, 2, x, y);
break;
}
case MotionEvent.ACTION_CANCEL: {
for (int i = 0; i < pointerCount; i++) {
final int id = event.getPointerId(i);
final float x = event.getX(i);
final float y = event.getY(i);
QuadNative.surfaceOnTouch(id, 3, x, y);
}
break;
}
default:
break;
}
return true;
}
// docs says getCharacters are deprecated
// but somehow on non-latyn input all keyCode and all the relevant fields in the KeyEvent are zeros
// and only getCharacters has some usefull data
@SuppressWarnings("deprecation")
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode != 0) {
QuadNative.surfaceOnKeyDown(keyCode);
}
if (event.getAction() == KeyEvent.ACTION_UP && keyCode != 0) {
QuadNative.surfaceOnKeyUp(keyCode);
}
if (event.getAction() == KeyEvent.ACTION_UP || event.getAction() == KeyEvent.ACTION_MULTIPLE) {
int character = event.getUnicodeChar();
if (character == 0) {
String characters = event.getCharacters();
if (characters != null && !characters.isEmpty()) {
character = characters.charAt(0);
}
}
if (character != 0) {
QuadNative.surfaceOnCharacter(character);
}
}
return true;
}
// There is an Android bug when screen is in landscape,
// the keyboard inset height is reported as 0.
// This code is a workaround which fixes the bug.
// See https://groups.google.com/g/android-developers/c/50XcWooqk7I
// For some reason it only works if placed here and not in the parent layout.
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
//% QUAD_SURFACE_ON_CREATE_INPUT_CONNECTION
InputConnection connection = super.onCreateInputConnection(outAttrs);
outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_FULLSCREEN;
return connection;
}
public Surface getNativeSurface() {
return getHolder().getSurface();
}
}
class ResizingLayout
extends
LinearLayout
implements
View.OnApplyWindowInsetsListener {
public ResizingLayout(Context context){
super(context);
// When viewing in landscape mode with keyboard shown, there are
// gaps on both sides so we fill the negative space with black.
setBackgroundColor(Color.BLACK);
setOnApplyWindowInsetsListener(this);
}
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (Build.VERSION.SDK_INT >= 30) {
Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
Insets sysInsets = insets.getInsets(WindowInsets.Type.systemBars());
int bottomPadding = sysInsets.bottom;
if (imeInsets.bottom > 0) {
bottomPadding = imeInsets.bottom;
}
// The sys insets change when orientation changes and sys bars
// change position.
v.setPadding(
sysInsets.left,
sysInsets.top,
sysInsets.right,
bottomPadding
);
}
return insets;
}
}
public class MainActivity extends Activity {
//% MAIN_ACTIVITY_BODY
private QuadSurface view;
static {
System.loadLibrary("LIBRARY_NAME");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
view = new QuadSurface(this);
// Put it inside a parent layout which can resize it using padding
ResizingLayout layout = new ResizingLayout(this);
layout.addView(view);
setContentView(layout);
QuadNative.activityOnCreate(this);
//% MAIN_ACTIVITY_ON_CREATE
}
@Override
protected void onResume() {
super.onResume();
QuadNative.activityOnResume();
//% MAIN_ACTIVITY_ON_RESUME
}
@Override
public void onBackPressed() {
Log.w("SAPP", "onBackPressed");
// TODO: here is the place to handle request_quit/order_quit/cancel_quit
super.onBackPressed();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
QuadNative.activityOnDestroy();
}
@Override
protected void onPause() {
super.onPause();
QuadNative.activityOnPause();
//% MAIN_ACTIVITY_ON_PAUSE
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//% MAIN_ACTIVITY_ON_ACTIVITY_RESULT
}
public void setFullScreen(final boolean fullscreen) {
runOnUiThread(new Runnable() {
@Override
public void run() {
View decorView = getWindow().getDecorView();
if (fullscreen) {
getWindow().setFlags(LayoutParams.FLAG_LAYOUT_NO_LIMITS, LayoutParams.FLAG_LAYOUT_NO_LIMITS);
if (Build.VERSION.SDK_INT >= 28) {
getWindow().getAttributes().layoutInDisplayCutoutMode = LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
if (Build.VERSION.SDK_INT >= 30) {
getWindow().setDecorFitsSystemWindows(false);
} else {
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
else {
if (Build.VERSION.SDK_INT >= 30) {
getWindow().setDecorFitsSystemWindows(true);
} else {
decorView.setSystemUiVisibility(0);
}
}
}
});
}
public void showKeyboard(final boolean show) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (show) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, 0);
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
}
});
}
public String getClipboardText() {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (!clipboard.hasPrimaryClip())
return null;
ClipData primaryClip = clipboard.getPrimaryClip();
if (primaryClip == null || primaryClip.getItemCount() < 1)
return null;
CharSequence clipData = clipboard.getPrimaryClip().getItemAt(0).getText();
if (clipData == null) {
return null;
}
return clipData.toString();
}
public void setClipboardText(String text) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", text);
clipboard.setPrimaryClip(clip);
}
}