fixed #18474: Provides an interface for getting SafeArea (#18489)

* fixed #18474: Provides an interface for getting SafeArea

* Adds getSafeAreaRect test demo in cpp-empty-test.

* @available(iOS 11.0, *) isn't supported in Xcode8. So we use the old way.
This commit is contained in:
James Chen 2017-11-23 09:34:23 +08:00 committed by minggo
parent bb479e4f6d
commit 14bca227e8
8 changed files with 97 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2013 cocos2d-x.org Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2011 Zynga Inc. Copyright (c) 2011 Zynga Inc.
@ -866,6 +866,18 @@ Vec2 Director::getVisibleOrigin() const
} }
} }
Rect Director::getSafeAreaRect() const
{
if (_openGLView)
{
return _openGLView->getSafeAreaRect();
}
else
{
return Rect::ZERO;
}
}
// scene management // scene management
void Director::runWithScene(Scene *scene) void Director::runWithScene(Scene *scene)

View File

@ -256,6 +256,11 @@ public:
/** Returns visible origin coordinate of the OpenGL view in points. */ /** Returns visible origin coordinate of the OpenGL view in points. */
Vec2 getVisibleOrigin() const; Vec2 getVisibleOrigin() const;
/**
* Returns safe area rectangle of the OpenGL view in points.
*/
Rect getSafeAreaRect() const;
/** /**
* Converts a screen coordinate to an OpenGL coordinate. * Converts a screen coordinate to an OpenGL coordinate.
* Useful to convert (multi) touch coordinates to the current layout (portrait or landscape). * Useful to convert (multi) touch coordinates to the current layout (portrait or landscape).

View File

@ -219,6 +219,11 @@ Rect GLView::getVisibleRect() const
return ret; return ret;
} }
Rect GLView::getSafeAreaRect() const
{
return getVisibleRect();
}
Size GLView::getVisibleSize() const Size GLView::getVisibleSize() const
{ {
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER) if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)

View File

@ -239,6 +239,11 @@ public:
*/ */
virtual Rect getVisibleRect() const; virtual Rect getVisibleRect() const;
/**
* Gets safe area rectangle
*/
virtual Rect getSafeAreaRect() const;
/** /**
* Set the design resolution size. * Set the design resolution size.
* @param width Design resolution width. * @param width Design resolution width.

View File

@ -90,6 +90,7 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved.
BOOL preserveBackbuffer_; BOOL preserveBackbuffer_;
CGSize size_; CGSize size_;
CGRect safeArea_;
BOOL discardFramebufferSupported_; BOOL discardFramebufferSupported_;
//fsaa addition //fsaa addition

View File

@ -75,6 +75,8 @@ public:
virtual void swapBuffers() override; virtual void swapBuffers() override;
virtual void setIMEKeyboardState(bool bOpen) override; virtual void setIMEKeyboardState(bool bOpen) override;
virtual Rect getSafeAreaRect() const override;
protected: protected:
GLViewImpl(); GLViewImpl();
virtual ~GLViewImpl(); virtual ~GLViewImpl();

View File

@ -32,6 +32,7 @@
#include "platform/ios/CCDirectorCaller-ios.h" #include "platform/ios/CCDirectorCaller-ios.h"
#include "platform/ios/CCGLViewImpl-ios.h" #include "platform/ios/CCGLViewImpl-ios.h"
#include "base/CCTouch.h" #include "base/CCTouch.h"
#include "base/CCDirector.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -227,6 +228,53 @@ void GLViewImpl::setIMEKeyboardState(bool open)
} }
} }
Rect GLViewImpl::getSafeAreaRect() const
{
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
float version = [[UIDevice currentDevice].systemVersion floatValue];
if (version >= 11.0f)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
UIEdgeInsets safeAreaInsets = eaglview.safeAreaInsets;
#pragma clang diagnostic pop
// Multiply contentScaleFactor since safeAreaInsets return points.
safeAreaInsets.left *= eaglview.contentScaleFactor;
safeAreaInsets.right *= eaglview.contentScaleFactor;
safeAreaInsets.top *= eaglview.contentScaleFactor;
safeAreaInsets.bottom *= eaglview.contentScaleFactor;
// Get leftBottom and rightTop point in UI coordinates
Vec2 leftBottom = Vec2(safeAreaInsets.left, _screenSize.height - safeAreaInsets.bottom);
Vec2 rightTop = Vec2(_screenSize.width - safeAreaInsets.right, safeAreaInsets.top);
// Convert a point from UI coordinates to which in design resolution coordinate.
leftBottom.x = (leftBottom.x - _viewPortRect.origin.x) / _scaleX,
leftBottom.y = (leftBottom.y - _viewPortRect.origin.y) / _scaleY;
rightTop.x = (rightTop.x - _viewPortRect.origin.x) / _scaleX,
rightTop.y = (rightTop.y - _viewPortRect.origin.y) / _scaleY;
// Adjust points to make them inside design resolution
leftBottom.x = MAX(leftBottom.x, 0);
leftBottom.y = MIN(leftBottom.y, _designResolutionSize.height);
rightTop.x = MIN(rightTop.x, _designResolutionSize.width);
rightTop.y = MAX(rightTop.y, 0);
// Convert to GL coordinates
leftBottom = Director::getInstance()->convertToGL(leftBottom);
rightTop = Director::getInstance()->convertToGL(rightTop);
return Rect(leftBottom.x, leftBottom.y, rightTop.x - leftBottom.x, rightTop.y - leftBottom.y);
}
#endif
// If running on iOS devices lower than 11.0, return visiable rect instead.
return GLView::getSafeAreaRect();
}
NS_CC_END NS_CC_END
#endif // CC_PLATFORM_IOS #endif // CC_PLATFORM_IOS

View File

@ -63,6 +63,13 @@ bool HelloWorld::init()
// add the sprite as a child to this layer // add the sprite as a child to this layer
this->addChild(sprite); this->addChild(sprite);
auto drawNode = DrawNode::create();
drawNode->setPosition(Vec2(0, 0));
addChild(drawNode);
Rect safeArea = Director::getInstance()->getSafeAreaRect();
drawNode->drawRect(safeArea.origin, safeArea.origin + safeArea.size, Color4F::BLUE);
return true; return true;
} }