forked from libgdx/gdx-liftoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplate.kt
414 lines (360 loc) · 14.9 KB
/
Template.kt
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
package gdx.liftoff.data.templates
import gdx.liftoff.config.GdxVersion
import gdx.liftoff.data.files.SourceFile
import gdx.liftoff.data.files.path
import gdx.liftoff.data.platforms.Android
import gdx.liftoff.data.platforms.Core
import gdx.liftoff.data.platforms.GWT
import gdx.liftoff.data.platforms.Headless
import gdx.liftoff.data.platforms.Lwjgl2
import gdx.liftoff.data.platforms.Lwjgl3
import gdx.liftoff.data.platforms.Server
import gdx.liftoff.data.platforms.iOS
import gdx.liftoff.data.platforms.iOSMOE
import gdx.liftoff.data.project.Project
/**
* Interface shared by all project templates. Templates should be annotated with ProjectTemplate.
*/
interface Template {
val id: String
// Sizes are kept as strings so you can set the sizes to static values, for example: MainClass.WIDTH.
val width: String
get() = "640"
val height: String
get() = "480"
/** Used as project description in README file. Optional. */
val description: String
get() = ""
/** File extension of the ApplicationListener implementation. */
val applicationListenerExtension: String
get() = "java"
/** File extension of the application launchers on each platform. */
val launcherExtension: String
get() = "java"
val defaultSourceFolder: String
get() = path("src", "main", "java")
/**
* @param project is being created. Should contain sources provided by this template.
*/
fun apply(project: Project) {
addApplicationListener(project)
addAndroidLauncher(project)
addDesktopLauncher(project)
addGwtLauncher(project)
addHeadlessLauncher(project)
addIOSLauncher(project)
addIOSMOELauncher(project)
addLwjgl3Launcher(project)
addServerLauncher(project)
project.readmeDescription = description
}
fun addApplicationListener(project: Project) {
addSourceFile(
project = project,
platform = Core.ID,
packageName = project.basic.rootPackage,
fileName = "${project.basic.mainClass}.$applicationListenerExtension",
content = getApplicationListenerContent(project)
)
}
/**
* @param project is being created.
* @return content of Java class implementing ApplicationListener.
*/
fun getApplicationListenerContent(project: Project): String
fun addDesktopLauncher(project: Project) {
addSourceFile(
project = project,
platform = Lwjgl2.ID,
packageName = "${project.basic.rootPackage}.desktop",
fileName = "DesktopLauncher.$launcherExtension",
content = getDesktopLauncherContent(project)
)
}
fun getDesktopLauncherContent(project: Project): String = """package ${project.basic.rootPackage}.desktop;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import ${project.basic.rootPackage}.${project.basic.mainClass};
/** Launches the desktop (LWJGL) application. */
public class DesktopLauncher {
public static void main(String[] args) {
createApplication();
}
private static LwjglApplication createApplication() {
return new LwjglApplication(new ${project.basic.mainClass}(), getDefaultConfiguration());
}
private static LwjglApplicationConfiguration getDefaultConfiguration() {
LwjglApplicationConfiguration configuration = new LwjglApplicationConfiguration();
configuration.title = "${project.basic.name}";
configuration.width = $width;
configuration.height = $height;
//// This prevents a confusing error that would appear after exiting normally.
configuration.forceExit = false;
for (int size : new int[] { 128, 64, 32, 16 }) {
configuration.addIcon("libgdx" + size + ".png", FileType.Internal);
}
return configuration;
}
}"""
fun addGwtLauncher(project: Project) {
addSourceFile(
project = project,
platform = GWT.ID,
packageName = "${project.basic.rootPackage}.gwt",
content = getGwtLauncherContent(project),
// GWT supports only Java sources:
fileName = "GwtLauncher.java",
sourceFolderPath = path("src", "main", "java")
)
}
fun getGwtLauncherContent(project: Project): String = """package ${project.basic.rootPackage}.gwt;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.gwt.GwtApplication;
import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
import ${project.basic.rootPackage}.${project.basic.mainClass};
/** Launches the GWT application. */
public class GwtLauncher extends GwtApplication {""" + (
if (GdxVersion.parseGdxVersion(project.advanced.gdxVersion) != null && GdxVersion.parseGdxVersion(project.advanced.gdxVersion)!! < GdxVersion(1, 9, 12))
"""
////USE THIS CODE FOR A FIXED SIZE APPLICATION
@Override
public GwtApplicationConfiguration getConfig () {
return new GwtApplicationConfiguration($width, $height);
}
////END CODE FOR FIXED SIZE APPLICATION
////UNCOMMENT THIS CODE FOR A RESIZABLE APPLICATION
// PADDING is to avoid scrolling in iframes, set to 20 if you have problems
// private static final int PADDING = 0;
//
// @Override
// public GwtApplicationConfiguration getConfig() {
// int w = Window.getClientWidth() - PADDING;
// int h = Window.getClientHeight() - PADDING;
// GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(w, h);
// Window.enableScrolling(false);
// Window.setMargin("0");
// Window.addResizeHandler(new ResizeListener());
// cfg.preferFlash = false;
// return cfg;
// }
//
// class ResizeListener implements ResizeHandler {
// @Override
// public void onResize(ResizeEvent event) {
// if (Gdx.graphics.isFullscreen()) return;
// int width = event.getWidth() - PADDING;
// int height = event.getHeight() - PADDING;
// getRootPanel().setWidth("" + width + "px");
// getRootPanel().setHeight("" + height + "px");
// getApplicationListener().resize(width, height);
// Gdx.graphics.setWindowedMode(width, height);
// }
// }
////END OF CODE FOR RESIZABLE APPLICATION
"""
else """
@Override
public GwtApplicationConfiguration getConfig () {
// Resizable application, uses available space in browser with no padding:
GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(true);
cfg.padVertical = 0;
cfg.padHorizontal = 0;
return cfg;
// If you want a fixed size application, comment out the above resizable section,
// and uncomment below:
//return new GwtApplicationConfiguration($width, $height);
}
"""
) +
"""
@Override
public ApplicationListener createApplicationListener () {
return new ${project.basic.mainClass}();
}
}
"""
fun addAndroidLauncher(project: Project) {
addSourceFile(
project = project,
platform = Android.ID,
packageName = project.basic.rootPackage,
fileName = "android/AndroidLauncher.$launcherExtension",
content = getAndroidLauncherContent(project)
)
}
fun getAndroidLauncherContent(project: Project): String = """package ${project.basic.rootPackage}.android;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import ${project.basic.rootPackage}.${project.basic.mainClass};
/** Launches the Android application. */
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration configuration = new AndroidApplicationConfiguration();
initialize(new ${project.basic.mainClass}(), configuration);
}
}"""
fun addHeadlessLauncher(project: Project) {
addSourceFile(
project = project,
platform = Headless.ID,
packageName = "${project.basic.rootPackage}.headless",
fileName = "HeadlessLauncher.$launcherExtension",
content = getHeadlessLauncherContent(project)
)
}
fun getHeadlessLauncherContent(project: Project): String = """package ${project.basic.rootPackage}.headless;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.backends.headless.HeadlessApplication;
import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration;
import ${project.basic.rootPackage}.${project.basic.mainClass};
/** Launches the headless application. Can be converted into a utilities project or a server application. */
public class HeadlessLauncher {
public static void main(String[] args) {
createApplication();
}
private static Application createApplication() {
// Note: you can use a custom ApplicationListener implementation for the headless project instead of ${project.basic.mainClass}.
return new HeadlessApplication(new ${project.basic.mainClass}(), getDefaultConfiguration());
}
private static HeadlessApplicationConfiguration getDefaultConfiguration() {
HeadlessApplicationConfiguration configuration = new HeadlessApplicationConfiguration();
configuration.updatesPerSecond = -1; // When this value is negative, ${project.basic.mainClass}#render() is never called.
//// If the above line doesn't compile, it is probably because the project libGDX version is older.
//// In that case, uncomment and use the below line.
//configuration.renderInterval = -1f; // When this value is negative, ${project.basic.mainClass}#render() is never called.
return configuration;
}
}"""
fun addIOSLauncher(project: Project) {
addSourceFile(
project = project,
platform = iOS.ID,
packageName = "${project.basic.rootPackage}.ios",
fileName = "IOSLauncher.$launcherExtension",
content = getIOSLauncherContent(project)
)
}
fun getIOSLauncherContent(project: Project): String = """package ${project.basic.rootPackage}.ios;
import org.robovm.apple.foundation.NSAutoreleasePool;
import org.robovm.apple.uikit.UIApplication;
import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
import ${project.basic.rootPackage}.${project.basic.mainClass};
/** Launches the iOS (RoboVM) application. */
public class IOSLauncher extends IOSApplication.Delegate {
@Override
protected IOSApplication createApplication() {
IOSApplicationConfiguration configuration = new IOSApplicationConfiguration();
return new IOSApplication(new ${project.basic.mainClass}(), configuration);
}
public static void main(String[] argv) {
NSAutoreleasePool pool = new NSAutoreleasePool();
UIApplication.main(argv, null, IOSLauncher.class);
pool.close();
}
}"""
fun addIOSMOELauncher(project: Project) {
addSourceFile(
project = project,
platform = iOSMOE.ID,
packageName = "${project.basic.rootPackage}.ios",
fileName = "IOSLauncher.$launcherExtension",
content = getIOSMOELauncherContent(project)
)
}
fun getIOSMOELauncherContent(project: Project): String = """package ${project.basic.rootPackage}.ios;
import apple.uikit.c.UIKit;
import com.badlogic.gdx.backends.iosmoe.IOSApplication;
import com.badlogic.gdx.backends.iosmoe.IOSApplicationConfiguration;
import org.moe.natj.general.Pointer;
import ${project.basic.rootPackage}.${project.basic.mainClass};
/** Launches the iOS (Multi-Os Engine) application. */
public class IOSLauncher extends IOSApplication.Delegate {
protected IOSLauncher(Pointer peer) {
super(peer);
}
@Override
protected IOSApplication createApplication() {
IOSApplicationConfiguration configuration = new IOSApplicationConfiguration();
return new IOSApplication(new ${project.basic.mainClass}(), configuration);
}
public static void main(String[] argv) {
UIKit.UIApplicationMain(0, null, null, IOSLauncher.class.getName());
}
}"""
fun addLwjgl3Launcher(project: Project) {
addSourceFile(
project = project,
platform = Lwjgl3.ID,
packageName = "${project.basic.rootPackage}.lwjgl3",
fileName = "Lwjgl3Launcher.$launcherExtension",
content = getLwjgl3LauncherContent(project)
)
}
fun getLwjgl3LauncherContent(project: Project): String = """package ${project.basic.rootPackage}.lwjgl3;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import ${project.basic.rootPackage}.${project.basic.mainClass};
/** Launches the desktop (LWJGL3) application. */
public class Lwjgl3Launcher {
public static void main(String[] args) {
createApplication();
}
private static Lwjgl3Application createApplication() {
return new Lwjgl3Application(new ${project.basic.mainClass}(), getDefaultConfiguration());
}
private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() {
Lwjgl3ApplicationConfiguration configuration = new Lwjgl3ApplicationConfiguration();
configuration.setTitle("${project.basic.name}");
configuration.useVsync(true);
//// Limits FPS to the refresh rate of the currently active monitor.
configuration.setForegroundFPS(Lwjgl3ApplicationConfiguration.getDisplayMode().refreshRate);
//// If you remove the above line and set Vsync to false, you can get unlimited FPS, which can be
//// useful for testing performance, but can also be very stressful to some hardware.
//// You may also need to configure GPU drivers to fully disable Vsync; this can cause screen tearing.
configuration.setWindowedMode($width, $height);
configuration.setWindowIcon("libgdx128.png", "libgdx64.png", "libgdx32.png", "libgdx16.png");
return configuration;
}
}"""
fun addServerLauncher(project: Project) {
addSourceFile(
project = project,
platform = Server.ID,
packageName = "${project.basic.rootPackage}.server",
fileName = "ServerLauncher.$launcherExtension",
content = getServerLauncherContent(project)
)
}
fun getServerLauncherContent(project: Project): String = """package ${project.basic.rootPackage}.server;
/** Launches the server application. */
public class ServerLauncher {
public static void main(String[] args) {
// TODO Implement server application.
}
}"""
fun addSourceFile(
project: Project,
platform: String,
packageName: String,
fileName: String,
content: String,
sourceFolderPath: String = defaultSourceFolder
) {
if (project.hasPlatform(platform)) {
project.files.add(
SourceFile(
projectName = platform,
sourceFolderPath = sourceFolderPath,
packageName = packageName,
fileName = fileName,
content = content
)
)
}
}
}