Skip to content

Commit

Permalink
feat: add drag to brushmode
Browse files Browse the repository at this point in the history
  • Loading branch information
matyson committed Feb 29, 2024
1 parent 97cc51c commit c7d9a87
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
4 changes: 4 additions & 0 deletions apps/pianno/components/annotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const Annotation = (props: AnnotationProps) => {

useEffect(() => {
const onPointerDown = (e: PIXI.FederatedPointerEvent) => {
if(brushMode === 'drag') return;

if (!context) return;
context.fillStyle = color;
if (e.pointerType === 'mouse') {
Expand All @@ -85,6 +87,7 @@ const Annotation = (props: AnnotationProps) => {
}

setIsPainting(true);
viewport?.plugins.pause('drag');
const pos = viewport?.toWorld(e.global) ?? e.global;
prevPosition.current = pos;

Expand Down Expand Up @@ -134,6 +137,7 @@ const Annotation = (props: AnnotationProps) => {
const onPointerUp = (e: PIXI.FederatedPointerEvent) => {
if (!canvas) return;
setIsPainting(false);
viewport?.plugins.resume('drag');
setLabel(canvas.toDataURL());
};

Expand Down
6 changes: 3 additions & 3 deletions apps/pianno/components/toolbar/open.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const OpenImageDialog: FC<OpenImageDialogProps> = ({}) => {

const { setImage, setImageMetadata, softReset } = useStoreActions();

useHotkeys(['3'], () => {
useHotkeys(['4'], () => {
setOpen(true);
});

Expand Down Expand Up @@ -215,11 +215,11 @@ const OpenImageDialog: FC<OpenImageDialogProps> = ({}) => {
<Button
className="group relative"
size={'icon'}
title={'load image -- 3'}
title={'load image -- 4'}
variant={'outline'}
>
<p className="absolute right-1 top-0 text-xs text-input group-hover:text-accent-foreground">
3
4
</p>
<ImageIcon className="h-4 w-4" />
</Button>
Expand Down
10 changes: 5 additions & 5 deletions apps/pianno/components/toolbar/save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const SaveDialog: FC<SaveDialogProps> = ({ disabled }) => {
}, [label]);

useHotkeys(
['4'],
['5'],
() => {
if (!toggled) {
togglePairs();
Expand Down Expand Up @@ -167,11 +167,11 @@ const SaveDialog: FC<SaveDialogProps> = ({ disabled }) => {
className="group relative"
disabled={disabled}
size={'icon'}
title={'save json -- 4'}
title={'save json -- 5'}
variant={'outline'}
>
<p className="absolute right-1 top-0 text-xs text-input group-hover:text-accent-foreground">
4
5
</p>
<SaveIcon className="h-4 w-4 group-hover:hidden group-hover:animate-out" />
<BanIcon className="hidden h-4 w-4 scale-125 stroke-red-600 group-hover:block group-hover:animate-in" />
Expand Down Expand Up @@ -209,11 +209,11 @@ const SaveDialog: FC<SaveDialogProps> = ({ disabled }) => {
className="group relative"
disabled={disabled}
size={'icon'}
title={'save json -- 4'}
title={'save json -- 5'}
variant={'outline'}
>
<p className="absolute right-1 top-0 text-xs text-input group-hover:text-accent-foreground">
4
5
</p>
<SaveIcon className="h-4 w-4" />
</Button>
Expand Down
6 changes: 3 additions & 3 deletions apps/pianno/components/toolbar/toggle-pairs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface TogglePairsProps {

const TogglePairs: FC<TogglePairsProps> = ({ disabled }) => {
useHotkeys(
'5',
'6',
() => {
toggle();
},
Expand All @@ -23,15 +23,15 @@ const TogglePairs: FC<TogglePairsProps> = ({ disabled }) => {
const { toggle } = useStoreActions();
return (
<Toggle
aria-label="toggle pairs -- 5"
aria-label="toggle pairs -- 6"
className="group relative"
disabled={disabled}
onPressedChange={toggle}
pressed={toggled}
title="toggle pairs -- 5"
>
<p className="absolute right-1 top-0 text-xs text-input group-hover:text-accent-foreground">
5
6
</p>
{toggled ? <Eye className="h-4 w-4" /> : <EyeOff className="h-4 w-4" />}
</Toggle>
Expand Down
19 changes: 9 additions & 10 deletions apps/pianno/components/toolbar/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import {
useStoreActions,
useStoreBrushParams,
useStoreImageMetadata,
useStoreImg,
} from '@/hooks/use-store';
Expand Down Expand Up @@ -79,20 +78,20 @@ const Toolbar = () => {
const canClick = img.src !== '#';

useHotkeys(
['6', 'ctrl+z'],
['7', 'ctrl+z'],
() => {
undo();
},
{ enabled: canUndo },
);
useHotkeys(
['7', 'ctrl+y'],
['8', 'ctrl+y'],
() => {
redo();
},
{ enabled: canRedo },
);
useHotkeys(['8'], () => {
useHotkeys(['9'], () => {
recenterViewport(
img.width ? img.width : width,
img.height ? img.height : height,
Expand All @@ -103,30 +102,30 @@ const Toolbar = () => {
{
children: <UndoIcon className="h-4 w-4" />,
disabled: !canUndo,
hotkey: '6',
hotkey: '7',
onClick: () => {
undo();
},
title: 'undo -- 6 or ctrl+z',
title: 'undo -- 7 or ctrl+z',
},
{
children: <RedoIcon className="h-4 w-4" />,
disabled: !canRedo,
hotkey: '7',
hotkey: '8',
onClick: () => {
redo();
},
title: 'redo -- 7 or ctrl+y',
title: 'redo -- 8 or ctrl+y',
},
{
children: <MaximizeIcon className="h-4 w-4" />,
hotkey: '8',
hotkey: '9',
onClick: () =>
recenterViewport(
img.width ? img.width : width,
img.height ? img.height : height,
),
title: 'fit-view -- 8',
title: 'fit-view -- 9',
},
];

Expand Down
10 changes: 9 additions & 1 deletion apps/pianno/components/toolbar/tools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BrushMode } from '@/lib/types';
import { Label } from '@/components/ui/label';
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
import { useStoreActions, useStoreBrushParams } from '@/hooks/use-store';
import { EraserIcon, PenIcon } from 'lucide-react';
import { EraserIcon, HandIcon, PenIcon } from 'lucide-react';
import { FC } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';

Expand Down Expand Up @@ -39,6 +39,11 @@ const tools: Tool[] = [
icon: <EraserIcon className="h-4 w-4" />,
title: 'eraser -- 2 or e',
},
{
hotkey: '3',
icon: <HandIcon className="h-4 w-4" />,
title: 'drag -- 3 or d',
},
];

interface ToolsProps {}
Expand All @@ -52,6 +57,9 @@ const Tools: FC<ToolsProps> = ({}) => {
useHotkeys(['2', 'e'], () => setBrushMode('eraser'), {
enabled: brushMode !== undefined,
});
useHotkeys(['3', 'd'], () => setBrushMode('drag'), {
enabled: brushMode !== undefined,
});
return (
<RadioGroup
className="flex flex-row gap-1"
Expand Down
2 changes: 1 addition & 1 deletion apps/pianno/components/viewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const PixiComponentViewport = PixiComponent('Viewport', {
ticker: props.app.ticker,
});
viewport
.drag({mouseButtons: 'middle-right'})
.drag({mouseButtons: 'middle-left'})
.pinch()
.wheel()
.decelerate()
Expand Down
4 changes: 2 additions & 2 deletions apps/pianno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { z } from 'zod';

import { ANNOTATION_DISTANCE_TYPES } from './constants';

export type BrushMode = 'eraser' | 'pen';
export type BrushMode = 'drag' | 'eraser' | 'pen';

export type Brush = {
eraserSize: number;
maxSize: number;
mode: 'eraser' | 'pen' | undefined;
mode: BrushMode | undefined;
penSize: number;
};

Expand Down

0 comments on commit c7d9a87

Please sign in to comment.