Skip to content

Commit

Permalink
feat: locking (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
webbertakken authored Oct 29, 2022
1 parent d8d20ee commit 8ad6bcc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/components/canvas/objects/CanvasSprite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
spriteMetasWithId,
} from '../../../state/SpritesState'
import { useTransformer } from './hooks/useTransformer'
import { noop } from 'lodash'

interface Props extends KonvaNodeEvents, Partial<ImageConfig> {
id: string
Expand All @@ -26,7 +27,7 @@ export const CanvasSprite = ({ id, ...props }: Props) => {

const isSelected = selectedIds.includes(id)

const { scale, rotation, position, opacity } = spriteData
const { scale, rotation, position, opacity, locked } = spriteData
const { src, isDragging } = spriteMeta

const [htmlImageElement] = useImage(src)
Expand Down Expand Up @@ -54,6 +55,10 @@ export const CanvasSprite = ({ id, ...props }: Props) => {
setSpriteMeta((meta) => ({ ...meta, isDragging: false }))
}

const onDoubleClick = (e: Konva.KonvaEventObject<MouseEvent>) => {
onClick(e)
}

const onClick = (e: Konva.KonvaEventObject<MouseEvent>) => {
// prevent deselection by clicking on the canvas
e.cancelBubble = true
Expand All @@ -77,13 +82,15 @@ export const CanvasSprite = ({ id, ...props }: Props) => {
setSpriteData((data) => ({ ...data, scale: { x: scaleX, y: scaleY, z: '1.0' }, rotation }))
}

const draggable = !(locked && !isSelected)

return (
<>
{isSelected && <Transformer />}
<KonvaImage
// @ts-ignore
ref={ref}
draggable={true}
draggable={draggable}
shadowOffsetX={isDragging ? 10 : 5}
shadowOffsetY={isDragging ? 10 : 5}
shadowColor="rgba(0, 0, 0, 0.5)"
Expand All @@ -103,8 +110,10 @@ export const CanvasSprite = ({ id, ...props }: Props) => {
opacity={opacity}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
onClick={onClick}
onTap={onClick}
onClick={locked ? noop : onClick}
onDblClick={onDoubleClick}
onTap={locked ? noop : onClick}
onDblTap={onDoubleClick}
onTransformEnd={onTransformEnd}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import Section from '../../layout/Section'
import { Box, Heading } from 'dracula-ui'
import { Box, Checkbox, Heading } from 'dracula-ui'
import FormRow from '../../atoms/FormRow'
import { Field } from '../../atoms/Field'
import { defaultPropertiesState } from '../../../state/AssetsState'
Expand All @@ -13,9 +13,12 @@ const DefaultPropertiesSection = ({}: Props): JSX.Element => {
const [defaultProps, setDefaultProps] = useRecoilState(defaultPropertiesState)

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { id, value } = e.target
console.log(id, value)
setDefaultProps((data) => set(cloneDeep(data), id, value))
const { id, value, checked } = e.target
if (e.target.type === 'checkbox') {
setDefaultProps((data) => set(cloneDeep(data), id, checked))
} else {
setDefaultProps((data) => set(cloneDeep(data), id, value))
}
}

return (
Expand Down Expand Up @@ -97,6 +100,13 @@ const DefaultPropertiesSection = ({}: Props): JSX.Element => {
/>
<label htmlFor="opacity">Opacity</label>
</FormRow>

<FormRow>
<Box mt="xs">
<Checkbox color="green" id="locked" checked={defaultProps.locked} onChange={onChange} />
<label htmlFor="opacity">Locked</label>
</Box>
</FormRow>
</Section>
)
}
Expand Down
17 changes: 14 additions & 3 deletions src/components/sidebars/inspector/InspectorSection.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { HTMLInputTypeAttribute } from 'react'
import { useRecoilState, useRecoilValue } from 'recoil'
import { selectedSpriteIdsState, spriteDatasWithId } from '../../../state/SpritesState'
import { Box, Heading } from 'dracula-ui'
import { Box, Checkbox, Heading } from 'dracula-ui'
import Section from '../../layout/Section'
import FormRow from '../../atoms/FormRow'
import CopyButton from '../../atoms/CopyButton'
Expand All @@ -13,8 +13,12 @@ export const InspectorSection = (): JSX.Element => {
const [spriteData, setSpriteData] = useRecoilState(spriteDatasWithId(selectionId))

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { id, value } = e.target
setSpriteData((data) => set(cloneDeep(data), id, value))
const { id, value, checked } = e.target
if (e.target.type === 'checkbox') {
setSpriteData((data) => set(cloneDeep(data), id, checked))
} else {
setSpriteData((data) => set(cloneDeep(data), id, value))
}
}

return (
Expand Down Expand Up @@ -148,6 +152,13 @@ export const InspectorSection = (): JSX.Element => {
/>
<label htmlFor="opacity">Opacity</label>
</FormRow>

<FormRow>
<Box mt="xs">
<Checkbox color="green" id="locked" checked={spriteData.locked} onChange={onChange} />
<label htmlFor="opacity">Locked</label>
</Box>
</FormRow>
</Section>
)
}
Expand Down
1 change: 1 addition & 0 deletions src/model/DefaultProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class DefaultProperties implements Partial<SpriteData> {
rotation: 0,
scale: { x: '1.0', y: '1.0', z: '1.0' },
opacity: 1,
locked: false,
}
}
}
2 changes: 2 additions & 0 deletions src/model/SpriteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export class SpriteData {
public opacity: number = 1
// Relative path to the asset
public relativePath: string = ''
// Whether you can drag the sprite
public locked: boolean = false

static createFromDragAndDrop(
x: number,
Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ interface SpriteData {
opacity: number
// Relative path to the asset
relativePath: string
// Whether you can drag the sprite
locked: boolean
}

interface Sprites {
Expand Down

0 comments on commit 8ad6bcc

Please sign in to comment.