Projekt

Obecné

Profil

Stáhnout (4.41 KB) Statistiky
| Větev: | Tag: | Revize:
1 8d384539 Fantič
import { Box } from 'native-base';
2
import React, { useRef } from 'react';
3
import { StyleSheet, Dimensions } from 'react-native';
4 28ab969f Fantič
import { PanGestureHandler, PinchGestureHandler } 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 8d384539 Fantič
import Svg, { SvgXml } from 'react-native-svg';
12 aac857cf Fantič
13 7a3ad946 Fantič
const CastlePlanView = (props: { mapImage: PlanImage, roomList: Room[], selectedRoom?: Room, fullScreenMode?: boolean, height?: number }) => {
14 8d384539 Fantič
15 28ab969f Fantič
    const DEFAULT_SCALE = 1
16
    const MIN_SCALE = 0.95
17
18
    const MAP_VIEW_SIZE = {
19 5cee436e Fantič
        height: 500,
20
        width: "100%"
21 28ab969f Fantič
    }
22
23 7a3ad946 Fantič
    const { mapImage, roomList, selectedRoom, fullScreenMode, height } = props;
24 28ab969f Fantič
25
    const panRef = useRef<React.ReactElement>();
26
    const pinchRef = useRef<React.ReactElement>();
27 8d384539 Fantič
    const svgRef = useRef<React.ReactElement>();
28
29
    const translateX = useSharedValue(0);
30
    const translateY = useSharedValue(0);
31 28ab969f Fantič
    const scale = useSharedValue(DEFAULT_SCALE);
32 8d384539 Fantič
33 28ab969f Fantič
    console.log(mapImage.svg.substring(0, 500))
34 8d384539 Fantič
35 28ab969f Fantič
    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
    });
46 aac857cf Fantič
47 8d384539 Fantič
    const onGestureEvent = useAnimatedGestureHandler({
48
        onActive: (event: any) => {
49
            // handle two fingers -> zoom  + - 
50 28ab969f Fantič
            if (event.numberOfPointers === 2) {
51
                if (event.scale < MIN_SCALE) {
52 8d384539 Fantič
                    scale.value = MIN_SCALE
53
                }
54 28ab969f Fantič
                else {
55 8d384539 Fantič
                    scale.value = event.scale;
56
                }
57
            }
58
        },
59
    });
60 f220395e Fantič
61 8d384539 Fantič
    const animatedStyle = useAnimatedStyle(() => {
62
        return {
63
            transform: [
64
                { translateX: translateX.value },
65
                { translateY: translateY.value },
66
                { scale: scale.value },
67
            ],
68
        };
69
    });
70 aac857cf Fantič
71 5cee436e Fantič
    console.log(fullScreenMode)
72
73
    console.log(Dimensions.get('window').height)
74
    console.log(Dimensions.get('window').width)
75
76 aac857cf Fantič
    return (
77 8d384539 Fantič
        // container
78 5cee436e Fantič
        <Box
79
            width={!fullScreenMode ? MAP_VIEW_SIZE.width : Dimensions.get('window').width - 5}
80 7a3ad946 Fantič
            height={height ? height : (!fullScreenMode ? MAP_VIEW_SIZE.height : Dimensions.get('window').height - 62.5)}
81 5cee436e Fantič
            // @ts-ignore
82
            style={
83
                fullScreenMode ? {
84
                    marginTop: 2.5,
85
                    marginLeft: 2.5,
86
                    marginRight: 2.5,
87
                }
88
                    :
89 7a3ad946 Fantič
                    { overflow: "hidden" }
90
            }
91
            borderColor={"light.300"}
92
            borderRadius={10}
93
            borderWidth={1}>
94 28ab969f Fantič
            <PinchGestureHandler
95
                ref={pinchRef}
96
                // @ts-ignore
97
                onGestureEvent={onGestureEvent}
98
                simultaneousHandlers={[svgRef, panRef]}>
99
                <Animated.View style={{ flex: 1 }}>
100
                    <PanGestureHandler
101
                        ref={panRef}
102
                        simultaneousHandlers={[svgRef, pinchRef]}
103
                        onGestureEvent={onPanEvent}
104 8d384539 Fantič
                    >
105 28ab969f Fantič
                        <Animated.View style={[{
106
                            flex: 1,
107
                        }, animatedStyle]}>
108
                            <Svg
109
                                ref={(ref: any) => (svgRef.current = ref)}
110
                            >
111
                                {mapImage && mapImage.viewBox &&
112
                                    // background image                    
113
                                    <SvgXml
114
                                        xml={mapImage.svg}
115
                                        width={"100%"}
116
                                        height={"100%"}
117 8d384539 Fantič
118 28ab969f Fantič
                                    />}
119
                                {mapImage && roomList && roomList.length > 0 &&
120
                                    <></>}
121 8d384539 Fantič
122 28ab969f Fantič
                            </Svg>
123
                        </Animated.View>
124
                    </PanGestureHandler>
125 8d384539 Fantič
                </Animated.View>
126
            </PinchGestureHandler>
127 aac857cf Fantič
        </Box>
128
    );
129 8d384539 Fantič
};
130
131
export default CastlePlanView;