Projekt

Obecné

Profil

Stáhnout (4.31 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Box } from 'native-base';
2
import React, { useRef } from 'react';
3
import { StyleSheet, Dimensions } from 'react-native';
4
import { PanGestureHandler, PinchGestureHandler } from 'react-native-gesture-handler';
5
import Animated, {
6
    useAnimatedGestureHandler,
7
    useAnimatedStyle,
8
    useSharedValue,
9
} from 'react-native-reanimated';
10
import { PlanImage, Room } from '../../types/plan';
11
import Svg, { SvgXml } from 'react-native-svg';
12

    
13
const CastlePlanView = (props: { mapImage: PlanImage, roomList: Room[], selectedRoom?: Room, fullScreenMode?: boolean }) => {
14

    
15
    const DEFAULT_SCALE = 1
16
    const MIN_SCALE = 0.95
17

    
18
    const MAP_VIEW_SIZE = {
19
        height: 500,
20
        width: "100%"
21
    }
22

    
23
    const { mapImage, roomList, selectedRoom, fullScreenMode } = props;
24

    
25
    const panRef = useRef<React.ReactElement>();
26
    const pinchRef = useRef<React.ReactElement>();
27
    const svgRef = useRef<React.ReactElement>();
28

    
29
    const translateX = useSharedValue(0);
30
    const translateY = useSharedValue(0);
31
    const scale = useSharedValue(DEFAULT_SCALE);
32

    
33
    console.log(mapImage.svg.substring(0, 500))
34

    
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
    });
46

    
47
    const onGestureEvent = useAnimatedGestureHandler({
48
        onActive: (event: any) => {
49
            // handle two fingers -> zoom  + - 
50
            if (event.numberOfPointers === 2) {
51
                if (event.scale < MIN_SCALE) {
52
                    scale.value = MIN_SCALE
53
                }
54
                else {
55
                    scale.value = event.scale;
56
                }
57
            }
58
        },
59
    });
60

    
61
    const animatedStyle = useAnimatedStyle(() => {
62
        return {
63
            transform: [
64
                { translateX: translateX.value },
65
                { translateY: translateY.value },
66
                { scale: scale.value },
67
            ],
68
        };
69
    });
70

    
71
    console.log(fullScreenMode)
72

    
73
    console.log(Dimensions.get('window').height)
74
    console.log(Dimensions.get('window').width)
75

    
76
    return (
77
        // container
78
        <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
            <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
                    >
102
                        <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

    
115
                                    />}
116
                                {mapImage && roomList && roomList.length > 0 &&
117
                                    <></>}
118

    
119
                            </Svg>
120
                        </Animated.View>
121
                    </PanGestureHandler>
122
                </Animated.View>
123
            </PinchGestureHandler>
124
        </Box>
125
    );
126
};
127

    
128
export default CastlePlanView;
129

    
    (1-1/1)