//
// Triple Play - utilities for use in PlayN-based games
// Copyright (c) 2011-2013, Three Rings Design, Inc. - All rights reserved.
// http://github.com/threerings/tripleplay/blob/master/LICENSE

package tripleplay.platform;

import cli.MonoTouch.CoreGraphics.CGAffineTransform;
import cli.MonoTouch.UIKit.UIScreen;
import cli.System.Drawing.RectangleF;
import playn.core.Image;
import playn.ios.IOSPlatform;
import tripleplay.ui.Field;

/**
 * Implements iOS-specific TriplePlay services.
 */
public class IOSTPPlatform extends TPPlatform
    implements IOSPlatform.OrientationChangeListener
{
    /** Registers the IOS TriplePlay platform. */
    public static IOSTPPlatform register (IOSPlatform platform) {
        IOSTPPlatform instance = new IOSTPPlatform(platform);
        TPPlatform.register(instance);
        platform.setOrientationChangeListener(instance);
        return instance;
    }

    /** The iOS platform with which this TPPlatform was registered. */
    public final IOSPlatform platform;

    @Override public boolean hasNativeTextFields () {
        return true;
    }

    @Override public NativeTextField createNativeTextField (Field.Native field) {
        return (field.resolveStyle(Field.MULTILINE) ?
            new IOSNativeTextField.MultiLine(_fieldHandler, null, field) :
            new IOSNativeTextField.SingleLine(_fieldHandler, null, field)).refresh();
    }

    @Override public NativeTextField refresh (NativeTextField previous) {
        return ((IOSNativeTextField)previous).refresh();
    }

    @Override public ImageOverlay createImageOverlay (Image image) {
        return new IOSImageOverlay(image);
    }

    @Override public void orientationChanged (CGAffineTransform trans, boolean landscape) {
        _uiOverlay.set_Transform(trans);

        RectangleF overlayBounds = _uiOverlay.get_Bounds();
        if ((overlayBounds.get_Width() > overlayBounds.get_Height()) != landscape) {
          // swap the width and height
          float width = overlayBounds.get_Width();
          overlayBounds.set_Width(overlayBounds.get_Height());
          overlayBounds.set_Height(width);
          _uiOverlay.set_Bounds(overlayBounds);
        }
        // update the overlay's hidden area, if any
        _uiOverlay.setHiddenArea(_hidden);
    }

    @Override public void updateHidden () {
        _uiOverlay.setHiddenArea(_hidden);
    }

    IOSUIOverlay overlay () {
        return _uiOverlay;
    }

    private IOSTPPlatform (IOSPlatform platform) {
        this.platform = platform;
        platform.gameView().Add(
            _uiOverlay = new IOSUIOverlay(UIScreen.get_MainScreen().get_Bounds()));
        _fieldHandler = new IOSTextFieldHandler(this);
    }

    protected final IOSTextFieldHandler _fieldHandler;
    protected final IOSUIOverlay _uiOverlay;
}

@@ -271,24 +271,72 @@
 eTransform;%0A
+import cli.MonoTouch.UIKit.UIDeviceOrientation;%0A
 import cli.M
@@ -449,24 +449,55 @@
 OSPlatform;%0A
+import pythagoras.f.FloatMath;%0A
 import tripl
@@ -1780,53 +1780,638 @@
 ed (
-CGAffineTransform trans, boolean landscape) %7B
+int orientationValue) %7B%0A        CGAffineTransform trans = CGAffineTransform.MakeIdentity();%0A        boolean landscape = false;%0A        switch (orientationValue) %7B%0A        default:%0A        case UIDeviceOrientation.Portrait:%0A          break;%0A        case UIDeviceOrientation.PortraitUpsideDown:%0A          trans.Rotate(FloatMath.PI);%0A          break;%0A        case UIDeviceOrientation.LandscapeLeft:%0A          landscape = true;%0A          trans.Rotate(FloatMath.PI / 2);%0A          break;%0A        case UIDeviceOrientation.LandscapeRight:%0A          landscape = true;%0A          trans.Rotate(-FloatMath.PI / 2);%0A          break;%0A        %7D%0A
 %0A   

Update to new playn and calculate the transform and landscape values locally.