-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
ScrollPane.java
1168 lines (1004 loc) · 39.5 KB
/
ScrollPane.java
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
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.scenes.scene2d.ui;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Event;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.badlogic.gdx.scenes.scene2d.utils.Cullable;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.Layout;
import com.badlogic.gdx.utils.Null;
/** A group that scrolls a child widget using scrollbars and/or mouse or touch dragging.
* <p>
* The widget is sized to its preferred size. If the widget's preferred width or height is less than the size of this scroll pane,
* it is set to the size of this scroll pane. Scrollbars appear when the widget is larger than the scroll pane.
* <p>
* The scroll pane's preferred size is that of the child widget. At this size, the child widget will not need to scroll, so the
* scroll pane is typically sized by ignoring the preferred size in one or both directions.
* @author mzechner
* @author Nathan Sweet */
public class ScrollPane extends WidgetGroup {
private ScrollPaneStyle style;
private Actor widget;
final Rectangle hScrollBounds = new Rectangle();
final Rectangle vScrollBounds = new Rectangle();
final Rectangle hKnobBounds = new Rectangle();
final Rectangle vKnobBounds = new Rectangle();
private final Rectangle widgetAreaBounds = new Rectangle();
private final Rectangle widgetCullingArea = new Rectangle();
private ActorGestureListener flickScrollListener;
boolean scrollX, scrollY;
boolean vScrollOnRight = true;
boolean hScrollOnBottom = true;
float amountX, amountY;
float visualAmountX, visualAmountY;
float maxX, maxY;
boolean touchScrollH, touchScrollV;
final Vector2 lastPoint = new Vector2();
float areaWidth, areaHeight;
boolean fadeScrollBars = true, smoothScrolling = true, scrollBarTouch = true;
float fadeAlpha, fadeAlphaSeconds = 1, fadeDelay, fadeDelaySeconds = 1;
boolean cancelTouchFocus = true;
boolean flickScroll = true;
float velocityX, velocityY;
float flingTimer;
private boolean overscrollX = true, overscrollY = true;
float flingTime = 1f;
private float overscrollDistance = 50, overscrollSpeedMin = 30, overscrollSpeedMax = 200;
private boolean forceScrollX, forceScrollY;
boolean disableX, disableY;
private boolean clamp = true;
private boolean scrollbarsOnTop;
private boolean variableSizeKnobs = true;
int draggingPointer = -1;
/** @param widget May be null. */
public ScrollPane (@Null Actor widget) {
this(widget, new ScrollPaneStyle());
}
/** @param widget May be null. */
public ScrollPane (@Null Actor widget, Skin skin) {
this(widget, skin.get(ScrollPaneStyle.class));
}
/** @param widget May be null. */
public ScrollPane (@Null Actor widget, Skin skin, String styleName) {
this(widget, skin.get(styleName, ScrollPaneStyle.class));
}
/** @param widget May be null. */
public ScrollPane (@Null Actor widget, ScrollPaneStyle style) {
if (style == null) throw new IllegalArgumentException("style cannot be null.");
this.style = style;
setActor(widget);
setSize(150, 150);
addCaptureListener(new InputListener() {
private float handlePosition;
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
if (draggingPointer != -1) return false;
if (pointer == 0 && button != 0) return false;
if (getStage() != null) getStage().setScrollFocus(ScrollPane.this);
if (!flickScroll) setScrollbarsVisible(true);
if (fadeAlpha == 0) return false;
if (scrollBarTouch && scrollX && hScrollBounds.contains(x, y)) {
event.stop();
setScrollbarsVisible(true);
if (hKnobBounds.contains(x, y)) {
lastPoint.set(x, y);
handlePosition = hKnobBounds.x;
touchScrollH = true;
draggingPointer = pointer;
return true;
}
setScrollX(amountX + areaWidth * (x < hKnobBounds.x ? -1 : 1));
return true;
}
if (scrollBarTouch && scrollY && vScrollBounds.contains(x, y)) {
event.stop();
setScrollbarsVisible(true);
if (vKnobBounds.contains(x, y)) {
lastPoint.set(x, y);
handlePosition = vKnobBounds.y;
touchScrollV = true;
draggingPointer = pointer;
return true;
}
setScrollY(amountY + areaHeight * (y < vKnobBounds.y ? 1 : -1));
return true;
}
return false;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer != draggingPointer) return;
cancel();
}
public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != draggingPointer) return;
if (touchScrollH) {
float delta = x - lastPoint.x;
float scrollH = handlePosition + delta;
handlePosition = scrollH;
scrollH = Math.max(hScrollBounds.x, scrollH);
scrollH = Math.min(hScrollBounds.x + hScrollBounds.width - hKnobBounds.width, scrollH);
float total = hScrollBounds.width - hKnobBounds.width;
if (total != 0) setScrollPercentX((scrollH - hScrollBounds.x) / total);
lastPoint.set(x, y);
} else if (touchScrollV) {
float delta = y - lastPoint.y;
float scrollV = handlePosition + delta;
handlePosition = scrollV;
scrollV = Math.max(vScrollBounds.y, scrollV);
scrollV = Math.min(vScrollBounds.y + vScrollBounds.height - vKnobBounds.height, scrollV);
float total = vScrollBounds.height - vKnobBounds.height;
if (total != 0) setScrollPercentY(1 - ((scrollV - vScrollBounds.y) / total));
lastPoint.set(x, y);
}
}
public boolean mouseMoved (InputEvent event, float x, float y) {
if (!flickScroll) setScrollbarsVisible(true);
return false;
}
});
flickScrollListener = new ActorGestureListener() {
public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
setScrollbarsVisible(true);
amountX -= deltaX;
amountY += deltaY;
clamp();
if (cancelTouchFocus && ((scrollX && deltaX != 0) || (scrollY && deltaY != 0))) cancelTouchFocus();
}
public void fling (InputEvent event, float x, float y, int button) {
if (Math.abs(x) > 150 && scrollX) {
flingTimer = flingTime;
velocityX = x;
if (cancelTouchFocus) cancelTouchFocus();
}
if (Math.abs(y) > 150 && scrollY) {
flingTimer = flingTime;
velocityY = -y;
if (cancelTouchFocus) cancelTouchFocus();
}
}
public boolean handle (Event event) {
if (super.handle(event)) {
if (((InputEvent)event).getType() == InputEvent.Type.touchDown) flingTimer = 0;
return true;
} else if (event instanceof InputEvent && ((InputEvent)event).isTouchFocusCancel()) //
cancel();
return false;
}
};
addListener(flickScrollListener);
addListener(new InputListener() {
public boolean scrolled (InputEvent event, float x, float y, float scrollAmountX, float scrollAmountY) {
setScrollbarsVisible(true);
if (scrollY || scrollX) {
setScrollY(amountY + getMouseWheelY() * scrollAmountY);
setScrollX(amountX + getMouseWheelX() * scrollAmountX);
} else
return false;
return true;
}
});
}
/** Shows or hides the scrollbars for when using {@link #setFadeScrollBars(boolean)}. */
public void setScrollbarsVisible (boolean visible) {
if (visible) {
fadeAlpha = fadeAlphaSeconds;
fadeDelay = fadeDelaySeconds;
} else {
fadeAlpha = 0;
fadeDelay = 0;
}
}
/** Cancels the stage's touch focus for all listeners except this scroll pane's flick scroll listener. This causes any widgets
* inside the scrollpane that have received touchDown to receive touchUp.
* @see #setCancelTouchFocus(boolean) */
public void cancelTouchFocus () {
Stage stage = getStage();
if (stage != null) stage.cancelTouchFocusExcept(flickScrollListener, this);
}
/** If currently scrolling by tracking a touch down, stop scrolling. */
public void cancel () {
draggingPointer = -1;
touchScrollH = false;
touchScrollV = false;
flickScrollListener.getGestureDetector().cancel();
}
void clamp () {
if (!clamp) return;
scrollX(overscrollX ? MathUtils.clamp(amountX, -overscrollDistance, maxX + overscrollDistance)
: MathUtils.clamp(amountX, 0, maxX));
scrollY(overscrollY ? MathUtils.clamp(amountY, -overscrollDistance, maxY + overscrollDistance)
: MathUtils.clamp(amountY, 0, maxY));
}
public void setStyle (ScrollPaneStyle style) {
if (style == null) throw new IllegalArgumentException("style cannot be null.");
this.style = style;
invalidateHierarchy();
}
/** Returns the scroll pane's style. Modifying the returned style may not have an effect until
* {@link #setStyle(ScrollPaneStyle)} is called. */
public ScrollPaneStyle getStyle () {
return style;
}
public void act (float delta) {
super.act(delta);
boolean panning = flickScrollListener.getGestureDetector().isPanning();
boolean animating = false;
if (fadeAlpha > 0 && fadeScrollBars && !panning && !touchScrollH && !touchScrollV) {
fadeDelay -= delta;
if (fadeDelay <= 0) fadeAlpha = Math.max(0, fadeAlpha - delta);
animating = true;
}
if (flingTimer > 0) {
setScrollbarsVisible(true);
float alpha = flingTimer / flingTime;
amountX -= velocityX * alpha * delta;
amountY -= velocityY * alpha * delta;
clamp();
// Stop fling if hit overscroll distance.
if (amountX == -overscrollDistance) velocityX = 0;
if (amountX >= maxX + overscrollDistance) velocityX = 0;
if (amountY == -overscrollDistance) velocityY = 0;
if (amountY >= maxY + overscrollDistance) velocityY = 0;
flingTimer -= delta;
if (flingTimer <= 0) {
velocityX = 0;
velocityY = 0;
}
animating = true;
}
if (smoothScrolling && flingTimer <= 0 && !panning && //
// Scroll smoothly when grabbing the scrollbar if one pixel of scrollbar movement is > 10% of the scroll area.
((!touchScrollH || (scrollX && maxX / (hScrollBounds.width - hKnobBounds.width) > areaWidth * 0.1f)) //
&& (!touchScrollV || (scrollY && maxY / (vScrollBounds.height - vKnobBounds.height) > areaHeight * 0.1f))) //
) {
if (visualAmountX != amountX) {
if (visualAmountX < amountX)
visualScrollX(Math.min(amountX, visualAmountX + Math.max(200 * delta, (amountX - visualAmountX) * 7 * delta)));
else
visualScrollX(Math.max(amountX, visualAmountX - Math.max(200 * delta, (visualAmountX - amountX) * 7 * delta)));
animating = true;
}
if (visualAmountY != amountY) {
if (visualAmountY < amountY)
visualScrollY(Math.min(amountY, visualAmountY + Math.max(200 * delta, (amountY - visualAmountY) * 7 * delta)));
else
visualScrollY(Math.max(amountY, visualAmountY - Math.max(200 * delta, (visualAmountY - amountY) * 7 * delta)));
animating = true;
}
} else {
if (visualAmountX != amountX) visualScrollX(amountX);
if (visualAmountY != amountY) visualScrollY(amountY);
}
if (!panning) {
if (overscrollX && scrollX) {
if (amountX < 0) {
setScrollbarsVisible(true);
amountX += (overscrollSpeedMin + (overscrollSpeedMax - overscrollSpeedMin) * -amountX / overscrollDistance)
* delta;
if (amountX > 0) scrollX(0);
animating = true;
} else if (amountX > maxX) {
setScrollbarsVisible(true);
amountX -= (overscrollSpeedMin
+ (overscrollSpeedMax - overscrollSpeedMin) * -(maxX - amountX) / overscrollDistance) * delta;
if (amountX < maxX) scrollX(maxX);
animating = true;
}
}
if (overscrollY && scrollY) {
if (amountY < 0) {
setScrollbarsVisible(true);
amountY += (overscrollSpeedMin + (overscrollSpeedMax - overscrollSpeedMin) * -amountY / overscrollDistance)
* delta;
if (amountY > 0) scrollY(0);
animating = true;
} else if (amountY > maxY) {
setScrollbarsVisible(true);
amountY -= (overscrollSpeedMin
+ (overscrollSpeedMax - overscrollSpeedMin) * -(maxY - amountY) / overscrollDistance) * delta;
if (amountY < maxY) scrollY(maxY);
animating = true;
}
}
}
if (animating) {
Stage stage = getStage();
if (stage != null && stage.getActionsRequestRendering()) Gdx.graphics.requestRendering();
}
}
public void layout () {
final Drawable bg = style.background;
final Drawable hScrollKnob = style.hScrollKnob;
final Drawable vScrollKnob = style.vScrollKnob;
float bgLeftWidth = 0, bgRightWidth = 0, bgTopHeight = 0, bgBottomHeight = 0;
if (bg != null) {
bgLeftWidth = bg.getLeftWidth();
bgRightWidth = bg.getRightWidth();
bgTopHeight = bg.getTopHeight();
bgBottomHeight = bg.getBottomHeight();
}
float width = getWidth();
float height = getHeight();
float scrollbarHeight = 0;
if (hScrollKnob != null) scrollbarHeight = hScrollKnob.getMinHeight();
if (style.hScroll != null) scrollbarHeight = Math.max(scrollbarHeight, style.hScroll.getMinHeight());
float scrollbarWidth = 0;
if (vScrollKnob != null) scrollbarWidth = vScrollKnob.getMinWidth();
if (style.vScroll != null) scrollbarWidth = Math.max(scrollbarWidth, style.vScroll.getMinWidth());
// Get available space size by subtracting background's padded area.
areaWidth = width - bgLeftWidth - bgRightWidth;
areaHeight = height - bgTopHeight - bgBottomHeight;
if (widget == null) return;
// Get widget's desired width.
float widgetWidth, widgetHeight;
if (widget instanceof Layout) {
Layout layout = (Layout)widget;
widgetWidth = layout.getPrefWidth();
widgetHeight = layout.getPrefHeight();
} else {
widgetWidth = widget.getWidth();
widgetHeight = widget.getHeight();
}
// Determine if horizontal/vertical scrollbars are needed.
scrollX = forceScrollX || (widgetWidth > areaWidth && !disableX);
scrollY = forceScrollY || (widgetHeight > areaHeight && !disableY);
boolean fade = fadeScrollBars;
if (!fade) {
// Check again, now taking into account the area that's taken up by any enabled scrollbars.
if (scrollY) {
areaWidth -= scrollbarWidth;
if (!scrollX && widgetWidth > areaWidth && !disableX) scrollX = true;
}
if (scrollX) {
areaHeight -= scrollbarHeight;
if (!scrollY && widgetHeight > areaHeight && !disableY) {
scrollY = true;
areaWidth -= scrollbarWidth;
}
}
}
// The bounds of the scrollable area for the widget.
widgetAreaBounds.set(bgLeftWidth, bgBottomHeight, areaWidth, areaHeight);
if (fade) {
// Make sure widget is drawn under fading scrollbars.
if (scrollX && scrollY) {
areaHeight -= scrollbarHeight;
areaWidth -= scrollbarWidth;
}
} else {
if (scrollbarsOnTop) {
// Make sure widget is drawn under non-fading scrollbars.
if (scrollX) widgetAreaBounds.height += scrollbarHeight;
if (scrollY) widgetAreaBounds.width += scrollbarWidth;
} else {
// Offset widget area y for horizontal scrollbar at bottom.
if (scrollX && hScrollOnBottom) widgetAreaBounds.y += scrollbarHeight;
// Offset widget area x for vertical scrollbar at left.
if (scrollY && !vScrollOnRight) widgetAreaBounds.x += scrollbarWidth;
}
}
// If the widget is smaller than the available space, make it take up the available space.
widgetWidth = disableX ? areaWidth : Math.max(areaWidth, widgetWidth);
widgetHeight = disableY ? areaHeight : Math.max(areaHeight, widgetHeight);
maxX = widgetWidth - areaWidth;
maxY = widgetHeight - areaHeight;
if (fade) {
// Make sure widget is drawn under fading scrollbars.
if (scrollX && scrollY) {
maxY -= scrollbarHeight;
maxX -= scrollbarWidth;
}
}
scrollX(MathUtils.clamp(amountX, 0, maxX));
scrollY(MathUtils.clamp(amountY, 0, maxY));
// Set the bounds and scroll knob sizes if scrollbars are needed.
if (scrollX) {
if (hScrollKnob != null) {
float hScrollHeight = style.hScroll != null ? style.hScroll.getMinHeight() : hScrollKnob.getMinHeight();
// The corner gap where the two scroll bars intersect might have to flip from right to left.
float boundsX = vScrollOnRight ? bgLeftWidth : bgLeftWidth + scrollbarWidth;
// Scrollbar on the top or bottom.
float boundsY = hScrollOnBottom ? bgBottomHeight : height - bgTopHeight - hScrollHeight;
hScrollBounds.set(boundsX, boundsY, areaWidth, hScrollHeight);
if (variableSizeKnobs)
hKnobBounds.width = Math.max(hScrollKnob.getMinWidth(), (int)(hScrollBounds.width * areaWidth / widgetWidth));
else
hKnobBounds.width = hScrollKnob.getMinWidth();
if (hKnobBounds.width > widgetWidth) hKnobBounds.width = 0;
hKnobBounds.height = hScrollKnob.getMinHeight();
hKnobBounds.x = hScrollBounds.x + (int)((hScrollBounds.width - hKnobBounds.width) * getScrollPercentX());
hKnobBounds.y = hScrollBounds.y;
} else {
hScrollBounds.set(0, 0, 0, 0);
hKnobBounds.set(0, 0, 0, 0);
}
}
if (scrollY) {
if (vScrollKnob != null) {
float vScrollWidth = style.vScroll != null ? style.vScroll.getMinWidth() : vScrollKnob.getMinWidth();
// the small gap where the two scroll bars intersect might have to flip from bottom to top
float boundsX, boundsY;
if (hScrollOnBottom) {
boundsY = height - bgTopHeight - areaHeight;
} else {
boundsY = bgBottomHeight;
}
// bar on the left or right
if (vScrollOnRight) {
boundsX = width - bgRightWidth - vScrollWidth;
} else {
boundsX = bgLeftWidth;
}
vScrollBounds.set(boundsX, boundsY, vScrollWidth, areaHeight);
vKnobBounds.width = vScrollKnob.getMinWidth();
if (variableSizeKnobs)
vKnobBounds.height = Math.max(vScrollKnob.getMinHeight(), (int)(vScrollBounds.height * areaHeight / widgetHeight));
else
vKnobBounds.height = vScrollKnob.getMinHeight();
if (vKnobBounds.height > widgetHeight) vKnobBounds.height = 0;
if (vScrollOnRight) {
vKnobBounds.x = width - bgRightWidth - vScrollKnob.getMinWidth();
} else {
vKnobBounds.x = bgLeftWidth;
}
vKnobBounds.y = vScrollBounds.y + (int)((vScrollBounds.height - vKnobBounds.height) * (1 - getScrollPercentY()));
} else {
vScrollBounds.set(0, 0, 0, 0);
vKnobBounds.set(0, 0, 0, 0);
}
}
updateWidgetPosition();
if (widget instanceof Layout) {
widget.setSize(widgetWidth, widgetHeight);
((Layout)widget).validate();
}
}
private void updateWidgetPosition () {
// Calculate the widget's position depending on the scroll state and available widget area.
float y = widgetAreaBounds.y;
if (!scrollY)
y -= (int)maxY;
else
y -= (int)(maxY - visualAmountY);
float x = widgetAreaBounds.x;
if (scrollX) x -= (int)visualAmountX;
if (!fadeScrollBars && scrollbarsOnTop) {
if (scrollX && hScrollOnBottom) {
float scrollbarHeight = 0;
if (style.hScrollKnob != null) scrollbarHeight = style.hScrollKnob.getMinHeight();
if (style.hScroll != null) scrollbarHeight = Math.max(scrollbarHeight, style.hScroll.getMinHeight());
y += scrollbarHeight;
}
if (scrollY && !vScrollOnRight) {
float scrollbarWidth = 0;
if (style.hScrollKnob != null) scrollbarWidth = style.hScrollKnob.getMinWidth();
if (style.hScroll != null) scrollbarWidth = Math.max(scrollbarWidth, style.hScroll.getMinWidth());
x += scrollbarWidth;
}
}
widget.setPosition(x, y);
if (widget instanceof Cullable) {
widgetCullingArea.x = widgetAreaBounds.x - x;
widgetCullingArea.y = widgetAreaBounds.y - y;
widgetCullingArea.width = widgetAreaBounds.width;
widgetCullingArea.height = widgetAreaBounds.height;
((Cullable)widget).setCullingArea(widgetCullingArea);
}
}
public void draw (Batch batch, float parentAlpha) {
if (widget == null) return;
validate();
// Setup transform for this group.
applyTransform(batch, computeTransform());
if (scrollX) hKnobBounds.x = hScrollBounds.x + (int)((hScrollBounds.width - hKnobBounds.width) * getVisualScrollPercentX());
if (scrollY)
vKnobBounds.y = vScrollBounds.y + (int)((vScrollBounds.height - vKnobBounds.height) * (1 - getVisualScrollPercentY()));
updateWidgetPosition();
// Draw the background ninepatch.
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
if (style.background != null) style.background.draw(batch, 0, 0, getWidth(), getHeight());
batch.flush();
if (clipBegin(widgetAreaBounds.x, widgetAreaBounds.y, widgetAreaBounds.width, widgetAreaBounds.height)) {
drawChildren(batch, parentAlpha);
batch.flush();
clipEnd();
}
// Render scrollbars and knobs on top if they will be visible
float alpha = color.a * parentAlpha;
if (fadeScrollBars) alpha *= Interpolation.fade.apply(fadeAlpha / fadeAlphaSeconds);
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
drawScrollBars(batch, color.r, color.g, color.b, alpha);
resetTransform(batch);
}
/** Renders the scrollbars after the children have been drawn. If the scrollbars faded out, a is zero and rendering can be
* skipped. */
protected void drawScrollBars (Batch batch, float r, float g, float b, float a) {
if (a <= 0) return;
batch.setColor(r, g, b, a);
boolean x = scrollX && hKnobBounds.width > 0;
boolean y = scrollY && vKnobBounds.height > 0;
if (x && y) {
if (style.corner != null) {
style.corner.draw(batch, hScrollBounds.x + hScrollBounds.width, hScrollBounds.y, vScrollBounds.width,
vScrollBounds.y);
}
}
if (x) {
if (style.hScroll != null)
style.hScroll.draw(batch, hScrollBounds.x, hScrollBounds.y, hScrollBounds.width, hScrollBounds.height);
if (style.hScrollKnob != null)
style.hScrollKnob.draw(batch, hKnobBounds.x, hKnobBounds.y, hKnobBounds.width, hKnobBounds.height);
}
if (y) {
if (style.vScroll != null)
style.vScroll.draw(batch, vScrollBounds.x, vScrollBounds.y, vScrollBounds.width, vScrollBounds.height);
if (style.vScrollKnob != null)
style.vScrollKnob.draw(batch, vKnobBounds.x, vKnobBounds.y, vKnobBounds.width, vKnobBounds.height);
}
}
/** Generate fling gesture.
* @param flingTime Time in seconds for which you want to fling last.
* @param velocityX Velocity for horizontal direction.
* @param velocityY Velocity for vertical direction. */
public void fling (float flingTime, float velocityX, float velocityY) {
this.flingTimer = flingTime;
this.velocityX = velocityX;
this.velocityY = velocityY;
}
public float getPrefWidth () {
float width = 0;
if (widget instanceof Layout)
width = ((Layout)widget).getPrefWidth();
else if (widget != null) //
width = widget.getWidth();
Drawable background = style.background;
if (background != null)
width = Math.max(width + background.getLeftWidth() + background.getRightWidth(), background.getMinWidth());
if (scrollY) {
float scrollbarWidth = 0;
if (style.vScrollKnob != null) scrollbarWidth = style.vScrollKnob.getMinWidth();
if (style.vScroll != null) scrollbarWidth = Math.max(scrollbarWidth, style.vScroll.getMinWidth());
width += scrollbarWidth;
}
return width;
}
public float getPrefHeight () {
float height = 0;
if (widget instanceof Layout)
height = ((Layout)widget).getPrefHeight();
else if (widget != null) //
height = widget.getHeight();
Drawable background = style.background;
if (background != null)
height = Math.max(height + background.getTopHeight() + background.getBottomHeight(), background.getMinHeight());
if (scrollX) {
float scrollbarHeight = 0;
if (style.hScrollKnob != null) scrollbarHeight = style.hScrollKnob.getMinHeight();
if (style.hScroll != null) scrollbarHeight = Math.max(scrollbarHeight, style.hScroll.getMinHeight());
height += scrollbarHeight;
}
return height;
}
public float getMinWidth () {
return 0;
}
public float getMinHeight () {
return 0;
}
/** Sets the {@link Actor} embedded in this scroll pane.
* @param actor May be null to remove any current actor. */
public void setActor (@Null Actor actor) {
if (widget == this) throw new IllegalArgumentException("widget cannot be the ScrollPane.");
if (this.widget != null) super.removeActor(this.widget);
this.widget = actor;
if (widget != null) super.addActor(widget);
}
/** Returns the actor embedded in this scroll pane, or null. */
@Null
public Actor getActor () {
return widget;
}
/** @deprecated Use {@link #setActor(Actor)}. */
@Deprecated
public void setWidget (@Null Actor actor) {
setActor(actor);
}
/** @deprecated Use {@link #getActor()}. */
@Deprecated
@Null
public Actor getWidget () {
return widget;
}
/** @deprecated ScrollPane may have only a single child.
* @see #setWidget(Actor) */
@Deprecated
public void addActor (Actor actor) {
throw new UnsupportedOperationException("Use ScrollPane#setWidget.");
}
/** @deprecated ScrollPane may have only a single child.
* @see #setWidget(Actor) */
@Deprecated
public void addActorAt (int index, Actor actor) {
throw new UnsupportedOperationException("Use ScrollPane#setWidget.");
}
/** @deprecated ScrollPane may have only a single child.
* @see #setWidget(Actor) */
@Deprecated
public void addActorBefore (Actor actorBefore, Actor actor) {
throw new UnsupportedOperationException("Use ScrollPane#setWidget.");
}
/** @deprecated ScrollPane may have only a single child.
* @see #setWidget(Actor) */
@Deprecated
public void addActorAfter (Actor actorAfter, Actor actor) {
throw new UnsupportedOperationException("Use ScrollPane#setWidget.");
}
public boolean removeActor (Actor actor) {
if (actor == null) throw new IllegalArgumentException("actor cannot be null.");
if (actor != widget) return false;
setActor(null);
return true;
}
public boolean removeActor (Actor actor, boolean unfocus) {
if (actor == null) throw new IllegalArgumentException("actor cannot be null.");
if (actor != widget) return false;
this.widget = null;
return super.removeActor(actor, unfocus);
}
public Actor removeActorAt (int index, boolean unfocus) {
Actor actor = super.removeActorAt(index, unfocus);
if (actor == widget) this.widget = null;
return actor;
}
@Null
public Actor hit (float x, float y, boolean touchable) {
if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) return null;
if (touchable && getTouchable() == Touchable.enabled && isVisible()) {
if (scrollX && touchScrollH && hScrollBounds.contains(x, y)) return this;
if (scrollY && touchScrollV && vScrollBounds.contains(x, y)) return this;
}
return super.hit(x, y, touchable);
}
/** Called whenever the x scroll amount is changed. */
protected void scrollX (float pixelsX) {
this.amountX = pixelsX;
}
/** Called whenever the y scroll amount is changed. */
protected void scrollY (float pixelsY) {
this.amountY = pixelsY;
}
/** Called whenever the visual x scroll amount is changed. */
protected void visualScrollX (float pixelsX) {
this.visualAmountX = pixelsX;
}
/** Called whenever the visual y scroll amount is changed. */
protected void visualScrollY (float pixelsY) {
this.visualAmountY = pixelsY;
}
/** Returns the amount to scroll horizontally when the mouse wheel is scrolled. */
protected float getMouseWheelX () {
return Math.min(areaWidth, Math.max(areaWidth * 0.9f, maxX * 0.1f) / 4);
}
/** Returns the amount to scroll vertically when the mouse wheel is scrolled. */
protected float getMouseWheelY () {
return Math.min(areaHeight, Math.max(areaHeight * 0.9f, maxY * 0.1f) / 4);
}
public void setScrollX (float pixels) {
scrollX(MathUtils.clamp(pixels, 0, maxX));
}
/** Returns the x scroll position in pixels, where 0 is the left of the scroll pane. */
public float getScrollX () {
return amountX;
}
public void setScrollY (float pixels) {
scrollY(MathUtils.clamp(pixels, 0, maxY));
}
/** Returns the y scroll position in pixels, where 0 is the top of the scroll pane. */
public float getScrollY () {
return amountY;
}
/** Sets the visual scroll amount equal to the scroll amount. This can be used when setting the scroll amount without
* animating. */
public void updateVisualScroll () {
visualAmountX = amountX;
visualAmountY = amountY;
}
public float getVisualScrollX () {
return !scrollX ? 0 : visualAmountX;
}
public float getVisualScrollY () {
return !scrollY ? 0 : visualAmountY;
}
public float getVisualScrollPercentX () {
if (maxX == 0) return 0;
return MathUtils.clamp(visualAmountX / maxX, 0, 1);
}
public float getVisualScrollPercentY () {
if (maxY == 0) return 0;
return MathUtils.clamp(visualAmountY / maxY, 0, 1);
}
public float getScrollPercentX () {
if (maxX == 0) return 0;
return MathUtils.clamp(amountX / maxX, 0, 1);
}
public void setScrollPercentX (float percentX) {
scrollX(maxX * MathUtils.clamp(percentX, 0, 1));
}
public float getScrollPercentY () {
if (maxY == 0) return 0;
return MathUtils.clamp(amountY / maxY, 0, 1);
}
public void setScrollPercentY (float percentY) {
scrollY(maxY * MathUtils.clamp(percentY, 0, 1));
}
public void setFlickScroll (boolean flickScroll) {
if (this.flickScroll == flickScroll) return;
this.flickScroll = flickScroll;
if (flickScroll)
addListener(flickScrollListener);
else
removeListener(flickScrollListener);
invalidate();
}
public void setFlickScrollTapSquareSize (float halfTapSquareSize) {
flickScrollListener.getGestureDetector().setTapSquareSize(halfTapSquareSize);
}
/** Sets the scroll offset so the specified rectangle is fully in view, if possible. Coordinates are in the scroll pane
* widget's coordinate system. */
public void scrollTo (float x, float y, float width, float height) {
scrollTo(x, y, width, height, false, false);
}
/** Sets the scroll offset so the specified rectangle is fully in view, and optionally centered vertically and/or horizontally,
* if possible. Coordinates are in the scroll pane widget's coordinate system. */
public void scrollTo (float x, float y, float width, float height, boolean centerHorizontal, boolean centerVertical) {
validate();
float amountX = this.amountX;
if (centerHorizontal) {
amountX = x - areaWidth / 2 + width / 2;
} else {
if (x + width > amountX + areaWidth) amountX = x + width - areaWidth;
if (x < amountX) amountX = x;
}
scrollX(MathUtils.clamp(amountX, 0, maxX));
float amountY = this.amountY;
if (centerVertical) {
amountY = maxY - y + areaHeight / 2 - height / 2;
} else {
if (amountY > maxY - y - height + areaHeight) amountY = maxY - y - height + areaHeight;
if (amountY < maxY - y) amountY = maxY - y;
}
scrollY(MathUtils.clamp(amountY, 0, maxY));
}
/** Returns the maximum scroll value in the x direction. */
public float getMaxX () {
return maxX;
}
/** Returns the maximum scroll value in the y direction. */
public float getMaxY () {
return maxY;
}
public float getScrollBarHeight () {
if (!scrollX) return 0;
float height = 0;
if (style.hScrollKnob != null) height = style.hScrollKnob.getMinHeight();
if (style.hScroll != null) height = Math.max(height, style.hScroll.getMinHeight());
return height;
}
public float getScrollBarWidth () {
if (!scrollY) return 0;
float width = 0;
if (style.vScrollKnob != null) width = style.vScrollKnob.getMinWidth();
if (style.vScroll != null) width = Math.max(width, style.vScroll.getMinWidth());
return width;
}
/** Returns the width of the scrolled viewport. */
public float getScrollWidth () {
return areaWidth;
}
/** Returns the height of the scrolled viewport. */
public float getScrollHeight () {
return areaHeight;
}
/** Returns true if the widget is larger than the scroll pane horizontally. */
public boolean isScrollX () {
return scrollX;
}
/** Returns true if the widget is larger than the scroll pane vertically. */
public boolean isScrollY () {
return scrollY;
}
/** Disables scrolling in a direction. The widget will be sized to the FlickScrollPane in the disabled direction. */
public void setScrollingDisabled (boolean x, boolean y) {
disableX = x;
disableY = y;
invalidate();
}
public boolean isScrollingDisabledX () {
return disableX;
}
public boolean isScrollingDisabledY () {
return disableY;
}
public boolean isLeftEdge () {
return !scrollX || amountX <= 0;
}
public boolean isRightEdge () {
return !scrollX || amountX >= maxX;
}
public boolean isTopEdge () {
return !scrollY || amountY <= 0;
}
public boolean isBottomEdge () {
return !scrollY || amountY >= maxY;
}
public boolean isDragging () {
return draggingPointer != -1;
}
public boolean isPanning () {
return flickScrollListener.getGestureDetector().isPanning();