pick and place editing working, saving and loading working

This commit is contained in:
2025-02-09 02:54:17 -07:00
parent 8dc0af650f
commit 3d4596f8fb
13 changed files with 879 additions and 309 deletions

View File

@@ -37,6 +37,13 @@ export function getContext() {
export function getContextItem<T>(prop: string): T {
return ctx[prop] as T;
}
export function getContextItemOrDefault<T>(prop: string, defaultValue: T): T {
try {
return ctx[prop] as T;
} catch {
return defaultValue;
}
}
export function setContextItem<T>(prop: string, value: T) {
Object.assign(contextStack[contextStack.length - 1] ?? defaultContext, {
[prop]: value,

View File

@@ -1,14 +1,36 @@
import { Vector, ZoomableDoodler } from "@bearmetal/doodler";
import { getContextItem } from "./context.ts";
function mouseButtonToString(button: number) {
switch (button) {
case 0:
return "left";
case 1:
return "middle";
case 2:
return "right";
}
}
function mouseButtonToNumber(button: string) {
switch (button) {
case "left":
return 0;
case "middle":
return 1;
case "right":
return 2;
}
}
export class InputManager {
private keyStates: Map<string | number, boolean> = new Map();
private mouseStates: Map<string | number, boolean> = new Map();
private keyStates: Map<string, boolean> = new Map();
private mouseStates: Map<string, boolean> = new Map();
private mouseLocation: { x: number; y: number } = { x: 0, y: 0 };
private mouseDelta: { x: number; y: number } = { x: 0, y: 0 };
private keyEvents: Map<string | number, () => void> = new Map();
private mouseEvents: Map<string | number, () => void> = new Map();
private keyEvents: Map<string, () => void> = new Map();
private mouseEvents: Map<string, () => void> = new Map();
constructor() {
document.addEventListener("keydown", (e) => {
@@ -19,11 +41,15 @@ export class InputManager {
this.keyStates.set(e.key, false);
});
document.addEventListener("mousedown", (e) => {
this.mouseStates.set(e.button, true);
this.mouseEvents.get(e.button)?.call(e);
const button = mouseButtonToString(e.button);
if (!button) throw "Mouse button not found: " + e.button;
this.mouseStates.set(button, true);
this.mouseEvents.get(button)?.call(e);
});
document.addEventListener("mouseup", (e) => {
this.mouseStates.set(e.button, false);
const button = mouseButtonToString(e.button);
if (!button) throw "Mouse button not found: " + e.button;
this.mouseStates.set(button, false);
});
self.addEventListener("mousemove", (e) => {
@@ -35,10 +61,10 @@ export class InputManager {
});
}
getKeyState(key: string | number) {
getKeyState(key: string) {
return this.keyStates.get(key);
}
getMouseState(key: string | number) {
getMouseState(key: string) {
return this.mouseStates.get(key);
}
getMouseLocation() {
@@ -65,19 +91,25 @@ export class InputManager {
return this.mouseDelta;
}
onKey(key: string | number, cb: () => void) {
onKey(key: string, cb: () => void) {
this.keyEvents.set(key, cb);
}
onMouse(key: string | number, cb: () => void) {
onMouse(key: string, cb: () => void) {
this.mouseEvents.set(key, cb);
}
offKey(key: string | number) {
offKey(key: string) {
const events = this.keyEvents.get(key);
this.keyEvents.delete(key);
return events;
}
offMouse(key: string | number) {
offMouse(key: string) {
this.mouseEvents.delete(key);
}
onNumberKey(arg0: (arg: number) => void) {
for (let i = 0; i < 10; i++) {
this.onKey(i.toString(), () => arg0(i));
}
}
}