Projekt

Obecné

Profil

Stáhnout (4.31 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 5cee436e Fantič
const CastlePlanView = (props: { mapImage: PlanImage, roomList: Room[], selectedRoom?: Room, fullScreenMode?: boolean }) => {
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 5cee436e Fantič
    const { mapImage, roomList, selectedRoom, fullScreenMode } = 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
            height={!fullScreenMode ? MAP_VIEW_SIZE.height : Dimensions.get('window').height - 62.5}
81
            // @ts-ignore
82
            style={
83
                fullScreenMode ? {
84
                    marginTop: 2.5,
85
                    marginLeft: 2.5,
86
                    marginRight: 2.5,
87
                }
88
                    :
89
                    { borderColor: "#F5F5F", borderWidth: 1, overflow: "hidden" }
90
            }>
91 28ab969f Fantič
            <PinchGestureHandler
92
                ref={pinchRef}
93
                // @ts-ignore
94
                onGestureEvent={onGestureEvent}
95
                simultaneousHandlers={[svgRef, panRef]}>
96
                <Animated.View style={{ flex: 1 }}>
97
                    <PanGestureHandler
98
                        ref={panRef}
99
                        simultaneousHandlers={[svgRef, pinchRef]}
100
                        onGestureEvent={onPanEvent}
101 8d384539 Fantič
                    >
102 28ab969f Fantič
                        <Animated.View style={[{
103
                            flex: 1,
104
                        }, animatedStyle]}>
105
                            <Svg
106
                                ref={(ref: any) => (svgRef.current = ref)}
107
                            >
108
                                {mapImage && mapImage.viewBox &&
109
                                    // background image                    
110
                                    <SvgXml
111
                                        xml={mapImage.svg}
112
                                        width={"100%"}
113
                                        height={"100%"}
114 8d384539 Fantič
115 28ab969f Fantič
                                    />}
116
                                {mapImage && roomList && roomList.length > 0 &&
117
                                    <></>}
118 8d384539 Fantič
119 28ab969f Fantič
                            </Svg>
120
                        </Animated.View>
121
                    </PanGestureHandler>
122 8d384539 Fantič
                </Animated.View>
123
            </PinchGestureHandler>
124 aac857cf Fantič
        </Box>
125
    );
126 8d384539 Fantič
};
127
128
export default CastlePlanView;