Files
haxework/haxework/gui/Root.hx
2015-07-02 11:12:52 +03:00

56 lines
1.6 KiB
Haxe
Executable File

package haxework.gui;
import flash.errors.Error;
import flash.Lib;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.display.Sprite;
class Root {
public static var instance(default, null):Root;
public var view(default, null):IView<Sprite>;
public var autoSize(default, default):Bool;
public function new(view:IView<Sprite>, autoSize:Bool = true) {
if (instance != null) throw new Error("Only one instance");
instance = this;
this.view = view;
this.autoSize = autoSize;
Lib.current.addChild(view.content);
var content:DisplayObject = view.content;
if (content.stage == null) {
content.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
} else {
onAddedToStage();
}
View.updater.update();
}
private function onAddedToStage(?_):Void {
var content:DisplayObject = view.content;
content.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
content.stage.scaleMode = StageScaleMode.NO_SCALE;
content.stage.align = StageAlign.TOP_LEFT;
View.updater.stage = content.stage;
content.stage.addEventListener(Event.RESIZE, onResize);
onResize();
}
private function onResize(?_):Void {
var content:DisplayObject = view.content;
if (autoSize) {
view.width = content.stage.stageWidth;
view.height = content.stage.stageHeight;
} else {
view.x = (content.stage.stageWidth - view.width) / 2;
view.y = (content.stage.stageHeight - view.height) / 2;
}
L.d("Screen", content.stage.stageWidth + "x" + content.stage.stageHeight);
}
}