Java Code Examples for com.badlogic.gdx.scenes.scene2d.Actor#localToStageCoordinates()

The following examples show how to use com.badlogic.gdx.scenes.scene2d.Actor#localToStageCoordinates() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Scene2dUtils.java    From gdx-vfx with Apache License 2.0 5 votes vote down vote up
public static Rectangle localToStageBounds(Actor actor, float x0, float y0, float x1, float y1) {
    Rectangle stageRect = Scene2dUtils.tmpRect;
    actor.localToStageCoordinates(tmpVec2.set(x0, y0));
    stageRect.setX(tmpVec2.x);
    stageRect.setY(tmpVec2.y);
    actor.localToStageCoordinates(tmpVec2.set(x1, y1));
    stageRect.setWidth(tmpVec2.x - stageRect.x);
    stageRect.setHeight(tmpVec2.y - stageRect.y);
    return stageRect;
}
 
Example 2
Source File: GameScreen.java    From riiablo with Apache License 2.0 5 votes vote down vote up
public void setDetails(Actor details, Item item, Actor parent, Actor slot) {
  if (this.details == details) return;
  this.details = details;
  if (slot != null) {
    details.setPosition(slot.getX() + slot.getWidth() / 2, slot.getY() + slot.getHeight(), Align.bottom | Align.center);
    tmpVec2.set(details.getX(), details.getY());
    parent.localToStageCoordinates(tmpVec2);
    tmpVec2.x = MathUtils.clamp(tmpVec2.x, 0, stage.getWidth()  - details.getWidth());
    tmpVec2.y = MathUtils.clamp(tmpVec2.y, 0, stage.getHeight() - details.getHeight());
    details.setPosition(tmpVec2.x, tmpVec2.y);
    tmpVec2.set(slot.getX(), slot.getY());
    parent.localToStageCoordinates(tmpVec2);
    if (details.getY() < tmpVec2.y + slot.getHeight()) {
      details.setPosition(slot.getX() + slot.getWidth() / 2, slot.getY(), Align.top | Align.center);
      tmpVec2.set(details.getX(), details.getY());
      parent.localToStageCoordinates(tmpVec2);
      tmpVec2.x = MathUtils.clamp(tmpVec2.x, 0, stage.getWidth()  - details.getWidth());
      tmpVec2.y = MathUtils.clamp(tmpVec2.y, 0, stage.getHeight() - details.getHeight());
      details.setPosition(tmpVec2.x, tmpVec2.y);
    }
  } else {
    details.setPosition(item.getX() + item.getWidth() / 2, item.getY(), Align.top | Align.center);
    tmpVec2.set(details.getX(), details.getY());
    parent.localToStageCoordinates(tmpVec2);
    tmpVec2.x = MathUtils.clamp(tmpVec2.x, 0, stage.getWidth()  - details.getWidth());
    tmpVec2.y = MathUtils.clamp(tmpVec2.y, 0, stage.getHeight() - details.getHeight());
    details.setPosition(tmpVec2.x, tmpVec2.y);
  }
}
 
Example 3
Source File: ControllerMenuStage.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
protected boolean isActorHittable(Actor actor) {
    Vector2 center = actor.localToStageCoordinates(new Vector2(actor.getWidth() / 2, actor.getHeight() / 2));
    Actor hitActor = hit(center.x, center.y, true);
    return hitActor != null && (hitActor.isDescendantOf(actor));
}
 
Example 4
Source File: ControllerMenuStage.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
protected boolean isActorInViewportArea(Actor actor) {
    Vector2 leftBottom = actor.localToStageCoordinates(new Vector2(0, 0));
    Vector2 rightTop = actor.localToStageCoordinates(new Vector2(actor.getWidth(), actor.getHeight()));

    return !(leftBottom.x > getWidth() || leftBottom.y > getHeight() || rightTop.x < 0 || rightTop.y < 0);
}
 
