Scene dependent orientation behaviour

I have an app consisting of quite a few scenes (mostly related to settings/options) these scenes I’d like to be “portrait locked” (nor auto-rotate) when the device is rotates.

The main scene, however, I’d like to create so that it rotates and rearranges itself when the device is rotated.

Can this be accomplished? How should the build.settings file look like?

The orientation settings in build.settings are global for the whole app, meaning all your scenes will share the same settings.

You *might* be able to hack it in your scenes where you want to lock it by listening for the “orientation” event and forcing the rotation property of the scene to the orientation you want. I haven’t tried it myself, but it might just work.

I’ve tried something ala this:

local function onOrientationEvent( event ) local currentOrientation = event.type if (currentOrientation == "landscapeLeft") then print("set scene rotation to 90") scene.rotation = 90 end if (currentOrientation == "landscapeRight") then print("set scene rotation to 270") scene.rotation = 270 end return true end Runtime:addEventListener( "orientation", onOrientationEvent )

Unfortunately it didn’t seem to have any effect.

Maybe my request is very strange (scene dependent rotation behavior). It’s just that I’d rather not code rotation behaviour for 15 scenes, just because I’l like one to behave properly…  :slight_smile:

[quote=“runewinse,post:3,topic:325390”]

I’ve tried something ala this:

local function onOrientationEvent( event ) local currentOrientation = event.type if (currentOrientation == "landscapeLeft") then print("set scene rotation to 90") scene.rotation = 90 end if (currentOrientation == "landscapeRight") then print("set scene rotation to 270") scene.rotation = 270 end return true end Runtime:addEventListener( "orientation", onOrientationEvent )

Unfortunately it didn’t seem to have any effect.

Maybe my request is very strange (scene dependent rotation behavior). It’s just that I’d rather not code rotation behaviour for 15 scenes, just because I’l like one to behave properly… :slight_smile: [/quote]
Afaik you can’t rotate the scene group (iirc).

So maybe try creating a “master group” inserting everything into that instead of the scene group, then insert that “master group” into the scene group.

After all that, try rotating the “master group” instead of the scene group and see if it works.

If needed you could always create a reference to the master group too… Something like this in your create scene event

self.view.masterGroup = masterGroup

Hope this helps.

I just tried that and it didn’t behave as I hoped. But thanks, for the tip!

I’ll try with a minimized app later and see if I can make it work.

No problem :slight_smile:

What were the issues? Might be able to throw out some more suggestions for you.

I was playing around with this and got it to work.

I used the stage object for rotation. It’s a bit tricky since the stage rotates around the (0,0) upper-left point so you need to adjust the x, y properties after rotation.

In my test I allowed all 4 orientations in build.settings, with portrait being the default.

In the scene where I wanted to lock the scene in portrait mode I used the following code:

function scene:orientation(event) newOrientation = event.type; stage = display.getCurrentStage(); print(newOrientation) if (newOrientation == "landscapeLeft") then stage.rotation = 90; stage.x = display.contentWidth; stage.y = 0; elseif (newOrientation == "landscapeRight") then stage.rotation = -90; stage.x = 0; stage.y = display.contentHeight; elseif (newOrientation == "portrait") then stage.rotation = 0; stage.x = 0; stage.y = 0; elseif (newOrientation == "portraitUpsideDown") then stage.rotation = 180; stage.x = display.contentWidth; stage.y = display.contentHeight; end end -- ------------------------------------------------------------------------------- -- the normal scene listeners are setup here scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- new rotation listener Runtime:addEventListener( "orientation", scene )

This code is mostly a proof-of-concept, and can certainly be tweaked. But It works on device. (tested on an iPad)

Great! I will certainly check this out when I get the time. Unfortunately I cannot devote 100% of my weekend time to Corona and app development as I have to spend a few ours on my wife and kids also, but I’ll try this as soon as I get some “time off” again… 

Thanks again!

Hehe, i know how that is :wink:

Take a portrait photo in a frame and then rotate that photo in the frame to be landscape.  You will see it no longer “fits”.  This is one of the problems of just rotating your UI around in one fell swoop.  Your best bet is to detect the orientation events, and then redraw your UI based on the current orientation. 

This will likely be a challenging task.

Rob

Yes, you’re perfectly right, of course. I don’t know what I was thinking about, because I never even hoped that the UI would magically rotate and look good. I was planning to rearrange it myself all the time. My big misunderstanding was that I thought I had to enable both portrait and landscape in the build.settings file to be able to…

