Revize 28ab969f
Přidáno uživatelem Fantič před více než 1 rok
src/components/plan/CastlePlanView.tsx | ||
---|---|---|
1 | 1 |
import { Box } from 'native-base'; |
2 | 2 |
import React, { useRef } from 'react'; |
3 | 3 |
import { StyleSheet, Dimensions } from 'react-native'; |
4 |
import { PinchGestureHandler } from 'react-native-gesture-handler'; |
|
4 |
import { PanGestureHandler, PinchGestureHandler } from 'react-native-gesture-handler';
|
|
5 | 5 |
import Animated, { |
6 | 6 |
useAnimatedGestureHandler, |
7 | 7 |
useAnimatedStyle, |
8 | 8 |
useSharedValue, |
9 | 9 |
} from 'react-native-reanimated'; |
10 |
import { PlanImage } from '../../types/plan'; |
|
11 |
import { Room } from '../../types/searchFormTypes'; |
|
10 |
import { PlanImage, Room } from '../../types/plan'; |
|
12 | 11 |
import Svg, { SvgXml } from 'react-native-svg'; |
13 | 12 |
|
14 |
const CastlePlanView = (props: { mapImage: PlanImage, selectedRoom?: Room }) => { |
|
15 |
const { mapImage } = props; |
|
13 |
const CastlePlanView = (props: { mapImage: PlanImage, roomList: Room[], selectedRoom?: Room }) => { |
|
16 | 14 |
|
15 |
const DEFAULT_SCALE = 1 |
|
16 |
const MIN_SCALE = 0.95 |
|
17 |
|
|
18 |
const MAP_VIEW_SIZE = { |
|
19 |
height : 400, |
|
20 |
width : "100%" |
|
21 |
} |
|
22 |
|
|
23 |
const { mapImage, roomList, selectedRoom } = props; |
|
24 |
|
|
25 |
const panRef = useRef<React.ReactElement>(); |
|
26 |
const pinchRef = useRef<React.ReactElement>(); |
|
17 | 27 |
const svgRef = useRef<React.ReactElement>(); |
18 | 28 |
|
19 | 29 |
const translateX = useSharedValue(0); |
20 | 30 |
const translateY = useSharedValue(0); |
21 |
const scale = useSharedValue(1);
|
|
31 |
const scale = useSharedValue(DEFAULT_SCALE);
|
|
22 | 32 |
|
23 |
console.log(mapImage.svg.substring(0,500)) |
|
33 |
console.log(mapImage.svg.substring(0, 500))
|
|
24 | 34 |
|
25 |
const DEFAULT_SCALE = 0 |
|
26 |
const MIN_SCALE = 0.95 |
|
35 |
const onPanEvent = useAnimatedGestureHandler({ |
|
36 |
// handle one finger -> drag / move |
|
37 |
onStart: (_, ctx: any) => { |
|
38 |
ctx.startX = translateX.value; |
|
39 |
ctx.startY = translateY.value; |
|
40 |
}, |
|
41 |
onActive: (event, ctx: any) => { |
|
42 |
translateX.value = ctx.startX + event.translationX; |
|
43 |
translateY.value = ctx.startY + event.translationY; |
|
44 |
}, |
|
45 |
}); |
|
27 | 46 |
|
28 | 47 |
const onGestureEvent = useAnimatedGestureHandler({ |
29 | 48 |
onActive: (event: any) => { |
30 |
|
|
31 |
// handle one finger -> drag |
|
32 |
if (event.numberOfPointers === 1) { |
|
33 |
|
|
34 |
} |
|
35 | 49 |
// handle two fingers -> zoom + - |
36 |
else if (event.numberOfPointers === 2) { |
|
37 |
|
|
38 |
if(event.scale < MIN_SCALE){ |
|
39 |
console.log("tohle je moc") |
|
50 |
if (event.numberOfPointers === 2) { |
|
51 |
if (event.scale < MIN_SCALE) { |
|
40 | 52 |
scale.value = MIN_SCALE |
41 | 53 |
} |
42 |
else{ |
|
54 |
else {
|
|
43 | 55 |
scale.value = event.scale; |
44 | 56 |
} |
45 |
|
|
46 |
console.log(scale.value) |
|
47 | 57 |
} |
48 | 58 |
}, |
49 | 59 |
}); |
... | ... | |
60 | 70 |
|
61 | 71 |
return ( |
62 | 72 |
// container |
63 |
<Box width="100%" height={370} style={{ borderColor: "#F5F5F", borderWidth: 1, overflow: "hidden" }}> |
|
64 |
<PinchGestureHandler onGestureEvent={onGestureEvent} simultaneousHandlers={svgRef}> |
|
65 |
<Animated.View style={[{ |
|
66 |
flex: 1, |
|
67 |
}, animatedStyle]}> |
|
68 |
<Svg |
|
69 |
ref={(ref: any) => (svgRef.current = ref)} |
|
70 |
|
|
73 |
<Box width={MAP_VIEW_SIZE.width} height={MAP_VIEW_SIZE.height} style={{ borderColor: "#F5F5F", borderWidth: 1, overflow: "hidden" }}> |
|
74 |
<PinchGestureHandler |
|
75 |
ref={pinchRef} |
|
76 |
// @ts-ignore |
|
77 |
onGestureEvent={onGestureEvent} |
|
78 |
simultaneousHandlers={[svgRef, panRef]}> |
|
79 |
<Animated.View style={{ flex: 1 }}> |
|
80 |
<PanGestureHandler |
|
81 |
ref={panRef} |
|
82 |
simultaneousHandlers={[svgRef, pinchRef]} |
|
83 |
onGestureEvent={onPanEvent} |
|
71 | 84 |
> |
72 |
{mapImage && mapImage.viewBox && |
|
73 |
// background image |
|
74 |
<SvgXml |
|
75 |
xml={mapImage.svg} |
|
76 |
width={"100%"} |
|
77 |
height={"100%"} |
|
85 |
<Animated.View style={[{ |
|
86 |
flex: 1, |
|
87 |
}, animatedStyle]}> |
|
88 |
<Svg |
|
89 |
ref={(ref: any) => (svgRef.current = ref)} |
|
90 |
> |
|
91 |
{mapImage && mapImage.viewBox && |
|
92 |
// background image |
|
93 |
<SvgXml |
|
94 |
xml={mapImage.svg} |
|
95 |
width={"100%"} |
|
96 |
height={"100%"} |
|
78 | 97 |
|
79 |
/>} |
|
98 |
/>} |
|
99 |
{mapImage && roomList && roomList.length > 0 && |
|
100 |
<></>} |
|
80 | 101 |
|
81 |
</Svg> |
|
102 |
</Svg> |
|
103 |
</Animated.View> |
|
104 |
</PanGestureHandler> |
|
82 | 105 |
</Animated.View> |
83 | 106 |
</PinchGestureHandler> |
84 | 107 |
</Box> |
Také k dispozici: Unified diff
re #10871: PlanViewPage: Drag/move castlePlan