Reputation: 1482
I'm using a Screen
.
When I use the Table
class in my Stage
I want to show my Table
at the top left of the screen but per default it is shown at the top right of my screen.
Calling tableLeft.left()
moves my table to the top-center of the screen. Using tableLeft.top()
moves the tabe out of the screen complettly.
If I'm not using any viewports it works perfectly fine but then I'm running into other issues.
Also note that I'm reading about cameras and viewpoints for about two weeks now and I'm still confused as hell as how it works even after reading like all of the official libgdx tutorials on the wiki and reading everythign else about it I could google. For example I have no clue why I can remove everything from the resize() method and the output does not change at all.
My code looks like that:
@Override
public void show() {
....
camera = new OrthographicCamera(1920, 1080);
viewport = new FitViewport(1920, 1080, camera);
viewport.apply();
....
batch = new SpriteBatch();
....
stage = new Stage(viewport, batch);
Table tableLeft = new Table();
tableLeft.add(new Label("Dummy Label", skin, "default"));
stage.addActor(tableLeft);
.....
}
@Override
public void resize(int width, int height) {
viewport.update(width, height, true);
stage.getViewport().update(width, height, true);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
}
Upvotes: 2
Views: 740
Reputation: 541
I guess that you don't need to update viewport nor center camera manualy:
viewport.update(width, height, true);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
because
stage.getViewport().update(width, height, true);
already does it in your case:
/** Configures this viewport's screen bounds using the specified screen size and calls {@link #apply(boolean)}. Typically called
* from {@link ApplicationListener#resize(int, int)} or {@link Screen#resize(int, int)}.
* <p>
* The default implementation only calls {@link #apply(boolean)}. */
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
apply(centerCamera);
}
/** Applies the viewport to the camera and sets the glViewport.
* @param centerCamera If true, the camera position is set to the center of the world. */
public void apply (boolean centerCamera) {
Gdx.gl.glViewport(screenX, screenY, screenWidth, screenHeight);
camera.viewportWidth = worldWidth;
camera.viewportHeight = worldHeight;
if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);
camera.update();
}
Upvotes: 1