/
IOSApplication.java
464 lines (388 loc) · 13.7 KB
/
IOSApplication.java
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.backends.iosrobovm;
import java.io.File;
import com.badlogic.gdx.ApplicationLogger;
import com.badlogic.gdx.backends.iosrobovm.objectal.OALIOSAudio;
import org.robovm.apple.coregraphics.CGRect;
import org.robovm.apple.foundation.NSMutableDictionary;
import org.robovm.apple.foundation.NSObject;
import org.robovm.apple.foundation.NSProcessInfo;
import org.robovm.apple.foundation.NSString;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIApplicationDelegateAdapter;
import org.robovm.apple.uikit.UIApplicationLaunchOptions;
import org.robovm.apple.uikit.UIDevice;
import org.robovm.apple.uikit.UIInterfaceOrientation;
import org.robovm.apple.uikit.UIPasteboard;
import org.robovm.apple.uikit.UIScreen;
import org.robovm.apple.uikit.UIViewController;
import org.robovm.apple.uikit.UIWindow;
import org.robovm.rt.bro.Bro;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Audio;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.LifecycleListener;
import com.badlogic.gdx.Net;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.backends.iosrobovm.objectal.OALAudioSession;
import com.badlogic.gdx.backends.iosrobovm.objectal.OALSimpleAudio;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Clipboard;
public class IOSApplication implements Application {
public static abstract class Delegate extends UIApplicationDelegateAdapter {
private IOSApplication app;
protected abstract IOSApplication createApplication ();
@Override
public boolean didFinishLaunching (UIApplication application, UIApplicationLaunchOptions launchOptions) {
application.addStrongRef(this); // Prevent this from being GCed until the ObjC UIApplication is deallocated
this.app = createApplication();
return app.didFinishLaunching(application, launchOptions);
}
@Override
public void didBecomeActive (UIApplication application) {
app.didBecomeActive(application);
}
@Override
public void willEnterForeground (UIApplication application) {
app.willEnterForeground(application);
}
@Override
public void willResignActive (UIApplication application) {
app.willResignActive(application);
}
@Override
public void willTerminate (UIApplication application) {
app.willTerminate(application);
}
}
UIApplication uiApp;
UIWindow uiWindow;
ApplicationListener listener;
IOSViewControllerListener viewControllerListener;
IOSApplicationConfiguration config;
IOSGraphics graphics;
IOSAudio audio;
IOSFiles files;
IOSInput input;
IOSNet net;
int logLevel = Application.LOG_DEBUG;
ApplicationLogger applicationLogger;
/** The display scale factor (1.0f for normal; 2.0f to use retina coordinates/dimensions). */
float pixelsPerPoint;
private IOSScreenBounds lastScreenBounds = null;
Array<Runnable> runnables = new Array<Runnable>();
Array<Runnable> executedRunnables = new Array<Runnable>();
Array<LifecycleListener> lifecycleListeners = new Array<LifecycleListener>();
public IOSApplication (ApplicationListener listener, IOSApplicationConfiguration config) {
this.listener = listener;
this.config = config;
}
final boolean didFinishLaunching (UIApplication uiApp, UIApplicationLaunchOptions options) {
setApplicationLogger(new IOSApplicationLogger());
Gdx.app = this;
this.uiApp = uiApp;
// enable or disable screen dimming
UIApplication.getSharedApplication().setIdleTimerDisabled(config.preventScreenDimming);
Gdx.app.debug("IOSApplication", "iOS version: " + UIDevice.getCurrentDevice().getSystemVersion());
Gdx.app.debug("IOSApplication", "Running in " + (Bro.IS_64BIT ? "64-bit" : "32-bit") + " mode");
// iOS counts in "points" instead of pixels. Points are logical pixels
pixelsPerPoint = (float)UIScreen.getMainScreen().getNativeScale();
Gdx.app.debug("IOSApplication", "Pixels per point: " + pixelsPerPoint);
// setup libgdx
this.input = createInput();
this.graphics = createGraphics();
Gdx.gl = Gdx.gl20 = graphics.gl20;
Gdx.gl30 = graphics.gl30;
this.files = new IOSFiles();
this.audio = createAudio(config);
this.net = new IOSNet(this, config);
Gdx.files = this.files;
Gdx.graphics = this.graphics;
Gdx.audio = this.audio;
Gdx.input = this.input;
Gdx.net = this.net;
this.input.setupPeripherals();
this.uiWindow = new UIWindow(UIScreen.getMainScreen().getBounds());
this.uiWindow.setRootViewController(this.graphics.viewController);
this.uiWindow.makeKeyAndVisible();
Gdx.app.debug("IOSApplication", "created");
return true;
}
protected IOSAudio createAudio (IOSApplicationConfiguration config) {
return new OALIOSAudio(config);
}
protected IOSGraphics createGraphics() {
return new IOSGraphics(this, config, input, config.useGL30);
}
protected IOSGraphics.IOSUIViewController createUIViewController (IOSGraphics graphics) {
return new IOSGraphics.IOSUIViewController(this, graphics);
}
protected IOSInput createInput() {
return new DefaultIOSInput(this);
}
/** Return the UI view controller of IOSApplication
* @return the view controller of IOSApplication */
public UIViewController getUIViewController () {
return graphics.viewController;
}
/** Return the UI Window of IOSApplication
* @return the window */
public UIWindow getUIWindow () {
return uiWindow;
}
/** @see IOSScreenBounds for detailed explanation
* @return logical dimensions of space we draw to, adjusted for device orientation */
protected IOSScreenBounds computeBounds () {
final CGRect screenBounds = UIScreen.getMainScreen().getBounds();
final CGRect statusBarFrame = uiApp.getStatusBarFrame();
final UIInterfaceOrientation statusBarOrientation = uiApp.getStatusBarOrientation();
double statusBarHeight = Math.min(statusBarFrame.getWidth(), statusBarFrame.getHeight());
double screenWidth = screenBounds.getWidth();
double screenHeight = screenBounds.getHeight();
// Make sure that the orientation is consistent with ratios. Should be, but may not be on older iOS versions
switch (statusBarOrientation) {
case LandscapeLeft:
case LandscapeRight:
if (screenHeight > screenWidth) {
debug("IOSApplication", "Switching reported width and height (original was w=" + screenWidth + " h="
+ screenHeight + ")");
double tmp = screenHeight;
// noinspection SuspiciousNameCombination
screenHeight = screenWidth;
screenWidth = tmp;
}
}
if (statusBarHeight != 0.0) {
debug("IOSApplication", "Status bar is visible (height = " + statusBarHeight + ")");
screenHeight -= statusBarHeight;
} else {
debug("IOSApplication", "Status bar is not visible");
}
final int offsetX = 0;
final int offsetY = (int)Math.round(statusBarHeight);
final int width = (int)Math.round(screenWidth);
final int height = (int)Math.round(screenHeight);
final int backBufferWidth = (int)Math.round(screenWidth * pixelsPerPoint);
final int backBufferHeight = (int)Math.round(screenHeight * pixelsPerPoint);
debug("IOSApplication", "Computed bounds are x=" + offsetX + " y=" + offsetY + " w=" + width + " h=" + height + " bbW= "
+ backBufferWidth + " bbH= " + backBufferHeight);
return lastScreenBounds = new IOSScreenBounds(offsetX, offsetY, width, height, backBufferWidth, backBufferHeight);
}
/** @return area of screen in UIKit points on which libGDX draws, with 0,0 being upper left corner */
public IOSScreenBounds getScreenBounds () {
if (lastScreenBounds == null)
return computeBounds();
else
return lastScreenBounds;
}
final void didBecomeActive (UIApplication uiApp) {
Gdx.app.debug("IOSApplication", "resumed");
// workaround for ObjectAL crash problem
// see: https://groups.google.com/forum/?fromgroups=#!topic/objectal-for-iphone/ubRWltp_i1Q
OALAudioSession audioSession = OALAudioSession.sharedInstance();
if (audioSession != null) {
audioSession.forceEndInterruption();
}
if (config.allowIpod) {
OALSimpleAudio audio = OALSimpleAudio.sharedInstance();
if (audio != null) {
audio.setUseHardwareIfAvailable(false);
}
}
graphics.makeCurrent();
graphics.resume();
}
final void willEnterForeground (UIApplication uiApp) {
// workaround for ObjectAL crash problem
// see: https://groups.google.com/forum/?fromgroups=#!topic/objectal-for-iphone/ubRWltp_i1Q
OALAudioSession audioSession = OALAudioSession.sharedInstance();
if (audioSession != null) {
audioSession.forceEndInterruption();
}
}
final void willResignActive (UIApplication uiApp) {
Gdx.app.debug("IOSApplication", "paused");
graphics.makeCurrent();
graphics.pause();
Gdx.gl.glFinish();
}
final void willTerminate (UIApplication uiApp) {
Gdx.app.debug("IOSApplication", "disposed");
graphics.makeCurrent();
Array<LifecycleListener> listeners = lifecycleListeners;
synchronized (listeners) {
for (LifecycleListener listener : listeners) {
listener.pause();
}
}
listener.dispose();
Gdx.gl.glFinish();
}
@Override
public ApplicationListener getApplicationListener () {
return listener;
}
@Override
public Graphics getGraphics () {
return graphics;
}
@Override
public Audio getAudio () {
return audio;
}
@Override
public Input getInput () {
return input;
}
@Override
public Files getFiles () {
return files;
}
@Override
public Net getNet () {
return net;
}
@Override
public void debug (String tag, String message) {
if (logLevel >= LOG_DEBUG) getApplicationLogger().debug(tag, message);
}
@Override
public void debug (String tag, String message, Throwable exception) {
if (logLevel >= LOG_DEBUG) getApplicationLogger().debug(tag, message, exception);
}
@Override
public void log (String tag, String message) {
if (logLevel >= LOG_INFO) getApplicationLogger().log(tag, message);
}
@Override
public void log (String tag, String message, Throwable exception) {
if (logLevel >= LOG_INFO) getApplicationLogger().log(tag, message, exception);
}
@Override
public void error (String tag, String message) {
if (logLevel >= LOG_ERROR) getApplicationLogger().error(tag, message);
}
@Override
public void error (String tag, String message, Throwable exception) {
if (logLevel >= LOG_ERROR) getApplicationLogger().error(tag, message, exception);
}
@Override
public void setLogLevel (int logLevel) {
this.logLevel = logLevel;
}
@Override
public int getLogLevel () {
return logLevel;
}
@Override
public void setApplicationLogger (ApplicationLogger applicationLogger) {
this.applicationLogger = applicationLogger;
}
@Override
public ApplicationLogger getApplicationLogger () {
return applicationLogger;
}
@Override
public ApplicationType getType () {
return ApplicationType.iOS;
}
@Override
public int getVersion () {
return (int) NSProcessInfo.getSharedProcessInfo().getOperatingSystemVersion().getMajorVersion();
}
@Override
public long getJavaHeap () {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
@Override
public long getNativeHeap () {
return getJavaHeap();
}
@Override
public Preferences getPreferences (String name) {
File libraryPath = new File(System.getenv("HOME"), "Library");
File finalPath = new File(libraryPath, name + ".plist");
@SuppressWarnings("unchecked")
NSMutableDictionary<NSString, NSObject> nsDictionary = (NSMutableDictionary<NSString, NSObject>)NSMutableDictionary
.read(finalPath);
// if it fails to get an existing dictionary, create a new one.
if (nsDictionary == null) {
nsDictionary = new NSMutableDictionary<NSString, NSObject>();
nsDictionary.write(finalPath, false);
}
return new IOSPreferences(nsDictionary, finalPath.getAbsolutePath());
}
@Override
public void postRunnable (Runnable runnable) {
synchronized (runnables) {
runnables.add(runnable);
Gdx.graphics.requestRendering();
}
}
public void processRunnables () {
synchronized (runnables) {
executedRunnables.clear();
executedRunnables.addAll(runnables);
runnables.clear();
}
for (int i = 0; i < executedRunnables.size; i++) {
try {
executedRunnables.get(i).run();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
@Override
public void exit () {
System.exit(0);
}
@Override
public Clipboard getClipboard () {
return new Clipboard() {
@Override
public void setContents (String content) {
UIPasteboard.getGeneralPasteboard().setString(content);
}
@Override
public String getContents () {
return UIPasteboard.getGeneralPasteboard().getString();
}
};
}
@Override
public void addLifecycleListener (LifecycleListener listener) {
synchronized (lifecycleListeners) {
lifecycleListeners.add(listener);
}
}
@Override
public void removeLifecycleListener (LifecycleListener listener) {
synchronized (lifecycleListeners) {
lifecycleListeners.removeValue(listener, true);
}
}
/** Add a listener to handle events from the libgdx root view controller
* @param listener The {#link IOSViewControllerListener} to add */
public void addViewControllerListener (IOSViewControllerListener listener) {
viewControllerListener = listener;
}
}