Example 5
Source File: ControllerMenuStage.java    From gdx-controllerutils with Apache License 2.0 4 votes vote down vote up
private Actor findNearestFocusableNeighbour(MoveFocusDirection direction) {
    Vector2 focusedPosition = focusedActor.localToStageCoordinates(
            new Vector2(direction == MoveFocusDirection.east ? focusedActor.getWidth() :
                    direction == MoveFocusDirection.west ? 0 : focusedActor.getWidth() / 2,
                    direction == MoveFocusDirection.north ? focusedActor.getHeight() :
                            direction == MoveFocusDirection.south ? 0 : focusedActor.getHeight() / 2));

    // in Frage kommende raussuchen
    Actor nearestInDirection = null;
    float distance = Float.MAX_VALUE;

    for (int i = 0; i < focusableActors.size; i++) {
        Actor currentActor = focusableActors.get(i);

        if (currentActor != focusedActor && isActorFocusable(currentActor)
                && isActorInViewportArea(currentActor)) {
            Vector2 currentActorPos = currentActor.localToStageCoordinates(
                    new Vector2(direction == MoveFocusDirection.west ? currentActor.getWidth() :
                            direction == MoveFocusDirection.east ? 0 : currentActor.getWidth() / 2,
                            direction == MoveFocusDirection.south ? currentActor.getHeight() :
                                    direction == MoveFocusDirection.south ? 0 : currentActor.getHeight() / 2));

            boolean isInDirection = false;

            isInDirection = (direction == MoveFocusDirection.south && currentActorPos.y <= focusedPosition.y)
                    || (direction == MoveFocusDirection.north && currentActorPos.y >= focusedPosition.y)
                    || (direction == MoveFocusDirection.west && currentActorPos.x <= focusedPosition.x)
                    || (direction == MoveFocusDirection.east && currentActorPos.x >= focusedPosition.x);

            if (isInDirection && isActorHittable(currentActor)) {
                float currentDist = calcNeighbourDistance(direction, focusedPosition, currentActorPos);

                if (currentDist < distance) {
                    nearestInDirection = currentActor;
                    distance = currentDist;
                }
            }

        }
    }
    return nearestInDirection;
}
 
Example 6
Source File: ArrowForceClick.java    From dice-heroes with GNU General Public License v3.0 4 votes vote down vote up
@Override public void start(final Callback callback) {
    init();
    final Actor target = getTargetActor();
    final Image arrow = new Image(getArrowDrawable());
    final Table message = getMessageTable();
    final Stage stage = target.getStage();
    ArrowDirection direction = getDirection();
    if (stage == null)
        throw new IllegalStateException("target is not on stage");
    addListener(stage, target, arrow, message, callback);
    stage.addActor(arrow);
    stage.addActor(message);
    Vector2 screenPosition = target.localToStageCoordinates(new Vector2());
    switch (direction) {
        case left:
            arrow.setPosition(
                screenPosition.x - arrow.getPrefWidth() - getArrowOffset(),
                screenPosition.y + target.getHeight() / 2f - arrow.getPrefHeight() / 2f
            );
            message.setSize(arrow.getX() - getArrowOffset(), stage.getHeight());
            break;
        case right:
            arrow.setPosition(
                screenPosition.x + target.getWidth() + getArrowOffset(),
                screenPosition.y + target.getHeight() / 2f - arrow.getPrefHeight() / 2f
            );
            message.setSize(stage.getWidth() - arrow.getX() - arrow.getPrefWidth() - getArrowOffset(), stage.getHeight());
            message.setX(arrow.getX() + arrow.getPrefWidth() + getArrowOffset());
            break;
        case top:
            arrow.setPosition(
                screenPosition.x + target.getWidth() / 2f - arrow.getPrefWidth() / 2f,
                screenPosition.y + target.getHeight() + getArrowOffset()
            );
            message.setSize(stage.getWidth(), stage.getHeight() - arrow.getX() - arrow.getPrefHeight() - getArrowOffset());
            message.setY(arrow.getY() + arrow.getPrefHeight() + getArrowOffset());
            break;
        case bottom:
            arrow.setPosition(
                screenPosition.x + target.getWidth() / 2f - arrow.getPrefWidth() / 2f,
                screenPosition.y - arrow.getPrefHeight() - getArrowOffset()
            );
            message.setSize(stage.getWidth(), arrow.getY() - getArrowOffset());
            break;
        default:
            throw new IllegalStateException("unknown direction: " + direction);
    }

}