-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
169 lines (154 loc) · 4.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React, { Component } from 'react';
import { Animated, PanResponder, StyleSheet, View } from 'react-native';
const generateRadialPositions = (count, radius, spread_angle, start_angle) => {
let span = spread_angle < 360 ? 1 : 0;
let start = start_angle * Math.PI / 180;
let rad = spread_angle * Math.PI * 2 / 360 / (count - span);
return [...Array(count)].map((_, i) => {
return {
x: -Math.cos(start + rad * i) * radius,
y: -Math.sin(start + rad * i) * radius,
};
});
};
export default class RadialMenu extends Component {
constructor(props) {
super(props);
this.itemPanListener = this.itemPanListener.bind(this);
this.releaseItem = this.releaseItem.bind(this);
this.createPanResponder = this.createPanResponder.bind(this);
this.computeNewSelected = this.computeNewSelected.bind(this);
let children = React.Children.toArray(this.props.children);
let initial_spots = generateRadialPositions(
children.length - 1,
this.props.menuRadius,
this.props.spreadAngle,
this.props.startAngle
);
initial_spots.unshift({x: 0, y: 0});
this.state = {
item_spots: initial_spots,
item_anims: initial_spots.map((_, i) => {
return new Animated.ValueXY();
}),
selectedItem: null,
itemPanResponder: null,
children: children,
};
this.RMOpening = false;
}
componentWillMount() {
this.setState({ itemPanResponder: this.createPanResponder() });
}
itemPanListener(e, gestureState) {
let newSelected = null;
if (!this.RMOpening) {
newSelected = this.computeNewSelected(gestureState);
if (this.state.selectedItem !== newSelected) {
if (this.state.selectedItem !== null) {
let restSpot = this.state.item_spots[this.state.selectedItem];
Animated.spring(this.state.item_anims[this.state.selectedItem], {
toValue: restSpot,
}).start();
}
if (newSelected !== null && newSelected !== 0) {
Animated.spring(this.state.item_anims[newSelected], {
toValue: this.state.item_anims[0],
}).start();
}
this.state.selectedItem = newSelected;
}
}
}
releaseItem() {
this.props.onClose && this.props.onClose();
this.state.selectedItem && !this.RMOpening &&
this.state.children[this.state.selectedItem].props.onSelect &&
this.state.children[this.state.selectedItem].props.onSelect();
this.state.selectedItem = null;
this.state.item_anims.forEach((item, i) => {
Animated.spring(item, {
toValue: {x: 0, y: 0},
tension: 60,
friction: 10
}).start();
});
}
createPanResponder() {
return PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderGrant: () => {
this.props.onOpen && this.props.onOpen();
this.RMOpening = true;
Animated.stagger(40,
this.state.item_spots.map((spot, idx) =>
Animated.spring(this.state.item_anims[idx], {
toValue: spot,
friction: 6,
tension: 80
})
)
).start();
// Make sure all items gets to innitial position
// before we start tracking them
setTimeout(() => {this.RMOpening = false}, 500);
},
onPanResponderMove: Animated.event(
[ null, {dx: this.state.item_anims[0].x, dy: this.state.item_anims[0].y} ],
{listener: this.itemPanListener}
),
onPanResponderRelease: this.releaseItem,
onPanResponderTerminate: this.releaseItem,
});
}
computeNewSelected(gestureState) {
let {dx, dy} = gestureState;
let minDist = Infinity;
let newSelected = null;
let pointRadius = Math.sqrt(dx * dx + dy * dy);
if (Math.abs(this.props.menuRadius - pointRadius) < this.props.menuRadius / 2) {
this.state.item_spots.forEach((spot, idx) => {
let delta = {x: spot.x - dx, y: spot.y - dy};
let dist = delta.x * delta.x + delta.y * delta.y;
if (dist < minDist) {
minDist = dist;
newSelected = idx;
}
});
}
return newSelected;
}
render() {
return (
<View style={[
{ position: 'relative',
backgroundColor: 'transparent',
width: this.props.itemRadius * 2,
height: this.props.itemRadius * 2,
},
this.props.style]}>
{this.state.item_anims.map((_, i) => {
let j = this.state.item_anims.length - i - 1;
let handlers = j > 0 ? {} : this.state.itemPanResponder.panHandlers;
return (
<Animated.View
{...handlers}
key={i}
style={{
transform: this.state.item_anims[j].getTranslateTransform(),
position: 'absolute',
}} >
{this.state.children[j]}
</Animated.View>
);
})}
</View>
);
}
}
RadialMenu.defaultProps = {
itemRadius: 30,
menuRadius: 100,
spreadAngle: 360,
startAngle: 0
};