Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,103 @@
package kha.input;
@:allow(kha.SystemImpl)
@:expose
class Gamepad {
var index: Int;
public static function get(index: Int = 0): Gamepad {
if (index >= instances.length)
return null;
return instances[index];
}
public static function notifyOnConnect(?connectListener: Int->Void, ?disconnectListener: Int->Void): Void {
if (connectListener != null)
connectListeners.push(connectListener);
if (disconnectListener != null)
disconnectListeners.push(disconnectListener);
}
public static function removeConnect(?connectListener: Int->Void, ?disconnectListener: Int->Void): Void {
if (connectListener != null)
connectListeners.remove(connectListener);
if (disconnectListener != null)
disconnectListeners.remove(disconnectListener);
}
public function notify(?axisListener: Int->Float->Void, ?buttonListener: Int->Float->Void): Void {
if (axisListener != null)
axisListeners.push(axisListener);
if (buttonListener != null)
buttonListeners.push(buttonListener);
}
public function remove(?axisListener: Int->Float->Void, ?buttonListener: Int->Float->Void): Void {
if (axisListener != null)
axisListeners.remove(axisListener);
if (buttonListener != null)
buttonListeners.remove(buttonListener);
}
static var instances: Array<Gamepad> = new Array();
var axisListeners: Array<Int->Float->Void>;
var buttonListeners: Array<Int->Float->Void>;
static var connectListeners: Array<Int->Void> = new Array();
static var disconnectListeners: Array<Int->Void> = new Array();
function new(index: Int = 0, id: String = "unknown") {
connected = false;
this.index = index;
axisListeners = new Array<Int->Float->Void>();
buttonListeners = new Array<Int->Float->Void>();
instances[index] = this;
}
public var id(get, null): String;
public var vendor(get, null): String;
public var connected(default, null): Bool;
public function rumble(leftAmount: Float, rightAmount: Float) {
SystemImpl.setGamepadRumble(index, leftAmount, rightAmount);
}
function get_id(): String {
return SystemImpl.getGamepadId(index);
}
function get_vendor(): String {
return SystemImpl.getGamepadVendor(index);
}
@input
function sendAxisEvent(axis: Int, value: Float): Void {
for (listener in axisListeners) {
listener(axis, value);
}
}
@input
function sendButtonEvent(button: Int, value: Float): Void {
for (listener in buttonListeners) {
listener(button, value);
}
}
@input
static function sendConnectEvent(index: Int): Void {
instances[index].connected = true;
for (listener in connectListeners) {
listener(index);
}
}
@input
static function sendDisconnectEvent(index: Int): Void {
instances[index].connected = false;
for (listener in disconnectListeners) {
listener(index);
}
}
}

View File

