Stage2D scene script redefinition

In libgdx you are given a complex Scene2D graph with several Group's and Actor's . You want the user to select multiple Actors and draw them at the end so that they look focused on top of other Actors .

I would like to repeat the Stage loop twice. The first time he draws an unselected Actors , the second time he draws a selected Actors . However, I do not see a “good” way to enforce this behavior.

I would prefer the parameters to be clean. I would not want to copy the entire implementation of the method just for the sake of this small addition.

What does not work:

  • Actor's toFront () method works only for his siblings.
  • swap Actor's in the workspace: this changes the transformations of Actors .

Scenario to think about: You have a Root with a gA group and a gB group. Group gA contains two images iA1 and iA2. Group gB contains one iB image. Given that Group gA is first added to the scene and that the image of iA1 overlaps with iB; Now you want to select iA1 and make it display through iB. I do not want to just call gA.toFront (); this would put the whole Group gA at the front, which means that iA2 is in the foreground. Putting an iA2 in front has an undesirable effect of hiding parts of images inside Group gB

enter image description here

+8
java libgdx scene2d scenegraph
source share
1 answer

There are two solutions -

1 - Stop using [multiple] groups. Sucks, but it could be easier. If you look at how the rendering / drawing is done, you start with the root group for the stage and get its children and render them. For each of these children, if they are a Group, then draw that Group of children. ZIndex is nothing more than the order of children in a group. If you look at the setZIndex Actor, you will see why toFront or setZIndex affects only siblings.

 public void setZIndex (int index) { if (index < 0) throw new IllegalArgumentException("ZIndex cannot be < 0."); Group parent = this.parent; if (parent == null) return; Array<Actor> children = parent.getChildren(); if (children.size == 1) return; if (!children.removeValue(this, true)) return; if (index >= children.size) children.add(this); else children.insert(index, this); } 

2 - The only other option is to change the drawing order of all participants. You need to extend the Stage and replace the draw method with draw based on a different order of your choice. You will probably have to include many functions from the Group.drawChildren method.

TL; DR; The way everything is implemented in LibGDX - a group is a layer. If you do not want layers, then either change which groups do or stop using groups.

+1
source share

All Articles