Skip to content

Commit

Permalink
Merge pull request #1 from joshuahhh/add-drag
Browse files Browse the repository at this point in the history
Add mouse click+drag to pan
  • Loading branch information
joshuahhh authored Dec 16, 2024
2 parents 20d87f9 + 6415459 commit 53f5e11
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ async function main() {
window.addEventListener("keyup", (e) => {
if (e.key === "Shift") {
shiftHeld = false;
isDragging = false;
}
if (e.key === "Alt") {
altHeld = false;
Expand All @@ -397,6 +398,7 @@ async function main() {
let mouseX = 0;
let mouseY = 0;
let mouseDown = false;
let isDragging = false;
c.addEventListener("mousemove", (e) => {
// clientX/Y works better than offsetX/Y for Chrome/Safari compatibility.

Expand All @@ -409,16 +411,19 @@ async function main() {
});
c.addEventListener("mouseup", () => {
mouseDown = false;
isDragging = false;
});
c.addEventListener("click", () => {
for (const { xywh, callback } of _clickables) {
if (inXYWH(mouseX, mouseY, xywh)) {
callback();
return; // only click one thing at a time
if (!isDragging) {
for (const { xywh, callback } of _clickables) {
if (inXYWH(mouseX, mouseY, xywh)) {
callback();
return; // only click one thing at a time
}
}
tool = { type: "pointer" };
return true;
}
tool = { type: "pointer" };
return true;
});
let draggedOver = false;
c.addEventListener("dragover", (e) => {
Expand Down Expand Up @@ -1273,7 +1278,8 @@ async function main() {

// panning
c.addEventListener("mousemove", (e) => {
if (e.shiftKey) {
if (e.shiftKey || mouseDown) {
isDragging = true;
setPan(add(pan, [e.movementX, e.movementY]));
}
});
Expand Down Expand Up @@ -1318,13 +1324,12 @@ async function main() {

_clickables = [];

c.style.cursor =
tool.type === "pointer"
c.style.cursor = isDragging
? "url('./glove1.png'), pointer"
: tool.type === "pointer"
? mouseDown
? "url('./glove2.png'), pointer"
: shiftHeld
? "url('./glove1.png'), pointer"
: "url('./glove3.png'), pointer"
: "url('./glove3.png'), pointer"
: tool.type === "dev-action"
? "help"
: "none";
Expand Down

0 comments on commit 53f5e11

Please sign in to comment.