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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import React, { useCallback, useMemo, useRef, useState } from "react"; import { TILE_SIZE, TOOL_SELECT, TOOL_TRIGGERS } from "consts"; import { PlusIcon, ResizeIcon } 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 { SceneCursorViewModel } from "../SceneCursorView"; import type { SceneCursorMode, SceneCursorMouseDownHandler, SceneCursorMouseMoveHandler, SceneCursorMouseUpHandler, } from "./SceneCursorMode"; export const useTriggerPlacementCursorMode = (): 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 [isResizing, setIsResizing] = useState(false); const resizeRef = useRef<{ isResizing: boolean; triggerId?: string; startX: number; startY: number; currentX?: number; currentY?: number; }>({ isResizing: false, startX: 0, startY: 0, }); const onMouseDown = useCallback<SceneCursorMouseDownHandler>( (e) => { const { x, y } = e; if (pasteMode) { dispatch( clipboardActions.pasteTriggerAt({ sceneId: e.sceneId, x, y, }), ); dispatch(editorActions.setTool({ tool: TOOL_SELECT })); resizeRef.current.isResizing = false; setIsResizing(false); resizeRef.current.triggerId = undefined; } else { const action = entitiesActions.addTrigger({ sceneId: e.sceneId, x, y, width: 1, height: 1, defaults: editorPrefabId ? { prefabId: editorPrefabId, } : undefined, }); resizeRef.current.triggerId = action.payload.triggerId; dispatch(action); resizeRef.current.isResizing = true; setIsResizing(true); } resizeRef.current.startX = x; resizeRef.current.startY = y; resizeRef.current.currentX = undefined; resizeRef.current.currentY = undefined; return true; }, [dispatch, editorPrefabId, pasteMode], ); const onMouseMove = useCallback<SceneCursorMouseMoveHandler>( (e) => { Iif (!resizeRef.current.isResizing) { return; } const triggerId = resizeRef.current.triggerId; Iif (tool !== TOOL_TRIGGERS || !e.isOverScene || !triggerId) { return; } const { x, y } = e; Iif ( resizeRef.current.currentX === x && resizeRef.current.currentY === y ) { return; } dispatch( entitiesActions.resizeTrigger({ triggerId, startX: resizeRef.current.startX, startY: resizeRef.current.startY, x, y, }), ); resizeRef.current.currentX = x; resizeRef.current.currentY = y; }, [dispatch, tool], ); const onMouseUp = useCallback<SceneCursorMouseUpHandler>(() => { Iif (!resizeRef.current.isResizing) { return; } resizeRef.current.isResizing = false; resizeRef.current.triggerId = undefined; setIsResizing(false); dispatch(editorActions.setTool({ tool: TOOL_SELECT })); }, [dispatch]); const view = useMemo<SceneCursorViewModel>( () => ({ variant: "triggers", width: TILE_SIZE, height: TILE_SIZE, bubble: isResizing ? <ResizeIcon /> : <PlusIcon />, }), [isResizing], ); return useMemo( () => ({ id: "triggerPlacement", enabled: tool === TOOL_TRIGGERS || isResizing, viewPriority: 10, eventPriority: 10, view: tool === TOOL_TRIGGERS ? view : undefined, onMouseDown, onMouseMove, onMouseUp, }), [isResizing, onMouseDown, onMouseMove, onMouseUp, tool, view], ); }; |