Make event managers always static

This commit is contained in:
Joshua Granick
2014-06-18 13:00:30 -07:00
parent 6a3d2df0b0
commit b2dc5f0001
5 changed files with 118 additions and 168 deletions

View File

@@ -101,12 +101,9 @@ class Application {
handle = lime_application_create (null); handle = lime_application_create (null);
#end #end
new KeyEventManager (); KeyEventManager.create ();
new MouseEventManager (); MouseEventManager.create ();
//new RenderEventManager (); TouchEventManager.create ();
new TouchEventManager ();
//new UpdateEventManager ();
//new WindowEventManager ();
KeyEventManager.onKeyDown.add (onKeyDown); KeyEventManager.onKeyDown.add (onKeyDown);
KeyEventManager.onKeyUp.add (onKeyUp); KeyEventManager.onKeyUp.add (onKeyUp);

View File

@@ -11,48 +11,41 @@ import flash.Lib;
#end #end
@:allow(lime.ui.Window)
class KeyEventManager { class KeyEventManager {
public static var onKeyDown = new Event<Int->Int->Void> (); public static var onKeyDown = new Event<Int->Int->Void> ();
public static var onKeyUp = new Event<Int->Int->Void> (); public static var onKeyUp = new Event<Int->Int->Void> ();
private static var instance:KeyEventManager; private static var eventInfo:KeyEventInfo;
private var eventInfo:KeyEventInfo;
public function new () { public static function create ():Void {
instance = this;
eventInfo = new KeyEventInfo (); eventInfo = new KeyEventInfo ();
#if (cpp || neko) #if js
lime_key_event_manager_register (dispatch, eventInfo);
Browser.window.addEventListener ("keydown", handleEvent, false);
Browser.window.addEventListener ("keyup", handleEvent, false);
#elseif flash
Lib.current.stage.addEventListener (flash.events.KeyboardEvent.KEY_DOWN, handleEvent);
Lib.current.stage.addEventListener (flash.events.KeyboardEvent.KEY_UP, handleEvent);
#elseif (cpp || neko)
lime_key_event_manager_register (handleEvent, eventInfo);
#end #end
} }
private function dispatch ():Void { private static function handleEvent (#if js event:js.html.KeyboardEvent #elseif flash event:flash.events.KeyboardEvent #end):Void {
switch (eventInfo.type) { #if js
case KEY_DOWN:
onKeyDown.dispatch (eventInfo.keyCode, eventInfo.modifier);
case KEY_UP:
onKeyUp.dispatch (eventInfo.keyCode, eventInfo.modifier);
}
}
#if js
private function handleDOMEvent (event:js.html.KeyboardEvent):Void {
//keyEvent.code = event.code; //keyEvent.code = event.code;
eventInfo.keyCode = (event.keyCode != null ? event.keyCode : event.which); eventInfo.keyCode = (event.keyCode != null ? event.keyCode : event.which);
@@ -67,14 +60,8 @@ class KeyEventManager {
//keyEvent.metaKey = event.metaKey; //keyEvent.metaKey = event.metaKey;
eventInfo.type = (event.type == "keydown" ? KEY_DOWN : KEY_UP); eventInfo.type = (event.type == "keydown" ? KEY_DOWN : KEY_UP);
dispatch ();
} #elseif flash
#end
#if flash
private function handleFlashEvent (event:flash.events.KeyboardEvent):Void {
eventInfo.keyCode = event.keyCode; eventInfo.keyCode = event.keyCode;
//keyEvent.key = event.charCode; //keyEvent.key = event.charCode;
@@ -85,23 +72,18 @@ class KeyEventManager {
//keyEvent.metaKey = event.commandKey; //keyEvent.metaKey = event.commandKey;
eventInfo.type = (event.type == flash.events.KeyboardEvent.KEY_DOWN ? KEY_DOWN : KEY_UP); eventInfo.type = (event.type == flash.events.KeyboardEvent.KEY_DOWN ? KEY_DOWN : KEY_UP);
dispatch ();
} #end
#end
private static function registerWindow (_):Void {
if (instance != null) { switch (eventInfo.type) {
#if js case KEY_DOWN:
Browser.window.addEventListener ("keydown", instance.handleDOMEvent, false);
Browser.window.addEventListener ("keyup", instance.handleDOMEvent, false); onKeyDown.dispatch (eventInfo.keyCode, eventInfo.modifier);
#elseif flash
Lib.current.stage.addEventListener (flash.events.KeyboardEvent.KEY_DOWN, instance.handleFlashEvent); case KEY_UP:
Lib.current.stage.addEventListener (flash.events.KeyboardEvent.KEY_UP, instance.handleFlashEvent);
#end onKeyUp.dispatch (eventInfo.keyCode, eventInfo.modifier);
} }

View File

@@ -19,47 +19,24 @@ class MouseEventManager {
public static var onMouseMove = new Event<Float->Float->Int->Void> (); public static var onMouseMove = new Event<Float->Float->Int->Void> ();
public static var onMouseUp = new Event<Float->Float->Int->Void> (); public static var onMouseUp = new Event<Float->Float->Int->Void> ();
private static var instance:MouseEventManager; private static var created:Bool;
private var eventInfo:MouseEventInfo; private static var eventInfo:MouseEventInfo;
public function new () { public static function create ():Void {
instance = this;
eventInfo = new MouseEventInfo (); eventInfo = new MouseEventInfo ();
#if (cpp || neko) #if (cpp || neko)
lime_mouse_event_manager_register (dispatch, eventInfo); lime_mouse_event_manager_register (handleEvent, eventInfo);
#end #end
} }
private function dispatch ():Void { private static function handleEvent (#if js event:js.html.MouseEvent #elseif flash event:flash.events.MouseEvent #end):Void {
switch (eventInfo.type) { #if js
case MOUSE_DOWN:
onMouseDown.dispatch (eventInfo.x, eventInfo.y, cast eventInfo.button);
case MOUSE_UP:
onMouseUp.dispatch (eventInfo.x, eventInfo.y, cast eventInfo.button);
case MOUSE_MOVE:
onMouseMove.dispatch (eventInfo.x, eventInfo.y, cast eventInfo.button);
default:
}
}
#if js
private function handleDOMEvent (event:js.html.MouseEvent):Void {
/* /*
var rect; var rect;
@@ -96,14 +73,7 @@ class MouseEventManager {
} }
dispatch (); #elseif flash
}
#end
#if flash
private function handleFlashEvent (event:flash.events.MouseEvent):Void {
eventInfo.x = event.stageX; eventInfo.x = event.stageX;
eventInfo.y = event.stageY; eventInfo.y = event.stageY;
@@ -116,37 +86,54 @@ class MouseEventManager {
} }
dispatch (); #end
switch (eventInfo.type) {
case MOUSE_DOWN:
onMouseDown.dispatch (eventInfo.x, eventInfo.y, cast eventInfo.button);
case MOUSE_UP:
onMouseUp.dispatch (eventInfo.x, eventInfo.y, cast eventInfo.button);
case MOUSE_MOVE:
onMouseMove.dispatch (eventInfo.x, eventInfo.y, cast eventInfo.button);
default:
}
} }
#end
private static function registerWindow (window:Window):Void { private static function registerWindow (window:Window):Void {
if (instance != null) { #if js
#if js window.element.addEventListener ("mousedown", handleEvent, true);
window.element.addEventListener ("mousedown", instance.handleDOMEvent, true); window.element.addEventListener ("mousemove", handleEvent, true);
window.element.addEventListener ("mousemove", instance.handleDOMEvent, true); window.element.addEventListener ("mouseup", handleEvent, true);
window.element.addEventListener ("mouseup", instance.handleDOMEvent, true); //window.element.addEventListener ("mousewheel", handleDOMEvent, true);
//window.element.addEventListener ("mousewheel", handleDOMEvent, true);
// Disable image drag on Firefox
// Disable image drag on Firefox /*Browser.document.addEventListener ("dragstart", function (e) {
/*Browser.document.addEventListener ("dragstart", function (e) { if (e.target.nodeName.toLowerCase() == "img") {
if (e.target.nodeName.toLowerCase() == "img") { e.preventDefault();
e.preventDefault(); return false;
return false; }
} return true;
return true; }, false);*/
}, false);*/
#elseif flash #elseif flash
Lib.current.stage.addEventListener (flash.events.MouseEvent.MOUSE_DOWN, instance.handleFlashEvent);
Lib.current.stage.addEventListener (flash.events.MouseEvent.MOUSE_MOVE, instance.handleFlashEvent); Lib.current.stage.addEventListener (flash.events.MouseEvent.MOUSE_DOWN, handleEvent);
Lib.current.stage.addEventListener (flash.events.MouseEvent.MOUSE_UP, instance.handleFlashEvent); Lib.current.stage.addEventListener (flash.events.MouseEvent.MOUSE_MOVE, handleEvent);
#end Lib.current.stage.addEventListener (flash.events.MouseEvent.MOUSE_UP, handleEvent);
} #end
} }

View File

@@ -11,53 +11,30 @@ import flash.Lib;
#end #end
@:allow(lime.ui.Window) @:allow(lime.ui.Window) class TouchEventManager {
class TouchEventManager {
public static var onTouchEnd = new Event<Float->Float->Int->Void> (); public static var onTouchEnd = new Event<Float->Float->Int->Void> ();
public static var onTouchMove = new Event<Float->Float->Int->Void> (); public static var onTouchMove = new Event<Float->Float->Int->Void> ();
public static var onTouchStart = new Event<Float->Float->Int->Void> (); public static var onTouchStart = new Event<Float->Float->Int->Void> ();
private static var instance:TouchEventManager; private static var eventInfo:TouchEventInfo;
private var eventInfo:TouchEventInfo;
public function new () { public static function create ():Void {
instance = this;
eventInfo = new TouchEventInfo (); eventInfo = new TouchEventInfo ();
#if (cpp || neko) #if (cpp || neko)
lime_touch_event_manager_register (dispatch, eventInfo); lime_touch_event_manager_register (handleEvent, eventInfo);
#end #end
} }
private function dispatch ():Void { private static function handleEvent (#if js event:js.html.TouchEvent #elseif flash event:flash.events.TouchEvent #end):Void {
switch (eventInfo.type) { #if js
case TOUCH_START:
onTouchStart.dispatch (eventInfo.x, eventInfo.y, eventInfo.id);
case TOUCH_END:
onTouchEnd.dispatch (eventInfo.x, eventInfo.y, eventInfo.id);
case TOUCH_MOVE:
onTouchMove.dispatch (eventInfo.x, eventInfo.y, eventInfo.id);
}
}
#if js
private function handleDOMEvent (event:js.html.TouchEvent):Void {
event.preventDefault (); event.preventDefault ();
@@ -76,8 +53,6 @@ class TouchEventManager {
} }
dispatch ();
/* /*
event.preventDefault (); event.preventDefault ();
@@ -126,12 +101,7 @@ class TouchEventManager {
} }
*/ */
} #elseif flash
#end
#if flash
private function handleFlashEvent (event:flash.events.TouchEvent):Void {
//touchEvent.id = event.touchPointID; //touchEvent.id = event.touchPointID;
eventInfo.x = event.stageX; eventInfo.x = event.stageX;
@@ -145,28 +115,43 @@ class TouchEventManager {
} }
dispatch (); #end
switch (eventInfo.type) {
case TOUCH_START:
onTouchStart.dispatch (eventInfo.x, eventInfo.y, eventInfo.id);
case TOUCH_END:
onTouchEnd.dispatch (eventInfo.x, eventInfo.y, eventInfo.id);
case TOUCH_MOVE:
onTouchMove.dispatch (eventInfo.x, eventInfo.y, eventInfo.id);
}
} }
#end
private static function registerWindow (window:Window):Void { private static function registerWindow (window:Window):Void {
if (instance != null) { #if js
#if js window.element.addEventListener ("touchstart", handleEvent, true);
window.element.addEventListener ("touchstart", instance.handleDOMEvent, true); window.element.addEventListener ("touchmove", handleEvent, true);
window.element.addEventListener ("touchmove", instance.handleDOMEvent, true); window.element.addEventListener ("touchend", handleEvent, true);
window.element.addEventListener ("touchend", instance.handleDOMEvent, true);
#elseif flash #elseif flash
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
Lib.current.stage.addEventListener (flash.events.TouchEvent.TOUCH_BEGIN, instance.handleFlashEvent); Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
Lib.current.stage.addEventListener (flash.events.TouchEvent.TOUCH_MOVE, instance.handleFlashEvent); Lib.current.stage.addEventListener (flash.events.TouchEvent.TOUCH_BEGIN, handleEvent);
Lib.current.stage.addEventListener (flash.events.TouchEvent.TOUCH_END, instance.handleFlashEvent); Lib.current.stage.addEventListener (flash.events.TouchEvent.TOUCH_MOVE, handleEvent);
#end Lib.current.stage.addEventListener (flash.events.TouchEvent.TOUCH_END, handleEvent);
} #end
} }

View File

@@ -184,7 +184,6 @@ class Window {
handle = lime_window_create (application.handle, flags); handle = lime_window_create (application.handle, flags);
#end #end
KeyEventManager.registerWindow (this);
MouseEventManager.registerWindow (this); MouseEventManager.registerWindow (this);
TouchEventManager.registerWindow (this); TouchEventManager.registerWindow (this);