@ -0,0 +1,191 @@
package kha.input;
enum abstract KeyCode(Int) to Int {
var Unknown = 0;
var Back = 1; // Android
var Cancel = 3;
var Help = 6;
var Backspace = 8;
var Tab = 9;
var Clear = 12;
var Return = 13;
var Shift = 16;
var Control = 17;
var Alt = 18;
var Pause = 19;
var CapsLock = 20;
var Kana = 21;
var Hangul = 21;
var Eisu = 22;
var Junja = 23;
var Final = 24;
var Hanja = 25;
var Kanji = 25;
var Escape = 27;
var Convert = 28;
var NonConvert = 29;
var Accept = 30;
var ModeChange = 31;
var Space = 32;
var PageUp = 33;
var PageDown = 34;
var End = 35;
var Home = 36;
var Left = 37;
var Up = 38;
var Right = 39;
var Down = 40;
var Select = 41;
var Print = 42;
var Execute = 43;
var PrintScreen = 44;
var Insert = 45;
var Delete = 46;
var Zero = 48;
var One = 49;
var Two = 50;
var Three = 51;
var Four = 52;
var Five = 53;
var Six = 54;
var Seven = 55;
var Eight = 56;
var Nine = 57;
var Colon = 58;
var Semicolon = 59;
var LessThan = 60;
var Equals = 61;
var GreaterThan = 62;
var QuestionMark = 63;
var At = 64;
var A = 65;
var B = 66;
var C = 67;
var D = 68;
var E = 69;
var F = 70;
var G = 71;
var H = 72;
var I = 73;
var J = 74;
var K = 75;
var L = 76;
var M = 77;
var N = 78;
var O = 79;
var P = 80;
var Q = 81;
var R = 82;
var S = 83;
var T = 84;
var U = 85;
var V = 86;
var W = 87;
var X = 88;
var Y = 89;
var Z = 90;
var Win = 91;
var ContextMenu = 93;
var Sleep = 95;
var Numpad0 = 96;
var Numpad1 = 97;
var Numpad2 = 98;
var Numpad3 = 99;
var Numpad4 = 100;
var Numpad5 = 101;
var Numpad6 = 102;
var Numpad7 = 103;
var Numpad8 = 104;
var Numpad9 = 105;
var Multiply = 106;
var Add = 107;
var Separator = 108;
var Subtract = 109;
var Decimal = 110;
var Divide = 111;
var F1 = 112;
var F2 = 113;
var F3 = 114;
var F4 = 115;
var F5 = 116;
var F6 = 117;
var F7 = 118;
var F8 = 119;
var F9 = 120;
var F10 = 121;
var F11 = 122;
var F12 = 123;
var F13 = 124;
var F14 = 125;
var F15 = 126;
var F16 = 127;
var F17 = 128;
var F18 = 129;
var F19 = 130;
var F20 = 131;
var F21 = 132;
var F22 = 133;
var F23 = 134;
var F24 = 135;
var NumLock = 144;
var ScrollLock = 145;
var WinOemFjJisho = 146;
var WinOemFjMasshou = 147;
var WinOemFjTouroku = 148;
var WinOemFjLoya = 149;
var WinOemFjRoya = 150;
var Circumflex = 160;
var Exclamation = 161;
var DoubleQuote = 162;
var Hash = 163;
var Dollar = 164;
var Percent = 165;
var Ampersand = 166;
var Underscore = 167;
var OpenParen = 168;
var CloseParen = 169;
var Asterisk = 170;
var Plus = 171;
var Pipe = 172;
var HyphenMinus = 173;
var OpenCurlyBracket = 174;
var CloseCurlyBracket = 175;
var Tilde = 176;
var VolumeMute = 181;
var VolumeDown = 182;
var VolumeUp = 183;
var Comma = 188;
var Period = 190;
var Slash = 191;
var BackQuote = 192;
var OpenBracket = 219;
var BackSlash = 220;
var CloseBracket = 221;
var Quote = 222;
var Meta = 224;
var AltGr = 225;
var WinIcoHelp = 227;
var WinIco00 = 228;
var WinIcoClear = 230;
var WinOemReset = 233;
var WinOemJump = 234;
var WinOemPA1 = 235;
var WinOemPA2 = 236;
var WinOemPA3 = 237;
var WinOemWSCTRL = 238;
var WinOemCUSEL = 239;
var WinOemATTN = 240;
var WinOemFinish = 241;
var WinOemCopy = 242;
var WinOemAuto = 243;
var WinOemENLW = 244;
var WinOemBackTab = 245;
var ATTN = 246;
var CRSEL = 247;
var EXSEL = 248;
var EREOF = 249;
var Play = 250;
var Zoom = 251;
var PA1 = 253;
var WinOemClear = 254;
}

View File

