-
Notifications
You must be signed in to change notification settings - Fork 44
/
Universal.hx
527 lines (421 loc) · 13.2 KB
/
Universal.hx
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package;
import openfl.Lib;
import openfl.display.OpenGLRenderer;
import openfl.display.Sprite;
import openfl.display.Stage;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.display.StageDisplayState;
import openfl.display.Shape;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import openfl.geom.Rectangle;
import openfl.system.Capabilities;
import openfl.ui.Keyboard;
import lime.ui.Window;
import com.stencyl.APIKeys;
import com.stencyl.Config;
import com.stencyl.Engine;
import com.stencyl.Input;
import com.stencyl.graphics.Scale;
import com.stencyl.graphics.ScaleMode;
#if stencyltools
import com.stencyl.utils.ToolsetInterface;
#end
import com.stencyl.utils.Utils;
import haxe.xml.Fast;
import haxe.CallStack;
import haxe.Log in HaxeLog;
import lime.utils.Log in LimeLog;
class Universal extends Sprite
{
private static var window:Window;
public static var logicalWidth = 0.0;
public static var logicalHeight = 0.0;
public static var windowWidth = 0.0;
public static var windowHeight = 0.0;
public static var leftInset = 0.0;
public static var topInset = 0.0;
public static var rightInset = 0.0;
public static var bottomInset = 0.0;
public var maskLayer:Shape;
public static function initWindow(window:Window):Void
{
Universal.window = window;
window.stage.align = StageAlign.TOP_LEFT;
window.stage.scaleMode = StageScaleMode.NO_SCALE;
#if mobile
window.stage.opaqueBackground = 0x000000;
#end
}
public function new()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(event:Event):Void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
initServices();
maskLayer = new Shape();
initScreen(Config.startInFullScreen);
}
public function initServices()
{
//Newgrounds and other APIs
#if flash
if(APIKeys.newgroundsID != "")
{
com.newgrounds.API.API.connect(root, APIKeys.newgroundsID, APIKeys.newgroundsKey);
}
#end
}
//isFullScreen is used on Web/Desktop for full screen mode
#if (!flash) @:access(openfl.display.Stage.__setLogicalSize) #end
public function initScreen(isFullScreen:Bool)
{
trace("initScreen");
#if mobile
isFullScreen = true;
#end
#if html5
isFullScreen = false;
#end
stage.displayState = isFullScreen ?
StageDisplayState.FULL_SCREEN_INTERACTIVE :
StageDisplayState.NORMAL;
#if !flash
stage.__setLogicalSize (0, 0);
#end
#if desktop
if(!isFullScreen)
{
window.resize(Std.int(Config.stageWidth * Config.gameScale), Std.int(Config.stageHeight * Config.gameScale));
}
#end
Lib.current.x = 0;
Lib.current.y = 0;
Lib.current.scaleX = 1;
Lib.current.scaleY = 1;
x = 0;
y = 0;
scaleX = 1;
scaleY = 1;
Engine.stage = stage;
//enabled scales
var scales = new Map<Scale,Bool>();
for(scale in Config.scales)
{
scales.set(scale, true);
}
windowWidth = isFullScreen ? stage.fullScreenWidth : Config.stageWidth * Config.gameScale;
windowHeight = isFullScreen ? stage.fullScreenHeight : Config.stageHeight * Config.gameScale;
trace("Game Width: " + Config.stageWidth);
trace("Game Height: " + Config.stageHeight);
trace("Game Scale: " + Config.gameScale);
trace("Window Width: " + windowWidth);
trace("Window Height: " + windowHeight);
trace("FullScreen Width: " + stage.fullScreenWidth);
trace("FullScreen Height: " + stage.fullScreenHeight);
trace("Enabled Scales: " + Config.scales);
trace("Scale Mode: " + Config.scaleMode);
#if ios
var larger = Math.max(windowWidth, windowHeight);
var smaller = Math.min(windowWidth, windowHeight);
if(smaller == 320 && larger == 480)
{
Engine.isStandardIOS = true;
}
else if(smaller == 640 && larger == 960)
{
Engine.isStandardIOS = true;
}
//iPhone 5, 5s, or iPhone 6 with Display Zoom
else if(smaller == 640 && larger == 1136)
{
Engine.isExtendedIOS = true;
}
else if(smaller == 750 && larger == 1334)
{
Engine.isIPhone6 = true;
}
else if(smaller == 1242 && larger == 2208)
{
Engine.isIPhone6Plus = true;
}
//iPhone 6+ with Display Zoom
else if(smaller == 1125 && larger == 2001)
{
Engine.isIPhone6Plus = true;
}
else if(smaller == 1125 && larger == 2436)
{
Engine.isIPhoneX = true;
}
else if(smaller == 1242 && larger >= 2688 && larger <= 2690)
{
Engine.isIPhoneXMax = true;
}
else if(smaller == 828 && larger == 1792)
{
Engine.isIPhoneXR = true;
}
else if
(
(smaller == 768 && larger == 1024) ||
(smaller == 1536 && larger == 2048) ||
(smaller == 1668 && larger == 2224) ||
(smaller == 1668 && larger == 2388) ||
(smaller == 2048 && larger == 2732)
)
{
Engine.isTabletIOS = true;
}
#end
var theoreticalWindowedScale = getDesiredScale(windowWidth, windowHeight, Config.stageWidth, Config.stageHeight);
var theoreticalFullscreenScale = getDesiredScale(stage.fullScreenWidth, stage.fullScreenHeight, Config.stageWidth, Config.stageHeight);
var theoreticalScale = Config.forceHiResAssets ? theoreticalFullscreenScale : theoreticalWindowedScale;
//4 scale scheme
if(theoreticalScale == 4 && scales.exists(Scale._4X))
{
Engine.SCALE = 4;
Engine.IMG_BASE = "4x";
}
else if(theoreticalScale >= 3 && scales.exists(Scale._3X))
{
Engine.SCALE = 3;
Engine.IMG_BASE = "3x";
}
else if(theoreticalScale >= 2 && scales.exists(Scale._2X))
{
Engine.SCALE = 2;
Engine.IMG_BASE = "2x";
}
else if(theoreticalScale >= 1.5 && scales.exists(Scale._1_5X))
{
Engine.SCALE = 1.5;
Engine.IMG_BASE = "1.5x";
}
else
{
Engine.SCALE = 1;
Engine.IMG_BASE = "1x";
}
trace("Theoretical Scale: " + theoreticalScale);
trace("Asset Scale: " + Engine.IMG_BASE);
//the dimensions of the game screen after being scaled up
//to the proper asset size.
var scaledStageWidth = Config.stageWidth * Engine.SCALE;
var scaledStageHeight = Config.stageHeight * Engine.SCALE;
//the remaining x/y scale needed to fit the game screen
//to the edges of the window.
var fitWidthScale = windowWidth / scaledStageWidth;
var fitHeightScale = windowHeight / scaledStageHeight;
if(Config.forceHiResAssets || windowWidth != Config.stageWidth || windowHeight != Config.stageHeight)
{
//after the basic assets scale, how do we fill out the rest of the screen?
//expand the playable area rather than further scaling it
if(Config.scaleMode == ScaleMode.FULLSCREEN)
{
if(Engine.SCALE != theoreticalWindowedScale)
{
scaleX = theoreticalWindowedScale / Engine.SCALE;
scaleY = scaleX;
}
//don't do anything
//stage width/height are already the size of the screen
}
//exactly match the game size to the window size for both width and height
else if(Config.scaleMode == ScaleMode.STRETCH_TO_FIT)
{
scaleX = fitWidthScale;
scaleY = fitHeightScale;
}
//keeping aspect ration, stretch until either side of the game screen touches the window's edge
//for "Scale to fit (fullscreen)", the rest of the space is expanded
else if(Config.scaleMode == ScaleMode.SCALE_TO_FIT_LETTERBOX || Config.scaleMode == ScaleMode.SCALE_TO_FIT_FULLSCREEN)
{
scaleX = Math.min(fitWidthScale, fitHeightScale);
scaleY = scaleX;
}
//keeping aspect ration, stretch until both sides of the game screen touch the window's edge
else if(Config.scaleMode == ScaleMode.SCALE_TO_FIT_FILL)
{
scaleX = Math.max(fitWidthScale, fitHeightScale);
scaleY = scaleX;
}
//no additional scaling
else if(Config.scaleMode == ScaleMode.NO_SCALING)
{
if(Engine.SCALE != theoreticalWindowedScale)
{
scaleX = theoreticalWindowedScale / Engine.SCALE;
scaleY = scaleX;
}
}
if(Config.scaleMode != ScaleMode.SCALE_TO_FIT_FULLSCREEN && Config.scaleMode != ScaleMode.FULLSCREEN)
{
x += (windowWidth - scaledStageWidth * scaleX) / 2;
y += (windowHeight - scaledStageHeight * scaleY) / 2;
}
}
logicalWidth = Config.stageWidth;
logicalHeight = Config.stageHeight;
if(isFullScreen && (Config.scaleMode == ScaleMode.SCALE_TO_FIT_FULLSCREEN || Config.scaleMode == ScaleMode.FULLSCREEN))
{
logicalWidth = (windowWidth / scaleX) / Engine.SCALE;
logicalHeight = (windowHeight / scaleY) / Engine.SCALE;
//bring logical size to the nearest full pixel of the desired value.
if(Std.int(logicalWidth) != logicalWidth || Std.int(logicalHeight) != logicalHeight)
{
logicalWidth = Std.int(logicalWidth);
logicalHeight = Std.int(logicalHeight);
scaleX = windowWidth / Engine.SCALE / logicalWidth;
scaleY = windowHeight / Engine.SCALE / logicalHeight;
}
}
Engine.screenScaleX = scaleX;
Engine.screenScaleY = scaleY;
#if mobile
var insets = com.stencyl.native.Native.getSafeInsets();
leftInset = insets.x;
rightInset = insets.width;
topInset = insets.y;
bottomInset = insets.height;
trace('Safe Area Insets: original = (left: $leftInset, top: $topInset, right: $rightInset, bottom: $bottomInset)');
if(Config.autorotate)
{
/*
TODO: We don't have a way to notify Stencyl of orientation changes at the moment.
Eventually, we should have lime listen for and pass on SDL's display events, including
the orientation one.
For now, to assure the safe areas are actually safe, we'll mirror the
max inset along an axis to both sides.
*/
leftInset = rightInset = Math.max(leftInset, rightInset);
topInset = bottomInset = Math.max(topInset, bottomInset);
trace('Safe Area Insets: mirrored = (left: $leftInset, top: $topInset, right: $rightInset, bottom: $bottomInset)');
}
if(x != 0 || y != 0)
{
// we don't need to inset if we're letterboxing over the inset area.
leftInset = Math.max(0, leftInset - x);
rightInset = Math.max(0, rightInset - x);
topInset = Math.max(0, topInset - y);
bottomInset = Math.max(0, bottomInset - y);
trace('Safe Area Insets: offset = (left: $leftInset, top: $topInset, right: $rightInset, bottom: $bottomInset)');
}
// scale to Stencyl's logical coordinates
leftInset = Math.ceil(leftInset / (Engine.SCALE * scaleX));
rightInset = Math.ceil(rightInset / (Engine.SCALE * scaleX));
topInset = Math.ceil(topInset / (Engine.SCALE * scaleY));
bottomInset = Math.ceil(bottomInset / (Engine.SCALE * scaleY));
trace('Safe Area Insets: scaled = (left: $leftInset, top: $topInset, right: $rightInset, bottom: $bottomInset)');
#end
maskLayer.graphics.clear();
if(isFullScreen && (Config.scaleMode == ScaleMode.SCALE_TO_FIT_LETTERBOX || Config.scaleMode == ScaleMode.NO_SCALING))
{
maskLayer.graphics.beginFill(stage.color);
maskLayer.graphics.drawRect(-x, -y, windowWidth, y);
maskLayer.graphics.drawRect(-x, 0, x, scaledStageHeight);
maskLayer.graphics.drawRect(scaledStageWidth, 0, x, scaledStageHeight);
maskLayer.graphics.drawRect(-x, scaledStageHeight, windowWidth, y);
maskLayer.graphics.endFill();
}
trace("Logical Width: " + logicalWidth);
trace("Logical Height: " + logicalHeight);
trace("Scale X: " + scaleX);
trace("Scale Y: " + scaleY);
}
private function getDesiredScale(checkWidth:Float, checkHeight:Float, baseWidth:Int, baseHeight:Int):Float
{
var x1 = baseWidth;
var y1 = baseHeight;
var x2 = x1 * 2;
var y2 = y1 * 2;
var x3 = x1 * 3;
var y3 = y1 * 3;
var x4 = x2 * 2;
var y4 = y2 * 2;
var x15 = x3 / 2;
var y15 = y3 / 2;
if(checkWidth >= x4 && checkHeight >= y4)
{
return 4;
}
else if(checkWidth >= x3 && checkHeight >= y3)
{
return 3;
}
else if(checkWidth >= x2 && checkHeight >= y2)
{
return 2;
}
else if(checkWidth >= x15 && checkHeight >= y15)
{
return 1.5;
}
else
{
return 1;
}
}
@:access(openfl.display.Stage)
public function preloaderComplete():Void
{
#if flash
new Engine(this);
#else
try {
new Engine(this);
} catch (e:Dynamic) {
#if stencyltools
if(Config.useGciLogging)
{
trace(e #if debug + "\n" + CallStack.toString(CallStack.exceptionStack()) #end);
ToolsetInterface.preloadedUpdate();
}
#end
stage.__handleError (e);
}
#end
}
//for Cppia, don't directly call ApplicationMain functions
private static var am:Class<Dynamic>;
private static var oldTrace:Dynamic;
public static function setupTracing(?forceEnable:Bool = false):Void
{
if(oldTrace == null)
oldTrace = HaxeLog.trace;
var enable = forceEnable || !Config.releaseMode;
if(enable)
{
#if (flash9 || flash10)
HaxeLog.trace = function(v,?pos) { untyped __global__["trace"]("Stencyl:" + pos.className+"#"+pos.methodName+"("+pos.lineNumber+"):",v); }
#elseif flash
HaxeLog.trace = function(v,?pos) { flash.Lib.trace("Stencyl:" + pos.className+"#"+pos.methodName+"("+pos.lineNumber+"): "+v); }
#else
HaxeLog.trace = oldTrace;
#end
#if stencyltools
if(Config.useGciLogging)
HaxeLog.trace = ToolsetInterface.gciTrace;
#end
LimeLog.level = VERBOSE;
}
else
{
HaxeLog.trace = function(v,?pos) { };
LimeLog.level = NONE;
}
}
public static function reloadGame()
{
Reflect.callMethod(am, Reflect.field(am, "reloadGame"), []);
}
public static function addReloadListener(reloadListener:Void->Void)
{
var reloadListeners:Array<Void->Void> = Reflect.field(am, "reloadListeners");
reloadListeners.push(reloadListener);
}
}