diff --git a/Backends/HTML5/kha/LoaderImpl.hx b/Backends/HTML5/kha/LoaderImpl.hx index acf27d2b6..355f1227b 100644 --- a/Backends/HTML5/kha/LoaderImpl.hx +++ b/Backends/HTML5/kha/LoaderImpl.hx @@ -1,16 +1,16 @@ package kha; -import js.html.FileReader; -import js.Syntax; +import haxe.io.Bytes; import js.Browser; +import js.Syntax; +import js.html.FileReader; import js.html.ImageElement; import js.html.XMLHttpRequest; -import haxe.io.Bytes; import kha.Blob; -import kha.js.WebAudioSound; -import kha.js.MobileWebAudioSound; import kha.graphics4.TextureFormat; import kha.graphics4.Usage; +import kha.js.MobileWebAudioSound; +import kha.js.WebAudioSound; using StringTools; @@ -22,7 +22,7 @@ class LoaderImpl { return ["png", "jpg", "hdr"]; } - public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: AssetError->Void) { + public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: (err: AssetError) -> Void) { var readable = Reflect.hasField(desc, "readable") ? desc.readable : false; if (StringTools.endsWith(desc.files[0], ".hdr")) { loadBlobFromDescription(desc, function(blob) { @@ -56,7 +56,7 @@ class LoaderImpl { return formats; } - public static function loadSoundFromDescription(desc: Dynamic, done: kha.Sound->Void, failed: AssetError->Void) { + public static function loadSoundFromDescription(desc: Dynamic, done: kha.Sound->Void, failed: (err: AssetError) -> Void) { if (SystemImpl._hasWebAudio) { #if !kha_debug_html5 var element = Browser.document.createAudioElement(); @@ -154,11 +154,11 @@ class LoaderImpl { #end } - public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: AssetError->Void): Void { + public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: (err: AssetError) -> Void): Void { kha.js.Video.fromFile(desc.files, done); } - public static function loadRemote(desc: Dynamic, done: Blob->Void, failed: AssetError->Void) { + public static function loadRemote(desc: Dynamic, done: Blob->Void, failed: (err: AssetError) -> Void) { var request = untyped new XMLHttpRequest(); request.open("GET", desc.files[0], true); request.responseType = "arraybuffer"; @@ -194,7 +194,7 @@ class LoaderImpl { request.send(null); } - public static function loadBlobFromDescription(desc: Dynamic, done: Blob->Void, failed: AssetError->Void) { + public static function loadBlobFromDescription(desc: Dynamic, done: Blob->Void, failed: (err: AssetError) -> Void) { #if kha_debug_html5 var file: String = desc.files[0]; @@ -228,7 +228,7 @@ class LoaderImpl { #end } - public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: AssetError->Void): Void { + public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: (err: AssetError) -> Void): Void { loadBlobFromDescription(desc, function(blob: Blob) { done(new Font(blob)); }, failed); diff --git a/Backends/HTML5/kha/audio2/Audio.hx b/Backends/HTML5/kha/audio2/Audio.hx index e659e21b3..3ef780fb9 100644 --- a/Backends/HTML5/kha/audio2/Audio.hx +++ b/Backends/HTML5/kha/audio2/Audio.hx @@ -1,14 +1,14 @@ package kha.audio2; -import js.Syntax; import js.Browser; +import js.Syntax; import js.html.URL; import js.html.audio.AudioContext; import js.html.audio.AudioProcessingEvent; import js.html.audio.ScriptProcessorNode; +import kha.Sound; import kha.internal.IntBox; import kha.js.AEAudioChannel; -import kha.Sound; class Audio { public static var disableGcInteractions = false; @@ -70,7 +70,7 @@ class Audio { public static var samplesPerSecond: Int; - public static var audioCallback: kha.internal.IntBox->Buffer->Void; + public static var audioCallback: (outputBufferLength: IntBox, buffer: Buffer) -> Void; static var virtualChannels: Array = []; diff --git a/Sources/kha/Assets.hx b/Sources/kha/Assets.hx index 605433ac6..3f0958def 100644 --- a/Sources/kha/Assets.hx +++ b/Sources/kha/Assets.hx @@ -1,7 +1,7 @@ package kha; -import haxe.io.Bytes; import haxe.Unserializer; +import haxe.io.Bytes; using StringTools; @@ -144,16 +144,14 @@ class Assets { onLoaded(bytes); } - function loadFunc(desc: Dynamic, done: (bytes: Int) -> Void, failure: (err: AssetError, bytes: Int) -> Void): Void { + function loadFunc(desc: AssetData, done: (bytes: Int) -> Void, failure: (err: AssetError, bytes: Int) -> Void): Void { final name = desc.name; final size = desc.file_sizes[0]; switch (desc.type) { case "image": - Assets.loadImage(name, function(image: Image) done(size), function(err: AssetError) { - onError(err, size); - }); + Assets.loadImage(name, image -> done(size), err -> onError(err, size)); case "sound": - Assets.loadSound(name, function(sound: Sound) { + Assets.loadSound(name, sound -> { if (uncompressSoundsFilter == null || uncompressSoundsFilter(desc)) { sound.uncompress(function() { done(size); @@ -162,21 +160,13 @@ class Assets { else { done(size); } - }, function(err: AssetError) { - onError(err, size); - }); + }, err -> onError(err, size)); case "blob": - Assets.loadBlob(name, function(blob: Blob) done(size), function(err: AssetError) { - onError(err, size); - }); + Assets.loadBlob(name, blob -> done(size), err -> onError(err, size)); case "font": - Assets.loadFont(name, function(font: Font) done(size), function(err: AssetError) { - onError(err, size); - }); + Assets.loadFont(name, font -> done(size), err -> onError(err, size)); case "video": - Assets.loadVideo(name, function(video: Video) done(size), function(err: AssetError) { - onError(err, size); - }); + Assets.loadVideo(name, video -> done(size), err -> onError(err, size)); } } @@ -203,7 +193,7 @@ class Assets { * @param name The name as defined by the khafile. * @param done A callback. */ - public static function loadImage(name: String, done: Image->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadImage(name: String, done: (image: Image) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = Reflect.field(images, name + "Description"); if (description == null) { reporter(failed, pos)({url: name, error: "Name not found"}); @@ -222,7 +212,8 @@ class Assets { * @param readable If true, a copy of the image will be kept in main memory for image read operations. * @param done A callback. */ - public static function loadImageFromPath(path: String, readable: Bool, done: Image->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadImageFromPath(path: String, readable: Bool, done: (image: Image) -> Void, ?failed: (err: AssetError) -> Void, + ?pos: haxe.PosInfos): Void { var description = {files: [path], readable: readable}; LoaderImpl.loadImageFromDescription(description, done, reporter(failed, pos)); } @@ -233,7 +224,7 @@ class Assets { return LoaderImpl.getImageFormats(); } - public static function loadBlob(name: String, done: Blob->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadBlob(name: String, done: (blob: Blob) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = Reflect.field(blobs, name + "Description"); if (description == null) { reporter(failed, pos)({url: name, error: "Name not found"}); @@ -245,12 +236,12 @@ class Assets { }, reporter(failed, pos)); } - public static function loadBlobFromPath(path: String, done: Blob->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadBlobFromPath(path: String, done: (blob: Blob) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = {files: [path]}; LoaderImpl.loadBlobFromDescription(description, done, reporter(failed, pos)); } - public static function loadSound(name: String, done: Sound->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadSound(name: String, done: (sound: Sound) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = Reflect.field(sounds, name + "Description"); if (description == null) { reporter(failed, pos)({url: name, error: "Name not found"}); @@ -262,7 +253,7 @@ class Assets { }, reporter(failed, pos)); } - public static function loadSoundFromPath(path: String, done: Sound->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadSoundFromPath(path: String, done: (sound: Sound) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = {files: [path]}; return LoaderImpl.loadSoundFromDescription(description, done, reporter(failed, pos)); } @@ -273,7 +264,7 @@ class Assets { return LoaderImpl.getSoundFormats(); } - public static function loadFont(name: String, done: Font->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadFont(name: String, done: (font: Font) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = Reflect.field(fonts, name + "Description"); if (description == null) { reporter(failed, pos)({url: name, error: "Name not found"}); @@ -285,7 +276,7 @@ class Assets { }, reporter(failed, pos)); } - public static function loadFontFromPath(path: String, done: Font->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadFontFromPath(path: String, done: (font: Font) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = {files: [path]}; return LoaderImpl.loadFontFromDescription(description, done, reporter(failed, pos)); } @@ -296,7 +287,7 @@ class Assets { return ["ttf"]; } - public static function loadVideo(name: String, done: Video->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadVideo(name: String, done: (video: Video) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = Reflect.field(videos, name + "Description"); if (description == null) { reporter(failed, pos)({url: name, error: "Name not found"}); @@ -308,7 +299,7 @@ class Assets { }, reporter(failed, pos)); } - public static function loadVideoFromPath(path: String, done: Video->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void { + public static function loadVideoFromPath(path: String, done: (video: Video) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void { var description = {files: [path]}; return LoaderImpl.loadVideoFromDescription(description, done, reporter(failed, pos)); } @@ -319,7 +310,7 @@ class Assets { return LoaderImpl.getVideoFormats(); } - public static function reporter(custom: AssetError->Void, ?pos: haxe.PosInfos) { + public static function reporter(custom: (err: AssetError) -> Void, ?pos: haxe.PosInfos) { return custom != null ? custom : haxe.Log.trace.bind(_, pos); } } diff --git a/Sources/kha/System.hx b/Sources/kha/System.hx index 1d4bd1dc5..5301ce812 100644 --- a/Sources/kha/System.hx +++ b/Sources/kha/System.hx @@ -103,7 +103,7 @@ class System { }); } - public static function start(options: SystemOptions, callback: Window->Void): Void { + public static function start(options: SystemOptions, callback: (window: Window) -> Void): Void { theTitle = options.title; SystemImpl.init(options, callback); } diff --git a/Sources/kha/audio2/Audio.hx b/Sources/kha/audio2/Audio.hx index f427ca4cf..90ec48c5f 100644 --- a/Sources/kha/audio2/Audio.hx +++ b/Sources/kha/audio2/Audio.hx @@ -1,5 +1,7 @@ package kha.audio2; +import kha.internal.IntBox; + extern class Audio { /** * The samples per second natively used by the target system. @@ -11,7 +13,7 @@ extern class Audio { * Beware: This is called from a separate audio thread on some targets. * See kha.audio2.Audio1 for sample code. */ - public static var audioCallback: Int->Buffer->Void; + public static var audioCallback: (outputBufferLength:IntBox, buffer:Buffer)->Void; /** * Similar to kha.audio1.Audio.stream, but only for hardware accelerated audio playback. diff --git a/Sources/kha/input/Gamepad.hx b/Sources/kha/input/Gamepad.hx index 693b7f399..fec7c339d 100644 --- a/Sources/kha/input/Gamepad.hx +++ b/Sources/kha/input/Gamepad.hx @@ -1,57 +1,69 @@ package kha.input; +private typedef GamepadIdCallback = (index: Int) -> Void; +private typedef GamepadAxisCallback = (axisId: Int, value: Float) -> Void; +private typedef GamepadButtonCallback = (buttonId: Int, value: Float) -> Void; + @:allow(kha.SystemImpl) @:expose class Gamepad { var index: Int; - public static function get(index: Int = 0): Gamepad { + public static function get(index: Int = 0): Null { if (index >= instances.length) return null; return instances[index]; } - public static function notifyOnConnect(?connectListener: Int->Void, ?disconnectListener: Int->Void): Void { + /** + Use this event to get connected gamepad `index` and listen to it with `Gamepad.get(index).notify(axisListener, buttonListener)`. + + Remember to also check `Gamepad.get(0)`, gamepads may already be connected before the application was initialized. + **/ + public static function notifyOnConnect(?connectListener: GamepadIdCallback, ?disconnectListener: GamepadIdCallback): Void { if (connectListener != null) connectListeners.push(connectListener); if (disconnectListener != null) disconnectListeners.push(disconnectListener); } - public static function removeConnect(?connectListener: Int->Void, ?disconnectListener: Int->Void): Void { + public static function removeConnect(?connectListener: GamepadIdCallback, ?disconnectListener: GamepadIdCallback): 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 { + /** + In `axisListener`, `axisId` is axis id (for example `axis == 0` is L-stick `x`, `1` is L-stick `y`, `2` is R-stick `x`, `3` is R-stick `y`, ...) and `value` is in `-1.0 - 1.0` range. + + In `buttonListener`, `buttonId` is pressed button id (layout depends on `vendor`), and `value` is in `0 - 1.0` range how hard the button is pressed. + **/ + public function notify(?axisListener: GamepadAxisCallback, ?buttonListener: GamepadButtonCallback): 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 { + public function remove(?axisListener: GamepadAxisCallback, ?buttonListener: GamepadButtonCallback): Void { if (axisListener != null) axisListeners.remove(axisListener); if (buttonListener != null) buttonListeners.remove(buttonListener); } - static var instances: Array = new Array(); + static var instances: Array = []; - var axisListeners: ArrayFloat->Void>; - var buttonListeners: ArrayFloat->Void>; + var axisListeners: Array = []; + var buttonListeners: Array = []; - static var connectListeners: ArrayVoid> = new Array(); - static var disconnectListeners: ArrayVoid> = new Array(); + static var connectListeners: Array = []; + static var disconnectListeners: Array = []; function new(index: Int = 0, id: String = "unknown") { connected = false; this.index = index; - axisListeners = new ArrayFloat->Void>(); - buttonListeners = new ArrayFloat->Void>(); instances[index] = this; }