@ -0,0 +1,115 @@
package kha.input;
import kha.netsync.Controller;
/** See `Keyboard.disableSystemInterventions` */
enum BlockInterventions {
Default;
Full;
None;
Custom(func: (code: KeyCode) -> Bool);
}
@:allow(kha.SystemImpl)
@:expose
class Keyboard extends Controller {
static var keyBehavior = BlockInterventions.Default;
/**
* Get current Keyboard.
* @param num (optional) keyboard id (0 by default).
*/
public static function get(num: Int = 0): Keyboard {
return SystemImpl.getKeyboard(num);
}
/**
* Disables system hotkeys (html5 only).
* @param behavior can be:
* Default - allow F-keys and char keys.
* Full - disable all keys (that browser allows).
* None - do not block any key.
* Custom(func:(code:Int)->Bool) - set custom handler for keydown event (should return true if keycode blocked).
*/
public static function disableSystemInterventions(behavior: BlockInterventions): Void {
keyBehavior = behavior;
}
/**
* Creates event handlers from passed functions.
* @param downListener (optional) function with `key:KeyCode` argument, fired when a key is pressed down.
* @param upListener (optional) function with `key:KeyCode` argument, fired when a key is released.
* @param pressListener (optional) function with `char:String` argument, fired when a key that produces a character value is pressed down.
*/
public function notify(?downListener: (key: KeyCode) -> Void, ?upListener: (key: KeyCode) -> Void, ?pressListener: (char: String) -> Void = null): Void {
if (downListener != null)
downListeners.push(downListener);
if (upListener != null)
upListeners.push(upListener);
if (pressListener != null)
pressListeners.push(pressListener);
}
/**
* Removes event handlers from the passed functions that were passed to `notify` function.
*/
public function remove(?downListener: (key: KeyCode) -> Void, ?upListener: (key: KeyCode) -> Void, ?pressListener: (char: String) -> Void): Void {
if (downListener != null)
downListeners.remove(downListener);
if (upListener != null)
upListeners.remove(upListener);
if (pressListener != null)
pressListeners.remove(pressListener);
}
/**
* Show virtual keyboard (if it exists).
*/
public function show(): Void {}
/**
* Hide virtual keyboard (if it exists).
*/
public function hide(): Void {}
static var instance: Keyboard;
var downListeners: Array<(key: KeyCode) -> Void>;
var upListeners: Array<(key: KeyCode) -> Void>;
var pressListeners: Array<(char: String) -> Void>;
function new() {
super();
downListeners = [];
upListeners = [];
pressListeners = [];
instance = this;
}
@input
function sendDownEvent(code: KeyCode): Void {
#if sys_server
// js.Node.console.log(kha.Scheduler.time() + " Down: " + key + " from " + kha.network.Session.the().me.id);
#end
for (listener in downListeners) {
listener(code);
}
}
@input
function sendUpEvent(code: KeyCode): Void {
#if sys_server
// js.Node.console.log(kha.Scheduler.time() + " Up: " + key + " from " + kha.network.Session.the().me.id);
#end
for (listener in upListeners) {
listener(code);
}
}
@input
function sendPressEvent(char: String): Void {
for (listener in pressListeners) {
listener(char);
}
}
}

View File

