From d843a33b32c27b57d18c295567a787a7077b2507 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Mon, 1 Jun 2020 16:12:23 -0400 Subject: [PATCH] Don't snap things back in-bounds if they've never been in-bounds yet This way, if you slightly adjust where an object is, you won't need to hold alt the whole time. This also makes pressing alt reset this behavior, so if you hold alt, drag out of bounds, release alt, and then move, you won't be thrown back in-bounds. --- macrovision.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/macrovision.js b/macrovision.js index a324e4e3..55f246e2 100644 --- a/macrovision.js +++ b/macrovision.js @@ -4,6 +4,7 @@ let selectedEntity = null; let entityIndex = 0; let clicked = null; +let movingInBounds = false; let dragging = false; let clickTimeout = null; let dragOffsetX = null; @@ -597,6 +598,7 @@ function combineInfo(existing, next) { function clickDown(target, x, y) { clicked = target; + movingInBounds = false; const rect = target.getBoundingClientRect(); let entX = document.querySelector("#entities").getBoundingClientRect().x; let entY = document.querySelector("#entities").getBoundingClientRect().y; @@ -2308,6 +2310,7 @@ document.addEventListener("DOMContentLoaded", () => { e.preventDefault(); } else if (e.key == "Alt") { altHeld = true; + movingInBounds = false; // don't snap the object back in bounds when we let go e.preventDefault(); } }); @@ -2879,7 +2882,17 @@ function clearFilter() { document.addEventListener("mousemove", (e) => { if (clicked) { - const position = snapPos(pix2pos({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY })); + let position = pix2pos({ x: e.clientX - dragOffsetX, y: e.clientY - dragOffsetY }); + + if (movingInBounds) { + position = snapPos(position); + } else { + let x = e.clientX - dragOffsetX; + let y = e.clientY - dragOffsetY; + if (x >= 0 && x <= canvasWidth && y >= 0 && y <= canvasHeight) { + movingInBounds = true; + } + } clicked.dataset.x = position.x; clicked.dataset.y = position.y; updateEntityElement(entities[clicked.dataset.key], clicked);