1 |
8d384539
|
Fantič
|
import { Box } from 'native-base';
|
2 |
|
|
import React, { useRef } from 'react';
|
3 |
bdedfcb4
|
Fantič
|
import { StyleSheet, Dimensions, TouchableOpacity } from 'react-native';
|
4 |
|
|
import { PanGestureHandler, PinchGestureHandler, State, TapGestureHandler } from 'react-native-gesture-handler';
|
5 |
8d384539
|
Fantič
|
import Animated, {
|
6 |
|
|
useAnimatedGestureHandler,
|
7 |
|
|
useAnimatedStyle,
|
8 |
|
|
useSharedValue,
|
9 |
|
|
} from 'react-native-reanimated';
|
10 |
28ab969f
|
Fantič
|
import { PlanImage, Room } from '../../types/plan';
|
11 |
08be9ecd
|
Fantič
|
import Svg, { Path, SvgXml, Text } from 'react-native-svg';
|
12 |
aac857cf
|
Fantič
|
|
13 |
bdedfcb4
|
Fantič
|
// @ts-ignore
|
14 |
|
|
import { pointInSvgPath } from 'point-in-svg-path';
|
15 |
|
|
import { getFloorList } from '../../stores/actions/planThunks';
|
16 |
|
|
|
17 |
|
|
|
18 |
7a3ad946
|
Fantič
|
const CastlePlanView = (props: { mapImage: PlanImage, roomList: Room[], selectedRoom?: Room, fullScreenMode?: boolean, height?: number }) => {
|
19 |
8d384539
|
Fantič
|
|
20 |
28ab969f
|
Fantič
|
const DEFAULT_SCALE = 1
|
21 |
|
|
const MIN_SCALE = 0.95
|
22 |
|
|
|
23 |
|
|
const MAP_VIEW_SIZE = {
|
24 |
5cee436e
|
Fantič
|
height: 500,
|
25 |
|
|
width: "100%"
|
26 |
28ab969f
|
Fantič
|
}
|
27 |
|
|
|
28 |
7a3ad946
|
Fantič
|
const { mapImage, roomList, selectedRoom, fullScreenMode, height } = props;
|
29 |
28ab969f
|
Fantič
|
|
30 |
|
|
const panRef = useRef<React.ReactElement>();
|
31 |
|
|
const pinchRef = useRef<React.ReactElement>();
|
32 |
bdedfcb4
|
Fantič
|
|
33 |
8d384539
|
Fantič
|
const svgRef = useRef<React.ReactElement>();
|
34 |
bdedfcb4
|
Fantič
|
const pathRefs = useRef<(Path | null)[]>([]);
|
35 |
8d384539
|
Fantič
|
|
36 |
|
|
const translateX = useSharedValue(0);
|
37 |
|
|
const translateY = useSharedValue(0);
|
38 |
28ab969f
|
Fantič
|
const scale = useSharedValue(DEFAULT_SCALE);
|
39 |
8d384539
|
Fantič
|
|
40 |
28ab969f
|
Fantič
|
console.log(mapImage.svg.substring(0, 500))
|
41 |
8d384539
|
Fantič
|
|
42 |
28ab969f
|
Fantič
|
const onPanEvent = useAnimatedGestureHandler({
|
43 |
|
|
// handle one finger -> drag / move
|
44 |
|
|
onStart: (_, ctx: any) => {
|
45 |
|
|
ctx.startX = translateX.value;
|
46 |
|
|
ctx.startY = translateY.value;
|
47 |
|
|
},
|
48 |
|
|
onActive: (event, ctx: any) => {
|
49 |
|
|
translateX.value = ctx.startX + event.translationX;
|
50 |
|
|
translateY.value = ctx.startY + event.translationY;
|
51 |
|
|
},
|
52 |
|
|
});
|
53 |
aac857cf
|
Fantič
|
|
54 |
8d384539
|
Fantič
|
const onGestureEvent = useAnimatedGestureHandler({
|
55 |
08be9ecd
|
Fantič
|
onStart: (_, ctx: any) => {
|
56 |
|
|
ctx.startScale = scale.value;
|
57 |
|
|
},
|
58 |
|
|
onActive: (event: any, ctx: any) => {
|
59 |
|
|
// handle two fingers -> zoom + -
|
60 |
28ab969f
|
Fantič
|
if (event.numberOfPointers === 2) {
|
61 |
08be9ecd
|
Fantič
|
scale.value = ctx.startScale * event.scale;
|
62 |
|
|
}
|
63 |
|
|
},
|
64 |
|
|
onEnd: (event) => {
|
65 |
|
|
// handle two fingers -> zoom + -
|
66 |
|
|
if (event.numberOfPointers === 2) {
|
67 |
|
|
if (scale.value < MIN_SCALE) {
|
68 |
|
|
scale.value = MIN_SCALE;
|
69 |
8d384539
|
Fantič
|
}
|
70 |
08be9ecd
|
Fantič
|
// You may add additional logic for maximum scale if needed
|
71 |
8d384539
|
Fantič
|
}
|
72 |
|
|
},
|
73 |
|
|
});
|
74 |
f220395e
|
Fantič
|
|
75 |
8d384539
|
Fantič
|
const animatedStyle = useAnimatedStyle(() => {
|
76 |
|
|
return {
|
77 |
|
|
transform: [
|
78 |
|
|
{ translateX: translateX.value },
|
79 |
|
|
{ translateY: translateY.value },
|
80 |
|
|
{ scale: scale.value },
|
81 |
|
|
],
|
82 |
|
|
};
|
83 |
|
|
});
|
84 |
aac857cf
|
Fantič
|
|
85 |
bdedfcb4
|
Fantič
|
const handleSvgPress = (event: any) => {
|
86 |
|
|
const locationX = Number(event.nativeEvent.locationX);
|
87 |
|
|
const locationY = Number(event.nativeEvent.locationY);
|
88 |
|
|
|
89 |
|
|
// Check if the touch event is within the bounds of a room
|
90 |
|
|
let clickedRoom: Room | undefined = undefined
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
for (let i = 0; i < pathRefs.current.length; i++) {
|
94 |
|
|
const pathRef: Path | null = pathRefs.current[i]
|
95 |
|
|
const id = pathRef?.props.id
|
96 |
5cee436e
|
Fantič
|
|
97 |
bdedfcb4
|
Fantič
|
if (id?.startsWith("roomList_")) {
|
98 |
|
|
const listIndex = parseInt(id.split("roomList_")[1])
|
99 |
|
|
console.log(listIndex)
|
100 |
|
|
if (pathRef && pathRef.isPointInFill({ x: locationX, y: locationY })) {
|
101 |
|
|
clickedRoom = roomList[listIndex]
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
};
|
106 |
|
|
|
107 |
|
|
if (clickedRoom) {
|
108 |
|
|
// TODO
|
109 |
|
|
console.log('Room clicked with id:', clickedRoom.id);
|
110 |
|
|
// Perform any actions you need with the clicked room
|
111 |
|
|
} else {
|
112 |
|
|
// TODO
|
113 |
|
|
console.log("no room found")
|
114 |
|
|
}
|
115 |
|
|
};
|
116 |
5cee436e
|
Fantič
|
|
117 |
aac857cf
|
Fantič
|
return (
|
118 |
8d384539
|
Fantič
|
// container
|
119 |
5cee436e
|
Fantič
|
<Box
|
120 |
08be9ecd
|
Fantič
|
width={!fullScreenMode ? MAP_VIEW_SIZE.width : Dimensions.get('window').width - 5
|
121 |
|
|
}
|
122 |
7a3ad946
|
Fantič
|
height={height ? height : (!fullScreenMode ? MAP_VIEW_SIZE.height : Dimensions.get('window').height - 62.5)}
|
123 |
5cee436e
|
Fantič
|
// @ts-ignore
|
124 |
|
|
style={
|
125 |
|
|
fullScreenMode ? {
|
126 |
|
|
marginTop: 2.5,
|
127 |
|
|
marginLeft: 2.5,
|
128 |
|
|
marginRight: 2.5,
|
129 |
|
|
}
|
130 |
|
|
:
|
131 |
7a3ad946
|
Fantič
|
{ overflow: "hidden" }
|
132 |
|
|
}
|
133 |
|
|
borderColor={"light.300"}
|
134 |
|
|
borderRadius={10}
|
135 |
08be9ecd
|
Fantič
|
borderWidth={1}
|
136 |
|
|
>
|
137 |
|
|
|
138 |
28ab969f
|
Fantič
|
<PinchGestureHandler
|
139 |
|
|
ref={pinchRef}
|
140 |
|
|
// @ts-ignore
|
141 |
|
|
onGestureEvent={onGestureEvent}
|
142 |
|
|
simultaneousHandlers={[svgRef, panRef]}>
|
143 |
|
|
<Animated.View style={{ flex: 1 }}>
|
144 |
|
|
<PanGestureHandler
|
145 |
|
|
ref={panRef}
|
146 |
|
|
simultaneousHandlers={[svgRef, pinchRef]}
|
147 |
|
|
onGestureEvent={onPanEvent}
|
148 |
bdedfcb4
|
Fantič
|
onHandlerStateChange={(nativeEvent: any) => {
|
149 |
|
|
if (nativeEvent.state === State.END) {
|
150 |
|
|
console.log(nativeEvent)
|
151 |
|
|
}
|
152 |
|
|
}}
|
153 |
8d384539
|
Fantič
|
>
|
154 |
28ab969f
|
Fantič
|
<Animated.View style={[{
|
155 |
|
|
flex: 1,
|
156 |
|
|
}, animatedStyle]}>
|
157 |
|
|
<Svg
|
158 |
bdedfcb4
|
Fantič
|
id="svgMap"
|
159 |
28ab969f
|
Fantič
|
ref={(ref: any) => (svgRef.current = ref)}
|
160 |
bdedfcb4
|
Fantič
|
onPress={handleSvgPress}
|
161 |
28ab969f
|
Fantič
|
>
|
162 |
|
|
{mapImage && mapImage.viewBox &&
|
163 |
|
|
// background image
|
164 |
|
|
<SvgXml
|
165 |
|
|
xml={mapImage.svg}
|
166 |
|
|
width={"100%"}
|
167 |
|
|
height={"100%"}
|
168 |
08be9ecd
|
Fantič
|
/>
|
169 |
|
|
}
|
170 |
|
|
|
171 |
28ab969f
|
Fantič
|
{mapImage && roomList && roomList.length > 0 &&
|
172 |
bdedfcb4
|
Fantič
|
|
173 |
|
|
roomList.map((room, index) => {
|
174 |
08be9ecd
|
Fantič
|
if (!room.in_plan) {
|
175 |
|
|
return
|
176 |
|
|
}
|
177 |
|
|
return (
|
178 |
bdedfcb4
|
Fantič
|
<Path
|
179 |
|
|
ref={(ref) => (pathRefs.current[index] = ref)}
|
180 |
|
|
id={"roomList_" + index.toString()}
|
181 |
|
|
key={'room_' + room.id}
|
182 |
|
|
d={room.svg_path} // The path data defining the shape of the room
|
183 |
|
|
fill={selectedRoom && room.id == selectedRoom.id ? "#E6F7FF" : "white"} // Fill the room shape with no color
|
184 |
|
|
stroke="#66B2FF" // Outline color
|
185 |
|
|
strokeWidth={0.3} // Outline width
|
186 |
|
|
fillOpacity={selectedRoom && room.id == selectedRoom.id ? 1 : 0.2}
|
187 |
|
|
/>
|
188 |
08be9ecd
|
Fantič
|
)
|
189 |
bdedfcb4
|
Fantič
|
})}
|
190 |
|
|
|
191 |
|
|
{mapImage && roomList && roomList.length > 0 &&
|
192 |
8d384539
|
Fantič
|
|
193 |
bdedfcb4
|
Fantič
|
roomList.map((room) => {
|
194 |
|
|
if (!room.in_plan) {
|
195 |
|
|
return
|
196 |
|
|
}
|
197 |
|
|
return (
|
198 |
|
|
< Text
|
199 |
|
|
x={room.number_x} // Adjust the x-coordinate based on your room data
|
200 |
|
|
y={room.number_y} // Adjust the y-coordinate based on your room data
|
201 |
|
|
fill="red" // Text color
|
202 |
|
|
fontSize={8} // Font size
|
203 |
|
|
textAnchor="middle" // Center the text horizontally
|
204 |
|
|
fontStyle='italic'
|
205 |
|
|
vectorEffect='non-scaling-stroke' // Helps prevent stroke from scaling
|
206 |
|
|
>
|
207 |
|
|
{room.id}
|
208 |
|
|
</Text>
|
209 |
|
|
)
|
210 |
|
|
})}
|
211 |
28ab969f
|
Fantič
|
</Svg>
|
212 |
|
|
</Animated.View>
|
213 |
|
|
</PanGestureHandler>
|
214 |
8d384539
|
Fantič
|
</Animated.View>
|
215 |
bdedfcb4
|
Fantič
|
</PinchGestureHandler >
|
216 |
08be9ecd
|
Fantič
|
</Box >
|
217 |
aac857cf
|
Fantič
|
);
|
218 |
8d384539
|
Fantič
|
};
|
219 |
|
|
|
220 |
|
|
export default CastlePlanView;
|