Skip to content

Commit

Permalink
More descriptive callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
RblSb committed Aug 31, 2024
1 parent 8dc93eb commit 067b4e3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 57 deletions.
22 changes: 11 additions & 11 deletions Backends/HTML5/kha/LoaderImpl.hx
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Backends/HTML5/kha/audio2/Audio.hx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<VirtualStreamChannel> = [];

Expand Down
49 changes: 20 additions & 29 deletions Sources/kha/Assets.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kha;

import haxe.io.Bytes;
import haxe.Unserializer;
import haxe.io.Bytes;

using StringTools;

Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}

Expand All @@ -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"});
Expand All @@ -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));
}
Expand All @@ -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"});
Expand All @@ -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"});
Expand All @@ -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));
}
Expand All @@ -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"});
Expand All @@ -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));
}
Expand All @@ -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"});
Expand All @@ -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));
}
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion Sources/kha/System.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/kha/audio2/Audio.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kha.audio2;

import kha.internal.IntBox;

extern class Audio {
/**
* The samples per second natively used by the target system.
Expand All @@ -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.
Expand Down
36 changes: 24 additions & 12 deletions Sources/kha/input/Gamepad.hx
Original file line number Diff line number Diff line change
@@ -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<Gamepad> {
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<Gamepad> = new Array();
static var instances: Array<Gamepad> = [];

var axisListeners: Array<Int->Float->Void>;
var buttonListeners: Array<Int->Float->Void>;
var axisListeners: Array<GamepadAxisCallback> = [];
var buttonListeners: Array<GamepadButtonCallback> = [];

static var connectListeners: Array<Int->Void> = new Array();
static var disconnectListeners: Array<Int->Void> = new Array();
static var connectListeners: Array<GamepadIdCallback> = [];
static var disconnectListeners: Array<GamepadIdCallback> = [];

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;
}

Expand Down

0 comments on commit 067b4e3

Please sign in to comment.