2021-02-06 13:32:38 +11:00
|
|
|
import React, { useEffect } from "react";
|
2020-07-31 14:20:36 +10:00
|
|
|
import { Group } from "react-konva";
|
2020-07-28 17:59:26 +10:00
|
|
|
|
2021-02-06 13:32:38 +11:00
|
|
|
import { useMapInteraction } from "../../contexts/MapInteractionContext";
|
|
|
|
|
import { useMapStage } from "../../contexts/MapStageContext";
|
|
|
|
|
import { useGrid } from "../../contexts/GridContext";
|
2020-07-28 17:59:26 +10:00
|
|
|
|
2020-07-31 14:20:36 +10:00
|
|
|
import {
|
|
|
|
|
getRelativePointerPositionNormalized,
|
|
|
|
|
Trail,
|
|
|
|
|
} from "../../helpers/konva";
|
2021-02-04 15:06:34 +11:00
|
|
|
import Vector2 from "../../helpers/Vector2";
|
2020-07-28 17:59:26 +10:00
|
|
|
|
|
|
|
|
import colors from "../../helpers/colors";
|
|
|
|
|
|
|
|
|
|
function MapPointer({
|
|
|
|
|
active,
|
|
|
|
|
position,
|
|
|
|
|
onPointerDown,
|
|
|
|
|
onPointerMove,
|
|
|
|
|
onPointerUp,
|
|
|
|
|
visible,
|
2021-01-28 15:12:30 +11:00
|
|
|
color,
|
2020-07-28 17:59:26 +10:00
|
|
|
}) {
|
2021-02-06 13:32:38 +11:00
|
|
|
const { mapWidth, mapHeight, interactionEmitter } = useMapInteraction();
|
|
|
|
|
const { gridStrokeWidth } = useGrid();
|
|
|
|
|
const mapStageRef = useMapStage();
|
2020-07-28 17:59:26 +10:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-06 13:32:38 +11:00
|
|
|
const size = 2 * gridStrokeWidth;
|
2020-07-28 17:59:26 +10:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Group>
|
|
|
|
|
{visible && (
|
2020-07-31 14:20:36 +10:00
|
|
|
<Trail
|
2021-02-04 15:06:34 +11:00
|
|
|
position={Vector2.multiply(position, { x: mapWidth, y: mapHeight })}
|
2021-01-28 15:12:30 +11:00
|
|
|
color={colors[color]}
|
2020-07-31 14:20:36 +10:00
|
|
|
size={size}
|
|
|
|
|
duration={200}
|
2020-07-28 17:59:26 +10:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-28 15:12:30 +11:00
|
|
|
MapPointer.defaultProps = {
|
|
|
|
|
color: "red",
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-28 17:59:26 +10:00
|
|
|
export default MapPointer;
|