/
DisplayObjectExplorer.as
166 lines (153 loc) · 4.88 KB
/
DisplayObjectExplorer.as
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
package
{
import feathers.examples.displayObjects.Main;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageOrientation;
import flash.display.StageScaleMode;
import flash.display3D.Context3DProfile;
import flash.display3D.Context3DRenderMode;
import flash.events.Event;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.geom.Rectangle;
import flash.system.Capabilities;
import flash.utils.ByteArray;
import starling.core.Starling;
[SWF(width="640",height="960",frameRate="60",backgroundColor="#4a4137")]
public class DisplayObjectExplorer extends Sprite
{
public function DisplayObjectExplorer()
{
if(this.stage)
{
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
}
this.mouseEnabled = this.mouseChildren = false;
this.showLaunchImage();
this.loaderInfo.addEventListener(Event.COMPLETE, loaderInfo_completeHandler);
}
private var _starling:Starling;
private var _launchImage:Loader;
private var _savedAutoOrients:Boolean;
private function showLaunchImage():void
{
var filePath:String;
var isPortraitOnly:Boolean = false;
if(Capabilities.manufacturer.indexOf("iOS") >= 0)
{
var isCurrentlyPortrait:Boolean = this.stage.orientation == StageOrientation.DEFAULT || this.stage.orientation == StageOrientation.UPSIDE_DOWN;
if(Capabilities.screenResolutionX == 1242 && Capabilities.screenResolutionY == 2208)
{
//iphone 6 plus
filePath = isCurrentlyPortrait ? "Default-414w-736h@3x.png" : "Default-414w-736h-Landscape@3x.png";
}
else if(Capabilities.screenResolutionX == 1536 && Capabilities.screenResolutionY == 2048)
{
//ipad retina
filePath = isCurrentlyPortrait ? "Default-Portrait@2x.png" : "Default-Landscape@2x.png";
}
else if(Capabilities.screenResolutionX == 768 && Capabilities.screenResolutionY == 1024)
{
//ipad classic
filePath = isCurrentlyPortrait ? "Default-Portrait.png" : "Default-Landscape.png";
}
else if(Capabilities.screenResolutionX == 750)
{
//iphone 6
isPortraitOnly = true;
filePath = "Default-375w-667h@2x.png";
}
else if(Capabilities.screenResolutionX == 640)
{
//iphone retina
isPortraitOnly = true;
if(Capabilities.screenResolutionY == 1136)
{
filePath = "Default-568h@2x.png";
}
else
{
filePath = "Default@2x.png";
}
}
else if(Capabilities.screenResolutionX == 320)
{
//iphone classic
isPortraitOnly = true;
filePath = "Default.png";
}
}
if(filePath)
{
var file:File = File.applicationDirectory.resolvePath(filePath);
if(file.exists)
{
var bytes:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(bytes, 0, stream.bytesAvailable);
stream.close();
this._launchImage = new Loader();
this._launchImage.loadBytes(bytes);
this.addChild(this._launchImage);
this._savedAutoOrients = this.stage.autoOrients;
this.stage.autoOrients = false;
if(isPortraitOnly)
{
this.stage.setOrientation(StageOrientation.DEFAULT);
}
}
}
}
private function loaderInfo_completeHandler(event:Event):void
{
Starling.multitouchEnabled = true;
this._starling = new Starling(Main, this.stage, null, null, Context3DRenderMode.AUTO, Context3DProfile.BASELINE);
this._starling.supportHighResolutions = true;
this._starling.start();
if(this._launchImage)
{
this._starling.addEventListener("rootCreated", starling_rootCreatedHandler);
}
this.stage.addEventListener(Event.RESIZE, stage_resizeHandler, false, int.MAX_VALUE, true);
this.stage.addEventListener(Event.DEACTIVATE, stage_deactivateHandler, false, 0, true);
}
private function starling_rootCreatedHandler(event:Object):void
{
if(this._launchImage)
{
this.removeChild(this._launchImage);
this._launchImage.unloadAndStop(true);
this._launchImage = null;
this.stage.autoOrients = this._savedAutoOrients;
}
}
private function stage_resizeHandler(event:Event):void
{
this._starling.stage.stageWidth = this.stage.stageWidth;
this._starling.stage.stageHeight = this.stage.stageHeight;
var viewPort:Rectangle = this._starling.viewPort;
viewPort.width = this.stage.stageWidth;
viewPort.height = this.stage.stageHeight;
try
{
this._starling.viewPort = viewPort;
}
catch(error:Error) {}
}
private function stage_deactivateHandler(event:Event):void
{
this._starling.stop(true);
this.stage.addEventListener(Event.ACTIVATE, stage_activateHandler, false, 0, true);
}
private function stage_activateHandler(event:Event):void
{
this.stage.removeEventListener(Event.ACTIVATE, stage_activateHandler);
this._starling.start();
}
}
}