wait event stuff for wasm backend
This commit is contained in:
parent
0f7c34a5ad
commit
4eb872944a
3 changed files with 49 additions and 3 deletions
|
|
@ -140,6 +140,9 @@ const mach = {
|
||||||
observer: undefined,
|
observer: undefined,
|
||||||
events: [],
|
events: [],
|
||||||
changes: [],
|
changes: [],
|
||||||
|
update: undefined,
|
||||||
|
wait_event_timeout: 0,
|
||||||
|
wait_event_timer: undefined,
|
||||||
|
|
||||||
init(wasm) {
|
init(wasm) {
|
||||||
this.wasm = wasm;
|
this.wasm = wasm;
|
||||||
|
|
@ -181,6 +184,14 @@ const mach = {
|
||||||
throw Error(mach.getString(str, len));
|
throw Error(mach.getString(str, len));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
machClearEventTimer() {
|
||||||
|
if (mach.wait_event_timer !== undefined) {
|
||||||
|
clearTimeout(mach.wait_event_timer);
|
||||||
|
mach.wait_event_timer = undefined;
|
||||||
|
window.requestAnimationFrame(mach.update);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
machCanvasInit(width, height, id) {
|
machCanvasInit(width, height, id) {
|
||||||
let canvas = document.createElement("canvas");
|
let canvas = document.createElement("canvas");
|
||||||
canvas.id = "#mach-canvas-" + mach.canvases.length;
|
canvas.id = "#mach-canvas-" + mach.canvases.length;
|
||||||
|
|
@ -198,32 +209,39 @@ const mach = {
|
||||||
|
|
||||||
canvas.addEventListener("keydown", (ev) => {
|
canvas.addEventListener("keydown", (ev) => {
|
||||||
mach.events.push(...[1, convertKeyCode(ev.code)]);
|
mach.events.push(...[1, convertKeyCode(ev.code)]);
|
||||||
|
mach.machClearEventTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
canvas.addEventListener("keyup", (ev) => {
|
canvas.addEventListener("keyup", (ev) => {
|
||||||
mach.events.push(...[2, convertKeyCode(ev.code)]);
|
mach.events.push(...[2, convertKeyCode(ev.code)]);
|
||||||
|
mach.machClearEventTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
canvas.addEventListener("mousemove", (ev) => {
|
canvas.addEventListener("mousemove", (ev) => {
|
||||||
mach.events.push(...[3, ev.clientX, ev.clientY]);
|
mach.events.push(...[3, ev.clientX, ev.clientY]);
|
||||||
|
mach.machClearEventTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
canvas.addEventListener("mousedown", (ev) => {
|
canvas.addEventListener("mousedown", (ev) => {
|
||||||
mach.events.push(...[4, ev.button]);
|
mach.events.push(...[4, ev.button]);
|
||||||
|
mach.machClearEventTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
canvas.addEventListener("mouseup", (ev) => {
|
canvas.addEventListener("mouseup", (ev) => {
|
||||||
mach.events.push(...[5, ev.button]);
|
mach.events.push(...[5, ev.button]);
|
||||||
|
mach.machClearEventTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
canvas.addEventListener("wheel", (ev) => {
|
canvas.addEventListener("wheel", (ev) => {
|
||||||
mach.events.push(...[6, ev.deltaX, ev.deltaY]);
|
mach.events.push(...[6, ev.deltaX, ev.deltaY]);
|
||||||
|
mach.machClearEventTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
canvas.addEventListener("mach-canvas-resize", (ev) => {
|
canvas.addEventListener("mach-canvas-resize", (ev) => {
|
||||||
const cv_index = mach.canvases.findIndex((el) => el.canvas === ev.currentTarget);
|
const cv_index = mach.canvases.findIndex((el) => el.canvas === ev.currentTarget);
|
||||||
const cv = mach.canvases[cv_index];
|
const cv = mach.canvases[cv_index];
|
||||||
mach.changes.push(...[1, cv.canvas.width, cv.canvas.height, window.devicePixelRatio]);
|
mach.changes.push(...[1, cv.canvas.width, cv.canvas.height, window.devicePixelRatio]);
|
||||||
|
mach.machClearEventTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
document.body.appendChild(canvas);
|
document.body.appendChild(canvas);
|
||||||
|
|
@ -278,8 +296,16 @@ const mach = {
|
||||||
window.dispatchEvent(new Event("mach-close"));
|
window.dispatchEvent(new Event("mach-close"));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
machSetWaitEvent(timeout) {
|
||||||
|
mach.wait_event_timeout = timeout;
|
||||||
|
},
|
||||||
|
|
||||||
|
machHasEvent() {
|
||||||
|
return (mach.events.length > 0);
|
||||||
|
},
|
||||||
|
|
||||||
machEventShift() {
|
machEventShift() {
|
||||||
if (mach.events.length < 0)
|
if (mach.events.length === 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return mach.events.shift();
|
return mach.events.shift();
|
||||||
|
|
@ -290,7 +316,7 @@ const mach = {
|
||||||
},
|
},
|
||||||
|
|
||||||
machChangeShift() {
|
machChangeShift() {
|
||||||
if (mach.changes.length < 0)
|
if (mach.changes.length === 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return mach.changes.shift();
|
return mach.changes.shift();
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ const js = struct {
|
||||||
extern fn machCanvasGetFramebufferWidth(canvas: CanvasId) u32;
|
extern fn machCanvasGetFramebufferWidth(canvas: CanvasId) u32;
|
||||||
extern fn machCanvasGetFramebufferHeight(canvas: CanvasId) u32;
|
extern fn machCanvasGetFramebufferHeight(canvas: CanvasId) u32;
|
||||||
extern fn machEmitCloseEvent() void;
|
extern fn machEmitCloseEvent() void;
|
||||||
|
extern fn machSetWaitEvent(timeout: f64) void;
|
||||||
|
extern fn machHasEvent() bool;
|
||||||
extern fn machEventShift() i32;
|
extern fn machEventShift() i32;
|
||||||
extern fn machEventShiftFloat() f64;
|
extern fn machEventShiftFloat() f64;
|
||||||
extern fn machChangeShift() u32;
|
extern fn machChangeShift() u32;
|
||||||
|
|
@ -68,6 +70,10 @@ pub const Platform = struct {
|
||||||
if (value) js.machEmitCloseEvent();
|
if (value) js.machEmitCloseEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn setWaitEvent(_: *Platform, timeout: f64) void {
|
||||||
|
js.machSetWaitEvent(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn getFramebufferSize(platform: *Platform) structs.Size {
|
pub fn getFramebufferSize(platform: *Platform) structs.Size {
|
||||||
return platform.last_framebuffer_size;
|
return platform.last_framebuffer_size;
|
||||||
}
|
}
|
||||||
|
|
@ -99,6 +105,10 @@ pub const Platform = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn hasEvent(_: *Platform) bool {
|
||||||
|
return js.machHasEvent();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn pollEvent(_: *Platform) ?structs.Event {
|
pub fn pollEvent(_: *Platform) ?structs.Event {
|
||||||
const event_type = js.machEventShift();
|
const event_type = js.machEventShift();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,19 @@
|
||||||
let update = function() {{
|
let update = function() {{
|
||||||
if (!frame) return;
|
if (!frame) return;
|
||||||
instance.exports.wasmUpdate();
|
instance.exports.wasmUpdate();
|
||||||
window.requestAnimationFrame(update);
|
if (mach.wait_event_timeout > 0) {{
|
||||||
|
mach.wait_event_timer = setTimeout(() => {{
|
||||||
|
window.requestAnimationFrame(update);
|
||||||
|
}},
|
||||||
|
mach.wait_event_timeout * 1000);
|
||||||
|
}}
|
||||||
|
else {{
|
||||||
|
window.requestAnimationFrame(update);
|
||||||
|
}}
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
mach.update = update;
|
||||||
|
|
||||||
window.requestAnimationFrame(update);
|
window.requestAnimationFrame(update);
|
||||||
|
|
||||||
window.addEventListener("mach-close", () => {{
|
window.addEventListener("mach-close", () => {{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue