Added simple pointer tool
This commit is contained in:
@@ -10,6 +10,7 @@ import MapDice from "./MapDice";
|
||||
import MapGrid from "./MapGrid";
|
||||
import MapMeasure from "./MapMeasure";
|
||||
import MapLoadingOverlay from "./MapLoadingOverlay";
|
||||
import NetworkedMapPointer from "../../network/NetworkedMapPointer";
|
||||
|
||||
import TokenDataContext from "../../contexts/TokenDataContext";
|
||||
|
||||
@@ -35,6 +36,7 @@ function Map({
|
||||
allowFogDrawing,
|
||||
allowMapChange,
|
||||
disabledTokens,
|
||||
session,
|
||||
}) {
|
||||
const { tokensById } = useContext(TokenDataContext);
|
||||
|
||||
@@ -139,6 +141,7 @@ function Map({
|
||||
if (!map) {
|
||||
disabledControls.push("pan");
|
||||
disabledControls.push("measure");
|
||||
disabledControls.push("pointer");
|
||||
}
|
||||
if (!allowFogDrawing) {
|
||||
disabledControls.push("fog");
|
||||
@@ -304,6 +307,14 @@ function Map({
|
||||
/>
|
||||
);
|
||||
|
||||
const mapPointer = (
|
||||
<NetworkedMapPointer
|
||||
active={selectedToolId === "pointer"}
|
||||
gridSize={gridSizeNormalized}
|
||||
session={session}
|
||||
/>
|
||||
);
|
||||
|
||||
return (
|
||||
<MapInteraction
|
||||
map={map}
|
||||
@@ -324,6 +335,7 @@ function Map({
|
||||
{mapDrawing}
|
||||
{mapTokens}
|
||||
{mapFog}
|
||||
{mapPointer}
|
||||
{mapMeasure}
|
||||
</MapInteraction>
|
||||
);
|
||||
|
||||
@@ -15,6 +15,7 @@ import FogToolIcon from "../../icons/FogToolIcon";
|
||||
import BrushToolIcon from "../../icons/BrushToolIcon";
|
||||
import MeasureToolIcon from "../../icons/MeasureToolIcon";
|
||||
import ExpandMoreIcon from "../../icons/ExpandMoreIcon";
|
||||
import PointerToolIcon from "../../icons/PointerToolIcon";
|
||||
|
||||
function MapContols({
|
||||
onMapChange,
|
||||
@@ -55,8 +56,13 @@ function MapContols({
|
||||
title: "Measure Tool",
|
||||
SettingsComponent: MeasureToolSettings,
|
||||
},
|
||||
pointer: {
|
||||
id: "pointer",
|
||||
icon: <PointerToolIcon />,
|
||||
title: "Pointer Tool",
|
||||
},
|
||||
};
|
||||
const tools = ["pan", "fog", "drawing", "measure"];
|
||||
const tools = ["pan", "fog", "drawing", "measure", "pointer"];
|
||||
|
||||
const sections = [
|
||||
{
|
||||
|
||||
81
src/components/map/MapPointer.js
Normal file
81
src/components/map/MapPointer.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import React, { useContext, useEffect } from "react";
|
||||
import { Group, Circle } from "react-konva";
|
||||
|
||||
import MapInteractionContext from "../../contexts/MapInteractionContext";
|
||||
import MapStageContext from "../../contexts/MapStageContext";
|
||||
|
||||
import { getStrokeWidth } from "../../helpers/drawing";
|
||||
import { getRelativePointerPositionNormalized } from "../../helpers/konva";
|
||||
|
||||
import colors from "../../helpers/colors";
|
||||
|
||||
function MapPointer({
|
||||
gridSize,
|
||||
active,
|
||||
position,
|
||||
onPointerDown,
|
||||
onPointerMove,
|
||||
onPointerUp,
|
||||
visible,
|
||||
}) {
|
||||
const { mapWidth, mapHeight, interactionEmitter } = useContext(
|
||||
MapInteractionContext
|
||||
);
|
||||
const mapStageRef = useContext(MapStageContext);
|
||||
|
||||
// const [isBrushDown, setIsBrushDown] = useState(false);
|
||||
// const [brushPosition, setBrushPosition] = useState({ x: 0, y: 0 });
|
||||
|
||||
useEffect(() => {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mapStage = mapStageRef.current;
|
||||
|
||||
function getBrushPosition() {
|
||||
const mapImage = mapStage.findOne("#mapImage");
|
||||
return getRelativePointerPositionNormalized(mapImage);
|
||||
}
|
||||
|
||||
function handleBrushDown() {
|
||||
onPointerDown && onPointerDown(getBrushPosition());
|
||||
}
|
||||
|
||||
function handleBrushMove() {
|
||||
onPointerMove && onPointerMove(getBrushPosition());
|
||||
}
|
||||
|
||||
function handleBrushUp() {
|
||||
onPointerMove && onPointerUp({ x: 0, y: 0 });
|
||||
}
|
||||
|
||||
interactionEmitter.on("dragStart", handleBrushDown);
|
||||
interactionEmitter.on("drag", handleBrushMove);
|
||||
interactionEmitter.on("dragEnd", handleBrushUp);
|
||||
|
||||
return () => {
|
||||
interactionEmitter.off("dragStart", handleBrushDown);
|
||||
interactionEmitter.off("drag", handleBrushMove);
|
||||
interactionEmitter.off("dragEnd", handleBrushUp);
|
||||
};
|
||||
});
|
||||
|
||||
const size = getStrokeWidth(2, gridSize, mapWidth, mapHeight);
|
||||
|
||||
return (
|
||||
<Group>
|
||||
{visible && (
|
||||
<Circle
|
||||
x={position.x * mapWidth}
|
||||
y={position.y * mapHeight}
|
||||
fill={colors.red}
|
||||
width={size}
|
||||
height={size}
|
||||
/>
|
||||
)}
|
||||
</Group>
|
||||
);
|
||||
}
|
||||
|
||||
export default MapPointer;
|
||||
Reference in New Issue
Block a user