4
4
using UnityEngine ;
5
5
6
6
namespace Unity . UIWidgets . gestures {
7
-
8
7
enum _ScaleState {
9
8
ready ,
10
9
possible ,
@@ -16,7 +15,7 @@ public class ScaleStartDetails {
16
15
public ScaleStartDetails ( Offset focalPoint = null ) {
17
16
this . focalPoint = focalPoint ?? Offset . zero ;
18
17
}
19
-
18
+
20
19
public readonly Offset focalPoint ;
21
20
22
21
public override string ToString ( ) {
@@ -34,7 +33,7 @@ public ScaleUpdateDetails(
34
33
float rotation = 0.0f
35
34
) {
36
35
focalPoint = focalPoint ?? Offset . zero ;
37
-
36
+
38
37
D . assert ( scale >= 0.0f ) ;
39
38
D . assert ( horizontalScale >= 0.0f ) ;
40
39
D . assert ( verticalScale >= 0.0f ) ;
@@ -45,7 +44,7 @@ public ScaleUpdateDetails(
45
44
this . verticalScale = verticalScale ;
46
45
this . rotation = rotation ;
47
46
}
48
-
47
+
49
48
public readonly Offset focalPoint ;
50
49
51
50
public readonly float scale ;
@@ -57,15 +56,16 @@ public ScaleUpdateDetails(
57
56
public readonly float rotation ;
58
57
59
58
public override string ToString ( ) {
60
- return $ "ScaleUpdateDetails(focalPoint: { this . focalPoint } , scale: { this . scale } , horizontalScale: { this . horizontalScale } , verticalScale: { this . verticalScale } , rotation: { this . rotation } ";
59
+ return
60
+ $ "ScaleUpdateDetails(focalPoint: { this . focalPoint } , scale: { this . scale } , horizontalScale: { this . horizontalScale } , verticalScale: { this . verticalScale } , rotation: { this . rotation } ";
61
61
}
62
62
}
63
63
64
64
public class ScaleEndDetails {
65
65
public ScaleEndDetails ( Velocity velocity = null ) {
66
66
this . velocity = velocity ?? Velocity . zero ;
67
67
}
68
-
68
+
69
69
public readonly Velocity velocity ;
70
70
71
71
public override string ToString ( ) {
@@ -95,15 +95,15 @@ public _LineBetweenPointers(
95
95
int pointerEndId = 1 ) {
96
96
pointerStartLocation = pointerEndLocation ?? Offset . zero ;
97
97
pointerEndLocation = pointerEndLocation ?? Offset . zero ;
98
-
98
+
99
99
D . assert ( pointerStartId != pointerEndId ) ;
100
100
101
101
this . pointerStartLocation = pointerStartLocation ;
102
102
this . pointerStartId = pointerStartId ;
103
103
this . pointerEndLocation = pointerEndLocation ;
104
104
this . pointerEndId = pointerEndId ;
105
105
}
106
-
106
+
107
107
public readonly Offset pointerStartLocation ;
108
108
109
109
public readonly int pointerStartId ;
@@ -116,17 +116,16 @@ public _LineBetweenPointers(
116
116
117
117
class ScaleGestureRecognizer : OneSequenceGestureRecognizer {
118
118
public ScaleGestureRecognizer ( object debugOwner ) : base ( debugOwner : debugOwner ) {
119
-
120
119
}
121
-
120
+
122
121
public GestureScaleStartCallback onStart ;
123
-
122
+
124
123
public GestureScaleUpdateCallback onUpdate ;
125
124
126
125
public GestureScaleEndCallback onEnd ;
127
126
128
127
_ScaleState _state = _ScaleState . ready ;
129
-
128
+
130
129
Offset _initialFocalPoint ;
131
130
Offset _currentFocalPoint ;
132
131
float _initialSpan ;
@@ -146,17 +145,24 @@ float _scaleFactor {
146
145
}
147
146
148
147
float _horizontalScaleFactor {
149
- get { return this . _initialHorizontalSpan > 0.0f ? this . _currentHorizontalSpan / this . _initialHorizontalSpan : 1.0f ; }
148
+ get {
149
+ return this . _initialHorizontalSpan > 0.0f
150
+ ? this . _currentHorizontalSpan / this . _initialHorizontalSpan
151
+ : 1.0f ;
152
+ }
150
153
}
151
154
152
155
float _verticalScaleFactor {
153
- get { return this . _initialVerticalSpan > 0.0f ? this . _currentVerticalSpan / this . _initialVerticalSpan : 1.0f ; }
156
+ get {
157
+ return this . _initialVerticalSpan > 0.0f ? this . _currentVerticalSpan / this . _initialVerticalSpan : 1.0f ;
158
+ }
154
159
}
155
-
160
+
156
161
float _computeRotationFactor ( ) {
157
162
if ( this . _initialLine == null || this . _currentLine == null ) {
158
163
return 0.0f ;
159
164
}
165
+
160
166
float fx = this . _initialLine . pointerStartLocation . dx ;
161
167
float fy = this . _initialLine . pointerStartLocation . dy ;
162
168
float sx = this . _initialLine . pointerEndLocation . dx ;
@@ -172,7 +178,7 @@ float _computeRotationFactor() {
172
178
173
179
return angle2 - angle1 ;
174
180
}
175
-
181
+
176
182
public override void addAllowedPointer ( PointerDownEvent evt ) {
177
183
this . startTrackingPointer ( evt . pointer ) ;
178
184
this . _velocityTrackers [ evt . pointer ] = new VelocityTracker ( ) ;
@@ -188,12 +194,12 @@ public override void addAllowedPointer(PointerDownEvent evt) {
188
194
this . _pointerQueue = new List < int > ( ) ;
189
195
}
190
196
}
191
-
197
+
192
198
protected override void handleEvent ( PointerEvent evt ) {
193
199
D . assert ( this . _state != _ScaleState . ready ) ;
194
200
bool didChangeConfiguration = false ;
195
201
bool shouldStartIfAccepted = false ;
196
-
202
+
197
203
if ( evt is PointerMoveEvent ) {
198
204
VelocityTracker tracker = this . _velocityTrackers [ evt . pointer ] ;
199
205
D . assert ( tracker != null ) ;
@@ -203,12 +209,14 @@ protected override void handleEvent(PointerEvent evt) {
203
209
204
210
this . _pointerLocations [ evt . pointer ] = evt . position ;
205
211
shouldStartIfAccepted = true ;
206
- } else if ( evt is PointerDownEvent ) {
212
+ }
213
+ else if ( evt is PointerDownEvent ) {
207
214
this . _pointerLocations [ evt . pointer ] = evt . position ;
208
215
this . _pointerQueue . Add ( evt . pointer ) ;
209
216
didChangeConfiguration = true ;
210
217
shouldStartIfAccepted = true ;
211
- } else if ( evt is PointerUpEvent || evt is PointerCancelEvent ) {
218
+ }
219
+ else if ( evt is PointerUpEvent || evt is PointerCancelEvent ) {
212
220
this . _pointerLocations . Remove ( evt . pointer ) ;
213
221
this . _pointerQueue . Remove ( evt . pointer ) ;
214
222
didChangeConfiguration = true ;
@@ -223,7 +231,7 @@ protected override void handleEvent(PointerEvent evt) {
223
231
224
232
this . stopTrackingIfPointerNoLongerDown ( evt ) ;
225
233
}
226
-
234
+
227
235
void _update ( ) {
228
236
int count = this . _pointerLocations . Keys . Count ;
229
237
@@ -237,33 +245,36 @@ void _update() {
237
245
float totalDeviation = 0.0f ;
238
246
float totalHorizontalDeviation = 0.0f ;
239
247
float totalVerticalDeviation = 0.0f ;
240
-
248
+
241
249
foreach ( int pointer in this . _pointerLocations . Keys ) {
242
250
totalDeviation += ( this . _currentFocalPoint - this . _pointerLocations [ pointer ] ) . distance ;
243
251
totalHorizontalDeviation += ( this . _currentFocalPoint . dx - this . _pointerLocations [ pointer ] . dx ) . abs ( ) ;
244
252
totalVerticalDeviation += ( this . _currentFocalPoint . dy - this . _pointerLocations [ pointer ] . dy ) . abs ( ) ;
245
253
}
254
+
246
255
this . _currentSpan = count > 0 ? totalDeviation / count : 0.0f ;
247
256
this . _currentHorizontalSpan = count > 0 ? totalHorizontalDeviation / count : 0.0f ;
248
257
this . _currentVerticalSpan = count > 0 ? totalVerticalDeviation / count : 0.0f ;
249
258
}
250
-
259
+
251
260
void _updateLines ( ) {
252
261
int count = this . _pointerLocations . Keys . Count ;
253
262
D . assert ( this . _pointerQueue . Count >= count ) ;
254
-
263
+
255
264
if ( count < 2 ) {
256
265
this . _initialLine = this . _currentLine ;
257
- } else if ( this . _initialLine != null &&
258
- this . _initialLine . pointerStartId == this . _pointerQueue [ 0 ] &&
259
- this . _initialLine . pointerEndId == this . _pointerQueue [ 1 ] ) {
266
+ }
267
+ else if ( this . _initialLine != null &&
268
+ this . _initialLine . pointerStartId == this . _pointerQueue [ 0 ] &&
269
+ this . _initialLine . pointerEndId == this . _pointerQueue [ 1 ] ) {
260
270
this . _currentLine = new _LineBetweenPointers (
261
271
pointerStartId : this . _pointerQueue [ 0 ] ,
262
272
pointerStartLocation : this . _pointerLocations [ this . _pointerQueue [ 0 ] ] ,
263
273
pointerEndId : this . _pointerQueue [ 1 ] ,
264
274
pointerEndLocation : this . _pointerLocations [ this . _pointerQueue [ 1 ] ]
265
275
) ;
266
- } else {
276
+ }
277
+ else {
267
278
this . _initialLine = new _LineBetweenPointers (
268
279
pointerStartId : this . _pointerQueue [ 0 ] ,
269
280
pointerStartLocation : this . _pointerLocations [ this . _pointerQueue [ 0 ] ] ,
@@ -273,7 +284,7 @@ void _updateLines() {
273
284
this . _currentLine = null ;
274
285
}
275
286
}
276
-
287
+
277
288
bool _reconfigure ( int pointer ) {
278
289
this . _initialFocalPoint = this . _currentFocalPoint ;
279
290
this . _initialSpan = this . _currentSpan ;
@@ -288,25 +299,33 @@ bool _reconfigure(int pointer) {
288
299
Velocity velocity = tracker . getVelocity ( ) ;
289
300
if ( _ScaleGestureUtils . _isFlingGesture ( velocity ) ) {
290
301
Offset pixelsPerSecond = velocity . pixelsPerSecond ;
291
- if ( pixelsPerSecond . distanceSquared > Constants . kMaxFlingVelocity * Constants . kMaxFlingVelocity )
292
- velocity = new Velocity ( pixelsPerSecond : ( pixelsPerSecond / pixelsPerSecond . distance ) * Constants . kMaxFlingVelocity ) ;
302
+ if ( pixelsPerSecond . distanceSquared >
303
+ Constants . kMaxFlingVelocity * Constants . kMaxFlingVelocity ) {
304
+ velocity = new Velocity (
305
+ pixelsPerSecond : ( pixelsPerSecond / pixelsPerSecond . distance ) *
306
+ Constants . kMaxFlingVelocity ) ;
307
+ }
308
+
293
309
this . invokeCallback < object > ( "onEnd" , ( ) => {
294
310
this . onEnd ( new ScaleEndDetails ( velocity : velocity ) ) ;
295
311
return null ;
296
312
} ) ;
297
- } else {
313
+ }
314
+ else {
298
315
this . invokeCallback < object > ( "onEnd" , ( ) => {
299
316
this . onEnd ( new ScaleEndDetails ( velocity : Velocity . zero ) ) ;
300
317
return null ;
301
318
} ) ;
302
319
}
303
320
}
321
+
304
322
this . _state = _ScaleState . accepted ;
305
323
return false ;
306
324
}
325
+
307
326
return true ;
308
327
}
309
-
328
+
310
329
void _advanceStateMachine ( bool shouldStartIfAccepted ) {
311
330
if ( this . _state == _ScaleState . ready ) {
312
331
this . _state = _ScaleState . possible ;
@@ -318,7 +337,8 @@ void _advanceStateMachine(bool shouldStartIfAccepted) {
318
337
if ( spanDelta > Constants . kScaleSlop || focalPointDelta > Constants . kPanSlop ) {
319
338
this . resolve ( GestureDisposition . accepted ) ;
320
339
}
321
- } else if ( this . _state >= _ScaleState . accepted ) {
340
+ }
341
+ else if ( this . _state >= _ScaleState . accepted ) {
322
342
this . resolve ( GestureDisposition . accepted ) ;
323
343
}
324
344
@@ -327,39 +347,41 @@ void _advanceStateMachine(bool shouldStartIfAccepted) {
327
347
this . _dispatchOnStartCallbackIfNeeded ( ) ;
328
348
}
329
349
330
- if ( this . _state == _ScaleState . started && this . onUpdate != null )
350
+ if ( this . _state == _ScaleState . started && this . onUpdate != null ) {
331
351
this . invokeCallback < object > ( "onUpdate" , ( ) => {
332
- this . onUpdate ( new ScaleUpdateDetails (
333
- scale : this . _scaleFactor ,
334
- horizontalScale : this . _horizontalScaleFactor ,
335
- verticalScale : this . _verticalScaleFactor ,
336
- focalPoint : this . _currentFocalPoint ,
337
- rotation : this . _computeRotationFactor ( )
338
- ) ) ;
352
+ this . onUpdate ( new ScaleUpdateDetails (
353
+ scale : this . _scaleFactor ,
354
+ horizontalScale : this . _horizontalScaleFactor ,
355
+ verticalScale : this . _verticalScaleFactor ,
356
+ focalPoint : this . _currentFocalPoint ,
357
+ rotation : this . _computeRotationFactor ( )
358
+ ) ) ;
339
359
return null ;
340
360
} ) ;
361
+ }
341
362
}
342
-
363
+
343
364
void _dispatchOnStartCallbackIfNeeded ( ) {
344
365
D . assert ( this . _state == _ScaleState . started ) ;
345
- if ( this . onStart != null )
366
+ if ( this . onStart != null ) {
346
367
this . invokeCallback < object > ( "onStart" , ( ) => {
347
368
this . onStart ( new ScaleStartDetails ( focalPoint : this . _currentFocalPoint ) ) ;
348
369
return null ;
349
370
} ) ;
371
+ }
350
372
}
351
-
373
+
352
374
public override void acceptGesture ( int pointer ) {
353
375
if ( this . _state == _ScaleState . possible ) {
354
376
this . _state = _ScaleState . started ;
355
377
this . _dispatchOnStartCallbackIfNeeded ( ) ;
356
378
}
357
379
}
358
-
380
+
359
381
public override void rejectGesture ( int pointer ) {
360
382
this . stopTrackingPointer ( pointer ) ;
361
383
}
362
-
384
+
363
385
protected override void didStopTrackingLastPointer ( int pointer ) {
364
386
switch ( this . _state ) {
365
387
case _ScaleState . possible :
@@ -374,10 +396,10 @@ protected override void didStopTrackingLastPointer(int pointer) {
374
396
D . assert ( false ) ;
375
397
break ;
376
398
}
377
-
399
+
378
400
this . _state = _ScaleState . ready ;
379
401
}
380
-
402
+
381
403
public override void dispose ( ) {
382
404
this . _velocityTrackers . Clear ( ) ;
383
405
base . dispose ( ) ;
0 commit comments