11<script lang="ts" setup>
2- import { computed , onMounted , ref , watch , watchEffect } from ' vue'
2+ import { computed , onMounted , ref , watchEffect } from ' vue'
33import type { Stage } from ' konva/lib/Stage'
44import type { Shape } from ' konva/lib/Shape'
55import type { KonvaEventObject } from ' konva/lib/Node'
@@ -10,9 +10,11 @@ import type { Size } from '@/models/common'
1010import { normalizeDegree , round , useAsyncComputedLegacy } from ' @/utils/utils'
1111import { useFileImg } from ' @/utils/file'
1212import { cancelBubble , getNodeId } from ' ./common'
13+ import type { SpriteLocalConfig } from ' ./quick-config/utils'
1314
1415const props = defineProps <{
1516 sprite: Sprite
17+ localConfig: SpriteLocalConfig | null
1618 selected: boolean
1719 project: Project
1820 mapSize: Size
@@ -28,12 +30,6 @@ const emit = defineEmits<{
2830 selected: []
2931 dragMove: [notifyCameraScroll : CameraScrollNotifyFn ]
3032 dragEnd: []
31- updatePos: [{ x: number ; y: number }]
32- updatePosEnd: [{ x: number ; y: number }]
33- updateHeading: [{ heading: number ; leftRight? : LeftRight }]
34- updateHeadingEnd: [{ heading: number ; leftRight? : LeftRight }]
35- updateSize: [{ size: number }]
36- updateSizeEnd: [{ size: number }]
3733}>()
3834
3935const nodeRef = ref <KonvaNodeInstance <Image >>()
@@ -62,75 +58,86 @@ onMounted(() => {
6258 }
6359})
6460
65- function updateSprite({
66- oldSize ,
67- size ,
68- oldHeading ,
69- heading ,
61+ function updateLocalConfig({
7062 oldX ,
7163 x ,
7264 oldY ,
7365 y ,
74- isEnd = false
66+ oldSize ,
67+ size ,
68+ oldHeading ,
69+ heading
7570}: {
76- oldSize: number
77- size: number
78- oldHeading: number
79- heading: number
8071 oldX: number
8172 x: number
8273 oldY: number
8374 y: number
84- isEnd? : boolean
75+ oldSize: number
76+ size: number
77+ oldHeading: number
78+ heading: number
8579}) {
86- if (oldSize !== size ) {
87- if (isEnd ) {
88- emit (' updateSizeEnd' , { size })
89- } else {
90- emit (' updateSize' , { size })
91- }
80+ const spriteLocalConfig = props .localConfig
81+ if (spriteLocalConfig == null ) return
82+ if (size !== oldSize ) {
83+ spriteLocalConfig .setSize (size )
9284 return
9385 }
94- if (oldHeading !== heading && props .sprite .rotationStyle !== RotationStyle .None ) {
95- let leftRight: LeftRight | undefined = undefined
96- if (props .sprite .rotationStyle === RotationStyle .LeftRight ) {
97- leftRight = headingToLeftRight (heading )
98- }
99- if (isEnd ) {
100- emit (' updateHeadingEnd' , { heading , leftRight })
101- } else {
102- emit (' updateHeading' , { heading , leftRight })
103- }
86+ if (heading !== oldHeading && spriteLocalConfig .rotationStyle === RotationStyle .Normal ) {
87+ spriteLocalConfig .setHeading (heading )
10488 return
10589 }
106- if (oldX !== x || oldY !== y ) {
107- if (isEnd ) {
108- emit (' updatePosEnd' , { x , y })
109- } else {
110- emit (' updatePos' , { x , y })
111- }
90+ if (x !== oldX || y !== oldY ) {
91+ spriteLocalConfig .setX (x )
92+ spriteLocalConfig .setY (y )
11293 }
11394}
114-
115- const notifyUpdateSprite = (node : Shape | Stage , isEnd = false ) => {
116- if (! props .selected ) return
95+ function updateLocalConfigByShape(node : Shape | Stage ) {
96+ if (! props .selected || props .localConfig == null ) return
11797 const { x, y } = toPosition (node )
118- updateSprite ({
119- oldSize: props .sprite .size ,
120- size: toSize (node ),
121- oldHeading: props .sprite .heading ,
122- heading: toHeading (node ),
98+ updateLocalConfig ({
12399 oldX: props .sprite .x ,
124100 x ,
125101 oldY: props .sprite .y ,
126102 y ,
127- isEnd
103+ oldSize: props .sprite .size ,
104+ size: toSize (node ),
105+ oldHeading: props .sprite .heading ,
106+ heading: toHeading (node )
107+ })
108+ }
109+
110+ function syncLocalConfig({ size , x , y , heading }: { size: number ; x: number ; y: number ; heading: number }) {
111+ const spriteLocalConfig = props .localConfig
112+ if (spriteLocalConfig == null ) return
113+ if (size != null && props .sprite .size !== size ) {
114+ spriteLocalConfig .setSize (size , false )
115+ spriteLocalConfig .syncSize ()
116+ return
117+ }
118+ if (props .sprite .heading !== heading ) {
119+ spriteLocalConfig .setHeading (heading , false )
120+ spriteLocalConfig .syncHeading ()
121+ return
122+ }
123+ if (props .sprite .x !== x || props .sprite .y !== y ) {
124+ spriteLocalConfig .setX (x , false )
125+ spriteLocalConfig .setX (x , false )
126+ spriteLocalConfig .syncPos ()
127+ }
128+ }
129+ function syncLocalConfigByShape(node : Shape | Stage ) {
130+ syncLocalConfig ({
131+ size: toSize (node ),
132+ x: toPosition (node ).x ,
133+ y: toPosition (node ).y ,
134+ heading: toHeading (node )
128135 })
129136}
130137
131138function handleDragMove(e : KonvaEventObject <unknown >) {
132139 cancelBubble (e )
133- notifyUpdateSprite (e .target )
140+ updateLocalConfigByShape (e .target )
134141 emit (' dragMove' , (delta ) => {
135142 // Adjust position if camera scrolled during dragging to keep the sprite visually unmoved
136143 e .target .x (e .target .x () - delta .x )
@@ -140,12 +147,12 @@ function handleDragMove(e: KonvaEventObject<unknown>) {
140147
141148function handleDragEnd(e : KonvaEventObject <unknown >) {
142149 cancelBubble (e )
143- notifyUpdateSprite (e .target , true )
150+ syncLocalConfigByShape (e .target )
144151 emit (' dragEnd' )
145152}
146153
147154function handleTransformed(e : KonvaEventObject <unknown >) {
148- notifyUpdateSprite (e .target , true )
155+ syncLocalConfigByShape (e .target )
149156}
150157
151158const config = computed <ImageConfig >(() => {
@@ -177,27 +184,6 @@ const config = computed<ImageConfig>(() => {
177184 return config
178185})
179186
180- // After Sprite changes, updateXXX events also need to be triggered
181- watch (config , () => {
182- const node = nodeRef .value ?.getNode ()
183- if (node != null ) {
184- const { x, y, heading, size } = props .sprite
185- const { x : oldX, y : oldY } = toPosition (node )
186- const oldHeading = toHeading (node )
187- const oldSize = toSize (node )
188- updateSprite ({
189- oldSize ,
190- size ,
191- oldHeading ,
192- heading ,
193- oldX ,
194- x ,
195- oldY ,
196- y
197- })
198- }
199- })
200-
201187function toPosition(node : Shape | Stage ) {
202188 const { mapSize } = props
203189 const x = round (node .x () - mapSize .width / 2 )
@@ -228,7 +214,7 @@ function handleClick() {
228214 :config =" config"
229215 @dragmove =" handleDragMove"
230216 @dragend =" handleDragEnd"
231- @transform =" notifyUpdateSprite ($event.target)"
217+ @transform =" updateLocalConfigByShape ($event.target)"
232218 @transformend =" handleTransformed"
233219 @click =" handleClick"
234220 />
0 commit comments