/** * animate the card being added to the hand, and add it to the hand. Removes the card from its * current parent and adds it to this group. Adds it to the right of the current hand. * * @param toAdd cardgroup to add to the hand * @param index index to add the card at (left is 0) */ public void addCard(final CardGroup toAdd, int index) { // click events for the card toAdd.getListeners().clear(); toAdd.addListener( new DragListener() { public int returnIndex = -1; public float offsetX; public float offsetY; public boolean zoomed = false; @Override public void dragStart(InputEvent event, float x, float y, int pointer) { Vector2 coords = toAdd.localToParentCoordinates(new Vector2(x, y)); offsetX = coords.x - toAdd.getX(); offsetY = coords.y - toAdd.getY(); if (getTouchDownY() > y) { if (!toAdd.hasRotated) { toAdd.rotate(); toAdd.hasRotated = true; } } } @Override public void dragStop(InputEvent event, float x, float y, int pointer) { if (toAdd.dragged) { stopMakingSpace(); addCard(toAdd, returnIndex); } } @Override public void drag(InputEvent event, float x, float y, int pointer) { // Only start dragging the card if we are not dragging down or rotated if ((toAdd.dragged || getTouchDownY() <= y) && !toAdd.hasRotated) { if (toAdd.dragged == false) { toAdd.dragged = true; returnIndex = cards.getChildren().indexOf(toAdd, true); removeCard(toAdd); addActor(toAdd); } Vector2 coords = toAdd.localToParentCoordinates(new Vector2(x, y)); toAdd.setPosition(coords.x - offsetX, coords.y - offsetY); // check for making space in the hand boolean stillNeedsSpace = false; for (int i = 0; i < cards.getChildren().size; i++) { Actor card = cards.getChildren().get(i); Rectangle cardBounds = new Rectangle(card.getX(), card.getY(), card.getWidth(), card.getHeight()); Rectangle thisBounds = new Rectangle(toAdd.getX(), toAdd.getY(), toAdd.getWidth(), toAdd.getHeight()); if (cardBounds.overlaps(thisBounds)) { stillNeedsSpace = true; if (card != invisibleCard) { // figure out which side. If the center of the dragged card is to the right // of the center of the collided with card, make space to the right if ((thisBounds.getX() + thisBounds.getWidth() / 2) > (cardBounds.getX() + cardBounds.getWidth() / 2)) { returnIndex = i + 1; makeSpace(i + 1); } else { returnIndex = i; makeSpace(i); } } break; } } if (!stillNeedsSpace) { stopMakingSpace(); } } } }); toAdd.addListener( new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { toAdd.hasRotated = false; toAdd.dragged = false; return true; } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (!toAdd.dragged && !toAdd.hasRotated) { zoomCard(toAdd); } } }); // save the old positions of the cards List<Vector2> oldPositions = getPositions(cards.getChildren()); oldPositions.add(index, new Vector2(toAdd.getX(), toAdd.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, toAdd.getWidth())); newGroup.align(Align.bottom); cards.addActorAt(index, toAdd); 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)); } interpolateActorPositions( cards.getChildren(), oldPositions, newPositions, centerOffset, Interpolation.pow2, 0.5f); }