|
@@ -0,0 +1,714 @@
|
|
1
|
+// @flow
|
|
2
|
+
|
|
3
|
+import React, { Component } from 'react';
|
|
4
|
+import { PanResponder, View } from 'react-native';
|
|
5
|
+import { connect } from 'react-redux';
|
|
6
|
+
|
|
7
|
+import { storeVideoTransform } from '../../actions';
|
|
8
|
+import styles from './styles';
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * The default/initial transform (= no transform).
|
|
12
|
+ */
|
|
13
|
+const DEFAULT_TRANSFORM = {
|
|
14
|
+ scale: 1,
|
|
15
|
+ translateX: 0,
|
|
16
|
+ translateY: 0
|
|
17
|
+};
|
|
18
|
+
|
|
19
|
+/**
|
|
20
|
+ * The minimum scale (magnification) multiplier. 1 is equal to objectFit
|
|
21
|
+ * = 'contain'.
|
|
22
|
+ */
|
|
23
|
+const MIN_SCALE = 1;
|
|
24
|
+
|
|
25
|
+/*
|
|
26
|
+ * The max distance from the edge of the screen where we let the user move the
|
|
27
|
+ * view to. This is large enough now to let the user drag the view to a position
|
|
28
|
+ * where no other displayed components cover it (such as filmstrip). If a
|
|
29
|
+ * ViewPort (hint) support is added to the LargeVideo component then this
|
|
30
|
+ * contant will not be necessary anymore.
|
|
31
|
+ */
|
|
32
|
+const MAX_OFFSET = 100;
|
|
33
|
+
|
|
34
|
+/**
|
|
35
|
+ * The max allowed scale (magnification) multiplier.
|
|
36
|
+ */
|
|
37
|
+const MAX_SCALE = 5;
|
|
38
|
+
|
|
39
|
+/**
|
|
40
|
+ * The length of a minimum movement after which we consider a gesture a move
|
|
41
|
+ * instead of a tap/long tap.
|
|
42
|
+ */
|
|
43
|
+const MOVE_THRESHOLD_DISMISSES_TOUCH = 2;
|
|
44
|
+
|
|
45
|
+/**
|
|
46
|
+ * A tap timeout after which we consider a gesture a long tap and will not
|
|
47
|
+ * trigger onPress (unless long tap gesture support is added in the future).
|
|
48
|
+ */
|
|
49
|
+const TAP_TIMEOUT_MS = 400;
|
|
50
|
+
|
|
51
|
+/**
|
|
52
|
+ * Type of a transform object this component is capable of handling.
|
|
53
|
+ */
|
|
54
|
+type Transform = {
|
|
55
|
+ scale: number,
|
|
56
|
+ translateX: number,
|
|
57
|
+ translateY: number
|
|
58
|
+};
|
|
59
|
+
|
|
60
|
+type Props = {
|
|
61
|
+
|
|
62
|
+ /**
|
|
63
|
+ * The children components of this view.
|
|
64
|
+ */
|
|
65
|
+ children: Object,
|
|
66
|
+
|
|
67
|
+ /**
|
|
68
|
+ * Transformation is only enabled when this flag is true.
|
|
69
|
+ */
|
|
70
|
+ enabled: boolean,
|
|
71
|
+
|
|
72
|
+ /**
|
|
73
|
+ * Function to invoke when a press event is detected.
|
|
74
|
+ */
|
|
75
|
+ onPress?: Function,
|
|
76
|
+
|
|
77
|
+ /**
|
|
78
|
+ * The id of the current stream that is displayed.
|
|
79
|
+ */
|
|
80
|
+ streamId: string,
|
|
81
|
+
|
|
82
|
+ /**
|
|
83
|
+ * Style of the top level transformable view.
|
|
84
|
+ */
|
|
85
|
+ style: Object,
|
|
86
|
+
|
|
87
|
+ /**
|
|
88
|
+ * The stored transforms retreived from Redux to be initially applied
|
|
89
|
+ * to different streams.
|
|
90
|
+ */
|
|
91
|
+ _transforms: Object,
|
|
92
|
+
|
|
93
|
+ /**
|
|
94
|
+ * Action to dispatch when the component is unmounted.
|
|
95
|
+ */
|
|
96
|
+ _onUnmount: Function
|
|
97
|
+};
|
|
98
|
+
|
|
99
|
+type State = {
|
|
100
|
+
|
|
101
|
+ /**
|
|
102
|
+ * The current (non-transformed) layout of the View.
|
|
103
|
+ */
|
|
104
|
+ layout: ?Object,
|
|
105
|
+
|
|
106
|
+ /**
|
|
107
|
+ * The current transform that is applied.
|
|
108
|
+ */
|
|
109
|
+ transform: Transform
|
|
110
|
+};
|
|
111
|
+
|
|
112
|
+/**
|
|
113
|
+ * An container that captures gestures such as pinch&zoom, touch or move.
|
|
114
|
+ */
|
|
115
|
+class VideoTransform extends Component<Props, State> {
|
|
116
|
+ /**
|
|
117
|
+ * The gesture handler object.
|
|
118
|
+ */
|
|
119
|
+ gestureHandlers: PanResponder;
|
|
120
|
+
|
|
121
|
+ /**
|
|
122
|
+ * The initial distance of the fingers on pinch start.
|
|
123
|
+ */
|
|
124
|
+ initialDistance: number;
|
|
125
|
+
|
|
126
|
+ /**
|
|
127
|
+ * The initial position of the finger on touch start.
|
|
128
|
+ */
|
|
129
|
+ initialPosition: {
|
|
130
|
+ x: number,
|
|
131
|
+ y: number
|
|
132
|
+ };
|
|
133
|
+
|
|
134
|
+ /**
|
|
135
|
+ * Time of the last tap.
|
|
136
|
+ */
|
|
137
|
+ lastTap: number;
|
|
138
|
+
|
|
139
|
+ /**
|
|
140
|
+ * Constructor of the component.
|
|
141
|
+ *
|
|
142
|
+ * @inheritdoc
|
|
143
|
+ */
|
|
144
|
+ constructor(props: Props) {
|
|
145
|
+ super(props);
|
|
146
|
+
|
|
147
|
+ this.state = {
|
|
148
|
+ layout: null,
|
|
149
|
+ transform: DEFAULT_TRANSFORM
|
|
150
|
+ };
|
|
151
|
+
|
|
152
|
+ this._getTransformStyle = this._getTransformStyle.bind(this);
|
|
153
|
+ this._onGesture = this._onGesture.bind(this);
|
|
154
|
+ this._onLayout = this._onLayout.bind(this);
|
|
155
|
+ this._onMoveShouldSetPanResponder
|
|
156
|
+ = this._onMoveShouldSetPanResponder.bind(this);
|
|
157
|
+ this._onPanResponderGrant = this._onPanResponderGrant.bind(this);
|
|
158
|
+ this._onPanResponderMove = this._onPanResponderMove.bind(this);
|
|
159
|
+ this._onPanResponderRelease = this._onPanResponderRelease.bind(this);
|
|
160
|
+ this._onStartShouldSetPanResponder
|
|
161
|
+ = this._onStartShouldSetPanResponder.bind(this);
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ /**
|
|
165
|
+ * Implements React Component's componentWillMount.
|
|
166
|
+ *
|
|
167
|
+ * @inheritdoc
|
|
168
|
+ */
|
|
169
|
+ componentWillMount() {
|
|
170
|
+ this.gestureHandlers = PanResponder.create({
|
|
171
|
+ onPanResponderGrant: this._onPanResponderGrant,
|
|
172
|
+ onPanResponderMove: this._onPanResponderMove,
|
|
173
|
+ onPanResponderRelease: this._onPanResponderRelease,
|
|
174
|
+ onPanResponderTerminationRequest: () => true,
|
|
175
|
+ onMoveShouldSetPanResponder: this._onMoveShouldSetPanResponder,
|
|
176
|
+ onShouldBlockNativeResponder: () => false,
|
|
177
|
+ onStartShouldSetPanResponder: this._onStartShouldSetPanResponder
|
|
178
|
+ });
|
|
179
|
+
|
|
180
|
+ const { streamId } = this.props;
|
|
181
|
+
|
|
182
|
+ this._restoreTransform(streamId);
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ /**
|
|
186
|
+ * Implements React Component's componentWillReceiveProps.
|
|
187
|
+ *
|
|
188
|
+ * @inheritdoc
|
|
189
|
+ */
|
|
190
|
+ componentWillReceiveProps({ streamId: newStreamId }) {
|
|
191
|
+ if (this.props.streamId !== newStreamId) {
|
|
192
|
+ this._storeTransform();
|
|
193
|
+ this._restoreTransform(newStreamId);
|
|
194
|
+ }
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ /**
|
|
198
|
+ * Implements React Component's componentWillUnmount.
|
|
199
|
+ *
|
|
200
|
+ * @inheritdoc
|
|
201
|
+ */
|
|
202
|
+ componentWillUnmount() {
|
|
203
|
+ this._storeTransform();
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ /**
|
|
207
|
+ * Renders the empty component that captures the gestures.
|
|
208
|
+ *
|
|
209
|
+ * @inheritdoc
|
|
210
|
+ */
|
|
211
|
+ render() {
|
|
212
|
+ const { children, style } = this.props;
|
|
213
|
+
|
|
214
|
+ return (
|
|
215
|
+ <View
|
|
216
|
+ onLayout = { this._onLayout }
|
|
217
|
+ pointerEvents = 'box-only'
|
|
218
|
+ style = { [
|
|
219
|
+ styles.videoTransformedViewContaier,
|
|
220
|
+ style
|
|
221
|
+ ] }
|
|
222
|
+ { ...this.gestureHandlers.panHandlers }>
|
|
223
|
+ <View
|
|
224
|
+ style = { [
|
|
225
|
+ styles.videoTranformedView,
|
|
226
|
+ this._getTransformStyle()
|
|
227
|
+ ] }>
|
|
228
|
+ { children }
|
|
229
|
+ </View>
|
|
230
|
+ </View>
|
|
231
|
+ );
|
|
232
|
+ }
|
|
233
|
+
|
|
234
|
+ /**
|
|
235
|
+ * Calculates the new transformation to be applied by merging the current
|
|
236
|
+ * transform values with the newly received incremental values.
|
|
237
|
+ *
|
|
238
|
+ * @param {Transform} transform - The new transform object.
|
|
239
|
+ * @private
|
|
240
|
+ * @returns {Transform}
|
|
241
|
+ */
|
|
242
|
+ _calculateTransformIncrement(transform: Transform) {
|
|
243
|
+ let {
|
|
244
|
+ scale,
|
|
245
|
+ translateX,
|
|
246
|
+ translateY
|
|
247
|
+ } = this.state.transform;
|
|
248
|
+ const {
|
|
249
|
+ scale: newScale,
|
|
250
|
+ translateX: newTranslateX,
|
|
251
|
+ translateY: newTranslateY
|
|
252
|
+ } = transform;
|
|
253
|
+
|
|
254
|
+ // Note: We don't limit MIN_SCALE here yet, as we need to detect a scale
|
|
255
|
+ // down gesture even if the scale is already at MIN_SCALE to let the
|
|
256
|
+ // user return the screen to center with that gesture. Scale is limited
|
|
257
|
+ // to MIN_SCALE right before it gets applied.
|
|
258
|
+ scale = Math.min(scale * (newScale || 1), MAX_SCALE);
|
|
259
|
+
|
|
260
|
+ translateX = translateX + ((newTranslateX || 0) / scale);
|
|
261
|
+ translateY = translateY + ((newTranslateY || 0) / scale);
|
|
262
|
+
|
|
263
|
+ return {
|
|
264
|
+ scale,
|
|
265
|
+ translateX,
|
|
266
|
+ translateY
|
|
267
|
+ };
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ _didMove: Object => boolean
|
|
271
|
+
|
|
272
|
+ /**
|
|
273
|
+ * Determines if there was large enough movement to be handled.
|
|
274
|
+ *
|
|
275
|
+ * @param {Object} gestureState - The gesture state.
|
|
276
|
+ * @returns {boolean}
|
|
277
|
+ */
|
|
278
|
+ _didMove({ dx, dy }) {
|
|
279
|
+ return Math.abs(dx) > MOVE_THRESHOLD_DISMISSES_TOUCH
|
|
280
|
+ || Math.abs(dy) > MOVE_THRESHOLD_DISMISSES_TOUCH;
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+ _getTouchDistance: Object => number;
|
|
284
|
+
|
|
285
|
+ /**
|
|
286
|
+ * Calculates the touch distance on a pinch event.
|
|
287
|
+ *
|
|
288
|
+ * @param {Object} evt - The touch event.
|
|
289
|
+ * @private
|
|
290
|
+ * @returns {number}
|
|
291
|
+ */
|
|
292
|
+ _getTouchDistance({ nativeEvent: { touches } }) {
|
|
293
|
+ const dx = Math.abs(touches[0].pageX - touches[1].pageX);
|
|
294
|
+ const dy = Math.abs(touches[0].pageY - touches[1].pageY);
|
|
295
|
+
|
|
296
|
+ return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
|
|
297
|
+ }
|
|
298
|
+
|
|
299
|
+ _getTouchPosition: Object => Object
|
|
300
|
+
|
|
301
|
+ /**
|
|
302
|
+ * Calculates the position of the touch event.
|
|
303
|
+ *
|
|
304
|
+ * @param {Object} evt - The touch event.
|
|
305
|
+ * @private
|
|
306
|
+ * @returns {Object}
|
|
307
|
+ */
|
|
308
|
+ _getTouchPosition({ nativeEvent: { touches } }) {
|
|
309
|
+ return {
|
|
310
|
+ x: touches[0].pageX,
|
|
311
|
+ y: touches[0].pageY
|
|
312
|
+ };
|
|
313
|
+ }
|
|
314
|
+
|
|
315
|
+ _getTransformStyle: () => Object
|
|
316
|
+
|
|
317
|
+ /**
|
|
318
|
+ * Generates a transform style object to be used on the component.
|
|
319
|
+ *
|
|
320
|
+ * @returns {{string: Array<{string: number}>}}
|
|
321
|
+ */
|
|
322
|
+ _getTransformStyle() {
|
|
323
|
+ const { enabled } = this.props;
|
|
324
|
+
|
|
325
|
+ if (!enabled) {
|
|
326
|
+ return null;
|
|
327
|
+ }
|
|
328
|
+
|
|
329
|
+ const {
|
|
330
|
+ scale,
|
|
331
|
+ translateX,
|
|
332
|
+ translateY
|
|
333
|
+ } = this.state.transform;
|
|
334
|
+
|
|
335
|
+ return {
|
|
336
|
+ transform: [
|
|
337
|
+ { scale },
|
|
338
|
+ { translateX },
|
|
339
|
+ { translateY }
|
|
340
|
+ ]
|
|
341
|
+ };
|
|
342
|
+ }
|
|
343
|
+
|
|
344
|
+ /**
|
|
345
|
+ * Limits the move matrix and then applies the transformation to the
|
|
346
|
+ * component (updates state).
|
|
347
|
+ *
|
|
348
|
+ * Note: Points A (top-left) and D (bottom-right) are opposite points of
|
|
349
|
+ * the View rectangle.
|
|
350
|
+ *
|
|
351
|
+ * @param {Transform} transform - The transformation object.
|
|
352
|
+ * @private
|
|
353
|
+ * @returns {void}
|
|
354
|
+ */
|
|
355
|
+ _limitAndApplyTransformation(transform: Transform) {
|
|
356
|
+ const { layout } = this.state;
|
|
357
|
+
|
|
358
|
+ if (layout) {
|
|
359
|
+ const { scale } = this.state.transform;
|
|
360
|
+ const { scale: newScaleUnlimited } = transform;
|
|
361
|
+ let {
|
|
362
|
+ translateX: newTranslateX,
|
|
363
|
+ translateY: newTranslateY
|
|
364
|
+ } = transform;
|
|
365
|
+
|
|
366
|
+ // Scale is only limited to MIN_SCALE here to detect downscale
|
|
367
|
+ // gesture later.
|
|
368
|
+ const newScale = Math.max(newScaleUnlimited, MIN_SCALE);
|
|
369
|
+
|
|
370
|
+ // The A and D points of the original View (before transform).
|
|
371
|
+ const originalLayout = {
|
|
372
|
+ a: {
|
|
373
|
+ x: layout.x,
|
|
374
|
+ y: layout.y
|
|
375
|
+ },
|
|
376
|
+ d: {
|
|
377
|
+ x: layout.x + layout.width,
|
|
378
|
+ y: layout.y + layout.height
|
|
379
|
+ }
|
|
380
|
+ };
|
|
381
|
+
|
|
382
|
+ // The center point (midpoint) of the transformed View.
|
|
383
|
+ const transformedCenterPoint = {
|
|
384
|
+ x: ((layout.x + layout.width) / 2) + (newTranslateX * newScale),
|
|
385
|
+ y: ((layout.y + layout.height) / 2) + (newTranslateY * newScale)
|
|
386
|
+ };
|
|
387
|
+
|
|
388
|
+ // The size of the transformed View.
|
|
389
|
+ const transformedSize = {
|
|
390
|
+ height: layout.height * newScale,
|
|
391
|
+ width: layout.width * newScale
|
|
392
|
+ };
|
|
393
|
+
|
|
394
|
+ // The A and D points of the transformed View.
|
|
395
|
+ const transformedLayout = {
|
|
396
|
+ a: {
|
|
397
|
+ x: transformedCenterPoint.x - (transformedSize.width / 2),
|
|
398
|
+ y: transformedCenterPoint.y - (transformedSize.height / 2)
|
|
399
|
+ },
|
|
400
|
+ d: {
|
|
401
|
+ x: transformedCenterPoint.x + (transformedSize.width / 2),
|
|
402
|
+ y: transformedCenterPoint.y + (transformedSize.height / 2)
|
|
403
|
+ }
|
|
404
|
+ };
|
|
405
|
+
|
|
406
|
+ let _MAX_OFFSET = MAX_OFFSET;
|
|
407
|
+
|
|
408
|
+ if (newScaleUnlimited < scale) {
|
|
409
|
+ // This is a negative scale event so we dynamycally reduce the
|
|
410
|
+ // MAX_OFFSET to get the screen back to the center on
|
|
411
|
+ // downscaling.
|
|
412
|
+ _MAX_OFFSET = Math.min(MAX_OFFSET, MAX_OFFSET * (newScale - 1));
|
|
413
|
+ }
|
|
414
|
+
|
|
415
|
+ // Correct move matrix if it goes out of the view
|
|
416
|
+ // too much (_MAX_OFFSET).
|
|
417
|
+ newTranslateX
|
|
418
|
+ -= Math.max(
|
|
419
|
+ transformedLayout.a.x - originalLayout.a.x - _MAX_OFFSET,
|
|
420
|
+ 0);
|
|
421
|
+ newTranslateX
|
|
422
|
+ += Math.max(
|
|
423
|
+ originalLayout.d.x - transformedLayout.d.x - _MAX_OFFSET,
|
|
424
|
+ 0);
|
|
425
|
+ newTranslateY
|
|
426
|
+ -= Math.max(
|
|
427
|
+ transformedLayout.a.y - originalLayout.a.y - _MAX_OFFSET,
|
|
428
|
+ 0);
|
|
429
|
+ newTranslateY
|
|
430
|
+ += Math.max(
|
|
431
|
+ originalLayout.d.y - transformedLayout.d.y - _MAX_OFFSET,
|
|
432
|
+ 0);
|
|
433
|
+
|
|
434
|
+ this.setState({
|
|
435
|
+ transform: {
|
|
436
|
+ scale: newScale,
|
|
437
|
+ translateX: Math.round(newTranslateX),
|
|
438
|
+ translateY: Math.round(newTranslateY)
|
|
439
|
+ }
|
|
440
|
+ });
|
|
441
|
+ }
|
|
442
|
+ }
|
|
443
|
+
|
|
444
|
+ _onGesture: (string, ?Object | number) => void
|
|
445
|
+
|
|
446
|
+ /**
|
|
447
|
+ * Handles gestures and converts them to transforms.
|
|
448
|
+ *
|
|
449
|
+ * Currently supported gestures:
|
|
450
|
+ * - scale (punch&zoom-type scale).
|
|
451
|
+ * - move
|
|
452
|
+ * - press.
|
|
453
|
+ *
|
|
454
|
+ * Note: This component supports onPress solely to overcome the problem of
|
|
455
|
+ * not being able to register gestures via the PanResponder due to the fact
|
|
456
|
+ * that the entire Conference component was a single touch responder
|
|
457
|
+ * component in the past (see base/react/.../Container with an onPress
|
|
458
|
+ * event) - and stock touch responder components seem to have exclusive
|
|
459
|
+ * priority in handling touches in React.
|
|
460
|
+ *
|
|
461
|
+ * @param {string} type - The type of the gesture.
|
|
462
|
+ * @param {?Object | number} value - The value of the gesture, if any.
|
|
463
|
+ * @returns {void}
|
|
464
|
+ */
|
|
465
|
+ _onGesture(type, value) {
|
|
466
|
+ let transform;
|
|
467
|
+
|
|
468
|
+ switch (type) {
|
|
469
|
+ case 'move':
|
|
470
|
+ transform = {
|
|
471
|
+ ...DEFAULT_TRANSFORM,
|
|
472
|
+ translateX: value.x,
|
|
473
|
+ translateY: value.y
|
|
474
|
+ };
|
|
475
|
+ break;
|
|
476
|
+ case 'scale':
|
|
477
|
+ transform = {
|
|
478
|
+ ...DEFAULT_TRANSFORM,
|
|
479
|
+ scale: value
|
|
480
|
+ };
|
|
481
|
+ break;
|
|
482
|
+
|
|
483
|
+ case 'press': {
|
|
484
|
+ const { onPress } = this.props;
|
|
485
|
+
|
|
486
|
+ typeof onPress === 'function' && onPress();
|
|
487
|
+ break;
|
|
488
|
+ }
|
|
489
|
+ }
|
|
490
|
+
|
|
491
|
+ if (transform) {
|
|
492
|
+ this._limitAndApplyTransformation(
|
|
493
|
+ this._calculateTransformIncrement(transform));
|
|
494
|
+ }
|
|
495
|
+
|
|
496
|
+ this.lastTap = 0;
|
|
497
|
+ }
|
|
498
|
+
|
|
499
|
+ _onLayout: Object => void
|
|
500
|
+
|
|
501
|
+ /**
|
|
502
|
+ * Callback for the onLayout of the component.
|
|
503
|
+ *
|
|
504
|
+ * @param {Object} event - The native props of the onLayout event.
|
|
505
|
+ * @private
|
|
506
|
+ * @returns {void}
|
|
507
|
+ */
|
|
508
|
+ _onLayout({ nativeEvent: { layout: { x, y, width, height } } }) {
|
|
509
|
+ this.setState({
|
|
510
|
+ layout: {
|
|
511
|
+ x,
|
|
512
|
+ y,
|
|
513
|
+ width,
|
|
514
|
+ height
|
|
515
|
+ }
|
|
516
|
+ });
|
|
517
|
+ }
|
|
518
|
+
|
|
519
|
+ _onMoveShouldSetPanResponder: (Object, Object) => boolean
|
|
520
|
+
|
|
521
|
+ /**
|
|
522
|
+ * Function to decide whether the responder should respond to a move event.
|
|
523
|
+ *
|
|
524
|
+ * @param {Object} evt - The event.
|
|
525
|
+ * @param {Object} gestureState - Gesture state.
|
|
526
|
+ * @private
|
|
527
|
+ * @returns {boolean}
|
|
528
|
+ */
|
|
529
|
+ _onMoveShouldSetPanResponder(evt, gestureState) {
|
|
530
|
+ return this.props.enabled
|
|
531
|
+ && (this._didMove(gestureState)
|
|
532
|
+ || gestureState.numberActiveTouches === 2);
|
|
533
|
+ }
|
|
534
|
+
|
|
535
|
+ _onPanResponderGrant: (Object, Object) => void
|
|
536
|
+
|
|
537
|
+ /**
|
|
538
|
+ * Calculates the initial touch distance.
|
|
539
|
+ *
|
|
540
|
+ * @param {Object} evt - Touch event.
|
|
541
|
+ * @param {Object} gestureState - Gesture state.
|
|
542
|
+ * @private
|
|
543
|
+ * @returns {void}
|
|
544
|
+ */
|
|
545
|
+ _onPanResponderGrant(evt, { numberActiveTouches }) {
|
|
546
|
+ if (numberActiveTouches === 1) {
|
|
547
|
+ this.initialPosition = this._getTouchPosition(evt);
|
|
548
|
+ this.lastTap = Date.now();
|
|
549
|
+
|
|
550
|
+ } else if (numberActiveTouches === 2) {
|
|
551
|
+ this.initialDistance = this._getTouchDistance(evt);
|
|
552
|
+ }
|
|
553
|
+ }
|
|
554
|
+
|
|
555
|
+ _onPanResponderMove: (Object, Object) => void
|
|
556
|
+
|
|
557
|
+ /**
|
|
558
|
+ * Handles the PanResponder move (touch move) event.
|
|
559
|
+ *
|
|
560
|
+ * @param {Object} evt - Touch event.
|
|
561
|
+ * @param {Object} gestureState - Gesture state.
|
|
562
|
+ * @private
|
|
563
|
+ * @returns {void}
|
|
564
|
+ */
|
|
565
|
+ _onPanResponderMove(evt, gestureState) {
|
|
566
|
+ if (gestureState.numberActiveTouches === 2) {
|
|
567
|
+ // this is a zoom event
|
|
568
|
+ if (
|
|
569
|
+ this.initialDistance === undefined
|
|
570
|
+ || isNaN(this.initialDistance)
|
|
571
|
+ ) {
|
|
572
|
+ // there is no initial distance because the user started
|
|
573
|
+ // with only one finger. We calculate it now.
|
|
574
|
+ this.initialDistance = this._getTouchDistance(evt);
|
|
575
|
+ } else {
|
|
576
|
+ const distance = this._getTouchDistance(evt);
|
|
577
|
+ const scale = distance / (this.initialDistance || 1);
|
|
578
|
+
|
|
579
|
+ this.initialDistance = distance;
|
|
580
|
+
|
|
581
|
+ this._onGesture('scale', scale);
|
|
582
|
+ }
|
|
583
|
+ } else if (gestureState.numberActiveTouches === 1
|
|
584
|
+ && isNaN(this.initialDistance)
|
|
585
|
+ && this._didMove(gestureState)) {
|
|
586
|
+ // this is a move event
|
|
587
|
+ const position = this._getTouchPosition(evt);
|
|
588
|
+ const move = {
|
|
589
|
+ x: position.x - this.initialPosition.x,
|
|
590
|
+ y: position.y - this.initialPosition.y
|
|
591
|
+ };
|
|
592
|
+
|
|
593
|
+ this.initialPosition = position;
|
|
594
|
+
|
|
595
|
+ this._onGesture('move', move);
|
|
596
|
+ }
|
|
597
|
+ }
|
|
598
|
+
|
|
599
|
+ _onPanResponderRelease: () => void
|
|
600
|
+
|
|
601
|
+ /**
|
|
602
|
+ * Handles the PanResponder gesture end event.
|
|
603
|
+ *
|
|
604
|
+ * @private
|
|
605
|
+ * @returns {void}
|
|
606
|
+ */
|
|
607
|
+ _onPanResponderRelease() {
|
|
608
|
+ if (this.lastTap && Date.now() - this.lastTap < TAP_TIMEOUT_MS) {
|
|
609
|
+ this._onGesture('press');
|
|
610
|
+ }
|
|
611
|
+ delete this.initialDistance;
|
|
612
|
+ delete this.initialPosition;
|
|
613
|
+ }
|
|
614
|
+
|
|
615
|
+ _onStartShouldSetPanResponder: () => boolean
|
|
616
|
+
|
|
617
|
+ /**
|
|
618
|
+ * Function to decide whether the responder should respond to a start
|
|
619
|
+ * (thouch) event.
|
|
620
|
+ *
|
|
621
|
+ * @private
|
|
622
|
+ * @returns {boolean}
|
|
623
|
+ */
|
|
624
|
+ _onStartShouldSetPanResponder() {
|
|
625
|
+ return typeof this.props.onPress === 'function';
|
|
626
|
+ }
|
|
627
|
+
|
|
628
|
+ /**
|
|
629
|
+ * Restores the last applied transform when the component is mounted, or
|
|
630
|
+ * a new stream is about to be rendered.
|
|
631
|
+ *
|
|
632
|
+ * @param {string} streamId - The stream id to restore transform for.
|
|
633
|
+ * @private
|
|
634
|
+ * @returns {void}
|
|
635
|
+ */
|
|
636
|
+ _restoreTransform(streamId) {
|
|
637
|
+ const { enabled, _transforms } = this.props;
|
|
638
|
+
|
|
639
|
+ if (enabled) {
|
|
640
|
+ const initialTransform = _transforms[streamId];
|
|
641
|
+
|
|
642
|
+ if (initialTransform) {
|
|
643
|
+ this.setState({
|
|
644
|
+ transform: initialTransform
|
|
645
|
+ });
|
|
646
|
+ }
|
|
647
|
+ }
|
|
648
|
+ }
|
|
649
|
+
|
|
650
|
+ /**
|
|
651
|
+ * Stores/saves the current transform when the component is destroyed, or a
|
|
652
|
+ * new stream is about to be rendered.
|
|
653
|
+ *
|
|
654
|
+ * @private
|
|
655
|
+ * @returns {void}
|
|
656
|
+ */
|
|
657
|
+ _storeTransform() {
|
|
658
|
+ const { _onUnmount, enabled, streamId } = this.props;
|
|
659
|
+
|
|
660
|
+ if (enabled) {
|
|
661
|
+ _onUnmount(streamId, this.state.transform);
|
|
662
|
+ }
|
|
663
|
+ }
|
|
664
|
+}
|
|
665
|
+
|
|
666
|
+/**
|
|
667
|
+ * Maps dispatching of some action to React component props.
|
|
668
|
+ *
|
|
669
|
+ * @param {Function} dispatch - Redux action dispatcher.
|
|
670
|
+ * @private
|
|
671
|
+ * @returns {{
|
|
672
|
+ * _onUnmount: Function
|
|
673
|
+ * }}
|
|
674
|
+ */
|
|
675
|
+function _mapDispatchToProps(dispatch) {
|
|
676
|
+ return {
|
|
677
|
+ /**
|
|
678
|
+ * Dispatches actions to store the last applied transform to a video.
|
|
679
|
+ *
|
|
680
|
+ * @param {string} streamId - The ID of the stream.
|
|
681
|
+ * @param {Transform} transform - The last applied transform.
|
|
682
|
+ * @private
|
|
683
|
+ * @returns {void}
|
|
684
|
+ */
|
|
685
|
+ _onUnmount(streamId, transform) {
|
|
686
|
+ dispatch(storeVideoTransform(streamId, transform));
|
|
687
|
+ }
|
|
688
|
+ };
|
|
689
|
+}
|
|
690
|
+
|
|
691
|
+/**
|
|
692
|
+ * Maps (parts of) the redux state to the component's props.
|
|
693
|
+ *
|
|
694
|
+ * @param {Object} state - The redux state.
|
|
695
|
+ * @param {Object} ownProps - The component's own props.
|
|
696
|
+ * @private
|
|
697
|
+ * @returns {{
|
|
698
|
+ * _transforms: Object
|
|
699
|
+ * }}
|
|
700
|
+ */
|
|
701
|
+function _mapStateToProps(state) {
|
|
702
|
+ return {
|
|
703
|
+ /**
|
|
704
|
+ * The stored transforms retreived from Redux to be initially applied to
|
|
705
|
+ * different streams.
|
|
706
|
+ *
|
|
707
|
+ * @private
|
|
708
|
+ * @type {Object}
|
|
709
|
+ */
|
|
710
|
+ _transforms: state['features/base/media'].video.transforms
|
|
711
|
+ };
|
|
712
|
+}
|
|
713
|
+
|
|
714
|
+export default connect(_mapStateToProps, _mapDispatchToProps)(VideoTransform);
|