This listener worked just fine:

Runtime:addEventListener(“orientation”, onOrientationChange)

So I’m using it and I’m redrawing the UI as you say. This turned out to be easy peasy. I just had to do some planning. I divided the screen into grids which contains clusters of UI parts in a way that fits the display both ways.Then I created one display group per such UI cluster and when an orientation change is detected I just move/rotate these display groups (5 of them) and everything within falls nicely into place.

Thanks for all help! I feel that I’m getting a bit less helpless each day  :smiley:

The orientation settings in build.settings are global for the whole app, meaning all your scenes will share the same settings.

You *might* be able to hack it in your scenes where you want to lock it by listening for the “orientation” event and forcing the rotation property of the scene to the orientation you want. I haven’t tried it myself, but it might just work.

I’ve tried something ala this:

local function onOrientationEvent( event ) local currentOrientation = event.type if (currentOrientation == "landscapeLeft") then print("set scene rotation to 90") scene.rotation = 90 end if (currentOrientation == "landscapeRight") then print("set scene rotation to 270") scene.rotation = 270 end return true end Runtime:addEventListener( "orientation", onOrientationEvent )

Unfortunately it didn’t seem to have any effect.

Maybe my request is very strange (scene dependent rotation behavior). It’s just that I’d rather not code rotation behaviour for 15 scenes, just because I’l like one to behave properly…  :slight_smile:

[quote=“runewinse,post:13,topic:325390”]

I’ve tried something ala this:

local function onOrientationEvent( event ) local currentOrientation = event.type if (currentOrientation == "landscapeLeft") then print("set scene rotation to 90") scene.rotation = 90 end if (currentOrientation == "landscapeRight") then print("set scene rotation to 270") scene.rotation = 270 end return true end Runtime:addEventListener( "orientation", onOrientationEvent )

Unfortunately it didn’t seem to have any effect.

Maybe my request is very strange (scene dependent rotation behavior). It’s just that I’d rather not code rotation behaviour for 15 scenes, just because I’l like one to behave properly… :slight_smile: [/quote]
Afaik you can’t rotate the scene group (iirc).

So maybe try creating a “master group” inserting everything into that instead of the scene group, then insert that “master group” into the scene group.

After all that, try rotating the “master group” instead of the scene group and see if it works.

If needed you could always create a reference to the master group too… Something like this in your create scene event

self.view.masterGroup = masterGroup

Hope this helps.

I just tried that and it didn’t behave as I hoped. But thanks, for the tip!

I’ll try with a minimized app later and see if I can make it work.

No problem :slight_smile:

What were the issues? Might be able to throw out some more suggestions for you.

I was playing around with this and got it to work.

I used the stage object for rotation. It’s a bit tricky since the stage rotates around the (0,0) upper-left point so you need to adjust the x, y properties after rotation.

In my test I allowed all 4 orientations in build.settings, with portrait being the default.

In the scene where I wanted to lock the scene in portrait mode I used the following code:

function scene:orientation(event) newOrientation = event.type; stage = display.getCurrentStage(); print(newOrientation) if (newOrientation == "landscapeLeft") then stage.rotation = 90; stage.x = display.contentWidth; stage.y = 0; elseif (newOrientation == "landscapeRight") then stage.rotation = -90; stage.x = 0; stage.y = display.contentHeight; elseif (newOrientation == "portrait") then stage.rotation = 0; stage.x = 0; stage.y = 0; elseif (newOrientation == "portraitUpsideDown") then stage.rotation = 180; stage.x = display.contentWidth; stage.y = display.contentHeight; end end -- ------------------------------------------------------------------------------- -- the normal scene listeners are setup here scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- new rotation listener Runtime:addEventListener( "orientation", scene )

This code is mostly a proof-of-concept, and can certainly be tweaked. But It works on device. (tested on an iPad)

Great! I will certainly check this out when I get the time. Unfortunately I cannot devote 100% of my weekend time to Corona and app development as I have to spend a few ours on my wife and kids also, but I’ll try this as soon as I get some “time off” again… 

Thanks again!

Hehe, i know how that is :wink:

Take a portrait photo in a frame and then rotate that photo in the frame to be landscape.  You will see it no longer “fits”.  This is one of the problems of just rotating your UI around in one fell swoop.  Your best bet is to detect the orientation events, and then redraw your UI based on the current orientation. 

This will likely be a challenging task.

Rob