From d73d3520fa4440e8748b3fcd70ecbfbdc9b2061e Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sat, 10 Oct 2020 18:31:53 +1100 Subject: [PATCH] Added arrow key nudging to map grid editor --- src/components/map/MapEditor.js | 24 ++++++++++++-------- src/components/map/MapGridEditor.js | 34 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/components/map/MapEditor.js b/src/components/map/MapEditor.js index cdf0ca9..fb649d8 100644 --- a/src/components/map/MapEditor.js +++ b/src/components/map/MapEditor.js @@ -1,4 +1,4 @@ -import React, { useState, useRef, useEffect } from "react"; +import React, { useState, useRef, useEffect, useContext } from "react"; import { Box, IconButton } from "theme-ui"; import { Stage, Layer, Image } from "react-konva"; import ReactResizeDetector from "react-resize-detector"; @@ -9,6 +9,7 @@ import useStageInteraction from "../../helpers/useStageInteraction"; import { getMapDefaultInset } from "../../helpers/map"; import { MapInteractionProvider } from "../../contexts/MapInteractionContext"; +import KeyboardContext from "../../contexts/KeyboardContext"; import ResetMapIcon from "../../icons/ResetMapIcon"; import GridOnIcon from "../../icons/GridOnIcon"; @@ -117,6 +118,9 @@ function MapEditor({ map, onSettingsChange }) { mapHeight, }; + // Get keyboard context to pass to Konva + const keyboardValue = useContext(KeyboardContext); + const canEditGrid = map.type !== "default"; const gridChanged = @@ -150,14 +154,16 @@ function MapEditor({ map, onSettingsChange }) { > - - {showGridControls && canEditGrid && ( - - )} - {showGridControls && canEditGrid && ( - - )} - + + + {showGridControls && canEditGrid && ( + + )} + {showGridControls && canEditGrid && ( + + )} + + diff --git a/src/components/map/MapGridEditor.js b/src/components/map/MapGridEditor.js index cd3a9e9..3f978df 100644 --- a/src/components/map/MapGridEditor.js +++ b/src/components/map/MapGridEditor.js @@ -4,6 +4,7 @@ import { Group, Circle, Rect } from "react-konva"; import MapInteractionContext from "../../contexts/MapInteractionContext"; import * as Vector2 from "../../helpers/vector2"; +import useKeyboard from "../../helpers/useKeyboard"; function MapGridEditor({ map, onGridChange }) { const { @@ -145,6 +146,39 @@ function MapGridEditor({ map, onGridChange }) { } } + function nudgeGrid(direction) { + const inset = map.grid.inset; + const gridSizeNormalized = Vector2.divide( + Vector2.subtract(inset.bottomRight, inset.topLeft), + map.grid.size + ); + const offset = Vector2.divide( + Vector2.multiply(direction, gridSizeNormalized), + stageScale * 2 + ); + onGridChange({ + topLeft: Vector2.add(inset.topLeft, offset), + bottomRight: Vector2.add(inset.bottomRight, offset), + }); + } + + function handleKeyDown({ key }) { + if (key === "ArrowUp") { + nudgeGrid({ x: 0, y: -1 }); + } + if (key === "ArrowLeft") { + nudgeGrid({ x: -1, y: 0 }); + } + if (key === "ArrowRight") { + nudgeGrid({ x: 1, y: 0 }); + } + if (key === "ArrowDown") { + nudgeGrid({ x: 0, y: 1 }); + } + } + + useKeyboard(handleKeyDown); + function getHandleNormalizedPosition(handle) { return Vector2.divide({ x: handle.x(), y: handle.y() }, mapSize); }