2020-07-28 17:59:26 +10:00
|
|
|
import React, { useContext, useEffect } from "react";
|
2020-07-31 14:20:36 +10:00
|
|
|
import { Group } from "react-konva";
|
2020-07-28 17:59:26 +10:00
|
|
|
|
|
|
|
|
import MapInteractionContext from "../../contexts/MapInteractionContext";
|
|
|
|
|
import MapStageContext from "../../contexts/MapStageContext";
|
|
|
|
|
|
|
|
|
|
import { getStrokeWidth } from "../../helpers/drawing";
|
2020-07-31 14:20:36 +10:00
|
|
|
import {
|
|
|
|
|
getRelativePointerPositionNormalized,
|
|
|
|
|
Trail,
|
|
|
|
|
} from "../../helpers/konva";
|
|
|
|
|
import { multiply } from "../../helpers/vector2";
|
2020-07-28 17:59:26 +10:00
|
|
|
|
|
|
|
|
import colors from "../../helpers/colors";
|
|
|
|
|
|
|
|
|
|
function MapPointer({
|
|
|
|
|
gridSize,
|
|
|
|
|
active,
|
|
|
|
|
position,
|
|
|
|
|
onPointerDown,
|
|
|
|
|
onPointerMove,
|
|
|
|
|
onPointerUp,
|
|
|
|
|
visible,
|
|
|
|
|
}) {
|
|
|
|
|
const { mapWidth, mapHeight, interactionEmitter } = useContext(
|
|
|
|
|
MapInteractionContext
|
|
|
|
|
);
|
|
|
|
|
const mapStageRef = useContext(MapStageContext);
|
|
|
|
|
|
|
|
|
|
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() {
|
2020-11-19 15:08:03 +11:00
|
|
|
onPointerMove && visible && onPointerMove(getBrushPosition());
|
2020-07-28 17:59:26 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleBrushUp() {
|
2020-07-31 11:03:12 +10:00
|
|
|
onPointerMove && onPointerUp(getBrushPosition());
|
2020-07-28 17:59:26 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 && (
|
2020-07-31 14:20:36 +10:00
|
|
|
<Trail
|
|
|
|
|
position={multiply(position, { x: mapWidth, y: mapHeight })}
|
|
|
|
|
color={colors.red}
|
|
|
|
|
size={size}
|
|
|
|
|
duration={200}
|
2020-07-28 17:59:26 +10:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapPointer;
|