/** * @param group an empty group to be the parent * @return the group with the current selection */ public Group createGroup(Group group) { // New group has the same transformation as this group.setBounds(getX(), getY(), getWidth(), getHeight()); group.setOrigin(getOriginX(), getOriginY()); group.setRotation(getRotation()); group.setScale(getScaleX(), getScaleY()); // Each children in the group must be contained by the new group Array<Actor> children = getChildren(); children.sort( new Comparator<Actor>() { @Override public int compare(Actor actor, Actor actor2) { return ((SelectionGhost) actor).getRepresentedActor().getZIndex() - ((SelectionGhost) actor2).getRepresentedActor().getZIndex(); } }); for (Actor actor : children) { SelectionGhost ghost = (SelectionGhost) actor; Actor representedActor = ghost.getRepresentedActor(); representedActor.setPosition(ghost.getX(), ghost.getY()); representedActor.setRotation(ghost.getRotation()); representedActor.setScale(ghost.getScaleX(), ghost.getScaleY()); group.addActor(representedActor); } return group; }
/** Adjusts the position and size of the given group to its children */ public static void adjustGroup(Actor root) { if (!(root instanceof Group)) { return; } Group group = (Group) root; if (group.getChildren().size == 0) { return; } Vector2 origin = Pools.obtain(Vector2.class); Vector2 size = Pools.obtain(Vector2.class); Vector2 tmp3 = Pools.obtain(Vector2.class); Vector2 tmp4 = Pools.obtain(Vector2.class); calculateBounds(group.getChildren(), origin, size); /* * minX and minY are the new origin (new 0, 0), so everything inside the * group must be translated that much. */ for (Actor actor : group.getChildren()) { actor.setPosition(actor.getX() - origin.x, actor.getY() - origin.y); } /* * Now, we calculate the current origin (0, 0) and the new origin (minX, * minY), and group is translated by that difference. */ group.localToParentCoordinates(tmp3.set(0, 0)); group.localToParentCoordinates(tmp4.set(origin.x, origin.y)); tmp4.sub(tmp3); group.setBounds(group.getX() + tmp4.x, group.getY() + tmp4.y, size.x, size.y); group.setOrigin(size.x / 2.0f, size.y / 2.0f); Pools.free(origin); Pools.free(size); Pools.free(tmp3); Pools.free(tmp4); }
@Override public void create() { texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg")); texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false); stage = new Stage(); float loc = (NUM_SPRITES * (32 + SPACING) - SPACING) / 2; for (int i = 0; i < NUM_GROUPS; i++) { Group group = new Group(); group.setX((float) Math.random() * (stage.getWidth() - NUM_SPRITES * (32 + SPACING))); group.setY((float) Math.random() * (stage.getHeight() - NUM_SPRITES * (32 + SPACING))); group.setOrigin(loc, loc); fillGroup(group, texture); stage.addActor(group); } uiTexture = new Texture(Gdx.files.internal("data/ui.png")); uiTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); ui = new Stage(); Image blend = new Image(new TextureRegion(uiTexture, 0, 0, 64, 32)); blend.setAlign(Align.center); blend.setScaling(Scaling.none); blend.addListener( new InputListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (stage.getSpriteBatch().isBlendingEnabled()) stage.getSpriteBatch().disableBlending(); else stage.getSpriteBatch().enableBlending(); return true; } }); blend.setY(ui.getHeight() - 64); Image rotate = new Image(new TextureRegion(uiTexture, 64, 0, 64, 32)); rotate.setAlign(Align.center); rotate.setScaling(Scaling.none); rotate.addListener( new InputListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { rotateSprites = !rotateSprites; return true; } }); rotate.setPosition(64, blend.getY()); Image scale = new Image(new TextureRegion(uiTexture, 64, 32, 64, 32)); scale.setAlign(Align.center); scale.setScaling(Scaling.none); scale.addListener( new InputListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { scaleSprites = !scaleSprites; return true; } }); scale.setPosition(128, blend.getY()); ui.addActor(blend); ui.addActor(rotate); ui.addActor(scale); fps = new Label("fps: 0", new Label.LabelStyle(font, Color.WHITE)); fps.setPosition(10, 30); fps.setColor(0, 1, 0, 1); ui.addActor(fps); renderer = new ShapeRenderer(); Gdx.input.setInputProcessor(this); }