/
Stage.java
866 lines (758 loc) · 31.5 KB
/
Stage.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
/*******************************************************************************
* 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;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent.Type;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.Table.Debug;
import com.badlogic.gdx.scenes.scene2d.utils.FocusListener;
import com.badlogic.gdx.scenes.scene2d.utils.FocusListener.FocusEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.Null;
import com.badlogic.gdx.utils.Pool.Poolable;
import com.badlogic.gdx.utils.Pools;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.SnapshotArray;
import com.badlogic.gdx.utils.viewport.ScalingViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
/** A 2D scene graph containing hierarchies of {@link Actor actors}. Stage handles the viewport and distributes input events.
* <p>
* {@link #setViewport(Viewport)} controls the coordinates used within the stage and sets up the camera used to convert between
* stage coordinates and screen coordinates.
* <p>
* A stage must receive input events so it can distribute them to actors. This is typically done by passing the stage to
* {@link Input#setInputProcessor(com.badlogic.gdx.InputProcessor) Gdx.input.setInputProcessor}. An {@link InputMultiplexer} may
* be used to handle input events before or after the stage does. If an actor handles an event by returning true from the input
* method, then the stage's input method will also return true, causing subsequent InputProcessors to not receive the event.
* <p>
* The Stage and its constituents (like Actors and Listeners) are not thread-safe and should only be updated and queried from a
* single thread (presumably the main render thread). Methods should be reentrant, so you can update Actors and Stages from within
* callbacks and handlers.
* @author mzechner
* @author Nathan Sweet */
public class Stage extends InputAdapter implements Disposable {
/** True if any actor has ever had debug enabled. */
static boolean debug;
private Viewport viewport;
private final Batch batch;
private boolean ownsBatch;
private Group root;
private final Vector2 tempCoords = new Vector2();
private final Actor[] pointerOverActors = new Actor[20];
private final boolean[] pointerTouched = new boolean[20];
private final int[] pointerScreenX = new int[20];
private final int[] pointerScreenY = new int[20];
private int mouseScreenX, mouseScreenY;
private @Null Actor mouseOverActor;
private @Null Actor keyboardFocus, scrollFocus;
final SnapshotArray<TouchFocus> touchFocuses = new SnapshotArray(true, 4, TouchFocus.class);
private boolean actionsRequestRendering = true;
private ShapeRenderer debugShapes;
private boolean debugInvisible, debugAll, debugUnderMouse, debugParentUnderMouse;
private Debug debugTableUnderMouse = Debug.none;
private final Color debugColor = new Color(0, 1, 0, 0.85f);
/** Creates a stage with a {@link ScalingViewport} set to {@link Scaling#stretch}. The stage will use its own {@link Batch}
* which will be disposed when the stage is disposed. */
public Stage () {
this(new ScalingViewport(Scaling.stretch, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera()),
new SpriteBatch());
ownsBatch = true;
}
/** Creates a stage with the specified viewport. The stage will use its own {@link Batch} which will be disposed when the stage
* is disposed. */
public Stage (Viewport viewport) {
this(viewport, new SpriteBatch());
ownsBatch = true;
}
/** Creates a stage with the specified viewport and batch. This can be used to specify an existing batch or to customize which
* batch implementation is used.
* @param batch Will not be disposed if {@link #dispose()} is called, handle disposal yourself. */
public Stage (Viewport viewport, Batch batch) {
if (viewport == null) throw new IllegalArgumentException("viewport cannot be null.");
if (batch == null) throw new IllegalArgumentException("batch cannot be null.");
this.viewport = viewport;
this.batch = batch;
root = new Group();
root.setStage(this);
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
}
public void draw () {
Camera camera = viewport.getCamera();
camera.update();
if (!root.isVisible()) return;
Batch batch = this.batch;
batch.setProjectionMatrix(camera.combined);
batch.begin();
root.draw(batch, 1);
batch.end();
if (debug) drawDebug();
}
private void drawDebug () {
if (debugShapes == null) {
debugShapes = new ShapeRenderer();
debugShapes.setAutoShapeType(true);
}
if (debugUnderMouse || debugParentUnderMouse || debugTableUnderMouse != Debug.none) {
screenToStageCoordinates(tempCoords.set(Gdx.input.getX(), Gdx.input.getY()));
Actor actor = hit(tempCoords.x, tempCoords.y, true);
if (actor == null) return;
if (debugParentUnderMouse && actor.parent != null) actor = actor.parent;
if (debugTableUnderMouse == Debug.none)
actor.setDebug(true);
else {
while (actor != null) {
if (actor instanceof Table) break;
actor = actor.parent;
}
if (actor == null) return;
((Table)actor).debug(debugTableUnderMouse);
}
if (debugAll && actor instanceof Group) ((Group)actor).debugAll();
disableDebug(root, actor);
} else {
if (debugAll) root.debugAll();
}
Gdx.gl.glEnable(GL20.GL_BLEND);
debugShapes.setProjectionMatrix(viewport.getCamera().combined);
debugShapes.begin();
root.drawDebug(debugShapes);
debugShapes.end();
Gdx.gl.glDisable(GL20.GL_BLEND);
}
/** Disables debug on all actors recursively except the specified actor and any children. */
private void disableDebug (Actor actor, Actor except) {
if (actor == except) return;
actor.setDebug(false);
if (actor instanceof Group) {
SnapshotArray<Actor> children = ((Group)actor).children;
for (int i = 0, n = children.size; i < n; i++)
disableDebug(children.get(i), except);
}
}
/** Calls {@link #act(float)} with {@link Graphics#getDeltaTime()}, limited to a minimum of 30fps. */
public void act () {
act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
}
/** Calls the {@link Actor#act(float)} method on each actor in the stage. Typically called each frame. This method also fires
* enter and exit events.
* @param delta Time in seconds since the last frame. */
public void act (float delta) {
// Update over actors. Done in act() because actors may change position, which can fire enter/exit without an input event.
for (int pointer = 0, n = pointerOverActors.length; pointer < n; pointer++) {
Actor overLast = pointerOverActors[pointer];
// Check if pointer is gone.
if (!pointerTouched[pointer]) {
if (overLast != null) {
pointerOverActors[pointer] = null;
screenToStageCoordinates(tempCoords.set(pointerScreenX[pointer], pointerScreenY[pointer]));
// Exit over last.
InputEvent event = Pools.obtain(InputEvent.class);
event.setType(InputEvent.Type.exit);
event.setStage(this);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
event.setRelatedActor(overLast);
event.setPointer(pointer);
overLast.fire(event);
Pools.free(event);
}
continue;
}
// Update over actor for the pointer.
pointerOverActors[pointer] = fireEnterAndExit(overLast, pointerScreenX[pointer], pointerScreenY[pointer], pointer);
}
// Update over actor for the mouse on the desktop.
ApplicationType type = Gdx.app.getType();
if (type == ApplicationType.Desktop || type == ApplicationType.Applet || type == ApplicationType.WebGL)
mouseOverActor = fireEnterAndExit(mouseOverActor, mouseScreenX, mouseScreenY, -1);
root.act(delta);
}
private @Null Actor fireEnterAndExit (@Null Actor overLast, int screenX, int screenY, int pointer) {
// Find the actor under the point.
screenToStageCoordinates(tempCoords.set(screenX, screenY));
Actor over = hit(tempCoords.x, tempCoords.y, true);
if (over == overLast) return overLast;
// Exit overLast.
if (overLast != null) {
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
event.setPointer(pointer);
event.setType(InputEvent.Type.exit);
event.setRelatedActor(over);
overLast.fire(event);
Pools.free(event);
}
// Enter over.
if (over != null) {
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
event.setPointer(pointer);
event.setType(InputEvent.Type.enter);
event.setRelatedActor(overLast);
over.fire(event);
Pools.free(event);
}
return over;
}
/** Applies a touch down event to the stage and returns true if an actor in the scene {@link Event#handle() handled} the
* event. */
public boolean touchDown (int screenX, int screenY, int pointer, int button) {
if (!isInsideViewport(screenX, screenY)) return false;
pointerTouched[pointer] = true;
pointerScreenX[pointer] = screenX;
pointerScreenY[pointer] = screenY;
screenToStageCoordinates(tempCoords.set(screenX, screenY));
InputEvent event = Pools.obtain(InputEvent.class);
event.setType(Type.touchDown);
event.setStage(this);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
event.setPointer(pointer);
event.setButton(button);
Actor target = hit(tempCoords.x, tempCoords.y, true);
if (target == null) {
if (root.getTouchable() == Touchable.enabled) root.fire(event);
} else
target.fire(event);
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Applies a touch moved event to the stage and returns true if an actor in the scene {@link Event#handle() handled} the
* event. Only {@link InputListener listeners} that returned true for touchDown will receive this event. */
public boolean touchDragged (int screenX, int screenY, int pointer) {
pointerScreenX[pointer] = screenX;
pointerScreenY[pointer] = screenY;
mouseScreenX = screenX;
mouseScreenY = screenY;
if (touchFocuses.size == 0) return false;
screenToStageCoordinates(tempCoords.set(screenX, screenY));
InputEvent event = Pools.obtain(InputEvent.class);
event.setType(Type.touchDragged);
event.setStage(this);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
event.setPointer(pointer);
SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses;
TouchFocus[] focuses = touchFocuses.begin();
for (int i = 0, n = touchFocuses.size; i < n; i++) {
TouchFocus focus = focuses[i];
if (focus.pointer != pointer) continue;
if (!touchFocuses.contains(focus, true)) continue; // Touch focus already gone.
event.setTarget(focus.target);
event.setListenerActor(focus.listenerActor);
if (focus.listener.handle(event)) event.handle();
}
touchFocuses.end();
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Applies a touch up event to the stage and returns true if an actor in the scene {@link Event#handle() handled} the event.
* Only {@link InputListener listeners} that returned true for touchDown will receive this event. */
public boolean touchUp (int screenX, int screenY, int pointer, int button) {
pointerTouched[pointer] = false;
pointerScreenX[pointer] = screenX;
pointerScreenY[pointer] = screenY;
if (touchFocuses.size == 0) return false;
screenToStageCoordinates(tempCoords.set(screenX, screenY));
InputEvent event = Pools.obtain(InputEvent.class);
event.setType(Type.touchUp);
event.setStage(this);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
event.setPointer(pointer);
event.setButton(button);
SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses;
TouchFocus[] focuses = touchFocuses.begin();
for (int i = 0, n = touchFocuses.size; i < n; i++) {
TouchFocus focus = focuses[i];
if (focus.pointer != pointer || focus.button != button) continue;
if (!touchFocuses.removeValue(focus, true)) continue; // Touch focus already gone.
event.setTarget(focus.target);
event.setListenerActor(focus.listenerActor);
if (focus.listener.handle(event)) event.handle();
Pools.free(focus);
}
touchFocuses.end();
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Applies a mouse moved event to the stage and returns true if an actor in the scene {@link Event#handle() handled} the
* event. This event only occurs on the desktop. */
public boolean mouseMoved (int screenX, int screenY) {
mouseScreenX = screenX;
mouseScreenY = screenY;
if (!isInsideViewport(screenX, screenY)) return false;
screenToStageCoordinates(tempCoords.set(screenX, screenY));
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setType(Type.mouseMoved);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
Actor target = hit(tempCoords.x, tempCoords.y, true);
if (target == null) target = root;
target.fire(event);
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Applies a mouse scroll event to the stage and returns true if an actor in the scene {@link Event#handle() handled} the
* event. This event only occurs on the desktop. */
public boolean scrolled (float amountX, float amountY) {
Actor target = scrollFocus == null ? root : scrollFocus;
screenToStageCoordinates(tempCoords.set(mouseScreenX, mouseScreenY));
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setType(InputEvent.Type.scrolled);
event.setScrollAmountX(amountX);
event.setScrollAmountY(amountY);
event.setStageX(tempCoords.x);
event.setStageY(tempCoords.y);
target.fire(event);
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Applies a key down event to the actor that has {@link Stage#setKeyboardFocus(Actor) keyboard focus}, if any, and returns
* true if the event was {@link Event#handle() handled}. */
public boolean keyDown (int keyCode) {
Actor target = keyboardFocus == null ? root : keyboardFocus;
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setType(InputEvent.Type.keyDown);
event.setKeyCode(keyCode);
target.fire(event);
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Applies a key up event to the actor that has {@link Stage#setKeyboardFocus(Actor) keyboard focus}, if any, and returns true
* if the event was {@link Event#handle() handled}. */
public boolean keyUp (int keyCode) {
Actor target = keyboardFocus == null ? root : keyboardFocus;
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setType(InputEvent.Type.keyUp);
event.setKeyCode(keyCode);
target.fire(event);
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Applies a key typed event to the actor that has {@link Stage#setKeyboardFocus(Actor) keyboard focus}, if any, and returns
* true if the event was {@link Event#handle() handled}. */
public boolean keyTyped (char character) {
Actor target = keyboardFocus == null ? root : keyboardFocus;
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setType(InputEvent.Type.keyTyped);
event.setCharacter(character);
target.fire(event);
boolean handled = event.isHandled();
Pools.free(event);
return handled;
}
/** Adds the listener to be notified for all touchDragged and touchUp events for the specified pointer and button. Touch focus
* is added automatically when true is returned from {@link InputListener#touchDown(InputEvent, float, float, int, int)
* touchDown}. The specified actors will be used as the {@link Event#getListenerActor() listener actor} and
* {@link Event#getTarget() target} for the touchDragged and touchUp events. */
public void addTouchFocus (EventListener listener, Actor listenerActor, Actor target, int pointer, int button) {
TouchFocus focus = Pools.obtain(TouchFocus.class);
focus.listenerActor = listenerActor;
focus.target = target;
focus.listener = listener;
focus.pointer = pointer;
focus.button = button;
touchFocuses.add(focus);
}
/** Removes touch focus for the specified listener, pointer, and button. Note the listener will not receive a touchUp event
* when this method is used. */
public void removeTouchFocus (EventListener listener, Actor listenerActor, Actor target, int pointer, int button) {
SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses;
for (int i = touchFocuses.size - 1; i >= 0; i--) {
TouchFocus focus = touchFocuses.get(i);
if (focus.listener == listener && focus.listenerActor == listenerActor && focus.target == target
&& focus.pointer == pointer && focus.button == button) {
touchFocuses.removeIndex(i);
Pools.free(focus);
}
}
}
/** Cancels touch focus for all listeners with the specified listener actor.
* @see #cancelTouchFocus() */
public void cancelTouchFocus (Actor listenerActor) {
// Cancel all current touch focuses for the specified listener, allowing for concurrent modification, and never cancel the
// same focus twice.
InputEvent event = null;
SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses;
TouchFocus[] items = touchFocuses.begin();
for (int i = 0, n = touchFocuses.size; i < n; i++) {
TouchFocus focus = items[i];
if (focus.listenerActor != listenerActor) continue;
if (!touchFocuses.removeValue(focus, true)) continue; // Touch focus already gone.
if (event == null) {
event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setType(InputEvent.Type.touchUp);
event.setStageX(Integer.MIN_VALUE);
event.setStageY(Integer.MIN_VALUE);
}
event.setTarget(focus.target);
event.setListenerActor(focus.listenerActor);
event.setPointer(focus.pointer);
event.setButton(focus.button);
focus.listener.handle(event);
// Cannot return TouchFocus to pool, as it may still be in use (eg if cancelTouchFocus is called from touchDragged).
}
touchFocuses.end();
if (event != null) Pools.free(event);
}
/** Removes all touch focus listeners, sending a touchUp event to each listener. Listeners typically expect to receive a
* touchUp event when they have touch focus. The location of the touchUp is {@link Integer#MIN_VALUE}. Listeners can use
* {@link InputEvent#isTouchFocusCancel()} to ignore this event if needed. */
public void cancelTouchFocus () {
cancelTouchFocusExcept(null, null);
}
/** Cancels touch focus for all listeners except the specified listener.
* @see #cancelTouchFocus() */
public void cancelTouchFocusExcept (EventListener exceptListener, Actor exceptActor) {
InputEvent event = Pools.obtain(InputEvent.class);
event.setStage(this);
event.setType(InputEvent.Type.touchUp);
event.setStageX(Integer.MIN_VALUE);
event.setStageY(Integer.MIN_VALUE);
// Cancel all current touch focuses except for the specified listener, allowing for concurrent modification, and never
// cancel the same focus twice.
SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses;
TouchFocus[] items = touchFocuses.begin();
for (int i = 0, n = touchFocuses.size; i < n; i++) {
TouchFocus focus = items[i];
if (focus.listener == exceptListener && focus.listenerActor == exceptActor) continue;
if (!touchFocuses.removeValue(focus, true)) continue; // Touch focus already gone.
event.setTarget(focus.target);
event.setListenerActor(focus.listenerActor);
event.setPointer(focus.pointer);
event.setButton(focus.button);
focus.listener.handle(event);
// Cannot return TouchFocus to pool, as it may still be in use (eg if cancelTouchFocus is called from touchDragged).
}
touchFocuses.end();
Pools.free(event);
}
/** Adds an actor to the root of the stage.
* @see Group#addActor(Actor) */
public void addActor (Actor actor) {
root.addActor(actor);
}
/** Adds an action to the root of the stage.
* @see Group#addAction(Action) */
public void addAction (Action action) {
root.addAction(action);
}
/** Returns the root's child actors.
* @see Group#getChildren() */
public Array<Actor> getActors () {
return root.children;
}
/** Adds a listener to the root.
* @see Actor#addListener(EventListener) */
public boolean addListener (EventListener listener) {
return root.addListener(listener);
}
/** Removes a listener from the root.
* @see Actor#removeListener(EventListener) */
public boolean removeListener (EventListener listener) {
return root.removeListener(listener);
}
/** Adds a capture listener to the root.
* @see Actor#addCaptureListener(EventListener) */
public boolean addCaptureListener (EventListener listener) {
return root.addCaptureListener(listener);
}
/** Removes a listener from the root.
* @see Actor#removeCaptureListener(EventListener) */
public boolean removeCaptureListener (EventListener listener) {
return root.removeCaptureListener(listener);
}
/** Removes the root's children, actions, and listeners. */
public void clear () {
unfocusAll();
root.clear();
}
/** Removes the touch, keyboard, and scroll focused actors. */
public void unfocusAll () {
setScrollFocus(null);
setKeyboardFocus(null);
cancelTouchFocus();
}
/** Removes the touch, keyboard, and scroll focus for the specified actor and any descendants. */
public void unfocus (Actor actor) {
cancelTouchFocus(actor);
if (scrollFocus != null && scrollFocus.isDescendantOf(actor)) setScrollFocus(null);
if (keyboardFocus != null && keyboardFocus.isDescendantOf(actor)) setKeyboardFocus(null);
}
/** Sets the actor that will receive key events.
* @param actor May be null.
* @return true if the unfocus and focus events were not cancelled by a {@link FocusListener}. */
public boolean setKeyboardFocus (@Null Actor actor) {
if (keyboardFocus == actor) return true;
FocusEvent event = Pools.obtain(FocusEvent.class);
event.setStage(this);
event.setType(FocusEvent.Type.keyboard);
Actor oldKeyboardFocus = keyboardFocus;
if (oldKeyboardFocus != null) {
event.setFocused(false);
event.setRelatedActor(actor);
oldKeyboardFocus.fire(event);
}
boolean success = !event.isCancelled();
if (success) {
keyboardFocus = actor;
if (actor != null) {
event.setFocused(true);
event.setRelatedActor(oldKeyboardFocus);
actor.fire(event);
success = !event.isCancelled();
if (!success) keyboardFocus = oldKeyboardFocus;
}
}
Pools.free(event);
return success;
}
/** Gets the actor that will receive key events.
* @return May be null. */
public @Null Actor getKeyboardFocus () {
return keyboardFocus;
}
/** Sets the actor that will receive scroll events.
* @param actor May be null.
* @return true if the unfocus and focus events were not cancelled by a {@link FocusListener}. */
public boolean setScrollFocus (@Null Actor actor) {
if (scrollFocus == actor) return true;
FocusEvent event = Pools.obtain(FocusEvent.class);
event.setStage(this);
event.setType(FocusEvent.Type.scroll);
Actor oldScrollFocus = scrollFocus;
if (oldScrollFocus != null) {
event.setFocused(false);
event.setRelatedActor(actor);
oldScrollFocus.fire(event);
}
boolean success = !event.isCancelled();
if (success) {
scrollFocus = actor;
if (actor != null) {
event.setFocused(true);
event.setRelatedActor(oldScrollFocus);
actor.fire(event);
success = !event.isCancelled();
if (!success) scrollFocus = oldScrollFocus;
}
}
Pools.free(event);
return success;
}
/** Gets the actor that will receive scroll events.
* @return May be null. */
public @Null Actor getScrollFocus () {
return scrollFocus;
}
public Batch getBatch () {
return batch;
}
public Viewport getViewport () {
return viewport;
}
public void setViewport (Viewport viewport) {
this.viewport = viewport;
}
/** The viewport's world width. */
public float getWidth () {
return viewport.getWorldWidth();
}
/** The viewport's world height. */
public float getHeight () {
return viewport.getWorldHeight();
}
/** The viewport's camera. */
public Camera getCamera () {
return viewport.getCamera();
}
/** Returns the root group which holds all actors in the stage. */
public Group getRoot () {
return root;
}
/** Replaces the root group. This can be useful, for example, to subclass the root group to be notified by
* {@link Group#childrenChanged()}. */
public void setRoot (Group root) {
if (root.parent != null) root.parent.removeActor(root, false);
this.root = root;
root.setParent(null);
root.setStage(this);
}
/** Returns the {@link Actor} at the specified location in stage coordinates. Hit testing is performed in the order the actors
* were inserted into the stage, last inserted actors being tested first. To get stage coordinates from screen coordinates, use
* {@link #screenToStageCoordinates(Vector2)}.
* @param touchable If true, the hit detection will respect the {@link Actor#setTouchable(Touchable) touchability}.
* @return May be null if no actor was hit. */
public @Null Actor hit (float stageX, float stageY, boolean touchable) {
root.parentToLocalCoordinates(tempCoords.set(stageX, stageY));
return root.hit(tempCoords.x, tempCoords.y, touchable);
}
/** Transforms the screen coordinates to stage coordinates.
* @param screenCoords Input screen coordinates and output for resulting stage coordinates. */
public Vector2 screenToStageCoordinates (Vector2 screenCoords) {
viewport.unproject(screenCoords);
return screenCoords;
}
/** Transforms the stage coordinates to screen coordinates.
* @param stageCoords Input stage coordinates and output for resulting screen coordinates. */
public Vector2 stageToScreenCoordinates (Vector2 stageCoords) {
viewport.project(stageCoords);
stageCoords.y = Gdx.graphics.getHeight() - stageCoords.y;
return stageCoords;
}
/** Transforms the coordinates to screen coordinates. The coordinates can be anywhere in the stage since the transform matrix
* describes how to convert them. The transform matrix is typically obtained from {@link Batch#getTransformMatrix()} during
* {@link Actor#draw(Batch, float)}.
* @see Actor#localToStageCoordinates(Vector2) */
public Vector2 toScreenCoordinates (Vector2 coords, Matrix4 transformMatrix) {
return viewport.toScreenCoordinates(coords, transformMatrix);
}
/** Calculates window scissor coordinates from local coordinates using the batch's current transformation matrix.
* @see ScissorStack#calculateScissors(Camera, float, float, float, float, Matrix4, Rectangle, Rectangle) */
public void calculateScissors (Rectangle localRect, Rectangle scissorRect) {
Matrix4 transformMatrix;
if (debugShapes != null && debugShapes.isDrawing())
transformMatrix = debugShapes.getTransformMatrix();
else
transformMatrix = batch.getTransformMatrix();
viewport.calculateScissors(transformMatrix, localRect, scissorRect);
}
/** If true, any actions executed during a call to {@link #act()}) will result in a call to {@link Graphics#requestRendering()}
* . Widgets that animate or otherwise require additional rendering may check this setting before calling
* {@link Graphics#requestRendering()}. Default is true. */
public void setActionsRequestRendering (boolean actionsRequestRendering) {
this.actionsRequestRendering = actionsRequestRendering;
}
public boolean getActionsRequestRendering () {
return actionsRequestRendering;
}
/** The default color that can be used by actors to draw debug lines. */
public Color getDebugColor () {
return debugColor;
}
/** If true, debug lines are shown for actors even when {@link Actor#isVisible()} is false. */
public void setDebugInvisible (boolean debugInvisible) {
this.debugInvisible = debugInvisible;
}
/** If true, debug lines are shown for all actors. */
public void setDebugAll (boolean debugAll) {
if (this.debugAll == debugAll) return;
this.debugAll = debugAll;
if (debugAll)
debug = true;
else
root.setDebug(false, true);
}
public boolean isDebugAll () {
return debugAll;
}
/** If true, debug is enabled only for the actor under the mouse. Can be combined with {@link #setDebugAll(boolean)}. */
public void setDebugUnderMouse (boolean debugUnderMouse) {
if (this.debugUnderMouse == debugUnderMouse) return;
this.debugUnderMouse = debugUnderMouse;
if (debugUnderMouse)
debug = true;
else
root.setDebug(false, true);
}
/** If true, debug is enabled only for the parent of the actor under the mouse. Can be combined with
* {@link #setDebugAll(boolean)}. */
public void setDebugParentUnderMouse (boolean debugParentUnderMouse) {
if (this.debugParentUnderMouse == debugParentUnderMouse) return;
this.debugParentUnderMouse = debugParentUnderMouse;
if (debugParentUnderMouse)
debug = true;
else
root.setDebug(false, true);
}
/** If not {@link Debug#none}, debug is enabled only for the first ascendant of the actor under the mouse that is a table. Can
* be combined with {@link #setDebugAll(boolean)}.
* @param debugTableUnderMouse May be null for {@link Debug#none}. */
public void setDebugTableUnderMouse (@Null Debug debugTableUnderMouse) {
if (debugTableUnderMouse == null) debugTableUnderMouse = Debug.none;
if (this.debugTableUnderMouse == debugTableUnderMouse) return;
this.debugTableUnderMouse = debugTableUnderMouse;
if (debugTableUnderMouse != Debug.none)
debug = true;
else
root.setDebug(false, true);
}
/** If true, debug is enabled only for the first ascendant of the actor under the mouse that is a table. Can be combined with
* {@link #setDebugAll(boolean)}. */
public void setDebugTableUnderMouse (boolean debugTableUnderMouse) {
setDebugTableUnderMouse(debugTableUnderMouse ? Debug.all : Debug.none);
}
public void dispose () {
clear();
if (ownsBatch) batch.dispose();
}
/** Check if screen coordinates are inside the viewport's screen area. */
protected boolean isInsideViewport (int screenX, int screenY) {
int x0 = viewport.getScreenX();
int x1 = x0 + viewport.getScreenWidth();
int y0 = viewport.getScreenY();
int y1 = y0 + viewport.getScreenHeight();
screenY = Gdx.graphics.getHeight() - 1 - screenY;
return screenX >= x0 && screenX < x1 && screenY >= y0 && screenY < y1;
}
/** Internal class for managing touch focus. Public only for GWT.
* @author Nathan Sweet */
public static final class TouchFocus implements Poolable {
EventListener listener;
Actor listenerActor, target;
int pointer, button;
public void reset () {
listenerActor = null;
listener = null;
target = null;
}
}
}