[SOLVED] orientation change on Android

Anything libgdx related goes here!

[SOLVED] orientation change on Android

Postby lucrus » Sun Apr 20, 2014 2:23 pm

Hello all,

my app is fixed portrait in AndroidManifest.xml. I use scene2d and I want a particular screen to render landscape and to get input coordinates as if the device were landscape. I'm using a FitViewport.

I've tried rotating the camera in the resize(w,h) method of my screen, but that seems not enough:

Code: Select all
            stage.getCamera().rotate(270, 0, 0, 1);


With only this in place I get this result (green circle highlights my viewport, around that there's only garbage): http://imgur.com/tocennK,82EA7Le,PTYE4hp#2
As you can see, my viewport seems limited by the screen width when the screen is portrait and does not extend to the available width of the landscape screen, causing my widgets to be cut (e.g. the "Play" red button). Moreover it is zoomed out to fit the world long edge in the screen short edge.

So I added a width/height values swap, and instead of calling viewport.update(w, h), I call viewport.update(h, w). Now I get this: http://imgur.com/tocennK,82EA7Le,PTYE4hp#1. The Viewport is correctly zoomed as if it were to fit the whole landscape screen, but is not centered and it's still cut as above.

Please help me understanding how I can I force scene2d render landscape in a portrait android app, thanks in advance.
Last edited by lucrus on Mon Apr 28, 2014 8:45 am, edited 1 time in total.
lucrus
 
Posts: 18
Joined: Sat Mar 29, 2014 6:12 am

Re: orientation change on Android

Postby BurningHand » Sun Apr 20, 2014 3:51 pm

Why don't you just change the orientation of the screen instead of rotating the camera? I have to mention, though, that changing the orientation for one screen is probably not the best idea.

Anyway, this is what I used to try out changing the screen rotation and it seems to work fine:

Code: Select all
package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;

    Viewport viewport;
    OrthographicCamera camera;

    OrientationHelper orientationHelper;

    public MyGdxGame(OrientationHelper orientationHelper) {
        this.orientationHelper = orientationHelper;
    }

    @Override
    public void create() {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
        camera = new OrthographicCamera();
        viewport = new FitViewport(800, 480, camera);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(img, 0, 0);
        batch.end();

        if (Gdx.input.justTouched()) {
            switch (orientationHelper.getOrientation()) {
                case LANDSCAPE: {
                    orientationHelper.requestOrientation(OrientationHelper.Orientation.PORTRAIT);
                    break;
                }
                case PORTRAIT: {
                    orientationHelper.requestOrientation(OrientationHelper.Orientation.LANDSCAPE);
                    break;
                }
            }

        }
    }

    @Override
    public void resize(int width, int height) {
        if (width > height && viewport.getWorldWidth() < viewport.getWorldHeight()) {
            viewport.setWorldSize(800, 480);
        }
        if (width < height && viewport.getWorldWidth() > viewport.getWorldHeight()) {
            viewport.setWorldSize(480, 800);
        }
        viewport.update(width, height, true);
    }

    public static interface OrientationHelper {

        public static enum Orientation {
            LANDSCAPE,
            PORTRAIT
        }

        public boolean requestOrientation(Orientation orientation);

        public Orientation getOrientation();
    }

}
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: orientation change on Android

Postby lucrus » Sun Apr 20, 2014 4:39 pm

BurningHand wrote:Why don't you just change the orientation of the screen instead of rotating the camera? I have to mention, though, that changing the orientation for one screen is probably not the best idea.


Because:
1. it's not the best idea
2. in IRC #libgdx channel they suggested me not to change the orientation but rotate the camera instead
3. changing orientation does not work for all devices in my case, at least it does not work on gt-i9505 while it does work on gt-i9000 (black screen after orientation change on the gt-i9505). Since it's not the best way to go, I decided to rotate the camera instead of digging into why setRequestedOrientation() renders the screen black on the gt-i9505.
lucrus
 
Posts: 18
Joined: Sat Mar 29, 2014 6:12 am

Re: orientation change on Android

Postby BurningHand » Sun Apr 20, 2014 4:56 pm

It works on every device - if it doesn't you did it wrong. But ok.

Changing the orientation is the thing that is not the best idea - whether you do it via setRequestedOrientation or by rotating the camera.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: orientation change on Android

Postby lucrus » Sun Apr 20, 2014 7:20 pm

BurningHand wrote:It works on every device - if it doesn't you did it wrong. But ok.

Changing the orientation is the thing that is not the best idea - whether you do it via setRequestedOrientation or by rotating the camera.


Please let's stick to the problem, everyone has a boss and I'm not in charge of deciding wether the app should rotate or not. The question is simple, maybe the answer is not, but you can help me only if you help me find that answer, not if you answer some other question I never placed.

So the question is: how do I force orientation change in scene2d without using setRequestedOrientation() (which, btw, is not portable and forces the use of an additional helper interface)?
lucrus
 
Posts: 18
Joined: Sat Mar 29, 2014 6:12 am

Re: orientation change on Android

Postby BurningHand » Sun Apr 20, 2014 7:31 pm

Set the world size of the viewport, as is shown in the example I already posted.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: orientation change on Android

Postby lucrus » Sun Apr 20, 2014 8:01 pm

BurningHand wrote:Set the world size of the viewport, as is shown in the example I already posted.


I'm already doing that, except my world units do not match screen units, but I get the results you can see in the posted screenshots. Maybe scene2d or FitViewport require some additional trick?
lucrus
 
Posts: 18
Joined: Sat Mar 29, 2014 6:12 am

Re: orientation change on Android

Postby lucrus » Mon Apr 21, 2014 7:40 am

Correction: I'm not doing that at all, but I'm not doing that because that's not what I need to do. The code you posted adapts the world orientation to the screen orientation. It works because you call setRequestedOrientation(), which causes a resize() call. In my case I need to rotate the screen without using setRequestedOrientation, so the width and height I receive in the resize() call will always be portrait. I'm actually swapping width and height world values though.

Anyway, I try to create a sample project and report back.
lucrus
 
Posts: 18
Joined: Sat Mar 29, 2014 6:12 am

Re: orientation change on Android

Postby lucrus » Mon Apr 21, 2014 9:22 am

Here is a sample project showing what I'm trying to achieve. The logo.png image is 300px wide, the FitViewport is always 300px wide (world units), be it portrait or landscape. In portrait it is 450 units tall, and in landscape it is (er... should be) 200 units tall. I'd expect the logo to fit the screen width both when in landscape and portrait, with upscaling if the display has more than 300 pixels. The listener toggles orientation every 3 seconds.

In portrait mode (which is the one requested in AndroidManifest.xml) it happens what I expected. In landscape mode it happens what you can see in the screenshots of my first post: the drawable area does not extend to the full landscape width of the screen (so the FitViewport does not really fit the landscape screen, but it's fitting the portrait screen, then the camera rotates it, but I'd like the viewport to fit the screen and the "fit" action take into account camera rotation).
Attachments
OrientationExample.zip
(84.89 KiB) Downloaded 260 times
lucrus
 
Posts: 18
Joined: Sat Mar 29, 2014 6:12 am

[SOLVED] orientation change on Android

Postby lucrus » Mon Apr 28, 2014 8:45 am

Just for the record, here is how I finally solved this problem, thanks to suggestions received in this forum and on the IRC channel.
lucrus
 
Posts: 18
Joined: Sat Mar 29, 2014 6:12 am


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], gadw and 1 guest