some stuff

This commit is contained in:
dylan
2024-04-03 22:53:11 -07:00
parent f9e21db529
commit 85cbf665f0
6 changed files with 101 additions and 21 deletions

View File

@ -35,7 +35,7 @@ export type PicoPlayerHandle = {
rightClick: boolean;
}) => void;
setGamepadCount: (count: number) => void;
readonly gpio: number[]; // read + write (should be 256-tuple)
gpio: number[] & {subscribe: (f: (gpio: number[]) => void) => void}; // read + write (should be 256-tuple)
// state
readonly state: {
@ -97,7 +97,8 @@ export const makePicoConsole = async (props: {
handle.pico8_state = {};
handle.pico8_buttons = [0,0,0,0,0,0,0,0];
handle.pico8_mouse = [0,0,0];
handle.pico8_gpio = [
let gpioChanged = (gpio: number[]) => {};
const gpioInner = [
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@ -115,6 +116,27 @@ export const makePicoConsole = async (props: {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
];
handle.pico8_gpio = new Proxy(gpioInner, {
get(target, prop) {
return target[prop as any];
},
set(target, prop, newValue) {
const t = target as any;
if (t.setting) {
return false;
}
const prev = [...target];
target[prop as any] = newValue;
const next = [...target];
if (!t.dontSend && prev.some((p, i) => p !== next[i])) {
gpioChanged(target);
}
return true;
}
});
(handle as any).pico8_gpio.subscribe = (f: (gpio: number[]) => void) => {
gpioChanged = f;
}
handle.pico8_gamepads = {count: 0};
return {
raw: handle,
@ -130,7 +152,7 @@ export const makePicoConsole = async (props: {
shutdownRequested: !!handle.pico8_state.shutdown_requested!,
soundVolume: handle.pico8_state.sound_volume!,
},
gpio: handle.pico8_gpio,
gpio: handle.pico8_gpio as PicoPlayerHandle["gpio"],
setMouse({x, y, leftClick, rightClick}) {
handle.pico8_mouse = [x, y, bitfield(leftClick, rightClick)];
},