Added checks for zero grid sizes
This commit is contained in:
parent
400ccc43fe
commit
f5c1cf8c98
@ -11,6 +11,8 @@ import { getStrokeWidth } from "../../helpers/drawing";
|
|||||||
import { getImageLightness } from "../../helpers/image";
|
import { getImageLightness } from "../../helpers/image";
|
||||||
|
|
||||||
function MapGrid({ map, strokeWidth }) {
|
function MapGrid({ map, strokeWidth }) {
|
||||||
|
const { mapWidth, mapHeight } = useContext(MapInteractionContext);
|
||||||
|
|
||||||
let mapSourceMap = map;
|
let mapSourceMap = map;
|
||||||
// Use lowest resolution for grid lightness
|
// Use lowest resolution for grid lightness
|
||||||
if (map && map.type === "file" && map.resolutions) {
|
if (map && map.type === "file" && map.resolutions) {
|
||||||
@ -22,16 +24,28 @@ function MapGrid({ map, strokeWidth }) {
|
|||||||
const mapSource = useDataSource(mapSourceMap, defaultMapSources);
|
const mapSource = useDataSource(mapSourceMap, defaultMapSources);
|
||||||
const [mapImage, mapLoadingStatus] = useImage(mapSource);
|
const [mapImage, mapLoadingStatus] = useImage(mapSource);
|
||||||
|
|
||||||
|
const [isImageLight, setIsImageLight] = useState(true);
|
||||||
|
|
||||||
|
// When the map changes find the average lightness of its pixels
|
||||||
|
useEffect(() => {
|
||||||
|
if (mapLoadingStatus === "loaded") {
|
||||||
|
setIsImageLight(getImageLightness(mapImage));
|
||||||
|
}
|
||||||
|
}, [mapImage, mapLoadingStatus]);
|
||||||
|
|
||||||
const gridX = map && map.grid.size.x;
|
const gridX = map && map.grid.size.x;
|
||||||
const gridY = map && map.grid.size.y;
|
const gridY = map && map.grid.size.y;
|
||||||
|
|
||||||
|
if (!gridX || !gridY) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const gridSizeNormalized = {
|
const gridSizeNormalized = {
|
||||||
x: gridX ? 1 / gridX : 0,
|
x: 1 / gridX,
|
||||||
y: gridY ? 1 / gridY : 0,
|
y: 1 / gridY,
|
||||||
};
|
};
|
||||||
const gridInset = map && map.grid.inset;
|
const gridInset = map && map.grid.inset;
|
||||||
|
|
||||||
const { mapWidth, mapHeight } = useContext(MapInteractionContext);
|
|
||||||
|
|
||||||
const insetWidth = (gridInset.bottomRight.x - gridInset.topLeft.x) * mapWidth;
|
const insetWidth = (gridInset.bottomRight.x - gridInset.topLeft.x) * mapWidth;
|
||||||
const insetHeight =
|
const insetHeight =
|
||||||
(gridInset.bottomRight.y - gridInset.topLeft.y) * mapHeight;
|
(gridInset.bottomRight.y - gridInset.topLeft.y) * mapHeight;
|
||||||
@ -42,15 +56,6 @@ function MapGrid({ map, strokeWidth }) {
|
|||||||
const offsetX = gridInset.topLeft.x * mapWidth * -1;
|
const offsetX = gridInset.topLeft.x * mapWidth * -1;
|
||||||
const offsetY = gridInset.topLeft.y * mapHeight * -1;
|
const offsetY = gridInset.topLeft.y * mapHeight * -1;
|
||||||
|
|
||||||
const [isImageLight, setIsImageLight] = useState(true);
|
|
||||||
|
|
||||||
// When the map changes find the average lightness of its pixels
|
|
||||||
useEffect(() => {
|
|
||||||
if (mapLoadingStatus === "loaded") {
|
|
||||||
setIsImageLight(getImageLightness(mapImage));
|
|
||||||
}
|
|
||||||
}, [mapImage, mapLoadingStatus]);
|
|
||||||
|
|
||||||
const lines = [];
|
const lines = [];
|
||||||
for (let x = 1; x < gridX; x++) {
|
for (let x = 1; x < gridX; x++) {
|
||||||
lines.push(
|
lines.push(
|
||||||
|
@ -15,6 +15,24 @@ function MapGridEditor({ map, onGridChange }) {
|
|||||||
|
|
||||||
const mapSize = { x: mapWidth, y: mapHeight };
|
const mapSize = { x: mapWidth, y: mapHeight };
|
||||||
|
|
||||||
|
function getHandlePositions() {
|
||||||
|
const topLeft = Vector2.multiply(map.grid.inset.topLeft, mapSize);
|
||||||
|
const bottomRight = Vector2.multiply(map.grid.inset.bottomRight, mapSize);
|
||||||
|
|
||||||
|
const size = Vector2.subtract(bottomRight, topLeft);
|
||||||
|
const offset = Vector2.multiply(topLeft, -1);
|
||||||
|
|
||||||
|
return {
|
||||||
|
topLeft,
|
||||||
|
topRight: { x: bottomRight.x, y: topLeft.y },
|
||||||
|
bottomRight,
|
||||||
|
bottomLeft: { x: topLeft.x, y: bottomRight.y },
|
||||||
|
size,
|
||||||
|
offset,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const handlePositions = getHandlePositions();
|
||||||
|
|
||||||
const handlePreviousPositionRef = useRef();
|
const handlePreviousPositionRef = useRef();
|
||||||
|
|
||||||
function handleScaleCircleDragStart(event) {
|
function handleScaleCircleDragStart(event) {
|
||||||
@ -127,23 +145,6 @@ function MapGridEditor({ map, onGridChange }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHandlePositions() {
|
|
||||||
const topLeft = Vector2.multiply(map.grid.inset.topLeft, mapSize);
|
|
||||||
const bottomRight = Vector2.multiply(map.grid.inset.bottomRight, mapSize);
|
|
||||||
|
|
||||||
const size = Vector2.subtract(bottomRight, topLeft);
|
|
||||||
const offset = Vector2.multiply(topLeft, -1);
|
|
||||||
|
|
||||||
return {
|
|
||||||
topLeft,
|
|
||||||
topRight: { x: bottomRight.x, y: topLeft.y },
|
|
||||||
bottomRight,
|
|
||||||
bottomLeft: { x: topLeft.x, y: bottomRight.y },
|
|
||||||
size,
|
|
||||||
offset,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function getHandleNormalizedPosition(handle) {
|
function getHandleNormalizedPosition(handle) {
|
||||||
return Vector2.divide({ x: handle.x(), y: handle.y() }, mapSize);
|
return Vector2.divide({ x: handle.x(), y: handle.y() }, mapSize);
|
||||||
}
|
}
|
||||||
@ -174,8 +175,6 @@ function MapGridEditor({ map, onGridChange }) {
|
|||||||
strokeWidth: editCircleRadius / 10,
|
strokeWidth: editCircleRadius / 10,
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePositions = getHandlePositions();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Group>
|
<Group>
|
||||||
<Rect
|
<Rect
|
||||||
|
@ -36,14 +36,16 @@ function MapSettings({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleGridSizeXChange(event) {
|
function handleGridSizeXChange(event) {
|
||||||
const value = parseInt(event.target.value);
|
const value = parseInt(event.target.value) || 0;
|
||||||
const gridY = map.grid.size.y;
|
const gridY = map.grid.size.y;
|
||||||
|
|
||||||
let inset = map.grid.inset;
|
let inset = map.grid.inset;
|
||||||
|
|
||||||
const gridScale =
|
if (value > 0) {
|
||||||
((inset.bottomRight.x - inset.topLeft.x) * map.width) / value;
|
const gridScale =
|
||||||
inset.bottomRight.y = (gridY * gridScale) / map.height;
|
((inset.bottomRight.x - inset.topLeft.x) * map.width) / value;
|
||||||
|
inset.bottomRight.y = (gridY * gridScale) / map.height;
|
||||||
|
}
|
||||||
|
|
||||||
onSettingsChange("grid", {
|
onSettingsChange("grid", {
|
||||||
...map.grid,
|
...map.grid,
|
||||||
@ -56,14 +58,16 @@ function MapSettings({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleGridSizeYChange(event) {
|
function handleGridSizeYChange(event) {
|
||||||
const value = parseInt(event.target.value);
|
const value = parseInt(event.target.value) || 0;
|
||||||
const gridX = map.grid.size.x;
|
const gridX = map.grid.size.x;
|
||||||
|
|
||||||
let inset = map.grid.inset;
|
let inset = map.grid.inset;
|
||||||
|
|
||||||
const gridScale =
|
if (value > 0) {
|
||||||
((inset.bottomRight.x - inset.topLeft.x) * map.width) / gridX;
|
const gridScale =
|
||||||
inset.bottomRight.y = (value * gridScale) / map.height;
|
((inset.bottomRight.x - inset.topLeft.x) * map.width) / gridX;
|
||||||
|
inset.bottomRight.y = (value * gridScale) / map.height;
|
||||||
|
}
|
||||||
|
|
||||||
onSettingsChange("grid", {
|
onSettingsChange("grid", {
|
||||||
...map.grid,
|
...map.grid,
|
||||||
|
Loading…
Reference in New Issue
Block a user