Refactor
This commit is contained in:
@@ -1,24 +1,9 @@
|
||||
package lime.app;
|
||||
|
||||
|
||||
import lime.graphics.IRenderEventListener;
|
||||
import lime.graphics.Renderer;
|
||||
import lime.graphics.RenderEvent;
|
||||
import lime.graphics.RenderEventManager;
|
||||
import lime.system.System;
|
||||
import lime.ui.IKeyEventListener;
|
||||
import lime.ui.IMouseEventListener;
|
||||
import lime.ui.ITouchEventListener;
|
||||
import lime.ui.IWindowEventListener;
|
||||
import lime.ui.KeyEvent;
|
||||
import lime.ui.KeyEventManager;
|
||||
import lime.ui.MouseEvent;
|
||||
import lime.ui.MouseEventManager;
|
||||
import lime.ui.TouchEvent;
|
||||
import lime.ui.TouchEventManager;
|
||||
import lime.ui.Window;
|
||||
import lime.ui.WindowEvent;
|
||||
import lime.ui.WindowEventManager;
|
||||
import lime.graphics.*;
|
||||
import lime.system.*;
|
||||
import lime.ui.*;
|
||||
|
||||
|
||||
class Application implements IKeyEventListener implements IMouseEventListener implements IRenderEventListener implements ITouchEventListener implements IUpdateEventListener implements IWindowEventListener {
|
||||
@@ -26,18 +11,31 @@ class Application implements IKeyEventListener implements IMouseEventListener im
|
||||
|
||||
public var handle:Dynamic;
|
||||
|
||||
private var config:Config;
|
||||
private var lastUpdate:Int;
|
||||
private var windows:Array<Window>;
|
||||
|
||||
|
||||
public function new () {
|
||||
|
||||
lastUpdate = 0;
|
||||
windows = new Array ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function addWindow (window:Window):Void {
|
||||
|
||||
windows.push (window);
|
||||
window.create ();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function create (config:Config):Void {
|
||||
|
||||
this.config = config;
|
||||
|
||||
#if (cpp || neko)
|
||||
handle = lime_application_create (null);
|
||||
#end
|
||||
@@ -57,10 +55,17 @@ class Application implements IKeyEventListener implements IMouseEventListener im
|
||||
WindowEventManager.addEventListener (this);
|
||||
|
||||
var window = new Window (this);
|
||||
window.create (config);
|
||||
|
||||
var renderer = new Renderer (window);
|
||||
|
||||
window.width = config.width;
|
||||
window.height = config.height;
|
||||
|
||||
#if js
|
||||
window.element = config.element;
|
||||
#end
|
||||
|
||||
addWindow (window);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,23 +1,33 @@
|
||||
package lime.app;
|
||||
|
||||
|
||||
import lime.graphics.RenderEventManager;
|
||||
import lime.system.System;
|
||||
import lime.ui.Window;
|
||||
|
||||
#if js
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
|
||||
@:allow(lime.ui.Window) @:access(lime.graphics.RenderEventManager)
|
||||
class UpdateEventManager extends EventManager<IUpdateEventListener> {
|
||||
|
||||
|
||||
private static var instance:UpdateEventManager;
|
||||
|
||||
private var event:UpdateEvent;
|
||||
|
||||
|
||||
public function new () {
|
||||
|
||||
super ();
|
||||
|
||||
instance = this;
|
||||
event = new UpdateEvent ();
|
||||
|
||||
#if (cpp || neko)
|
||||
lime_update_event_manager_register (handleEvent, new UpdateEvent ());
|
||||
lime_update_event_manager_register (handleEvent, event);
|
||||
#end
|
||||
|
||||
}
|
||||
@@ -47,6 +57,65 @@ class UpdateEventManager extends EventManager<IUpdateEventListener> {
|
||||
}
|
||||
|
||||
|
||||
private static function registerWindow (window:Window):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
#if js
|
||||
|
||||
untyped __js__ ("
|
||||
var lastTime = 0;
|
||||
var vendors = ['ms', 'moz', 'webkit', 'o'];
|
||||
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
|
||||
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
|
||||
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|
||||
|| window[vendors[x]+'CancelRequestAnimationFrame'];
|
||||
}
|
||||
|
||||
if (!window.requestAnimationFrame)
|
||||
window.requestAnimationFrame = function(callback, element) {
|
||||
var currTime = new Date().getTime();
|
||||
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
|
||||
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
|
||||
timeToCall);
|
||||
lastTime = currTime + timeToCall;
|
||||
return id;
|
||||
};
|
||||
|
||||
if (!window.cancelAnimationFrame)
|
||||
window.cancelAnimationFrame = function(id) {
|
||||
clearTimeout(id);
|
||||
};
|
||||
|
||||
window.requestAnimFrame = window.requestAnimationFrame;
|
||||
");
|
||||
|
||||
instance.triggerFrame ();
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function triggerFrame ():Void {
|
||||
|
||||
handleEvent (event);
|
||||
|
||||
if (RenderEventManager.instance != null) {
|
||||
|
||||
RenderEventManager.instance.render ();
|
||||
|
||||
}
|
||||
|
||||
#if js
|
||||
Browser.window.requestAnimationFrame (cast triggerFrame);
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function removeEventListener (listener:IUpdateEventListener):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
@@ -3,12 +3,10 @@ package lime.graphics;
|
||||
|
||||
import lime.app.EventManager;
|
||||
import lime.system.System;
|
||||
#if js
|
||||
import js.Browser;
|
||||
#end
|
||||
import lime.ui.Window;
|
||||
|
||||
|
||||
@:access(lime.ui.Window)
|
||||
@:allow(lime.ui.Window)
|
||||
class RenderEventManager extends EventManager<IRenderEventListener> {
|
||||
|
||||
|
||||
@@ -24,38 +22,7 @@ class RenderEventManager extends EventManager<IRenderEventListener> {
|
||||
instance = this;
|
||||
event = new RenderEvent ();
|
||||
|
||||
#if js
|
||||
|
||||
untyped __js__ ("
|
||||
var lastTime = 0;
|
||||
var vendors = ['ms', 'moz', 'webkit', 'o'];
|
||||
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
|
||||
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
|
||||
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|
||||
|| window[vendors[x]+'CancelRequestAnimationFrame'];
|
||||
}
|
||||
|
||||
if (!window.requestAnimationFrame)
|
||||
window.requestAnimationFrame = function(callback, element) {
|
||||
var currTime = new Date().getTime();
|
||||
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
|
||||
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
|
||||
timeToCall);
|
||||
lastTime = currTime + timeToCall;
|
||||
return id;
|
||||
};
|
||||
|
||||
if (!window.cancelAnimationFrame)
|
||||
window.cancelAnimationFrame = function(id) {
|
||||
clearTimeout(id);
|
||||
};
|
||||
|
||||
window.requestAnimFrame = window.requestAnimationFrame;
|
||||
");
|
||||
|
||||
handleFrame ();
|
||||
|
||||
#elseif (cpp || neko)
|
||||
#if (cpp || neko)
|
||||
|
||||
lime_render_event_manager_register (handleEvent, event);
|
||||
|
||||
@@ -88,14 +55,17 @@ class RenderEventManager extends EventManager<IRenderEventListener> {
|
||||
}
|
||||
|
||||
|
||||
private function handleFrame ():Void {
|
||||
private static function registerWindow (window:Window):Void {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function render ():Void {
|
||||
|
||||
handleEvent (event);
|
||||
|
||||
#if js
|
||||
Browser.window.requestAnimationFrame (cast handleFrame);
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,12 @@ package lime.ui;
|
||||
import lime.app.EventManager;
|
||||
import lime.system.System;
|
||||
|
||||
#if js
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
|
||||
@:allow(lime.ui.Window)
|
||||
class KeyEventManager extends EventManager<IKeyEventListener> {
|
||||
|
||||
|
||||
@@ -18,7 +23,9 @@ class KeyEventManager extends EventManager<IKeyEventListener> {
|
||||
instance = this;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
lime_key_event_manager_register (handleEvent, new KeyEvent ());
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
@@ -62,6 +69,31 @@ class KeyEventManager extends EventManager<IKeyEventListener> {
|
||||
}
|
||||
|
||||
|
||||
private static function registerWindow (_):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
#if js
|
||||
|
||||
Browser.window.addEventListener ("keydown", function (event) {
|
||||
|
||||
instance.handleEvent (new KeyEvent (KEY_DOWN, 0));
|
||||
|
||||
}, false);
|
||||
|
||||
Browser.window.addEventListener ("keyup", function (event) {
|
||||
|
||||
instance.handleEvent (new KeyEvent (KEY_UP, 0));
|
||||
|
||||
}, false);
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function removeEventListener (listener:IKeyEventListener):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import lime.app.EventManager;
|
||||
import lime.system.System;
|
||||
|
||||
|
||||
@:allow(lime.ui.Window)
|
||||
class MouseEventManager extends EventManager<IMouseEventListener> {
|
||||
|
||||
|
||||
@@ -18,7 +19,9 @@ class MouseEventManager extends EventManager<IMouseEventListener> {
|
||||
instance = this;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
lime_mouse_event_manager_register (handleEvent, new MouseEvent ());
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
@@ -72,6 +75,43 @@ class MouseEventManager extends EventManager<IMouseEventListener> {
|
||||
}
|
||||
|
||||
|
||||
private static function registerWindow (window:Window):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
#if js
|
||||
|
||||
window.element.addEventListener ("mousedown", function (event) {
|
||||
|
||||
instance.handleEvent (new MouseEvent (MOUSE_DOWN, 0, 0));
|
||||
|
||||
}, true);
|
||||
|
||||
window.element.addEventListener ("mousemove", function (event) {
|
||||
|
||||
instance.handleEvent (new MouseEvent (MOUSE_MOVE, 0, 0));
|
||||
|
||||
}, true);
|
||||
|
||||
window.element.addEventListener ("mouseup", function (event) {
|
||||
|
||||
instance.handleEvent (new MouseEvent (MOUSE_UP, 0, 0));
|
||||
|
||||
}, true);
|
||||
|
||||
window.element.addEventListener ("mousewheel", function (event) {
|
||||
|
||||
//instance.handleEvent (new MouseEvent (MOUSE_DOWN, 0, 0));
|
||||
|
||||
}, true);
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function removeEventListener (listener:IMouseEventListener):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import lime.app.EventManager;
|
||||
import lime.system.System;
|
||||
|
||||
|
||||
@:allow(lime.ui.Window)
|
||||
class TouchEventManager extends EventManager<ITouchEventListener> {
|
||||
|
||||
|
||||
@@ -18,7 +19,9 @@ class TouchEventManager extends EventManager<ITouchEventListener> {
|
||||
instance = this;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
lime_touch_event_manager_register (handleEvent, new TouchEvent ());
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
@@ -70,6 +73,37 @@ class TouchEventManager extends EventManager<ITouchEventListener> {
|
||||
}
|
||||
|
||||
|
||||
private static function registerWindow (window:Window):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
#if js
|
||||
|
||||
window.element.addEventListener ("touchstart", function (event) {
|
||||
|
||||
instance.handleEvent (new TouchEvent (TOUCH_START, 0, 0, 0));
|
||||
|
||||
}, true);
|
||||
|
||||
window.element.addEventListener ("touchmove", function (event) {
|
||||
|
||||
instance.handleEvent (new TouchEvent (TOUCH_MOVE, 0, 0, 0));
|
||||
|
||||
}, true);
|
||||
|
||||
window.element.addEventListener ("touchend", function (event) {
|
||||
|
||||
instance.handleEvent (new TouchEvent (TOUCH_END, 0, 0, 0));
|
||||
|
||||
}, true);
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function removeEventListener (listener:ITouchEventListener):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package lime.ui;
|
||||
|
||||
import lime.app.Application;
|
||||
import lime.app.Config;
|
||||
import lime.app.UpdateEventManager;
|
||||
import lime.graphics.RenderEvent;
|
||||
import lime.graphics.RenderEventManager;
|
||||
import lime.system.System;
|
||||
@@ -11,6 +12,7 @@ import lime.system.System;
|
||||
import lime.graphics.opengl.GL;
|
||||
import js.html.webgl.RenderingContext;
|
||||
import js.html.CanvasElement;
|
||||
import js.html.HtmlElement;
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
@@ -18,6 +20,11 @@ import js.Browser;
|
||||
class Window {
|
||||
|
||||
|
||||
public static var instance:Window;
|
||||
|
||||
#if js
|
||||
public var element:HtmlElement;
|
||||
#end
|
||||
public var handle:Dynamic;
|
||||
public var height:Int;
|
||||
public var width:Int;
|
||||
@@ -30,18 +37,20 @@ class Window {
|
||||
|
||||
public function new (application:Application) {
|
||||
|
||||
instance = this;
|
||||
|
||||
this.application = application;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function create (config:Config):Void {
|
||||
public function create ():Void {
|
||||
|
||||
#if js
|
||||
|
||||
if (Std.is (config.element, CanvasElement)) {
|
||||
if (Std.is (element, CanvasElement)) {
|
||||
|
||||
canvas = cast config.element;
|
||||
canvas = cast element;
|
||||
|
||||
} else {
|
||||
|
||||
@@ -67,18 +76,15 @@ class Window {
|
||||
style.setProperty ("-webkit-transform", "translateZ(0)", null);
|
||||
style.setProperty ("transform", "translateZ(0)", null);
|
||||
|
||||
width = config.width;
|
||||
height = config.height;
|
||||
|
||||
//__originalWidth = width;
|
||||
//__originalHeight = height;
|
||||
|
||||
if (width == 0 && height == 0) {
|
||||
|
||||
if (config.element != null) {
|
||||
if (element != null) {
|
||||
|
||||
width = config.element.clientWidth;
|
||||
height = config.element.clientHeight;
|
||||
width = element.clientWidth;
|
||||
height = element.clientHeight;
|
||||
|
||||
} else {
|
||||
|
||||
@@ -112,13 +118,13 @@ class Window {
|
||||
//Browser.window.addEventListener ("focus", window_onFocus);
|
||||
//Browser.window.addEventListener ("blur", window_onBlur);
|
||||
|
||||
if (config.element != null) {
|
||||
if (element != null) {
|
||||
|
||||
if (canvas != null) {
|
||||
|
||||
if (config.element != cast canvas) {
|
||||
if (element != cast canvas) {
|
||||
|
||||
config.element.appendChild (canvas);
|
||||
element.appendChild (canvas);
|
||||
|
||||
}
|
||||
|
||||
@@ -176,6 +182,13 @@ class Window {
|
||||
handle = lime_window_create (application.handle);
|
||||
#end
|
||||
|
||||
KeyEventManager.registerWindow (this);
|
||||
MouseEventManager.registerWindow (this);
|
||||
RenderEventManager.registerWindow (this);
|
||||
TouchEventManager.registerWindow (this);
|
||||
UpdateEventManager.registerWindow (this);
|
||||
WindowEventManager.registerWindow (this);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,12 @@ package lime.ui;
|
||||
import lime.app.EventManager;
|
||||
import lime.system.System;
|
||||
|
||||
#if js
|
||||
import js.Browser;
|
||||
#end
|
||||
|
||||
|
||||
@:allow(lime.ui.Window)
|
||||
class WindowEventManager extends EventManager<IWindowEventListener> {
|
||||
|
||||
|
||||
@@ -18,7 +23,9 @@ class WindowEventManager extends EventManager<IWindowEventListener> {
|
||||
instance = this;
|
||||
|
||||
#if (cpp || neko)
|
||||
|
||||
lime_window_event_manager_register (handleEvent, new WindowEvent ());
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
@@ -62,6 +69,31 @@ class WindowEventManager extends EventManager<IWindowEventListener> {
|
||||
}
|
||||
|
||||
|
||||
private static function registerWindow (_):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
#if js
|
||||
|
||||
Browser.window.addEventListener ("focus", function (event) {
|
||||
|
||||
instance.handleEvent (new WindowEvent (WINDOW_ACTIVATE));
|
||||
|
||||
}, false);
|
||||
|
||||
Browser.window.addEventListener ("blur", function (event) {
|
||||
|
||||
instance.handleEvent (new WindowEvent (WINDOW_DEACTIVATE));
|
||||
|
||||
}, false);
|
||||
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function removeEventListener (listener:IWindowEventListener):Void {
|
||||
|
||||
if (instance != null) {
|
||||
|
||||
Reference in New Issue
Block a user