gravity/select.js
2026-01-03 18:40:04 -06:00

75 lines
1.9 KiB
JavaScript

import {copy} from './vector.js';
export class Select {
sim = undefined;
box = {
start: undefined,
end: undefined,
};
selectedSingle = undefined;
selectedGroup = [];
constructor(sim) {
this.sim = sim;
// TODO: Move this to a new Keyboard class singleton
window.addEventListener('keydown', (e) => {
switch (e.key) {
case 'Tab': {
e.preventDefault();
if (!this.selectedGroup.length) return;
const currentIdx = this.selectedGroup.indexOf(this.selectedSingle);
const newIdx = (currentIdx + 1) % this.selectedGroup.length;
this.selectedSingle = this.selectedGroup[newIdx];
break;
}
}
});
}
handlePointerDown({x: clientX, y: clientY}) {
this.box.start = this.sim.screenToSim(clientX, clientY);
this.box.end = this.box.start;
this.getSelectedObjects();
}
handlePointerMove({x: clientX, y: clientY}) {
if (!this.box.start) return;
this.box.end = this.sim.screenToSim(clientX, clientY);
}
handlePointerUp() {
if (!this.box.start) return;
const start = copy(this.box.start);
const end = copy(this.box.end);
this.box.start = {
x: Math.min(start.x, end.x),
y: Math.min(start.y, end.y),
};
this.box.end = {
x: Math.max(start.x, end.x),
y: Math.max(start.y, end.y),
};
this.getSelectedObjects();
this.box = {
start: undefined,
end: undefined,
};
}
getSelectedObjects() {
const {start, end} = this.box;
if (!start) return;
this.selectedGroup = this.sim.system.filter(({position: {x, y}}) => {
return x >= start.x && x <= end.x && y >= start.y && y <= end.y;
});
// For now, first object in group is selected single
this.selectedSingle = this.selectedGroup[0] ?? undefined;
}
frame() {
if (!this.box.start) return;
this.sim.display.drawBox(this.box)
}
}