user4959397
user4959397

Reputation:

LibGDX: Problems with getting Bounds

I have a Problem with getting x and y of a Table.

I have a menu that contains Cells of MenuItems. Now I whant to check if an touchDown outside of the table has happen with a sipmle AABB dedection. This works fine!

Now I whant to add subMenus outside of the Table and do table.addActor(subMenu). Now it doesnt works.

If i print x and y i get 1.0, 1.0 but this are not correct values. With table.getX(align) I get also bad values.

public void addItem (MenuItem item) {
    add(item).fillX().row();
    pack();
    item.addListener(menuItemListener);
    PopupMenu subMenu = item.getSubMenu();
    if (subMenu != null) {
        subMenu.setParent(this);
        subMenu.setStage(getStage());
    }
}

public void showMenu() {
    getParent().addActor(this);
    Stage stage = getStage();
    if (stage != null) stage.addListener(stageListener);
}

/** Hides this popupMenu and all its subMenus */
protected boolean hideMenu() {
    if (!getParent().getChildren().removeValue(this, true)) return false;
    Stage stage = getStage();
    if (stage != null) {
        stage.removeListener(stageListener);
        stage.unfocus(this);
    }
    childrenChanged();
    return true;
}


public boolean contains (float x, float y) {
    return getX() <= x && getX() + getWidth() >= x && getY() <= y && getY() + getHeight() >= y;
}

public boolean menuStructureContains (float x, float y) {
    return contains(x, y) || subMenu != null && subMenu.menuStructureContains(x, y);
}

I but I need the x and y of the Cells (getting the values of them doesnt work too).

EDIT: The position of the Menu gets changed by a other class. If I convert x, y of the Menu to stage and do not set its position it works.

If i set the potition it doubles the converted x / y value.

Upvotes: 1

Views: 579

Answers (1)

EssEllDee
EssEllDee

Reputation: 167

From here, simply create a class which gives you the location of the actor on the stage.

public static Vector2 getStageLocation(Actor actor) {
    return actor.localToStageCoordinates(new Vector2(0, 0));
}

The reason you need to use a new vector at (0,0) is because that is the bottom left corner of you actor. It will then convert this to the stage coordinates and give you the actual location of the actor.

Then you can do getStageLocation.x; and getStageLocation.y;

Hope this helps

Upvotes: 1

Related Questions