forked from dimgrav/react-native-snap-dial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dial.js
237 lines (216 loc) · 6.63 KB
/
Dial.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
Dimensions,
PanResponder,
StyleSheet,
View,
} from 'react-native'
import { throttle } from 'lodash'
const GREY_LIGHT = '#eeeeee'
export class Dial extends Component {
static propTypes = {
elastic: PropTypes.bool,
fixed: PropTypes.bool,
incrementBy: PropTypes.number,
initialAngle: PropTypes.number,
initialRadius: PropTypes.number,
onAngleXChange: PropTypes.func,
onPress: PropTypes.func,
onValueChange: PropTypes.func,
pointerEvents: PropTypes.oneOf(['box-none', 'none', 'box-only', 'auto']),
precision: PropTypes.number,
radiusMax: PropTypes.number,
radiusMin: PropTypes.number,
responderStyle: PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.object)
]),
wrapperStyle: PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.object)
])
}
static defaultProps = {
initialRadius: 1,
initialAngle: 0,
precision: 0,
}
constructor(props) {
super(props)
this.state = {
startingAngle: this.props.initialAngle,
startingRadius: this.props.initialRadius,
releaseAngle: this.props.initialAngle,
releaseRadius: this.props.initialRadius,
angle: this.props.initialAngle,
radius: this.props.initialRadius,
angleX: this.props.initialAngle
}
this.offset = { x: 0, y: 0 }
this.updateState = throttle(this.updateState.bind(this), 16)
}
componentWillMount() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (e, gestureState) => true,
onStartShouldSetPanResponderCapture: (e, gestureState) => {
this.measureOffset() // measure again
const { deg, radius } = this.calcAngle(e.nativeEvent)
this.setState({ startingAngle: deg, startingRadius: radius })
return true
},
onMoveShouldSetPanResponder: (e, g) => true,
onMoveShouldSetPanResponderCapture: (e, gestureState) => true,
onPanResponderGrant: (e, gestureState) => true,
onPanResponderMove: (e, gestureState) => {
this.updateAngle(gestureState)
},
onPanResponderRelease: (e, gestureState) => {
const {
angle,
radius,
releaseAngle,
releaseRadius,
} = this.state
if (angle !== releaseAngle || radius !== releaseRadius) {
this.setState({
releaseAngle: angle,
releaseRadius: radius,
})
}
},
})
}
onLayout(nativeEvent) {
/*
* Multiple measures to avoid the gap between animated
* and not animated views
*/
this.measureOffset()
setTimeout(() => this.measureOffset(), 200)
}
measureOffset() {
/*
* const {x, y, width, height} = nativeEvent.layout
* onlayout values are different than measureInWindow
* x and y are the distances to its previous element
* but in measureInWindow they are relative to the window
*/
const { width: screenWidth } = Dimensions.get('window')
this.self.measureInWindow((x, y, width, height) => {
this.offset = {
x: x % screenWidth + width / 2,
y: y + height / 2,
}
this.radius = width / 2
})
}
updateAngle(gestureState) {
let { deg, radius } = this.calcAngle(gestureState)
const degX = deg
if (deg < 0) deg += 360
if (Math.abs(this.state.angle - deg) > this.props.precision) {
this.updateState({ deg, degX, radius })
}
}
calcAngle(gestureState) {
const { pageX, pageY, moveX, moveY } = gestureState
const [x, y] = [pageX || moveX, pageY || moveY]
const [dx, dy] = [x - this.offset.x, y - this.offset.y]
return {
deg: Math.atan2(dy, dx) * 180 / Math.PI + 120,
radius: Math.sqrt(dy * dy + dx * dx) / this.radius, // pitagoras r^2 = x^2 + y^2 normalizado
}
}
updateState({ deg, degX, radius = this.state.radius }) {
radius = this.state.releaseRadius + radius - this.state.startingRadius
if (radius < this.props.radiusMin) radius = this.props.radiusMin
else if (radius > this.props.radiusMax) radius = this.props.radiusMax
const angle = deg + this.state.releaseAngle - this.state.startingAngle
if (deg < 0) deg += 360
if (angle !== this.state.angle || radius !== this.state.radius) {
this.setState({ angle, radius })
if (this.props.onValueChange) this.props.onValueChange(angle, radius)
}
this.setState({ angleX: degX })
if (this.props.onAngleXChange) this.props.onAngleXChange(degX)
}
forceUpdate = (deg, radius) => {
this.setState({
angle: deg === undefined ? this.state.angle : deg,
radius: radius === undefined ? this.state.radius : radius,
})
}
render() {
const rotate = this.props.fixed ? '0deg' : `${this.state.angle}deg`
const scale = this.props.elastic ? this.state.radius : 1
/**
* `pointerEvents` are now ignored.
* Should be handled based on the component's internal structure and be added (if there are any)
* to the corresponding view(s).
*/
const pevProp = this.props.pointerEvents != null ? { pointerEvents: this.props.pointerEvents } : {}
return (
<View
onLayout={(nativeEvent) => this.onLayout(nativeEvent)}
ref={(node) => { this.self = node }}
style={[styles.coverResponder, this.props.responderStyle]}
{...pevProp}
{...this._panResponder.panHandlers}
>
{this.props.children
? <View {...pevProp} style={[this.props.wrapperStyle, { transform: [{ rotate }, { scale }] }]}>
{this.props.children}
</View>
: <DefaultDial style={this.props.style} rotate={rotate} scale={scale} />
}
</View>
)
}
}
export const DefaultDial = ({ style = {}, rotate = '0rad', scale = 1 }) => (
<View
style={[styles.dial, style, {
transform: [
{ rotate }, { scale },
]
}]} >
<View style={styles.innerDialDecorator}>
<View style={styles.pointer} />
</View>
</View>
)
const styles = StyleSheet.create({
coverResponder: {
padding: 20, // needs a minimum
},
dial: {
width: 120,
height: 120,
backgroundColor: 'white',
borderRadius: 60,
elevation: 5,
shadowColor: GREY_LIGHT,
shadowOffset: { width: 1, height: 2 },
shadowOpacity: 0.8,
shadowRadius: 1,
},
innerDialDecorator: {
top: 10,
left: 10,
width: 100,
height: 100,
borderRadius: 50,
backgroundColor: 'white',
elevation: 3,
},
pointer: {
top: 20,
left: 20,
position: 'absolute',
width: 10,
height: 10,
backgroundColor: 'rgb(221,223,226)',
borderRadius: 5,
},
})