-
Notifications
You must be signed in to change notification settings - Fork 2
[feat/MAT-360] 입력 어댑터 패턴 + 지우개 bounds 최적화 #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
b0nsu
wants to merge
1
commit into
refactor/mat-358-undo-redo
Choose a base branch
from
refactor/mat-360-input-eraser
base: refactor/mat-358-undo-redo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/pointer-native-drawing/src/input/inputAdapterTypes.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { type ReactNode } from 'react'; | ||
|
|
||
| import { type DrawingInputCallbacks } from './inputTypes'; | ||
|
|
||
| export type InputAdapterConfig = { | ||
| eraserMode: boolean; | ||
| pencilOnly: boolean; | ||
| minDistance: number; | ||
| callbacks: DrawingInputCallbacks; | ||
| }; | ||
|
|
||
| export type InputAdapter<TGesture = unknown> = { | ||
| gesture: TGesture; | ||
| }; | ||
|
|
||
| export type InputOverlayAdapter = { | ||
| overlay: ReactNode; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { type InputEvent } from '../model/drawingTypes'; | ||
|
|
||
| export type CancelReason = 'gesture_failed' | 'interrupted' | 'unknown'; | ||
|
|
||
| export type DrawingInputCallbacks = { | ||
| onInteractionBegin: () => void; | ||
| onInteractionFinalize: () => void; | ||
| onDrawStart: (input: InputEvent) => void; | ||
| onDrawMove: (input: InputEvent) => void; | ||
| onDrawEnd: () => void; | ||
| onDrawCancel: (reason?: CancelReason) => void; | ||
| onEraseStart: (input: InputEvent) => void; | ||
| onEraseMove: (input: InputEvent) => void; | ||
| }; |
150 changes: 150 additions & 0 deletions
150
packages/pointer-native-drawing/src/input/rnghPanAdapter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { useCallback, useMemo, useRef } from 'react'; | ||
| import { Gesture, PointerType as RnghPointerType } from 'react-native-gesture-handler'; | ||
| import { runOnJS, useSharedValue } from 'react-native-reanimated'; | ||
|
|
||
| import { type InputEvent, type PointerType } from '../model/drawingTypes'; | ||
|
|
||
| import { type DrawingInputCallbacks } from './inputTypes'; | ||
| import { type InputAdapter, type InputAdapterConfig } from './inputAdapterTypes'; | ||
|
|
||
| const RNGH_POINTER_TYPE_MAP: Record<number, PointerType> = { | ||
| [RnghPointerType.TOUCH]: 'touch', | ||
| [RnghPointerType.STYLUS]: 'pen', | ||
| [RnghPointerType.MOUSE]: 'mouse', | ||
| }; | ||
|
|
||
| type RnghEventLike = { | ||
| x: number; | ||
| y: number; | ||
| pointerType?: number; | ||
| }; | ||
|
|
||
| const createInputEvent = (event: RnghEventLike, timestamp: number): InputEvent => { | ||
| 'worklet'; | ||
| const pointerType = | ||
| event.pointerType !== undefined | ||
| ? (RNGH_POINTER_TYPE_MAP[event.pointerType] ?? 'unknown') | ||
| : 'unknown'; | ||
| return { x: event.x, y: event.y, timestamp, pointerType }; | ||
| }; | ||
|
|
||
| export const useRnghPanAdapter = ({ | ||
| eraserMode, | ||
| pencilOnly, | ||
| minDistance, | ||
| callbacks, | ||
| enabled = true, | ||
| }: InputAdapterConfig & { enabled?: boolean }): InputAdapter<ReturnType<typeof Gesture.Pan>> => { | ||
| // callbacksRef로 stable closure 유지 — gesture 재생성 트리거 회피 | ||
| const callbacksRef = useRef<DrawingInputCallbacks>(callbacks); | ||
| callbacksRef.current = callbacks; | ||
|
|
||
| const eraserModeShared = useSharedValue(eraserMode); | ||
| eraserModeShared.value = eraserMode; | ||
| const pencilOnlyShared = useSharedValue(pencilOnly); | ||
| pencilOnlyShared.value = pencilOnly; | ||
| const isActiveShared = useSharedValue(false); | ||
|
|
||
| const handleInteractionBegin = useCallback(() => { | ||
| callbacksRef.current.onInteractionBegin(); | ||
| }, []); | ||
|
|
||
| const handleDrawStart = useCallback((input: InputEvent) => { | ||
| callbacksRef.current.onDrawStart(input); | ||
| }, []); | ||
|
|
||
| const handleDrawMove = useCallback((input: InputEvent) => { | ||
| callbacksRef.current.onDrawMove(input); | ||
| }, []); | ||
|
|
||
| const handleDrawEnd = useCallback(() => { | ||
| callbacksRef.current.onDrawEnd(); | ||
| }, []); | ||
|
|
||
| const handleDrawCancel = useCallback(() => { | ||
| callbacksRef.current.onDrawCancel('gesture_failed'); | ||
| }, []); | ||
|
|
||
| const handleEraseStart = useCallback((input: InputEvent) => { | ||
| callbacksRef.current.onEraseStart(input); | ||
| }, []); | ||
|
|
||
| const handleEraseMove = useCallback((input: InputEvent) => { | ||
| callbacksRef.current.onEraseMove(input); | ||
| }, []); | ||
|
|
||
| const handleInteractionFinalize = useCallback(() => { | ||
| callbacksRef.current.onInteractionFinalize(); | ||
| }, []); | ||
|
|
||
| const gesture = useMemo( | ||
| () => | ||
| Gesture.Pan() | ||
| .enabled(enabled) | ||
| .maxPointers(1) | ||
| .averageTouches(true) | ||
| .minDistance(minDistance) | ||
| .onBegin(() => { | ||
| 'worklet'; | ||
| runOnJS(handleInteractionBegin)(); | ||
| }) | ||
| .onStart((event) => { | ||
| 'worklet'; | ||
| if (pencilOnlyShared.value && event.pointerType === RnghPointerType.TOUCH) { | ||
| return; | ||
| } | ||
| const input = createInputEvent(event, Date.now()); | ||
| isActiveShared.value = true; | ||
|
|
||
| if (eraserModeShared.value) { | ||
| runOnJS(handleEraseStart)(input); | ||
| return; | ||
| } | ||
| runOnJS(handleDrawStart)(input); | ||
| }) | ||
| .onUpdate((event) => { | ||
| 'worklet'; | ||
| if (pencilOnlyShared.value && event.pointerType === RnghPointerType.TOUCH) { | ||
| return; | ||
| } | ||
| const input = createInputEvent(event, Date.now()); | ||
|
|
||
| if (eraserModeShared.value) { | ||
| runOnJS(handleEraseMove)(input); | ||
| return; | ||
| } | ||
| runOnJS(handleDrawMove)(input); | ||
| }) | ||
| .onEnd(() => { | ||
| 'worklet'; | ||
| isActiveShared.value = false; | ||
| if (eraserModeShared.value) return; | ||
| runOnJS(handleDrawEnd)(); | ||
| }) | ||
| .onFinalize(() => { | ||
| 'worklet'; | ||
| if (!eraserModeShared.value && isActiveShared.value) { | ||
| runOnJS(handleDrawCancel)(); | ||
| } | ||
| isActiveShared.value = false; | ||
| runOnJS(handleInteractionFinalize)(); | ||
| }), | ||
| [ | ||
| enabled, | ||
| minDistance, | ||
| eraserModeShared, | ||
| pencilOnlyShared, | ||
| isActiveShared, | ||
| handleInteractionBegin, | ||
| handleDrawStart, | ||
| handleDrawMove, | ||
| handleDrawEnd, | ||
| handleDrawCancel, | ||
| handleEraseStart, | ||
| handleEraseMove, | ||
| handleInteractionFinalize, | ||
| ] | ||
| ); | ||
|
|
||
| return { gesture }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.