All files / src/components/world/entities/scenes/cursor/modes useActorPlacementCursorMode.tsx

0% Statements 0/26
0% Branches 0/4
0% Functions 0/7
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                                                                                                                   
import React, { useCallback, useMemo } from "react";
import { TILE_SIZE, TOOL_ACTORS, TOOL_SELECT } from "consts";
import { PlusIcon } from "ui/icons/Icons";
import editorActions from "store/features/editor/editorActions";
import entitiesActions from "store/features/entities/entitiesActions";
import clipboardActions from "store/features/clipboard/clipboardActions";
import { useAppDispatch, useAppSelector } from "store/hooks";
import type {
  SceneCursorMode,
  SceneCursorMouseDownHandler,
} from "./SceneCursorMode";
import type { SceneCursorViewModel } from "../SceneCursorView";
 
export const useActorPlacementCursorMode = (): SceneCursorMode => {
  const dispatch = useAppDispatch();
  const tool = useAppSelector((state) => state.editor.tool);
  const pasteMode = useAppSelector((state) => state.editor.pasteMode);
  const editorPrefabId = useAppSelector((state) => state.editor.prefabId);
 
  const onMouseDown = useCallback<SceneCursorMouseDownHandler>(
    (e) => {
      if (pasteMode) {
        dispatch(
          clipboardActions.pasteActorAt({
            sceneId: e.sceneId,
            x: e.x,
            y: e.y,
          }),
        );
      } else {
        dispatch(
          entitiesActions.addActor({
            sceneId: e.sceneId,
            x: e.x,
            y: e.y,
            defaults: editorPrefabId
              ? {
                  prefabId: editorPrefabId,
                }
              : undefined,
          }),
        );
      }
 
      dispatch(editorActions.setTool({ tool: TOOL_SELECT }));
 
      return true;
    },
    [dispatch, editorPrefabId, pasteMode],
  );
 
  const view = useMemo<SceneCursorViewModel>(
    () => ({
      variant: "actors",
      width: TILE_SIZE * 2,
      height: TILE_SIZE,
      bubble: <PlusIcon />,
    }),
    [],
  );
 
  return useMemo(
    () => ({
      id: "actorPlacement",
      enabled: tool === TOOL_ACTORS,
      viewPriority: 10,
      eventPriority: 10,
      view,
      onMouseDown,
    }),
    [onMouseDown, tool, view],
  );
};