Cannot get Viewports to work on iOS

brbbrb AUMember ✭✭

I am trying to get ViewPorts to work without success. I looked in the Test Harnesses on GitHub and found that there is no explicit test for multiple ViewPorts. I am trying to set the viewport bounds to be 50:50 scaled horizontally and full-height vertically.
My intention is to set up two distinct animation areas with their own scenes. My test code is trying to place a monkey image in each Viewport and scale it to the dimensions of the available visible bounds.
I note that the layer's VisibleBoundsWorldspace size is that of the CCWindow and has not been altered in line with the scaling set up with Viewport. So it seems like the Viewport values are not being used with the CCScene when it is created.

I stripped down my test code to be (apologies but the code does not present properly for some reason) :

` public override void ApplicationDidFinishLaunching (CCApplication application, CCWindow mainWindow)
{
application.PreferMultiSampling = false;
application.ContentRootDirectory = "Content";

        var bounds = mainWindow.WindowSizeInPixels;
        CCScene.SetDefaultDesignResolution (bounds.Width, bounds.Height , CCSceneResolutionPolicy.ShowAll);

        var topLeftViewport = new CCViewport(new CCRect(0.0f, 0.0f, 0.5f, 1.0f));
        var sceneDirector1 = new CCDirector();
        var scene1 = new CCScene(mainWindow, topLeftViewport, sceneDirector1);
        scene1.SceneResolutionPolicy = CCSceneResolutionPolicy.Custom;
        CCLayer layer1 = new CCLayer();
        scene1.AddChild (layer1);
        AddMonkey (layer1);

        var topRightViewport = new CCViewport(new CCRect(0.5f, 0.0f, 0.5f, 1.0f));
        var sceneDirector2 = new CCDirector();
        var scene2 = new CCScene(mainWindow, topRightViewport, sceneDirector2);
        scene2.SceneResolutionPolicy = CCSceneResolutionPolicy.Custom;
        CCLayer layer2 = new CCLayer();
        scene2.AddChild (layer2);
        AddMonkey (layer2);

        sceneDirector1.RunWithScene (scene1);
        sceneDirector2.RunWithScene (scene2);
    }

    static void AddMonkey( CCLayer layer )
    {
        var monkeySprite = new CCSprite ();

        monkeySprite = new CCSprite ("monkey");
        monkeySprite.Position = layer.VisibleBoundsWorldspace.Center;
        float scaleWidth = layer.VisibleBoundsWorldspace.MaxX / monkeySprite.ContentSize.Width;
        float scaleHeight = layer.VisibleBoundsWorldspace.MaxY / monkeySprite.ContentSize.Height;
        monkeySprite.ScaleX = scaleWidth;                   // Scales by multiplying the current size.
        monkeySprite.ScaleY = scaleHeight;
        layer.AddChild (monkeySprite,1);
    }

`

What have I got wrong?
Thanks,
Bryon

Posts

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    Bryon

    I have set up an issue for tracking this.

  • RamiTabbaraRamiTabbara AUMember, Xamarin Team Xamurai

    Hi Bryon,

    Check out the github wiki here that outlines how to setup multiple viewports.

  • brbbrb AUMember ✭✭

    Thanks Rami.
    The only difference I had was the points for the ViewPoint. I changed them to match and it still does not work.
    I have a question about the example. The code indicates that the first two points represent the origin on the physical screen as a ratio of the screen. But the example uses 0.0,0.0 for both the top left nd top right. Shouldn't it be 0,0 and 0.5,0 if you want to split the screen down the middle vertically?
    Thanks
    Bryon

  • RamiTabbaraRamiTabbara AUMember, Xamarin Team Xamurai

    Hi Bryon,

    A sample project that showcases how to use multiple viewports can be found here

Sign In or Register to comment.