@ -0,0 +1,326 @@
package kha.input;
import kha.netsync.Controller;
/** See `Mouse.setWheelEventBlockBehavior` */
enum MouseEventBlockBehavior {
Full;
None;
Custom(func: (event: Dynamic) -> Bool);
}
enum MouseCursor {
Default;
Pointer;
Text;
EastWestResize;
NorthSouthResize;
NorthEastResize;
SouthEastResize;
NorthWestResize;
SouthWestResize;
Grab;
Grabbing;
NotAllowed;
Wait;
Crosshair;
Custom(image: kha.Image);
}
@:allow(kha.SystemImpl)
@:expose
class Mouse extends Controller {
static var wheelEventBlockBehavior = MouseEventBlockBehavior.Full;
/**
* Get current Mouse.
* @param num (optional) mouse id (0 by default).
*/
public static function get(num: Int = 0): Mouse {
return SystemImpl.getMouse(num);
}
/**
* Allows fine grained control of mouse wheel browser default actions (html5 only).
* @param behavior can be:
* Full - block wheel events.
* None - do not block wheel events.
* Custom(func:(event:WheelEvent)->Bool) - set custom handler for wheel event (should return true if wheel event blocked).
*/
public static function setWheelEventBlockBehavior(behavior: MouseEventBlockBehavior): Void {
wheelEventBlockBehavior = behavior;
}
/**
* Creates event handlers from passed functions.
* @param downListener (optional) function with `button:Int`,`x:Int`,`y:Int` arguments, fired when a mouse is pressed down. `button:Int` is `0` for left button, `1` for right and `2` for middle.
* @param upListener (optional) function with `button:Int`,`x:Int`,`y:Int` arguments, fired when a mouse is released.
* @param moveListener (optional) function with `x:Int`,`y:Int`,`moveX:Int`,`moveY:Int` arguments, fired when a mouse is moved. `moveX`/`moveY` is the difference between the current coordinates and the last position of the mouse.
* @param wheelListener (optional) function with `delta:Int` argument, fired when the wheel rotates. It can have a value of `1` or `-1` depending on the rotation.
* @param leaveListener (optional) function without` arguments, when fired mouse leave canvas.
*/
public function notify(?downListener: (button: Int, x: Int, y: Int) -> Void, ?upListener: (button: Int, x: Int, y: Int) -> Void,
?moveListener: (x: Int, y: Int, moveX: Int, moveY: Int) -> Void, ?wheelListener: (delta: Int) -> Void, ?leaveListener: () -> Void = null): Void {
notifyWindowed(0, downListener, upListener, moveListener, wheelListener, leaveListener);
}
/**
* Removes event handlers from the passed functions that were passed to `notify` function.
*/
public function remove(?downListener: (button: Int, x: Int, y: Int) -> Void, ?upListener: (button: Int, x: Int, y: Int) -> Void,
?moveListener: (x: Int, y: Int, moveX: Int, moveY: Int) -> Void, ?wheelListener: (delta: Int) -> Void, ?leaveListener: () -> Void = null): Void {
removeWindowed(0, downListener, upListener, moveListener, wheelListener, leaveListener);
}
/**
* Creates event handlers from passed functions like `notify` function, but only for window with `windowId:Int` id argument. The windows are not supported by all the targets.
*/
public function notifyWindowed(windowId: Int, ?downListener: Int->Int->Int->Void, ?upListener: Int->Int->Int->Void,
?moveListener: Int->Int->Int->Int->Void, ?wheelListener: Int->Void, ?leaveListener: Void->Void = null): Void {
if (downListener != null) {
if (windowDownListeners == null) {
windowDownListeners = new Array();
}
while (windowDownListeners.length <= windowId) {
windowDownListeners.push(new Array());
}
windowDownListeners[windowId].push(downListener);
}
if (upListener != null) {
if (windowUpListeners == null) {
windowUpListeners = new Array();
}
while (windowUpListeners.length <= windowId) {
windowUpListeners.push(new Array());
}
windowUpListeners[windowId].push(upListener);
}
if (moveListener != null) {
if (windowMoveListeners == null) {
windowMoveListeners = new Array();
}
while (windowMoveListeners.length <= windowId) {
windowMoveListeners.push(new Array());
}
windowMoveListeners[windowId].push(moveListener);
}
if (wheelListener != null) {
if (windowWheelListeners == null) {
windowWheelListeners = new Array();
}
while (windowWheelListeners.length <= windowId) {
windowWheelListeners.push(new Array());
}
windowWheelListeners[windowId].push(wheelListener);
}
if (leaveListener != null) {
if (windowLeaveListeners == null) {
windowLeaveListeners = new Array();
}
while (windowLeaveListeners.length <= windowId) {
windowLeaveListeners.push(new Array());
}
windowLeaveListeners[windowId].push(leaveListener);
}
}
/**
* Removes event handlers for `windowId:Int` from the passed functions that were passed to `notifyWindowed` function.
*/
public function removeWindowed(windowId: Int, ?downListener: Int->Int->Int->Void, ?upListener: Int->Int->Int->Void,
?moveListener: Int->Int->Int->Int->Void, ?wheelListener: Int->Void, ?leaveListener: Void->Void = null): Void {
if (downListener != null) {
if (windowDownListeners != null) {
if (windowId < windowDownListeners.length) {
windowDownListeners[windowId].remove(downListener);
}
else {
trace('no downListeners for window "${windowId}" are registered');
}
}
else {
trace('no downListeners were ever registered');
}
}
if (upListener != null) {
if (windowUpListeners != null) {
if (windowId < windowUpListeners.length) {
windowUpListeners[windowId].remove(upListener);
}
else {
trace('no upListeners for window "${windowId}" are registered');
}
}
else {
trace('no upListeners were ever registered');
}
}
if (moveListener != null) {
if (windowMoveListeners != null) {
if (windowId < windowMoveListeners.length) {
windowMoveListeners[windowId].remove(moveListener);
}
else {
trace('no moveListeners for window "${windowId}" are registered');
}
}
else {
trace('no moveListeners were ever registered');
}
}
if (wheelListener != null) {
if (windowWheelListeners != null) {
if (windowId < windowWheelListeners.length) {
windowWheelListeners[windowId].remove(wheelListener);
}
else {
trace('no wheelListeners for window "${windowId}" are registered');
}
}
else {
trace('no wheelListeners were ever registered');
}
}
if (leaveListener != null) {
if (windowLeaveListeners != null) {
if (windowId < windowLeaveListeners.length) {
windowLeaveListeners[windowId].remove(leaveListener);
}
else {
trace('no leaveListeners for window "${windowId}" are registered');
}
}
else {
trace('no leaveListeners were ever registered');
}
}
}
/**
* Locks the cursor position and hides it. For catching movements, use the `moveX`/`moveY` arguments of your `moveListener` handler.
*/
public function lock(): Void {}
/**
* Unlock the cursor position and hides it. For catching movements, use the `moveX`/`moveY` arguments of your `moveListener` handler.
*/
public function unlock(): Void {}
/**
* Unlocks the cursor position and displays it.
*/
public function canLock(): Bool {
return false;
}
/**
* Returns the status of the cursor lock
*/
public function isLocked(): Bool {
return false;
}
/**
* Creates event handlers from passed functions.
* @param change function fired when the lock is turned on / off.
* @param error function fired when a toggle error occurs.
*/
public function notifyOnLockChange(change: Void->Void, error: Void->Void): Void {}
/**
* Removes event handlers from the passed functions that were passed to `notifyOnLockChange` function.
*/
public function removeFromLockChange(change: Void->Void, error: Void->Void): Void {}
/**
* Hides the system cursor (without locking)
*/
public function hideSystemCursor(): Void {}
/**
* Show the system cursor
*/
public function showSystemCursor(): Void {}
/**
* Set the native system cursor
* @param cursor The native cursor to show.
*/
public function setSystemCursor(cursor: MouseCursor): Void {}
static var instance: Mouse;
var windowDownListeners: Array<Array<Int->Int->Int->Void>>;
var windowUpListeners: Array<Array<Int->Int->Int->Void>>;
var windowMoveListeners: Array<Array<Int->Int->Int->Int->Void>>;
var windowWheelListeners: Array<Array<Int->Void>>;
var windowLeaveListeners: Array<Array<Void->Void>>;
function new() {
super();
instance = this;
}
@input
function sendLeaveEvent(windowId: Int): Void {
if (windowLeaveListeners != null) {
for (listener in windowLeaveListeners[windowId]) {
listener();
}
}
}
@input
function sendDownEvent(windowId: Int, button: Int, x: Int, y: Int): Void {
if (windowDownListeners != null) {
for (listener in windowDownListeners[windowId]) {
listener(button, x, y);
}
}
}
@input
function sendUpEvent(windowId: Int, button: Int, x: Int, y: Int): Void {
if (windowUpListeners != null) {
for (listener in windowUpListeners[windowId]) {
listener(button, x, y);
}
}
}
@input
function sendMoveEvent(windowId: Int, x: Int, y: Int, movementX: Int, movementY: Int): Void {
if (windowMoveListeners != null) {
for (listener in windowMoveListeners[windowId]) {
listener(x, y, movementX, movementY);
}
}
}
@input
function sendWheelEvent(windowId: Int, delta: Int): Void {
if (windowWheelListeners != null) {
for (listener in windowWheelListeners[windowId]) {
listener(delta);
}
}
}
}

