81 lines
1.6 KiB
TypeScript
81 lines
1.6 KiB
TypeScript
import { D, I } from "./engine/public.ts";
|
|
import { Rect } from "./engine/datatypes.ts";
|
|
|
|
export type Handlers = {
|
|
onClick?: () => void;
|
|
onSqueeze?: () => void;
|
|
};
|
|
|
|
export class DrawPile {
|
|
#draws: { depth: number; op: () => void; handlers?: Handlers }[];
|
|
#hoveredIndex: number | null;
|
|
|
|
constructor() {
|
|
this.#draws = [];
|
|
this.#hoveredIndex = null;
|
|
}
|
|
|
|
clear() {
|
|
this.#draws = [];
|
|
this.#hoveredIndex = null;
|
|
}
|
|
|
|
add(depth: number, op: () => void) {
|
|
this.#draws.push({ depth, op });
|
|
}
|
|
|
|
addClickable(
|
|
depth: number,
|
|
op: (hover: boolean) => void,
|
|
rect: Rect,
|
|
enabled: boolean,
|
|
handlers: {
|
|
onClick?: () => void;
|
|
onSqueeze?: () => void;
|
|
},
|
|
) {
|
|
let position = I.mousePosition?.offset(D.camera);
|
|
let hovered = false;
|
|
if (position != null) {
|
|
hovered = rect.contains(position);
|
|
}
|
|
if (!enabled) {
|
|
hovered = false;
|
|
}
|
|
if (hovered) {
|
|
this.#hoveredIndex = this.#draws.length;
|
|
}
|
|
this.#draws.push({ depth, op: () => op(hovered), handlers });
|
|
}
|
|
|
|
executeOnClick() {
|
|
if (I.isMouseClicked("leftMouse")) {
|
|
let hi = this.#hoveredIndex;
|
|
if (hi != null) {
|
|
let cb = this.#draws[hi]?.handlers?.onClick;
|
|
if (cb != null) {
|
|
cb();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (I.isMouseDown("leftMouse")) {
|
|
let hi = this.#hoveredIndex;
|
|
if (hi != null) {
|
|
let cb = this.#draws[hi]?.handlers?.onSqueeze;
|
|
if (cb != null) {
|
|
cb();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
draw() {
|
|
let draws = [...this.#draws];
|
|
draws.sort((d0, d1) => d0.depth - d1.depth);
|
|
for (let d of draws.values()) {
|
|
d.op();
|
|
}
|
|
}
|
|
}
|