Browse files

Fixed some iOS screen position problems.

  • Loading branch information...
1 parent d5dc5b2 commit e5172b88e9e6333ade8750cf64bab54fed4b0cfe @julianstorer committed Sep 21, 2012
View
146 modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm
@@ -25,6 +25,49 @@ Public License (Version 2), as published by the Free Software Foundation.
class UIViewComponentPeer;
+namespace Orientations
+{
+ static Desktop::DisplayOrientation convertToJuce (UIInterfaceOrientation orientation)
+ {
+ switch (orientation)
+ {
+ case UIInterfaceOrientationPortrait: return Desktop::upright;
+ case UIInterfaceOrientationPortraitUpsideDown: return Desktop::upsideDown;
+ case UIInterfaceOrientationLandscapeLeft: return Desktop::rotatedClockwise;
+ case UIInterfaceOrientationLandscapeRight: return Desktop::rotatedAntiClockwise;
+ default: jassertfalse; // unknown orientation!
+ }
+
+ return Desktop::upright;
+ }
+
+ static CGAffineTransform getCGTransformFor (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;
+ }
+
+ static NSUInteger getSupportedOrientations()
+ {
+ NSUInteger allowed = 0;
+ Desktop& d = Desktop::getInstance();
+
+ if (d.isOrientationEnabled (Desktop::upright)) allowed |= UIInterfaceOrientationMaskPortrait;
+ if (d.isOrientationEnabled (Desktop::upsideDown)) allowed |= UIInterfaceOrientationMaskPortraitUpsideDown;
+ if (d.isOrientationEnabled (Desktop::rotatedClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeLeft;
+ if (d.isOrientationEnabled (Desktop::rotatedAntiClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeRight;
+
+ return allowed;
+ }
+}
+
//==============================================================================
} // (juce namespace)
@@ -57,7 +100,9 @@ @interface JuceUIViewController : UIViewController
{
}
+- (NSUInteger) supportedInterfaceOrientations;
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation;
+- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration;
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation;
@end
@@ -128,8 +173,7 @@ - (void) becomeKeyWindow;
void updateHiddenTextContent (TextInputTarget* target);
void globalFocusChanged (Component*);
- virtual BOOL shouldRotate (UIInterfaceOrientation interfaceOrientation);
- virtual void displayRotated();
+ void updateTransformAndScreenBounds();
void handleTouches (UIEvent* e, bool isDown, bool isUp, bool isCancel);
@@ -215,37 +259,57 @@ static int64 getMouseTime (UIEvent* e)
@implementation JuceUIViewController
+- (NSUInteger) supportedInterfaceOrientations
+{
+ return Orientations::getSupportedOrientations();
+}
+
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
- JuceUIView* juceView = (JuceUIView*) [self view];
- jassert (juceView != nil && juceView->owner != nullptr);
- return juceView->owner->shouldRotate (interfaceOrientation);
+ return Desktop::getInstance().isOrientationEnabled (Orientations::convertToJuce (interfaceOrientation));
+}
+
+- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
+ duration: (NSTimeInterval) duration
+{
+ [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap.
}
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
{
JuceUIView* juceView = (JuceUIView*) [self view];
jassert (juceView != nil && juceView->owner != nullptr);
- juceView->owner->displayRotated();
+ juceView->owner->updateTransformAndScreenBounds();
[UIView setAnimationsEnabled: YES];
}
-- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
- duration: (NSTimeInterval) duration
+- (void) viewDidLoad
{
- [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap.
+ JuceUIView* juceView = (JuceUIView*) [self view];
+ jassert (juceView != nil && juceView->owner != nullptr);
+ juceView->owner->updateTransformAndScreenBounds();
+}
+
+- (void) viewWillAppear: (BOOL) animated
+{
+ [self viewDidLoad];
+}
+
+- (void) viewDidAppear: (BOOL) animated
+{
+ [self viewDidLoad];
}
@end
@implementation JuceUIView
-- (JuceUIView*) initWithOwner: (UIViewComponentPeer*) owner_
+- (JuceUIView*) initWithOwner: (UIViewComponentPeer*) peer
withFrame: (CGRect) frame
{
[super initWithFrame: frame];
- owner = owner_;
+ owner = peer;
hiddenTextView = [[UITextView alloc] initWithFrame: CGRectZero];
[self addSubview: hiddenTextView];
@@ -352,9 +416,9 @@ - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) ran
//==============================================================================
@implementation JuceUIWindow
-- (void) setOwner: (UIViewComponentPeer*) owner_
+- (void) setOwner: (UIViewComponentPeer*) peer
{
- owner = owner_;
+ owner = peer;
isZooming = false;
}
@@ -383,14 +447,15 @@ - (void) becomeKeyWindow
fullScreen (false),
insideDrawRect (false)
{
- CGRect r = convertToCGRect (component.getLocalBounds());
+ CGRect r = convertToCGRect (component.getBounds());
view = [[JuceUIView alloc] initWithOwner: this withFrame: r];
view.multipleTouchEnabled = YES;
view.hidden = ! component.isVisible();
view.opaque = component.isOpaque();
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
+ view.transform = CGAffineTransformIdentity;
if (isSharedWindow)
{
@@ -406,6 +471,8 @@ - (void) becomeKeyWindow
r.origin.y = [UIScreen mainScreen].bounds.size.height - (r.origin.y + r.size.height);
window = [[JuceUIWindow alloc] init];
+ window.autoresizesSubviews = NO;
+ window.transform = CGAffineTransformIdentity;
window.frame = r;
window.opaque = component.isOpaque();
window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
@@ -417,7 +484,7 @@ - (void) becomeKeyWindow
view.frame = CGRectMake (0, 0, r.size.width, r.size.height);
- [window setRootViewController: controller];
+ window.rootViewController = controller;
[window addSubview: view];
window.hidden = view.hidden;
@@ -601,50 +668,15 @@ - (void) becomeKeyWindow
return fullScreen;
}
-namespace
-{
- static Desktop::DisplayOrientation convertToJuceOrientation (UIInterfaceOrientation interfaceOrientation)
- {
- switch (interfaceOrientation)
- {
- case UIInterfaceOrientationPortrait: return Desktop::upright;
- case UIInterfaceOrientationPortraitUpsideDown: return Desktop::upsideDown;
- case UIInterfaceOrientationLandscapeLeft: return Desktop::rotatedClockwise;
- case UIInterfaceOrientationLandscapeRight: return Desktop::rotatedAntiClockwise;
- default: jassertfalse; // unknown orientation!
- }
-
- 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)
-{
- return Desktop::getInstance().isOrientationEnabled (convertToJuceOrientation (interfaceOrientation));
-}
-
-void UIViewComponentPeer::displayRotated()
+void UIViewComponentPeer::updateTransformAndScreenBounds()
{
Desktop& desktop = Desktop::getInstance();
const Rectangle<int> oldArea (component.getBounds());
const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
const_cast <Desktop::Displays&> (desktop.getDisplays()).refresh();
- window.transform = getCGTransformForDisplayOrientation (desktop.getCurrentOrientation());
+ window.transform = Orientations::getCGTransformFor (desktop.getCurrentOrientation());
view.transform = CGAffineTransformIdentity;
if (fullScreen)
@@ -665,6 +697,8 @@ static CGAffineTransform getCGTransformForDisplayOrientation (const Desktop::Dis
setBounds (x, y, oldArea.getWidth(), oldArea.getHeight(), false);
}
+
+ [view setNeedsDisplay];
}
bool UIViewComponentPeer::contains (const Point<int>& position, bool trueIfInAChildWindow) const
@@ -896,8 +930,8 @@ static CGAffineTransform getCGTransformForDisplayOrientation (const Desktop::Dis
if (! component.isOpaque())
CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
- CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, view.bounds.size.height));
- CoreGraphicsContext g (cg, view.bounds.size.height, [UIScreen mainScreen].scale);
+ CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
+ CoreGraphicsContext g (cg, getComponent().getHeight(), [UIScreen mainScreen].scale);
insideDrawRect = true;
handlePaint (g);
@@ -944,8 +978,8 @@ static CGAffineTransform getCGTransformForDisplayOrientation (const Desktop::Dis
UIViewComponentPeer* const peer;
const Rectangle<int> rect;
- AsyncRepaintMessage (UIViewComponentPeer* const peer_, const Rectangle<int>& rect_)
- : peer (peer_), rect (rect_)
+ AsyncRepaintMessage (UIViewComponentPeer* const p, const Rectangle<int>& r)
+ : peer (p), rect (r)
{
}
View
2 modules/juce_gui_basics/native/juce_ios_Windowing.mm
@@ -289,7 +289,7 @@ Image juce_createIconForFile (const File& file)
Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
{
- return convertToJuceOrientation ([[UIApplication sharedApplication] statusBarOrientation]);
+ return Orientations::convertToJuce ([[UIApplication sharedApplication] statusBarOrientation]);
}
void Desktop::Displays::findDisplays()

0 comments on commit e5172b8

Please sign in to comment.