View File

@ -0,0 +1,230 @@
package kha.input;
@:allow(kha.SystemImpl)
class Pen {
/**
* Get current Pen.
* @param num (optional) pen id (0 by default).
*/
public static function get(num: Int = 0): Pen {
return SystemImpl.getPen(num);
}
/**
* Creates event handlers from passed functions.
* @param downListener (optional) function with `x:Int`,`y:Int`,`pressure:Float` arguments, fired when a pen is pressed down. `pressure` is force of pressure on the screen in the range from `0` to `1`.
* @param upListener (optional) function with `x:Int`,`y:Int`,`pressure:Float` arguments, fired when a pen is released.
* @param moveListener (optional) function with `x:Int`,`y:Int`,`pressure:Float` arguments, fired when a pen is moved.
*/
public function notify(?downListener: Int->Int->Float->Void, ?upListener: Int->Int->Float->Void, ?moveListener: Int->Int->Float->Void): Void {
notifyWindowed(0, downListener, upListener, moveListener);
}
/**
* Creates event handlers from passed functions specific to the pen's eraser.
* @param downListener function with `x:Int`,`y:Int`,`pressure:Float` arguments, fired when an eraser is pressed down. `pressure` is force of pressure on the screen in the range from `0` to `1`.
* @param upListener function with `x:Int`,`y:Int`,`pressure:Float` arguments, fired when an eraser is released.
* @param moveListener function with `x:Int`,`y:Int`,`pressure:Float` arguments, fired when an eraser is moved.
*/
public function notifyEraser(eraserDownListener: Int->Int->Float->Void, eraserUpListener: Int->Int->Float->Void,
eraserMoveListener: Int->Int->Float->Void): Void {
notifyEraserWindowed(0, eraserDownListener, eraserUpListener, eraserMoveListener);
}
/**
* Removes event handlers from the passed functions that were passed to `notify` function.
*/
public function remove(?downListener: Int->Int->Float->Void, ?upListener: Int->Int->Float->Void, ?moveListener: Int->Int->Float->Void): Void {
removeWindowed(0, downListener, upListener, moveListener);
}
/**
* Removes event handlers from the passed functions that were passed to `notifyEraser` function.
*/
public function removeEraser(eraserDownListener: Int->Int->Float->Void, eraserUpListener: Int->Int->Float->Void,
eraserMoveListener: Int->Int->Float->Void): Void {
removeEraserWindowed(0, eraserDownListener, eraserUpListener, eraserMoveListener);
}
/**
* Creates event handlers from passed functions like `notify` function, but only for window with `windowId:Int` id argument. The windows are not supported by all the targets.
*/
public function notifyWindowed(windowId: Int, ?downListener: Int->Int->Float->Void, ?upListener: Int->Int->Float->Void,
?moveListener: Int->Int->Float->Void): Void {
if (downListener != null) {
if (windowDownListeners == null) {
windowDownListeners = [];
}
while (windowDownListeners.length <= windowId) {
windowDownListeners.push([]);
}
windowDownListeners[windowId].push(downListener);
}
if (upListener != null) {
if (windowUpListeners == null) {
windowUpListeners = [];
}
while (windowUpListeners.length <= windowId) {
windowUpListeners.push([]);
}
windowUpListeners[windowId].push(upListener);
}
if (moveListener != null) {
if (windowMoveListeners == null) {
windowMoveListeners = [];
}
while (windowMoveListeners.length <= windowId) {
windowMoveListeners.push([]);
}
windowMoveListeners[windowId].push(moveListener);
}
}
/**
* Creates event handlers from passed functions like `notifyEraser` function, but only for window with `windowId:Int` id argument. The windows are not supported by all the targets.
*/
public function notifyEraserWindowed(windowId: Int, eraserDownListener: Int->Int->Float->Void, eraserUpListener: Int->Int->Float->Void,
eraserMoveListener: Int->Int->Float->Void): Void {
if (eraserDownListener != null) {
if (windowEraserDownListeners == null) {
windowEraserDownListeners = [];
}
while (windowEraserDownListeners.length <= windowId) {
windowEraserDownListeners.push([]);
}
windowEraserDownListeners[windowId].push(eraserDownListener);
}
if (eraserUpListener != null) {
if (windowEraserUpListeners == null) {
windowEraserUpListeners = [];
}
while (windowEraserUpListeners.length <= windowId) {
windowEraserUpListeners.push([]);
}
windowEraserUpListeners[windowId].push(eraserUpListener);
}
if (eraserMoveListener != null) {
if (windowEraserMoveListeners == null) {
windowEraserMoveListeners = [];
}
while (windowEraserMoveListeners.length <= windowId) {
windowEraserMoveListeners.push([]);
}
windowEraserMoveListeners[windowId].push(eraserMoveListener);
}
}
/**
* Removes event handlers for `windowId:Int` from the passed functions that were passed to `notifyWindowed` function.
*/
public function removeWindowed(windowId: Int, ?downListener: Int->Int->Float->Void, ?upListener: Int->Int->Float->Void,
?moveListener: Int->Int->Float->Void): Void {
if (downListener != null && windowDownListeners != null) {
if (windowId < windowDownListeners.length) {
windowDownListeners[windowId].remove(downListener);
}
}
if (upListener != null && windowUpListeners != null) {
if (windowId < windowUpListeners.length) {
windowUpListeners[windowId].remove(upListener);
}
}
if (moveListener != null && windowMoveListeners != null) {
if (windowId < windowMoveListeners.length) {
windowMoveListeners[windowId].remove(moveListener);
}
}
}
/**
* Removes event handlers for `windowId:Int` from the passed functions that were passed to `notifyEraserWindowed` function.
*/
public function removeEraserWindowed(windowId: Int, eraserDownListener: Int->Int->Float->Void, eraserUpListener: Int->Int->Float->Void,
eraserMoveListener: Int->Int->Float->Void): Void {
if (eraserDownListener != null && windowEraserDownListeners != null) {
if (windowId < windowEraserDownListeners.length) {
windowEraserDownListeners[windowId].remove(eraserDownListener);
}
}
if (eraserUpListener != null && windowEraserUpListeners != null) {
if (windowId < windowEraserUpListeners.length) {
windowEraserUpListeners[windowId].remove(eraserUpListener);
}
}
if (eraserMoveListener != null && windowEraserMoveListeners != null) {
if (windowId < windowEraserMoveListeners.length) {
windowEraserMoveListeners[windowId].remove(eraserMoveListener);
}
}
}
static var instance: Pen;
var windowDownListeners: Array<Array<Int->Int->Float->Void>>;
var windowUpListeners: Array<Array<Int->Int->Float->Void>>;
var windowMoveListeners: Array<Array<Int->Int->Float->Void>>;
var windowEraserDownListeners: Array<Array<Int->Int->Float->Void>>;
var windowEraserUpListeners: Array<Array<Int->Int->Float->Void>>;
var windowEraserMoveListeners: Array<Array<Int->Int->Float->Void>>;
function new() {
instance = this;
}
function sendDownEvent(windowId: Int, x: Int, y: Int, pressure: Float): Void {
if (windowDownListeners != null) {
for (listener in windowDownListeners[windowId]) {
listener(x, y, pressure);
}
}
}
function sendUpEvent(windowId: Int, x: Int, y: Int, pressure: Float): Void {
if (windowUpListeners != null) {
for (listener in windowUpListeners[windowId]) {
listener(x, y, pressure);
}
}
}
function sendMoveEvent(windowId: Int, x: Int, y: Int, pressure: Float): Void {
if (windowMoveListeners != null) {
for (listener in windowMoveListeners[windowId]) {
listener(x, y, pressure);
}
}
}
function sendEraserDownEvent(windowId: Int, x: Int, y: Int, pressure: Float): Void {
if (windowEraserDownListeners != null) {
for (listener in windowEraserDownListeners[windowId]) {
listener(x, y, pressure);
}
}
}
function sendEraserUpEvent(windowId: Int, x: Int, y: Int, pressure: Float): Void {
if (windowEraserUpListeners != null) {
for (listener in windowEraserUpListeners[windowId]) {
listener(x, y, pressure);
}
}
}
function sendEraserMoveEvent(windowId: Int, x: Int, y: Int, pressure: Float): Void {
if (windowEraserMoveListeners != null) {
for (listener in windowEraserMoveListeners[windowId]) {
listener(x, y, pressure);
}
}
}
}

