/** * makes space for a card (adds an invisible card) * * @param index index to make space for a card at */ private void makeSpace(int index) { if (cards.getChildren().size > 0) { stopMakingSpace(); invisibleCard = new Group(); invisibleCard.setBounds( 0, 0, cards.getChildren().get(0).getWidth(), cards.getChildren().get(0).getHeight()); // save the old positions of the cards List<Vector2> oldPositions = getPositions(cards.getChildren()); oldPositions.add(index, new Vector2(invisibleCard.getX(), invisibleCard.getY())); // Determine what the new positions would be // by adding them to a horizontal group and checking their positions HorizontalGroup newGroup = new HorizontalGroup() .space(calculateSpacing(cards.getChildren().size + 1, invisibleCard.getWidth())); newGroup.align(Align.bottom); cards.addActorAt(index, invisibleCard); while (cards.getChildren().size > 0) { newGroup.addActor(cards.getChildren().get(0)); } newGroup.layout(); List<Vector2> newPositions = getPositions(newGroup.getChildren()); // calculate what is needed to center the cards float centerOffset = UIConstants.WORLD_WIDTH / 2 - newGroup.getPrefWidth() / 2; // remove them from the horizontal group and add them back to the normal group so it doesn't // try to move them around while (newGroup.getChildren().size > 0) { cards.addActor(newGroup.getChildren().get(0)); } // interpolate all but the invisible card cards .getChildren() .get(index) .setPosition(newPositions.get(index).x, newPositions.get(index).y); cards.removeActor(invisibleCard); oldPositions.remove(index); newPositions.remove(index); interpolateActorPositions( cards.getChildren(), oldPositions, newPositions, centerOffset, Interpolation.linear, 0.2f); cards.addActor(invisibleCard); } }
@Override public void act(float delta) { // coordinate fix // TODO to STAGE COORDS for relativ coords float relativeX = getX(); float relativeY = getY(); float relativeRotation = 0; Group currParent = this.getParent(); while (currParent != null) { relativeX += currParent.getX(); relativeY += currParent.getY(); relativeRotation += currParent.getRotation(); currParent = currParent.getParent(); } if (lightObject != null) { float yy = 0; float xx = 0; if (relativeRotation != 0) { xx = getX() * MathUtils.cosDeg(relativeRotation) - getY() * MathUtils.sinDeg(relativeRotation); yy = getY() * MathUtils.cosDeg(relativeRotation) + getX() * MathUtils.sinDeg(relativeRotation); yy = getY() - yy; xx = getX() - xx; } lightObject.setPosition(relativeX - xx, relativeY - yy); } if (dataVO.type == LightType.CONE) { lightObject.setDirection(direction + relativeRotation); } // debugImg.setX(relativeX); // debugImg.setY(relativeY); super.act(delta); }
/** 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 render() { Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); if (Gdx.input.isTouched()) { stage.screenToStageCoordinates(stageCoords.set(Gdx.input.getX(), Gdx.input.getY())); Actor actor = stage.hit(stageCoords.x, stageCoords.y, true); if (actor instanceof Image) ((Image) actor) .setColor( (float) Math.random(), (float) Math.random(), (float) Math.random(), 0.5f + 0.5f * (float) Math.random()); } Array<Actor> actors = stage.getActors(); int len = actors.size; if (rotateSprites) { for (int i = 0; i < len; i++) actors.get(i).rotateBy(Gdx.graphics.getDeltaTime() * 10); } scale += vScale * Gdx.graphics.getDeltaTime(); if (scale > 1) { scale = 1; vScale = -vScale; } if (scale < 0.5f) { scale = 0.5f; vScale = -vScale; } len = images.size(); for (int i = 0; i < len; i++) { Image img = images.get(i); if (rotateSprites) img.rotateBy(-40 * Gdx.graphics.getDeltaTime()); else img.setRotation(0); if (scaleSprites) { img.setScale(scale); } else { img.setScale(1); } img.invalidate(); } stage.draw(); renderer.begin(ShapeType.Point); renderer.setColor(1, 0, 0, 1); len = actors.size; for (int i = 0; i < len; i++) { Group group = (Group) actors.get(i); renderer.point(group.getX() + group.getOriginX(), group.getY() + group.getOriginY(), 0); } renderer.end(); fps.setText( "fps: " + Gdx.graphics.getFramesPerSecond() + ", actors " + images.size() + ", groups " + actors.size); ui.draw(); }