From 0e898a5d7c2f2bf6fa60d4a63e149721edbf6829 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 13 Jun 2012 15:12:13 +0100 Subject: [PATCH] iOS rotation and desktop positioning fixes. --- .../native/juce_ios_UIViewComponentPeer.mm | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm index c59de998ce..766b4ea106 100644 --- a/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm @@ -230,6 +230,14 @@ private: JuceUIView* juceView = (JuceUIView*) [self view]; jassert (juceView != nil && juceView->owner != nullptr); juceView->owner->displayRotated(); + + [UIView setAnimationsEnabled: YES]; +} + +- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation + duration: (NSTimeInterval) duration +{ + [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap. } @end @@ -488,7 +496,7 @@ void UIViewComponentPeer::setBounds (int x, int y, int w, int h, const bool isNo { const Rectangle bounds (rotatedScreenPosToReal (Rectangle (x, y, w, h))); window.frame = convertToCGRect (bounds); - view.frame = CGRectMake (0, 0, (CGFloat) bounds.getWidth(), (CGFloat) bounds.getHeight()); + view.frame = CGRectMake (0, 0, (CGFloat) w, (CGFloat) h); handleMovedOrResized(); } @@ -500,13 +508,10 @@ Rectangle UIViewComponentPeer::getBounds (const bool global) const if (global && view.window != nil) { - r = [view convertRect: r toView: nil]; - CGRect wr = view.window.frame; + r = [view convertRect: r toView: view.window]; + r = [view.window convertRect: r toWindow: nil]; - const Rectangle windowBounds (realScreenPosToRotated (convertToRectInt (wr))); - - r.origin.x += windowBounds.getX(); - r.origin.y += windowBounds.getY(); + return realScreenPosToRotated (convertToRectInt (r)); } return convertToRectInt (r); @@ -601,7 +606,7 @@ bool UIViewComponentPeer::isFullScreen() const namespace { - Desktop::DisplayOrientation convertToJuceOrientation (UIInterfaceOrientation interfaceOrientation) + static Desktop::DisplayOrientation convertToJuceOrientation (UIInterfaceOrientation interfaceOrientation) { switch (interfaceOrientation) { @@ -614,6 +619,19 @@ namespace return Desktop::upright; } + + static CGAffineTransform getCGTransformForDisplayOrientation (const Desktop::DisplayOrientation orientation) noexcept + { + switch (orientation) + { + case Desktop::upsideDown: return CGAffineTransformMake (-1, 0, 0, -1, 0, 0); + case Desktop::rotatedClockwise: return CGAffineTransformMake (0, -1, 1, 0, 0, 0); + case Desktop::rotatedAntiClockwise: return CGAffineTransformMake (0, 1, -1, 0, 0, 0); + default: break; + } + + return CGAffineTransformIdentity; + } } BOOL UIViewComponentPeer::shouldRotate (UIInterfaceOrientation interfaceOrientation) @@ -629,6 +647,9 @@ void UIViewComponentPeer::displayRotated() const_cast (desktop.getDisplays()).refresh(); + window.transform = getCGTransformForDisplayOrientation (desktop.getCurrentOrientation()); + view.transform = CGAffineTransformIdentity; + if (fullScreen) { fullScreen = false; @@ -636,18 +657,16 @@ void UIViewComponentPeer::displayRotated() } else if (! isSharedWindow) { - const float l = oldArea.getX() / (float) oldDesktop.getWidth(); - const float r = oldArea.getRight() / (float) oldDesktop.getWidth(); - const float t = oldArea.getY() / (float) oldDesktop.getHeight(); - const float b = oldArea.getBottom() / (float) oldDesktop.getHeight(); + // this will re-centre the window, but leave its size unchanged + const float centreRelX = oldArea.getCentreX() / (float) oldDesktop.getWidth(); + const float centreRelY = oldArea.getCentreY() / (float) oldDesktop.getHeight(); const Rectangle newDesktop (desktop.getDisplays().getMainDisplay().userArea); - setBounds ((int) (l * newDesktop.getWidth()), - (int) (t * newDesktop.getHeight()), - (int) ((r - l) * newDesktop.getWidth()), - (int) ((b - t) * newDesktop.getHeight()), - false); + const int x = ((int) (newDesktop.getWidth() * centreRelX)) - (oldArea.getWidth() / 2); + const int y = ((int) (newDesktop.getHeight() * centreRelY)) - (oldArea.getHeight() / 2); + + setBounds (x, y, oldArea.getWidth(), oldArea.getHeight(), false); } }