View File

@ -0,0 +1,17 @@
package kha.input;
#if (cpp || hl)
extern class Sensor {
public static function get(type: SensorType): Sensor;
public function notify(listener: Float->Float->Float->Void): Void;
}
#else
class Sensor {
public static function get(type: SensorType): Sensor {
return null;
}
public function notify(listener: Float->Float->Float->Void): Void {}
}
#end

View File

@ -0,0 +1,6 @@
package kha.input;
enum abstract SensorType(Int) {
var Accelerometer = 0;
var Gyroscope = 1;
}

View File

@ -0,0 +1,95 @@
package kha.input;
/** See `Surface.setTouchDownEventBlockBehavior` */
enum TouchDownEventBlockBehavior {
Full;
None;
Custom(func: (event: Dynamic) -> Bool);
}
@:allow(kha.SystemImpl)
@:expose
class Surface {
static var touchDownEventBlockBehavior = TouchDownEventBlockBehavior.Full;
/**
* Get current Surface.
* @param num (optional) surface id (0 by default).
*/
public static function get(num: Int = 0): Surface {
if (num != 0)
return null;
return instance;
}
/**
* Allows fine grained control of touch down browser default actions (html5 only).
* @param behavior can be:
* Full - block touch down events.
* None - do not block touch down events.
* Custom(func:(event:TouchEvent)->Bool) - set custom handler for touch down event (should return true if touch down event blocked).
*/
public static function setTouchDownEventBlockBehavior(behavior: TouchDownEventBlockBehavior): Void {
touchDownEventBlockBehavior = behavior;
}
/**
* Creates event handlers from passed functions.
* @param touchStartListener (optional) function with `id:Int`,`x:Int`,`y:Int` arguments, fired when a surface is pressed down. The finger `id` goes from 0 increasing by one. When the finger releases the screen, the old `id` is freed up and will be occupied with pressing the next finger (when releasing a finger, the shift of ids does not occur).
* @param touchEndListener (optional) function with `id:Int`,`x:Int`,`y:Int` arguments, fired when a surface is released.
* @param moveListener (optional) function with `id:Int`,`x:Int`,`y:Int` arguments, fired when a surface is moved.
*/
public function notify(?touchStartListener: (id: Int, x: Int, y: Int) -> Void, ?touchEndListener: (id: Int, x: Int, y: Int) -> Void,
?moveListener: (id: Int, x: Int, y: Int) -> Void): Void {
if (touchStartListener != null)
touchStartListeners.push(touchStartListener);
if (touchEndListener != null)
touchEndListeners.push(touchEndListener);
if (moveListener != null)
moveListeners.push(moveListener);
}
/**
* Removes event handlers from the passed functions that were passed to `notify` function.
*/
public function remove(?touchStartListener: (id: Int, x: Int, y: Int) -> Void, ?touchEndListener: (id: Int, x: Int, y: Int) -> Void,
?moveListener: (id: Int, x: Int, y: Int) -> Void): Void {
if (touchStartListener != null)
touchStartListeners.remove(touchStartListener);
if (touchEndListener != null)
touchEndListeners.remove(touchEndListener);
if (moveListener != null)
moveListeners.remove(moveListener);
}
static var instance: Surface;
var touchStartListeners: Array<Int->Int->Int->Void>;
var touchEndListeners: Array<Int->Int->Int->Void>;
var moveListeners: Array<Int->Int->Int->Void>;
function new() {
touchStartListeners = new Array<Int->Int->Int->Void>();
touchEndListeners = new Array<Int->Int->Int->Void>();
moveListeners = new Array<Int->Int->Int->Void>();
instance = this;
}
function sendTouchStartEvent(index: Int, x: Int, y: Int): Void {
for (listener in touchStartListeners) {
listener(index, x, y);
}
}
function sendTouchEndEvent(index: Int, x: Int, y: Int): Void {
for (listener in touchEndListeners) {
listener(index, x, y);
}
}
function sendMoveEvent(index: Int, x: Int, y: Int): Void {
for (listener in moveListeners) {
listener(index, x, y);
}
}
}