-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathScroller.hx
More file actions
1622 lines (1386 loc) · 46.4 KB
/
Scroller.hx
File metadata and controls
1622 lines (1386 loc) · 46.4 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Feathers UI
Copyright 2026 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.utils;
import feathers.events.ScrollEvent;
import motion.Actuate;
import motion.actuators.SimpleActuator;
import motion.easing.IEasing;
import motion.easing.Quart;
import openfl.display.DisplayObjectContainer;
import openfl.display.InteractiveObject;
import openfl.display.Stage;
import openfl.events.Event;
import openfl.events.EventDispatcher;
import openfl.events.MouseEvent;
import openfl.events.TouchEvent;
#if html5
import js.html.WheelEvent;
#end
#if air
import openfl.ui.Multitouch;
#end
/**
Utility that provides touch and mouse wheel scrolling capabilities for any
interactive display object.
@event feathers.events.ScrollEvent.SCROLL Dispatched when the scroll
position changes, or when the minimum or maximum scroll positions change.
@event feathers.events.ScrollEvent.SCROLL_START Dispatched when scrolling
begins.
@event feathers.events.ScrollEvent.SCROLL_COMPLETE Dispatched when scrolling
ends.
@since 1.0.0
**/
@:event(feathers.events.ScrollEvent.SCROLL)
@:event(feathers.events.ScrollEvent.SCROLL_START)
@:event(feathers.events.ScrollEvent.SCROLL_COMPLETE)
@:access(motion.actuators.SimpleActuator)
class Scroller extends EventDispatcher {
private static final MINIMUM_VELOCITY = 0.02;
// A special pointer ID for the mouse.
private static final POINTER_ID_MOUSE:Int = -1000;
/**
Creates a new `Scroller` object with the given arguments.
@since 1.0.0
**/
public function new(?target:InteractiveObject) {
super();
this.target = target;
}
/**
Determines if the target can be scrolled horizontally (on the x-axis).
@since 1.0.0
**/
public var enabledX = true;
/**
Determines if the target can be scrolled vertically (on the y-axis).
@since 1.0.0
**/
public var enabledY = true;
private var _scrollX:Float = 0.0;
/**
The current horizontal scroll position.
When the value of the `scrollX` property changes, the scroller will
dispatch an event of type `ScrollEvent.SCROLL`. This event is dispatched
when other scroll position properties change too.
@see `feathers.events.ScrollEvent.SCROLL`
@since 1.0.0
**/
@:bindable("scroll")
public var scrollX(get, set):Float;
private function get_scrollX():Float {
return this._scrollX;
}
private function set_scrollX(value:Float):Float {
if (this._scrollX == value) {
return this._scrollX;
}
this._scrollX = value;
ScrollEvent.dispatch(this, ScrollEvent.SCROLL, false, false, this._scrollX, this._scrollY);
return this._scrollX;
}
private var _scrollY:Float = 0.0;
/**
The current vertical scroll position.
When the value of the `scrollY` property changes, the scroller will
dispatch an event of type `ScrollEvent.SCROLL`. This event is dispatched
when other scroll position properties change too.
@see `feathers.events.ScrollEvent.SCROLL`
@since 1.0.0
**/
@:bindable("scroll")
public var scrollY(get, set):Float;
private function get_scrollY():Float {
return this._scrollY;
}
private function set_scrollY(value:Float):Float {
if (this._scrollY == value) {
return this._scrollY;
}
this._scrollY = value;
ScrollEvent.dispatch(this, ScrollEvent.SCROLL, false, false, this._scrollX, this._scrollY);
return this._scrollY;
}
/**
Setting `restrictedScrollX` will clamp the value to the range between
`minScrollX` and `maxScrollX`.
@since 1.0.0
**/
@:bindable("scroll")
public var restrictedScrollX(get, set):Float;
private function get_restrictedScrollX():Float {
return this._scrollX;
}
private function set_restrictedScrollX(value:Float):Float {
if (value < this._minScrollX) {
value = this._minScrollX;
} else if (value > this._maxScrollX) {
value = this._maxScrollX;
}
if (this._scrollX == value) {
return this._scrollX;
}
this._scrollX = value;
ScrollEvent.dispatch(this, ScrollEvent.SCROLL, false, false, this._scrollX, this._scrollY);
return this._scrollX;
}
/**
Setting `restrictedScrollY` will clamp the value to the range between
`minScrollY` and `maxScrollY`.
@since 1.0.0
**/
@:bindable("scroll")
public var restrictedScrollY(get, set):Float;
private function get_restrictedScrollY():Float {
return this._scrollY;
}
private function set_restrictedScrollY(value:Float):Float {
if (value < this._minScrollY) {
value = this._minScrollY;
} else if (value > this._maxScrollY) {
value = this._maxScrollY;
}
if (this._scrollY == value) {
return this._scrollY;
}
this._scrollY = value;
ScrollEvent.dispatch(this, ScrollEvent.SCROLL, false, false, this._scrollX, this._scrollY);
return this._scrollY;
}
private var _minScrollX:Float = 0.0;
/**
The minimum horizontal scroll position.
@since 1.0.0
**/
public var minScrollX(get, never):Float;
private function get_minScrollX():Float {
return this._minScrollX;
}
private var _minScrollY:Float = 0.0;
/**
The minimum vertical scroll position.
@since 1.0.0
**/
public var minScrollY(get, never):Float;
private function get_minScrollY():Float {
return this._minScrollY;
}
private var _maxScrollX:Float = 0.0;
/**
The maximum horizontal scroll position.
@since 1.0.0
**/
public var maxScrollX(get, never):Float;
private function get_maxScrollX():Float {
return this._maxScrollX;
}
private var _maxScrollY:Float = 0.0;
/**
The maximum vertical scroll position.
@since 1.0.0
**/
public var maxScrollY(get, never):Float;
private function get_maxScrollY():Float {
return this._maxScrollY;
}
private var _visibleWidth:Float = 0.0;
/**
The width of the target's scrollable region.
@default 0.0
@since 1.0.0
**/
public var visibleWidth(get, never):Float;
private function get_visibleWidth():Float {
return this._visibleWidth;
}
private var _visibleHeight:Float = 0.0;
/**
The height of the target's scrollable region.
@default 0.0
@since 1.0.0
**/
public var visibleHeight(get, never):Float;
private function get_visibleHeight():Float {
return this._visibleHeight;
}
private var _contentWidth:Float = 0.0;
/**
The width of the target's content. Will not scroll unless the width
of the content is larger than the width of the target.
@default 0.0
@since 1.0.0
**/
public var contentWidth(get, never):Float;
private function get_contentWidth():Float {
return this._contentWidth;
}
private var _contentHeight:Float = 0.0;
/**
The height of the target's content. Will not scroll unless the height
of the content is larger than the height of the target.
@default 0.0
@since 1.0.0
**/
public var contentHeight(get, never):Float;
private function get_contentHeight():Float {
return this._contentHeight;
}
private var _scrolling:Bool = false;
/**
Determines if scrolling is currently active.
@since 1.0.0
**/
public var scrolling(get, never):Bool;
private function get_scrolling():Bool {
return this._scrolling;
}
private var _draggingX:Bool = false;
/**
Determines if a touch is dragging the target horizontally (on the x-axis).
@since 1.0.0
**/
public var draggingX(get, never):Bool;
private function get_draggingX():Bool {
return this._draggingX;
}
private var _draggingY:Bool = false;
/**
Determines if a touch is dragging the target vertically (on the y-axis).
@since 1.0.0
**/
public var draggingY(get, never):Bool;
private function get_draggingY():Bool {
return this._draggingY;
}
/**
The minimum distance, measured in pixels, that the target must be
dragged to begin scrolling.
@default 6.0
@since 1.0.0
**/
public var minDragDistance:Float = 6.0;
/**
Determines if the scrolling can go beyond the edges of the viewport and
snap back to the minimum or maximum when released.
@default true
@see `Scroller.elasticity`
@since 1.0.0
**/
public var elasticEdges:Bool = true;
/**
Forces elasticity on the top edge, even if the height of the target's
content is not larger than the width height the target.
If `elasticEdges` is `false`, this property is ignored.
@default false
@see `Scroller.elasticEdges`
@since 1.0.0
**/
public var forceElasticTop:Bool = false;
/**
Forces elasticity on the right edge, even if the width of the target's
content is not larger than the width of the target.
If `elasticEdges` is `false`, this property is ignored.
@default false
@see `Scroller.elasticEdges`
@since 1.0.0
**/
public var forceElasticRight:Bool = false;
/**
Forces elasticity on the bottom edge, even if the height of the target's
content is not larger than the width height the target.
If `elasticEdges` is `false`, this property is ignored.
@default false
@see `Scroller.elasticEdges`
@since 1.0.0
**/
public var forceElasticBottom:Bool = false;
/**
Forces elasticity on the left edge, even if the width of the target's
content is not larger than the width of the target.
If `elasticEdges` is `false`, this property is ignored.
@default false
@see `Scroller.elasticEdges`
@since 1.0.0
**/
public var forceElasticLeft:Bool = false;
/**
If the scroll position goes outside the minimum or maximum bounds when
the scroller's content is being actively dragged, the scrolling will be
constrained using this multiplier. A value of `0.0` means that the
scroller will not go beyond its minimum or maximum bounds. A value of
`1.0` means that going beyond the minimum or maximum bounds is
completely unrestrained.
If `elasticEdges` is `false`, this property is ignored.
@see Scroller.elasticEdges
@default 0.33
@since 1.0.0
**/
public var elasticity:Float = 0.33;
/**
If the scroll position goes outside the minimum or maximum bounds when
when the scroller's content is "thrown", the scrolling will be
constrained using this multiplier. A value of `0.0` means that the
scroller will not go beyond its minimum or maximum bounds. A value of
`1.0` means that going beyond the minimum or maximum bounds is
completely unrestrained.
If `elasticEdges` is `false`, this property is ignored.
@see Scroller.elasticEdges
@see Scroller.elasticity
@default 0.05
@since 1.0.0
**/
public var throwElasticity:Float = 0.05;
/**
The duration, measured in seconds, of the animation when a the scroller
snaps back to the minimum or maximum position after going out of bounds.
If `elasticEdges` is `false`, this property is ignored.
@default 0.5
@since 1.0.0
**/
public var elasticSnapDuration:Float = 0.5;
/**
The easing function to use when animating the scroll position.
@default motion.easing.Quart.easeOut
@since 1.0.0
**/
public var ease:IEasing = Quart.easeOut;
/**
The easing function to use when the scroll position goes outside of
the minimum or maximum edge and bounces back.
@see `Scroller.ease`
@since 1.0.0
**/
public var bounceEase:IEasing = null;
/**
The distance to scroll when the mouse wheel is scrolled horizontally.
@default 10.0
@since 1.0.0
**/
public var mouseWheelDeltaX:Float = 10.0;
/**
The distance to scroll when the mouse wheel is scrolled vertically.
@default 10.0
@since 1.0.0
**/
public var mouseWheelDeltaY:Float = 10.0;
/**
Determines if rotating the mouse wheel vertically changes the `scrollX`
position instead of `scrollY`.
@since 1.0.0
**/
public var mouseWheelYScrollsX:Bool = false;
private var _mouseWheelDeltaMode:Int = 1;
/**
The duration, measured in seconds, of the animation when scrolling with
the mouse wheel.
@default 0.0
@since 1.0.0
**/
public var mouseWheelDuration:Float = 0.0;
/**
Determines if mouse events should be treated like touch events.
@default false
@since 1.0.0
**/
public var simulateTouch:Bool = false;
private var _decelerationRate:Float = 0.998;
/**
This value is used to decelerate the scroller when "thrown". The
velocity of a throw is multiplied by this value once per millisecond to
decelerate. A value greater than `0.0` and less than `1.0` is expected.
@default 0.998
@since 1.0.0
**/
public var decelerationRate(get, set):Float;
private function get_decelerationRate():Float {
return this._decelerationRate;
}
private function set_decelerationRate(value:Float):Float {
if (this._decelerationRate == value) {
return this._decelerationRate;
}
this._decelerationRate = value;
this._logDecelerationRate = Math.log(this._decelerationRate);
this._fixedThrowDuration = -0.1 / Math.log(Math.pow(this._decelerationRate, 1000.0 / 60.0));
return this._decelerationRate;
}
/**
If not `null`, and the scroller is dragged with touch, the `scrollX`
position is snapped to the nearest position in the array when the drag
completes.
@since 1.0.0
**/
public var snapPositionsX:Array<Float> = null;
/**
If not `null`, and the scroller is dragged with touch, the `scrollY`
position is snapped to the nearest position in the array when the drag
completes.
@since 1.0.0
**/
public var snapPositionsY:Array<Float> = null;
// this value is precalculated. See the `decelerationRate` setter for the dynamic calculation.
private var _logDecelerationRate:Float = -0.0020020026706730793;
private var _fixedThrowDuration:Float = 2.996998998998728;
private var restoreMouseChildren:Bool = false;
private var startTouchX:Float = 0.0;
private var startTouchY:Float = 0.0;
private var startScrollX:Float = 0.0;
private var startScrollY:Float = 0.0;
private var savedScrollMoves:Array<Float> = [];
private var animateScrollX:SimpleActuator<Dynamic, Dynamic> = null;
private var animateScrollY:SimpleActuator<Dynamic, Dynamic> = null;
private var animateScrollXEndRatio:Float = 1.0;
private var animateScrollYEndRatio:Float = 1.0;
private var targetScrollX:Float = 0.0;
private var targetScrollY:Float = 0.0;
private var snappingToEdge:Bool = false;
private var _target:InteractiveObject;
/**
The container used for scrolling.
@since 1.0.0
**/
public var target(get, set):InteractiveObject;
private function get_target():InteractiveObject {
return this._target;
}
private function set_target(value:InteractiveObject):InteractiveObject {
if (this._target == value) {
return this._target;
}
if (this._target != null) {
this.cleanupAfterDrag();
this._target.removeEventListener(Event.REMOVED_FROM_STAGE, scroller_target_removedFromStageHandler);
this._target.removeEventListener(MouseEvent.MOUSE_DOWN, scroller_target_mouseDownHandler);
this._target.removeEventListener(MouseEvent.MOUSE_DOWN, scroller_target_mouseDownCaptureHandler, true);
this._target.removeEventListener(MouseEvent.MOUSE_WHEEL, scroller_target_mouseWheelHandler);
#if (html5 && !feathersui_innogames_openfl)
var window = cast(js.Lib.global, js.html.Window);
window.removeEventListener("wheel", scroller_window_wheelCaptureHandler, {capture: true});
#end
this._target.removeEventListener(TouchEvent.TOUCH_BEGIN, scroller_target_touchBeginHandler);
this._target.removeEventListener(TouchEvent.TOUCH_BEGIN, scroller_target_touchBeginCaptureHandler, true);
this._target.removeEventListener(MouseEvent.CLICK, scroller_target_clickCaptureHandler, true);
this._target.removeEventListener(TouchEvent.TOUCH_TAP, scroller_target_touchTapCaptureHandler, true);
}
this._target = value;
if (this._target != null) {
this._target.addEventListener(MouseEvent.MOUSE_DOWN, scroller_target_mouseDownHandler, false, 0, true);
this._target.addEventListener(MouseEvent.MOUSE_DOWN, scroller_target_mouseDownCaptureHandler, true, 0, true);
this._target.addEventListener(MouseEvent.MOUSE_WHEEL, scroller_target_mouseWheelHandler, false, 0, true);
#if (html5 && !feathersui_innogames_openfl)
var window = cast(js.Lib.global, js.html.Window);
window.addEventListener("wheel", scroller_window_wheelCaptureHandler, {capture: true});
#end
this._target.addEventListener(TouchEvent.TOUCH_BEGIN, scroller_target_touchBeginHandler, false, 0, true);
this._target.addEventListener(TouchEvent.TOUCH_BEGIN, scroller_target_touchBeginCaptureHandler, true, 0, true);
this._target.addEventListener(MouseEvent.CLICK, scroller_target_clickCaptureHandler, true, 0, true);
#if (openfl >= "9.0.0")
this._target.addEventListener(TouchEvent.TOUCH_TAP, scroller_target_touchTapCaptureHandler, true, 0, true);
#end
}
return this._target;
}
private var _previousTouchPointID:Null<Int> = null;
private var _touchPointID:Null<Int> = null;
/**
The touch point that is currently dragging the scroll target. Returns
`null` if no touch point is currently associated with the drag.
If `simulateTouch` is `true`, the `touchPointIsSimulated` property will
indicate if the mouse is current dragging the scroll target.
@see `Scroller.touchPointIsSimulated`
@since 1.0.0
**/
public var touchPointID(get, never):Null<Int>;
private function get_touchPointID():Null<Int> {
return this._touchPointID;
}
private var _touchPointIsSimulated:Bool = false;
/**
Returns `true` if the mouse is dragging the scroll target as a simulated
touch point.
@see `Scroller.simulateTouch`
@see `Scroller.touchPointID`
@since 1.0.0
**/
public var touchPointIsSimulated(get, never):Bool;
private function get_touchPointIsSimulated():Bool {
return this._touchPointIsSimulated;
}
/**
Updates the dimensions of both the target and its content.
@since 1.0.0
**/
public function setDimensions(?visibleWidth:Null<Float>, ?visibleHeight:Null<Float>, ?contentWidth:Null<Float>, ?contentHeight:Null<Float>):Void {
this._visibleWidth = visibleWidth != null ? visibleWidth : 0.0;
this._visibleHeight = visibleHeight != null ? visibleHeight : 0.0;
this._contentWidth = contentWidth != null ? contentWidth : 0.0;
this._contentHeight = contentHeight != null ? contentHeight : 0.0;
this.calculateMinAndMax();
}
/**
Applies the `minScrollX` and `maxScrollX` restrictions to the current
`scrollX`, and applies the `minScrollY` and `maxScrollY` restrictions to
the current `scrollY`.
@since 1.0.0
**/
public function applyScrollRestrictions():Void {
var scrollChanged = false;
if (this._scrollX < this._minScrollX) {
this._scrollX = this._minScrollX;
scrollChanged = true;
} else if (this._scrollX > this._maxScrollX) {
this._scrollX = this._maxScrollX;
scrollChanged = true;
}
if (this._scrollY < this._minScrollY) {
this._scrollY = this._minScrollY;
scrollChanged = true;
} else if (this._scrollY > this._maxScrollY) {
this._scrollY = this._maxScrollY;
scrollChanged = true;
}
if (scrollChanged) {
ScrollEvent.dispatch(this, ScrollEvent.SCROLL, false, false, this._scrollX, this._scrollY);
}
}
/**
Immediately stops any animation that affects the scrolling.
@since 1.0.0
**/
public function stop():Void {
if (this.animateScrollX != null) {
Actuate.stop(this.animateScrollX, null, false, false);
this.animateScrollX = null;
}
if (this.animateScrollY != null) {
Actuate.stop(this.animateScrollY, null, false, false);
this.animateScrollY = null;
}
this.cleanupAfterDrag();
this._draggingX = false;
this._draggingY = false;
this.completeScroll();
}
/**
An advanced method used to adjust the current scroll position and the
target scroll position of `throwTo()` if there is a shift in the
content's layout. For example, this may be used by containers with
virtual layouts, where the dimensions of items might change from an
estimated size to their actual rendered size.
@since 1.2.0
**/
public function applyLayoutShift(x:Null<Float>, y:Null<Float>):Void {
if (x != null) {
var animateScrollX = this.animateScrollX;
this.animateScrollX = null;
if (animateScrollX != null) {
Actuate.stop(animateScrollX, null, false, false);
}
this.startScrollX += x;
this.scrollX += x;
var i = 0;
while (i < this.savedScrollMoves.length) {
this.savedScrollMoves[i] += x;
i += 3;
}
if (animateScrollX != null) {
var newTargetScrollX = this.targetScrollX + x;
var position = (openfl.Lib.getTimer() / 1000.0) - animateScrollX.startTime;
var newDuration = animateScrollX.duration - position;
this.throwTo(newTargetScrollX, null, newDuration, animateScrollX._ease);
}
}
if (y != null) {
var animateScrollY = this.animateScrollY;
this.animateScrollY = null;
if (animateScrollY != null) {
Actuate.stop(animateScrollY, null, false, false);
}
this.startScrollY += y;
this.scrollY += y;
var i = 1;
while (i < this.savedScrollMoves.length) {
this.savedScrollMoves[i] += y;
i += 3;
}
if (animateScrollY != null) {
var newTargetScrollY = this.targetScrollY + y;
var position = (openfl.Lib.getTimer() / 1000.0) - animateScrollY.startTime;
var newDuration = animateScrollY.duration - position;
this.throwTo(null, newTargetScrollY, newDuration, animateScrollY._ease);
}
}
}
/**
Immediately throws the scroller to the specified position, with optional
animation. If you want to throw in only one direction, pass in `null`
for the value that you do not want to change.
@since 1.0.0
**/
public function throwTo(scrollX:Null<Float>, scrollY:Null<Float>, duration:Null<Float> = null, ease:IEasing = null):Void {
if (duration == null) {
duration = this._fixedThrowDuration;
}
if (ease == null) {
ease = this.ease;
}
var scrollChanged = false;
if (scrollX != null) {
if (this.animateScrollX != null) {
Actuate.stop(this.animateScrollX, null, false, false);
this.animateScrollX = null;
}
if (this._scrollX != scrollX) {
scrollChanged = true;
this.startScroll();
if (duration == 0.0) {
// use the setter
this.scrollX = scrollX;
} else {
this.startScrollX = this._scrollX;
this.targetScrollX = scrollX;
var tween = Actuate.update((scrollX:Null<Float>) -> {
if (scrollX == null) {
// workaround for jgranick/actuate#108
scrollX = this.targetScrollX;
}
// use the setter
this.scrollX = scrollX;
}, duration, [this._scrollX], [this.targetScrollX], true);
this.animateScrollX = cast tween;
this.animateScrollX.ease(ease);
this.animateScrollX.onComplete(this.animateScrollX_onComplete);
this.refreshAnimateScrollXEndRatio();
}
} else {
this.finishScrollX();
}
}
if (scrollY != null) {
if (this.animateScrollY != null) {
Actuate.stop(this.animateScrollY, null, false, false);
this.animateScrollY = null;
}
if (this._scrollY != scrollY) {
scrollChanged = true;
this.startScroll();
if (duration == 0.0) {
// use the setter
this.scrollY = scrollY;
} else {
this.startScrollY = this._scrollY;
this.targetScrollY = scrollY;
var tween = Actuate.update((scrollY:Null<Float>) -> {
if (scrollY == null) {
// workaround for jgranick/actuate#108
scrollY = this.targetScrollY;
}
// use the setter
this.scrollY = scrollY;
}, duration, [this._scrollY], [this.targetScrollY], true);
this.animateScrollY = cast tween;
this.animateScrollY.ease(ease);
this.animateScrollY.onComplete(this.animateScrollY_onComplete);
this.refreshAnimateScrollYEndRatio();
}
} else {
this.finishScrollY();
}
}
if (scrollChanged && duration == 0.0) {
this.completeScroll();
}
}
private function throwWithVelocity(velocityX:Null<Float>, velocityY:Null<Float>):Void {
var targetX:Null<Float> = null;
var targetY:Null<Float> = null;
if (velocityX != null) {
if (Math.abs(velocityX) <= MINIMUM_VELOCITY) {
this.finishScrollX();
} else if (this.snapPositionsX != null) {
for (i in 0...this.snapPositionsX.length) {
var posX = this.snapPositionsX[i];
if (velocityX < 0.0) {
targetX = posX;
if (posX > this._scrollX) {
break;
}
}
if (velocityX > 0.0) {
targetX = (i == 0) ? posX : this.snapPositionsX[i - 1];
if (posX >= this._scrollX) {
break;
}
}
}
} else {
targetX = this._scrollX + this.calculateDistanceFromVelocity(velocityX);
}
}
if (velocityY != null) {
if (Math.abs(velocityY) <= MINIMUM_VELOCITY) {
this.finishScrollY();
} else if (this.snapPositionsY != null) {
for (i in 0...this.snapPositionsY.length) {
var posY = this.snapPositionsY[i];
if (velocityY < 0.0) {
targetY = posY;
if (posY > this._scrollY) {
break;
}
}
if (velocityY > 0.0) {
targetY = (i == 0) ? posY : this.snapPositionsY[i - 1];
if (posY >= this._scrollY) {
break;
}
}
}
} else {
targetY = this._scrollY + this.calculateDistanceFromVelocity(velocityY);
}
}
this.throwTo(targetX, targetY, this._fixedThrowDuration);
}
private function calculateDistanceFromVelocity(velocity:Float):Float {
return (velocity - MINIMUM_VELOCITY) / this._logDecelerationRate;
}
private function refreshAnimateScrollXEndRatio():Void {
var distance = Math.abs(this.targetScrollX - this.startScrollX);
var ratioOutOfBounds = 0.0;
if (this.targetScrollX > this._maxScrollX) {
ratioOutOfBounds = (this.targetScrollX - this._maxScrollX) / distance;
} else if (this.targetScrollX < this._minScrollX) {
ratioOutOfBounds = (this._minScrollX - this.targetScrollX) / distance;
}
if (ratioOutOfBounds > 0.0) {
if (this.elasticEdges) {
this.animateScrollXEndRatio = (1.0 - ratioOutOfBounds) + (ratioOutOfBounds * this.throwElasticity);
} else {
this.animateScrollXEndRatio = 1.0 - ratioOutOfBounds;
}
} else {
this.animateScrollXEndRatio = 1.0;
}
if (this.animateScrollX != null) {
if (this.animateScrollXEndRatio < 1.0) {
this.animateScrollX.onUpdate(this.animateScrollX_endRatio_onUpdate);
} else {
this.animateScrollX.onUpdate(null);
}
}
}
private function refreshAnimateScrollYEndRatio():Void {
var distance = Math.abs(this.targetScrollY - this.startScrollY);
var ratioOutOfBounds = 0.0;
if (this.targetScrollY > this._maxScrollY) {
ratioOutOfBounds = (this.targetScrollY - this._maxScrollY) / distance;
} else if (this.targetScrollY < this._minScrollY) {
ratioOutOfBounds = (this._minScrollY - this.targetScrollY) / distance;
}
if (ratioOutOfBounds > 0.0) {
if (this.elasticEdges) {
this.animateScrollYEndRatio = (1.0 - ratioOutOfBounds) + (ratioOutOfBounds * this.throwElasticity);
} else {
this.animateScrollYEndRatio = 1.0 - ratioOutOfBounds;
}
} else {
this.animateScrollYEndRatio = 1.0;
}
if (this.animateScrollY != null) {
if (this.animateScrollYEndRatio < 1.0) {
this.animateScrollY.onUpdate(this.animateScrollY_endRatio_onUpdate);
} else {
this.animateScrollY.onUpdate(null);
}
}
}
private function calculateMinAndMax():Void {
var oldMinScrollX = this._minScrollX;
var oldMaxScrollX = this._maxScrollX;
var oldMinScrollY = this._minScrollY;
var oldMaxScrollY = this._maxScrollY;
this._minScrollX = 0.0;
this._minScrollY = 0.0;
this._maxScrollX = Math.max(this._contentWidth, this._visibleWidth) - this._visibleWidth;
this._maxScrollY = Math.max(this._contentHeight, this._visibleHeight) - this._visibleHeight;
if (oldMinScrollX != this._minScrollX || oldMaxScrollX != this._maxScrollX) {
this.refreshAnimateScrollXEndRatio();