diff --git a/lime/app/Application.hx b/lime/app/Application.hx index e79a5a75d..3dbc8062a 100644 --- a/lime/app/Application.hx +++ b/lime/app/Application.hx @@ -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; 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); + } diff --git a/lime/app/UpdateEventManager.hx b/lime/app/UpdateEventManager.hx index ea3ccbcba..215ce150d 100644 --- a/lime/app/UpdateEventManager.hx +++ b/lime/app/UpdateEventManager.hx @@ -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 { 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 { } + 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) { diff --git a/lime/graphics/RenderEventManager.hx b/lime/graphics/RenderEventManager.hx index c6ebfe59c..500771429 100644 --- a/lime/graphics/RenderEventManager.hx +++ b/lime/graphics/RenderEventManager.hx @@ -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 { @@ -24,38 +22,7 @@ class RenderEventManager extends EventManager { 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 { } - 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 - } diff --git a/lime/ui/KeyEventManager.hx b/lime/ui/KeyEventManager.hx index ec095d0a1..b7daafd0e 100644 --- a/lime/ui/KeyEventManager.hx +++ b/lime/ui/KeyEventManager.hx @@ -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 { @@ -18,7 +23,9 @@ class KeyEventManager extends EventManager { instance = this; #if (cpp || neko) + lime_key_event_manager_register (handleEvent, new KeyEvent ()); + #end } @@ -62,6 +69,31 @@ class KeyEventManager extends EventManager { } + 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) { diff --git a/lime/ui/MouseEventManager.hx b/lime/ui/MouseEventManager.hx index ed5a132ed..8662e291a 100644 --- a/lime/ui/MouseEventManager.hx +++ b/lime/ui/MouseEventManager.hx @@ -5,6 +5,7 @@ import lime.app.EventManager; import lime.system.System; +@:allow(lime.ui.Window) class MouseEventManager extends EventManager { @@ -18,7 +19,9 @@ class MouseEventManager extends EventManager { instance = this; #if (cpp || neko) + lime_mouse_event_manager_register (handleEvent, new MouseEvent ()); + #end } @@ -72,6 +75,43 @@ class MouseEventManager extends EventManager { } + 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) { diff --git a/lime/ui/TouchEventManager.hx b/lime/ui/TouchEventManager.hx index 964d17ce5..3e8c9ae69 100644 --- a/lime/ui/TouchEventManager.hx +++ b/lime/ui/TouchEventManager.hx @@ -5,6 +5,7 @@ import lime.app.EventManager; import lime.system.System; +@:allow(lime.ui.Window) class TouchEventManager extends EventManager { @@ -18,7 +19,9 @@ class TouchEventManager extends EventManager { instance = this; #if (cpp || neko) + lime_touch_event_manager_register (handleEvent, new TouchEvent ()); + #end } @@ -70,6 +73,37 @@ class TouchEventManager extends EventManager { } + 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) { diff --git a/lime/ui/Window.hx b/lime/ui/Window.hx index 6b1dd8dc7..92bb6c9a0 100644 --- a/lime/ui/Window.hx +++ b/lime/ui/Window.hx @@ -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); + } diff --git a/lime/ui/WindowEventManager.hx b/lime/ui/WindowEventManager.hx index 87a5a41db..2d1c19f67 100644 --- a/lime/ui/WindowEventManager.hx +++ b/lime/ui/WindowEventManager.hx @@ -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 { @@ -18,7 +23,9 @@ class WindowEventManager extends EventManager { instance = this; #if (cpp || neko) + lime_window_event_manager_register (handleEvent, new WindowEvent ()); + #end } @@ -62,6 +69,31 @@ class WindowEventManager extends EventManager { } + 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) {