Revize bdedfcb4
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 |
import { StyleSheet, Dimensions } from 'react-native'; |
|
4 |
import { PanGestureHandler, PinchGestureHandler } from 'react-native-gesture-handler'; |
|
3 |
import { StyleSheet, Dimensions, TouchableOpacity } from 'react-native';
|
|
4 |
import { PanGestureHandler, PinchGestureHandler, State, TapGestureHandler } from 'react-native-gesture-handler';
|
|
5 | 5 |
import Animated, { |
6 | 6 |
useAnimatedGestureHandler, |
7 | 7 |
useAnimatedStyle, |
... | ... | |
10 | 10 |
import { PlanImage, Room } from '../../types/plan'; |
11 | 11 |
import Svg, { Path, SvgXml, Text } from 'react-native-svg'; |
12 | 12 |
|
13 |
// @ts-ignore |
|
14 |
import { pointInSvgPath } from 'point-in-svg-path'; |
|
15 |
import { getFloorList } from '../../stores/actions/planThunks'; |
|
16 |
|
|
17 |
|
|
13 | 18 |
const CastlePlanView = (props: { mapImage: PlanImage, roomList: Room[], selectedRoom?: Room, fullScreenMode?: boolean, height?: number }) => { |
14 | 19 |
|
15 | 20 |
const DEFAULT_SCALE = 1 |
... | ... | |
24 | 29 |
|
25 | 30 |
const panRef = useRef<React.ReactElement>(); |
26 | 31 |
const pinchRef = useRef<React.ReactElement>(); |
32 |
|
|
27 | 33 |
const svgRef = useRef<React.ReactElement>(); |
34 |
const pathRefs = useRef<(Path | null)[]>([]); |
|
28 | 35 |
|
29 | 36 |
const translateX = useSharedValue(0); |
30 | 37 |
const translateY = useSharedValue(0); |
... | ... | |
75 | 82 |
}; |
76 | 83 |
}); |
77 | 84 |
|
78 |
console.log(fullScreenMode) |
|
85 |
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 |
|
79 | 96 |
|
80 |
console.log(Dimensions.get('window').height) |
|
81 |
console.log(Dimensions.get('window').width) |
|
97 |
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 |
}; |
|
82 | 116 |
|
83 | 117 |
return ( |
84 | 118 |
// container |
... | ... | |
111 | 145 |
ref={panRef} |
112 | 146 |
simultaneousHandlers={[svgRef, pinchRef]} |
113 | 147 |
onGestureEvent={onPanEvent} |
148 |
onHandlerStateChange={(nativeEvent: any) => { |
|
149 |
if (nativeEvent.state === State.END) { |
|
150 |
console.log(nativeEvent) |
|
151 |
} |
|
152 |
}} |
|
114 | 153 |
> |
115 | 154 |
<Animated.View style={[{ |
116 | 155 |
flex: 1, |
117 | 156 |
}, animatedStyle]}> |
118 | 157 |
<Svg |
158 |
id="svgMap" |
|
119 | 159 |
ref={(ref: any) => (svgRef.current = ref)} |
160 |
onPress={handleSvgPress} |
|
120 | 161 |
> |
121 | 162 |
{mapImage && mapImage.viewBox && |
122 | 163 |
// background image |
... | ... | |
124 | 165 |
xml={mapImage.svg} |
125 | 166 |
width={"100%"} |
126 | 167 |
height={"100%"} |
127 |
|
|
128 | 168 |
/> |
129 | 169 |
} |
130 | 170 |
|
131 | 171 |
{mapImage && roomList && roomList.length > 0 && |
132 |
roomList.map((room) => { |
|
172 |
|
|
173 |
roomList.map((room, index) => { |
|
133 | 174 |
if (!room.in_plan) { |
134 | 175 |
return |
135 | 176 |
} |
136 | 177 |
return ( |
137 |
<React.Fragment key={room.id}> |
|
138 |
<Path |
|
139 |
|
|
140 |
d={room.svg_path} // The path data defining the shape of the room |
|
141 |
fill={selectedRoom && room.id == selectedRoom.id ? "#E6F7FF" : "none"} // Fill the room shape with no color |
|
142 |
stroke="#66B2FF" // Outline color |
|
143 |
strokeWidth={0.3} // Outline width |
|
144 |
/> |
|
145 |
{/* Text label in the middle of the room */} |
|
146 |
<Text |
|
147 |
x={room.number_x} // Adjust the x-coordinate based on your room data |
|
148 |
y={room.number_y} // Adjust the y-coordinate based on your room data |
|
149 |
fill="red" // Text color |
|
150 |
fontSize={8} // Font size |
|
151 |
textAnchor="middle" // Center the text horizontally |
|
152 |
fontStyle='italic' |
|
153 |
vectorEffect='non-scaling-stroke' // Helps prevent stroke from scaling |
|
154 |
> |
|
155 |
{room.id} |
|
156 |
</Text> |
|
157 |
</React.Fragment> |
|
158 |
|
|
178 |
<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 |
/> |
|
159 | 188 |
) |
160 |
}) |
|
161 |
} |
|
189 |
})} |
|
190 |
|
|
191 |
{mapImage && roomList && roomList.length > 0 && |
|
162 | 192 |
|
193 |
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 |
})} |
|
163 | 211 |
</Svg> |
164 | 212 |
</Animated.View> |
165 | 213 |
</PanGestureHandler> |
166 | 214 |
</Animated.View> |
167 |
</PinchGestureHandler> |
|
215 |
</PinchGestureHandler >
|
|
168 | 216 |
</Box > |
169 | 217 |
); |
170 | 218 |
}; |
Také k dispozici: Unified diff
re #10871: PlanViewPage: Handle room clicked debug