/
Group.java
513 lines (467 loc) · 18.6 KB
/
Group.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
/*******************************************************************************
* 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.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Affine2;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.utils.Cullable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Null;
import com.badlogic.gdx.utils.SnapshotArray;
/** 2D scene graph node that may contain other actors.
* <p>
* Actors have a z-order equal to the order they were inserted into the group. Actors inserted later will be drawn on top of
* actors added earlier. Touch events that hit more than one actor are distributed to topmost actors first.
* @author mzechner
* @author Nathan Sweet */
public class Group extends Actor implements Cullable {
static private final Vector2 tmp = new Vector2();
final SnapshotArray<Actor> children = new SnapshotArray(true, 4, Actor.class);
private final Affine2 worldTransform = new Affine2();
private final Matrix4 computedTransform = new Matrix4();
private final Matrix4 oldTransform = new Matrix4();
boolean transform = true;
private @Null Rectangle cullingArea;
public void act (float delta) {
super.act(delta);
Actor[] actors = children.begin();
for (int i = 0, n = children.size; i < n; i++)
actors[i].act(delta);
children.end();
}
/** Draws the group and its children. The default implementation calls {@link #applyTransform(Batch, Matrix4)} if needed, then
* {@link #drawChildren(Batch, float)}, then {@link #resetTransform(Batch)} if needed. */
public void draw (Batch batch, float parentAlpha) {
if (transform) applyTransform(batch, computeTransform());
drawChildren(batch, parentAlpha);
if (transform) resetTransform(batch);
}
/** Draws all children. {@link #applyTransform(Batch, Matrix4)} should be called before and {@link #resetTransform(Batch)}
* after this method if {@link #setTransform(boolean) transform} is true. If {@link #setTransform(boolean) transform} is false
* these methods don't need to be called, children positions are temporarily offset by the group position when drawn. This
* method avoids drawing children completely outside the {@link #setCullingArea(Rectangle) culling area}, if set. */
protected void drawChildren (Batch batch, float parentAlpha) {
parentAlpha *= this.color.a;
SnapshotArray<Actor> children = this.children;
Actor[] actors = children.begin();
Rectangle cullingArea = this.cullingArea;
if (cullingArea != null) {
// Draw children only if inside culling area.
float cullLeft = cullingArea.x;
float cullRight = cullLeft + cullingArea.width;
float cullBottom = cullingArea.y;
float cullTop = cullBottom + cullingArea.height;
if (transform) {
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible()) continue;
float cx = child.x, cy = child.y;
if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom)
child.draw(batch, parentAlpha);
}
} else {
// No transform for this group, offset each child.
float offsetX = x, offsetY = y;
x = 0;
y = 0;
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible()) continue;
float cx = child.x, cy = child.y;
if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom) {
child.x = cx + offsetX;
child.y = cy + offsetY;
child.draw(batch, parentAlpha);
child.x = cx;
child.y = cy;
}
}
x = offsetX;
y = offsetY;
}
} else {
// No culling, draw all children.
if (transform) {
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible()) continue;
child.draw(batch, parentAlpha);
}
} else {
// No transform for this group, offset each child.
float offsetX = x, offsetY = y;
x = 0;
y = 0;
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible()) continue;
float cx = child.x, cy = child.y;
child.x = cx + offsetX;
child.y = cy + offsetY;
child.draw(batch, parentAlpha);
child.x = cx;
child.y = cy;
}
x = offsetX;
y = offsetY;
}
}
children.end();
}
/** Draws this actor's debug lines if {@link #getDebug()} is true and, regardless of {@link #getDebug()}, calls
* {@link Actor#drawDebug(ShapeRenderer)} on each child. */
public void drawDebug (ShapeRenderer shapes) {
drawDebugBounds(shapes);
if (transform) applyTransform(shapes, computeTransform());
drawDebugChildren(shapes);
if (transform) resetTransform(shapes);
}
/** Draws all children. {@link #applyTransform(Batch, Matrix4)} should be called before and {@link #resetTransform(Batch)}
* after this method if {@link #setTransform(boolean) transform} is true. If {@link #setTransform(boolean) transform} is false
* these methods don't need to be called, children positions are temporarily offset by the group position when drawn. This
* method avoids drawing children completely outside the {@link #setCullingArea(Rectangle) culling area}, if set. */
protected void drawDebugChildren (ShapeRenderer shapes) {
SnapshotArray<Actor> children = this.children;
Actor[] actors = children.begin();
// No culling, draw all children.
if (transform) {
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible()) continue;
if (!child.getDebug() && !(child instanceof Group)) continue;
child.drawDebug(shapes);
}
shapes.flush();
} else {
// No transform for this group, offset each child.
float offsetX = x, offsetY = y;
x = 0;
y = 0;
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible()) continue;
if (!child.getDebug() && !(child instanceof Group)) continue;
float cx = child.x, cy = child.y;
child.x = cx + offsetX;
child.y = cy + offsetY;
child.drawDebug(shapes);
child.x = cx;
child.y = cy;
}
x = offsetX;
y = offsetY;
}
children.end();
}
/** Returns the transform for this group's coordinate system. */
protected Matrix4 computeTransform () {
Affine2 worldTransform = this.worldTransform;
float originX = this.originX, originY = this.originY;
worldTransform.setToTrnRotScl(x + originX, y + originY, rotation, scaleX, scaleY);
if (originX != 0 || originY != 0) worldTransform.translate(-originX, -originY);
// Find the first parent that transforms.
Group parentGroup = parent;
while (parentGroup != null) {
if (parentGroup.transform) break;
parentGroup = parentGroup.parent;
}
if (parentGroup != null) worldTransform.preMul(parentGroup.worldTransform);
computedTransform.set(worldTransform);
return computedTransform;
}
/** Set the batch's transformation matrix, often with the result of {@link #computeTransform()}. Note this causes the batch to
* be flushed. {@link #resetTransform(Batch)} will restore the transform to what it was before this call. */
protected void applyTransform (Batch batch, Matrix4 transform) {
oldTransform.set(batch.getTransformMatrix());
batch.setTransformMatrix(transform);
}
/** Restores the batch transform to what it was before {@link #applyTransform(Batch, Matrix4)}. Note this causes the batch to
* be flushed. */
protected void resetTransform (Batch batch) {
batch.setTransformMatrix(oldTransform);
}
/** Set the shape renderer transformation matrix, often with the result of {@link #computeTransform()}. Note this causes the
* shape renderer to be flushed. {@link #resetTransform(ShapeRenderer)} will restore the transform to what it was before this
* call. */
protected void applyTransform (ShapeRenderer shapes, Matrix4 transform) {
oldTransform.set(shapes.getTransformMatrix());
shapes.setTransformMatrix(transform);
shapes.flush();
}
/** Restores the shape renderer transform to what it was before {@link #applyTransform(Batch, Matrix4)}. Note this causes the
* shape renderer to be flushed. */
protected void resetTransform (ShapeRenderer shapes) {
shapes.setTransformMatrix(oldTransform);
}
/** Children completely outside of this rectangle will not be drawn. This is only valid for use with unrotated and unscaled
* actors.
* @param cullingArea May be null. */
public void setCullingArea (@Null Rectangle cullingArea) {
this.cullingArea = cullingArea;
}
/** @return May be null.
* @see #setCullingArea(Rectangle) */
public @Null Rectangle getCullingArea () {
return cullingArea;
}
public @Null Actor hit (float x, float y, boolean touchable) {
if (touchable && getTouchable() == Touchable.disabled) return null;
if (!isVisible()) return null;
Vector2 point = tmp;
Actor[] childrenArray = children.items;
for (int i = children.size - 1; i >= 0; i--) {
Actor child = childrenArray[i];
child.parentToLocalCoordinates(point.set(x, y));
Actor hit = child.hit(point.x, point.y, touchable);
if (hit != null) return hit;
}
return super.hit(x, y, touchable);
}
/** Called when actors are added to or removed from the group. */
protected void childrenChanged () {
}
/** Adds an actor as a child of this group, removing it from its previous parent. If the actor is already a child of this
* group, no changes are made. */
public void addActor (Actor actor) {
if (actor.parent != null) {
if (actor.parent == this) return;
actor.parent.removeActor(actor, false);
}
children.add(actor);
actor.setParent(this);
actor.setStage(getStage());
childrenChanged();
}
/** Adds an actor as a child of this group at a specific index, removing it from its previous parent. If the actor is already a
* child of this group, no changes are made.
* @param index May be greater than the number of children. */
public void addActorAt (int index, Actor actor) {
if (actor.parent != null) {
if (actor.parent == this) return;
actor.parent.removeActor(actor, false);
}
if (index >= children.size)
children.add(actor);
else
children.insert(index, actor);
actor.setParent(this);
actor.setStage(getStage());
childrenChanged();
}
/** Adds an actor as a child of this group immediately before another child actor, removing it from its previous parent. If the
* actor is already a child of this group, no changes are made. */
public void addActorBefore (Actor actorBefore, Actor actor) {
if (actor.parent != null) {
if (actor.parent == this) return;
actor.parent.removeActor(actor, false);
}
int index = children.indexOf(actorBefore, true);
children.insert(index, actor);
actor.setParent(this);
actor.setStage(getStage());
childrenChanged();
}
/** Adds an actor as a child of this group immediately after another child actor, removing it from its previous parent. If the
* actor is already a child of this group, no changes are made. If <code>actorAfter</code> is not in this group, the actor is
* added as the last child. */
public void addActorAfter (Actor actorAfter, Actor actor) {
if (actor.parent != null) {
if (actor.parent == this) return;
actor.parent.removeActor(actor, false);
}
int index = children.indexOf(actorAfter, true);
if (index == children.size || index == -1)
children.add(actor);
else
children.insert(index + 1, actor);
actor.setParent(this);
actor.setStage(getStage());
childrenChanged();
}
/** Removes an actor from this group and unfocuses it. Calls {@link #removeActor(Actor, boolean)} with true. */
public boolean removeActor (Actor actor) {
return removeActor(actor, true);
}
/** Removes an actor from this group. Calls {@link #removeActorAt(int, boolean)} with the actor's child index. */
public boolean removeActor (Actor actor, boolean unfocus) {
int index = children.indexOf(actor, true);
if (index == -1) return false;
removeActorAt(index, unfocus);
return true;
}
/** Removes an actor from this group. If the actor will not be used again and has actions, they should be
* {@link Actor#clearActions() cleared} so the actions will be returned to their
* {@link Action#setPool(com.badlogic.gdx.utils.Pool) pool}, if any. This is not done automatically.
* @param unfocus If true, {@link Stage#unfocus(Actor)} is called.
* @return the actor removed from this group. */
public Actor removeActorAt (int index, boolean unfocus) {
Actor actor = children.removeIndex(index);
Stage stage = getStage();
if (stage != null) {
if (unfocus) stage.unfocus(actor);
stage.actorRemoved(actor);
}
actor.setParent(null);
actor.setStage(null);
childrenChanged();
return actor;
}
/** Removes all actors from this group and unfocuses them. Calls {@link #clearChildren(boolean)} with true. */
public void clearChildren () {
clearChildren(true);
}
/** Removes all actors from this group. */
public void clearChildren (boolean unfocus) {
Actor[] actors = children.begin();
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (unfocus) {
Stage stage = getStage();
if (stage != null) stage.unfocus(child);
}
child.setStage(null);
child.setParent(null);
}
children.end();
children.clear();
childrenChanged();
}
/** Removes all children, actions, and listeners from this group. The children are unfocused. */
public void clear () {
super.clear();
clearChildren(true);
}
/** Removes all children, actions, and listeners from this group. */
public void clear (boolean unfocus) {
super.clear();
clearChildren(unfocus);
}
/** Returns the first actor found with the specified name. Note this recursively compares the name of every actor in the
* group. */
public @Null <T extends Actor> T findActor (String name) {
Array<Actor> children = this.children;
for (int i = 0, n = children.size; i < n; i++)
if (name.equals(children.get(i).getName())) return (T)children.get(i);
for (int i = 0, n = children.size; i < n; i++) {
Actor child = children.get(i);
if (child instanceof Group) {
Actor actor = ((Group)child).findActor(name);
if (actor != null) return (T)actor;
}
}
return null;
}
protected void setStage (Stage stage) {
super.setStage(stage);
Actor[] childrenArray = children.items;
for (int i = 0, n = children.size; i < n; i++)
childrenArray[i].setStage(stage); // StackOverflowError here means the group is its own ascendant.
}
/** Swaps two actors by index. Returns false if the swap did not occur because the indexes were out of bounds. */
public boolean swapActor (int first, int second) {
int maxIndex = children.size;
if (first < 0 || first >= maxIndex) return false;
if (second < 0 || second >= maxIndex) return false;
children.swap(first, second);
return true;
}
/** Swaps two actors. Returns false if the swap did not occur because the actors are not children of this group. */
public boolean swapActor (Actor first, Actor second) {
int firstIndex = children.indexOf(first, true);
int secondIndex = children.indexOf(second, true);
if (firstIndex == -1 || secondIndex == -1) return false;
children.swap(firstIndex, secondIndex);
return true;
}
/** Returns the child at the specified index. */
public Actor getChild (int index) {
return children.get(index);
}
/** Returns an ordered list of child actors in this group. */
public SnapshotArray<Actor> getChildren () {
return children;
}
public boolean hasChildren () {
return children.size > 0;
}
/** When true (the default), the Batch is transformed so children are drawn in their parent's coordinate system. This has a
* performance impact because {@link Batch#flush()} must be done before and after the transform. If the actors in a group are
* not rotated or scaled, then the transform for the group can be set to false. In this case, each child's position will be
* offset by the group's position for drawing, causing the children to appear in the correct location even though the Batch has
* not been transformed. */
public void setTransform (boolean transform) {
this.transform = transform;
}
public boolean isTransform () {
return transform;
}
/** Converts coordinates for this group to those of a descendant actor. The descendant does not need to be an immediate child.
* @throws IllegalArgumentException if the specified actor is not a descendant of this group. */
public Vector2 localToDescendantCoordinates (Actor descendant, Vector2 localCoords) {
Group parent = descendant.parent;
if (parent == null) throw new IllegalArgumentException("Actor is not a descendant: " + descendant);
// First convert to the actor's parent coordinates.
if (parent != this) localToDescendantCoordinates(parent, localCoords);
// Then from each parent down to the descendant.
descendant.parentToLocalCoordinates(localCoords);
return localCoords;
}
/** If true, {@link #drawDebug(ShapeRenderer)} will be called for this group and, optionally, all children recursively. */
public void setDebug (boolean enabled, boolean recursively) {
setDebug(enabled);
if (recursively) {
for (Actor child : children) {
if (child instanceof Group) {
((Group)child).setDebug(enabled, recursively);
} else {
child.setDebug(enabled);
}
}
}
}
/** Calls {@link #setDebug(boolean, boolean)} with {@code true, true}. */
public Group debugAll () {
setDebug(true, true);
return this;
}
/** Returns a description of the actor hierarchy, recursively. */
public String toString () {
StringBuilder buffer = new StringBuilder(128);
toString(buffer, 1);
buffer.setLength(buffer.length() - 1);
return buffer.toString();
}
void toString (StringBuilder buffer, int indent) {
buffer.append(super.toString());
buffer.append('\n');
Actor[] actors = children.begin();
for (int i = 0, n = children.size; i < n; i++) {
for (int ii = 0; ii < indent; ii++)
buffer.append("| ");
Actor actor = actors[i];
if (actor instanceof Group)
((Group)actor).toString(buffer, indent + 1);
else {
buffer.append(actor);
buffer.append('\n');
}
}
children.end();
}
}