From 7b7435f55ae05fd25bd3c2b73cc68a8e469aab18 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Tue, 14 Apr 2020 09:41:10 +1000 Subject: [PATCH] Add a check for map bounds in token menu Removed hard coded dead zones in proxy token --- src/components/Map.js | 2 +- src/components/ProxyToken.js | 28 ++++++++++++---------------- src/components/TokenMenu.js | 14 ++++++++++++++ src/components/TokenStatus.js | 2 +- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/components/Map.js b/src/components/Map.js index fe6413a..fdf49cf 100644 --- a/src/components/Map.js +++ b/src/components/Map.js @@ -121,7 +121,7 @@ function Map({ > 80 && - proxyRect.left > 112; - } + const proxyRect = proxy.getBoundingClientRect(); + const map = document.querySelector(".map"); + const mapRect = map.getBoundingClientRect(); + proxyOnMap.current = + proxyRect.left > mapRect.left && proxyRect.right < mapRect.right; // update the posiion attributes proxy.setAttribute("data-x", x); @@ -78,19 +76,17 @@ function ProxyToken({ tokenClassName, onProxyDragEnd }) { let proxy = proxyRef.current; if (proxy) { if (onProxyDragEnd) { - // TODO: look at cleaning this up with a props instead of - // hard coding an id - const map = document.getElementById("map"); - const mapRect = map.getBoundingClientRect(); + const mapImage = document.querySelector(".mapImage"); + const mapImageRect = mapImage.getBoundingClientRect(); let x = parseFloat(proxy.getAttribute("data-x")) || 0; let y = parseFloat(proxy.getAttribute("data-y")) || 0; // Convert coordiantes to be relative to the map - x = x - mapRect.left; - y = y - mapRect.top; + x = x - mapImageRect.left; + y = y - mapImageRect.top; // Normalize to map width - x = x / (mapRect.right - mapRect.left); - y = y / (mapRect.bottom - mapRect.top); + x = x / (mapImageRect.right - mapImageRect.left); + y = y / (mapImageRect.bottom - mapImageRect.top); target.setAttribute("data-x", x); target.setAttribute("data-y", y); diff --git a/src/components/TokenMenu.js b/src/components/TokenMenu.js index 817b18f..126c8ee 100644 --- a/src/components/TokenMenu.js +++ b/src/components/TokenMenu.js @@ -107,6 +107,20 @@ function TokenMenu({ tokenClassName, onTokenChange }) { }, { once: true } ); + + // Ensure menu is in bounds + const nodeRect = node.getBoundingClientRect(); + const map = document.querySelector(".map"); + const mapRect = map.getBoundingClientRect(); + setMenuLeft((prevLeft) => + Math.min( + mapRect.right - nodeRect.width, + Math.max(mapRect.left, prevLeft) + ) + ); + setMenuTop((prevTop) => + Math.min(mapRect.bottom - nodeRect.height, prevTop) + ); } } diff --git a/src/components/TokenStatus.js b/src/components/TokenStatus.js index 6da6302..b79c463 100644 --- a/src/components/TokenStatus.js +++ b/src/components/TokenStatus.js @@ -21,7 +21,7 @@ function TokenStatus({ statuses }) { height: "100%", position: "absolute", opacity: 0.8, - transform: `scale(1.${index + 1})`, + transform: `scale(${1 - index / 10})`, }} >