no message
This commit is contained in:
40
cocos/platform/ios/AppDelegateBridge.h
Normal file
40
cocos/platform/ios/AppDelegateBridge.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
#include "platform/UniversalPlatform.h"
|
||||
|
||||
@interface AppDelegateBridge : NSObject
|
||||
- (float)getPixelRatio;
|
||||
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
|
||||
- (void)applicationDidBecomeActive:(UIApplication *)application;
|
||||
- (void)applicationWillResignActive:(UIApplication *)application;
|
||||
- (void)applicationWillTerminate:(UIApplication *)application;
|
||||
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
|
||||
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator;
|
||||
- (void)dispatchTouchEvent:(cc::TouchEvent::Type)type touches:(NSSet *)touches withEvent:(UIEvent *)event;
|
||||
@end
|
||||
121
cocos/platform/ios/AppDelegateBridge.mm
Normal file
121
cocos/platform/ios/AppDelegateBridge.mm
Normal file
@@ -0,0 +1,121 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#import "platform/ios/AppDelegateBridge.h"
|
||||
#include "platform/ios/IOSPlatform.h"
|
||||
#include "platform/interfaces/modules/IScreen.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
|
||||
@implementation AppDelegateBridge
|
||||
cc::IOSPlatform *_platform = nullptr;
|
||||
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||
_platform = dynamic_cast<cc::IOSPlatform *>(cc::BasePlatform::getPlatform());
|
||||
CC_ASSERT_NOT_NULL(_platform);
|
||||
|
||||
// Create main system window
|
||||
CGRect bounds = [[UIScreen mainScreen] bounds];
|
||||
cc::ISystemWindowInfo info;
|
||||
info.width = static_cast<int32_t>(bounds.size.width);
|
||||
info.height = static_cast<int32_t>(bounds.size.height);
|
||||
auto *windowMgr = _platform->getInterface<cc::ISystemWindowManager>();
|
||||
windowMgr->createWindow(info);
|
||||
|
||||
_platform->loop();
|
||||
}
|
||||
|
||||
- (void)applicationWillResignActive:(UIApplication *)application {
|
||||
_platform->onPause();
|
||||
}
|
||||
|
||||
- (void)applicationDidBecomeActive:(UIApplication *)application {
|
||||
_platform->onResume();
|
||||
}
|
||||
|
||||
- (void)applicationWillTerminate:(UIApplication *)application {
|
||||
_platform->onClose();
|
||||
_platform->onDestroy();
|
||||
_platform = nullptr;
|
||||
}
|
||||
|
||||
|
||||
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
|
||||
cc::events::LowMemory::broadcast();
|
||||
}
|
||||
|
||||
- (float)getPixelRatio {
|
||||
cc::BasePlatform *platform = cc::BasePlatform::getPlatform();
|
||||
cc::IScreen *screenIntf = platform->getInterface<cc::IScreen>();
|
||||
return (float)screenIntf->getDevicePixelRatio();
|
||||
}
|
||||
|
||||
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
|
||||
cc::IScreen::Orientation orientation;
|
||||
// reference: https://developer.apple.com/documentation/uikit/uiinterfaceorientation?language=objc
|
||||
// UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
|
||||
// UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight
|
||||
switch ([UIDevice currentDevice].orientation) {
|
||||
case UIDeviceOrientationPortrait:
|
||||
orientation = cc::IScreen::Orientation::PORTRAIT;
|
||||
break;
|
||||
case UIDeviceOrientationLandscapeRight:
|
||||
orientation = cc::IScreen::Orientation::LANDSCAPE_LEFT;
|
||||
break;
|
||||
case UIDeviceOrientationPortraitUpsideDown:
|
||||
orientation = cc::IScreen::Orientation::PORTRAIT_UPSIDE_DOWN;
|
||||
break;
|
||||
case UIDeviceOrientationLandscapeLeft:
|
||||
orientation = cc::IScreen::Orientation::LANDSCAPE_RIGHT;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
cc::DeviceEvent ev;
|
||||
cc::BasePlatform *platform = cc::BasePlatform::getPlatform();
|
||||
cc::IScreen *screenIntf = platform->getInterface<cc::IScreen>();
|
||||
cc::events::Orientation::broadcast((int)screenIntf->getDeviceOrientation());
|
||||
|
||||
float pixelRatio = screenIntf->getDevicePixelRatio();
|
||||
cc::WindowEvent resizeEv;
|
||||
resizeEv.windowId = 1;
|
||||
resizeEv.type = cc::WindowEvent::Type::RESIZED;
|
||||
resizeEv.width = size.width * pixelRatio;
|
||||
resizeEv.height = size.height * pixelRatio;
|
||||
cc::events::WindowEvent::broadcast(resizeEv);
|
||||
}
|
||||
|
||||
- (void)dispatchTouchEvent:(cc::TouchEvent::Type)type touches:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
cc::TouchEvent touchEvent;
|
||||
touchEvent.windowId = 1;
|
||||
touchEvent.type = type;
|
||||
for (UITouch *touch in touches) {
|
||||
touchEvent.touches.push_back({static_cast<float>([touch locationInView:[touch view]].x),
|
||||
static_cast<float>([touch locationInView:[touch view]].y),
|
||||
static_cast<int>((intptr_t)touch)});
|
||||
}
|
||||
cc::events::Touch::broadcast(touchEvent);
|
||||
touchEvent.touches.clear();
|
||||
}
|
||||
|
||||
@end
|
||||
71
cocos/platform/ios/IOSPlatform.h
Normal file
71
cocos/platform/ios/IOSPlatform.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/UniversalPlatform.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class IOSPlatform : public UniversalPlatform {
|
||||
public:
|
||||
IOSPlatform() = default;
|
||||
/**
|
||||
* @brief Destructor of WindowPlatform.
|
||||
*/
|
||||
~IOSPlatform() override;
|
||||
/**
|
||||
* @brief Implementation of Windows platform initialization.
|
||||
*/
|
||||
int32_t init() override;
|
||||
|
||||
/**
|
||||
* @brief Start base platform initialization.
|
||||
*/
|
||||
int32_t run(int argc, const char **argv) override;
|
||||
|
||||
void requestExit();
|
||||
|
||||
void exit() override;
|
||||
/**
|
||||
* @brief Implement the main logic of the base platform.
|
||||
*/
|
||||
int32_t loop() override;
|
||||
void setFps(int32_t fps) override;
|
||||
|
||||
int32_t getFps() const override;
|
||||
|
||||
void onPause() override;
|
||||
void onResume() override;
|
||||
void onClose() override;
|
||||
void onDestroy() override;
|
||||
ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override;
|
||||
|
||||
private:
|
||||
bool _requestExit{false};
|
||||
bool _quitLoop{false};
|
||||
ThreadCallback _cb;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
183
cocos/platform/ios/IOSPlatform.mm
Normal file
183
cocos/platform/ios/IOSPlatform.mm
Normal file
@@ -0,0 +1,183 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/IOSPlatform.h"
|
||||
#include "platform/interfaces/OSInterface.h"
|
||||
#include "platform/interfaces/modules/ISystemWindow.h"
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#include "base/memory/Memory.h"
|
||||
#include "modules/Accelerometer.h"
|
||||
#include "modules/Battery.h"
|
||||
#include "modules/Network.h"
|
||||
#include "modules/Screen.h"
|
||||
#include "modules/System.h"
|
||||
#include "modules/SystemWindow.h"
|
||||
#include "modules/SystemWindowManager.h"
|
||||
#include "modules/Vibrator.h"
|
||||
|
||||
extern int cocos_main(int argc, const char **argv);
|
||||
|
||||
@interface MyTimer : NSObject {
|
||||
cc::IOSPlatform *_platform;
|
||||
CADisplayLink *_displayLink;
|
||||
int _fps;
|
||||
}
|
||||
- (instancetype)initWithApp:(cc::IOSPlatform *)platform fps:(int)fps;
|
||||
- (void)start;
|
||||
- (void)changeFPS:(int)fps;
|
||||
- (void)pause;
|
||||
- (void)resume;
|
||||
@end
|
||||
|
||||
@implementation MyTimer
|
||||
|
||||
- (instancetype)initWithApp:(cc::IOSPlatform *)platform fps:(int)fps {
|
||||
if (self = [super init]) {
|
||||
_fps = fps;
|
||||
_platform = platform;
|
||||
_displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(renderScene:)];
|
||||
_displayLink.preferredFramesPerSecond = _fps;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)start {
|
||||
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
}
|
||||
|
||||
- (void)pause {
|
||||
_displayLink.paused = TRUE;
|
||||
}
|
||||
|
||||
- (void)resume {
|
||||
_displayLink.paused = FALSE;
|
||||
}
|
||||
|
||||
- (void)changeFPS:(int)fps {
|
||||
_displayLink.preferredFramesPerSecond = fps;
|
||||
}
|
||||
|
||||
- (int)getFPS {
|
||||
return (int)_displayLink.preferredFramesPerSecond;
|
||||
}
|
||||
|
||||
- (void)renderScene:(id)sender {
|
||||
_platform->runTask();
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
namespace {
|
||||
MyTimer *_timer;
|
||||
}
|
||||
|
||||
namespace cc {
|
||||
|
||||
IOSPlatform::~IOSPlatform() = default;
|
||||
|
||||
int32_t IOSPlatform::init() {
|
||||
_timer = [[MyTimer alloc] initWithApp:this fps:60];
|
||||
registerInterface(std::make_shared<Accelerometer>());
|
||||
registerInterface(std::make_shared<Battery>());
|
||||
registerInterface(std::make_shared<Network>());
|
||||
registerInterface(std::make_shared<Screen>());
|
||||
registerInterface(std::make_shared<System>());
|
||||
registerInterface(std::make_shared<Vibrator>());
|
||||
registerInterface(std::make_shared<SystemWindowManager>());
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IOSPlatform::exit() {
|
||||
if(_requestExit) {
|
||||
// Manual quit requires a call to onDestory.
|
||||
onDestroy();
|
||||
::exit(0);
|
||||
} else {
|
||||
_quitLoop = true;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t IOSPlatform::loop() {
|
||||
cocos_main(0, nullptr);
|
||||
[_timer start];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t IOSPlatform::run(int argc, const char **argv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IOSPlatform::setFps(int32_t fps) {
|
||||
[_timer changeFPS:fps];
|
||||
}
|
||||
|
||||
int32_t IOSPlatform::getFps() const {
|
||||
return [_timer getFPS];
|
||||
}
|
||||
|
||||
void IOSPlatform::onPause() {
|
||||
[_timer pause];
|
||||
|
||||
cc::WindowEvent ev;
|
||||
ev.type = cc::WindowEvent::Type::HIDDEN;
|
||||
cc::events::WindowEvent::broadcast(ev);
|
||||
}
|
||||
|
||||
void IOSPlatform::onResume() {
|
||||
[_timer resume];
|
||||
|
||||
cc::WindowEvent ev;
|
||||
ev.type = cc::WindowEvent::Type::SHOW;
|
||||
cc::events::WindowEvent::broadcast(ev);
|
||||
}
|
||||
|
||||
void IOSPlatform::onClose() {
|
||||
cc::WindowEvent ev;
|
||||
ev.type = cc::WindowEvent::Type::CLOSE;
|
||||
cc::events::WindowEvent::broadcast(ev);
|
||||
}
|
||||
|
||||
void IOSPlatform::requestExit() {
|
||||
_requestExit = true;
|
||||
onClose();
|
||||
}
|
||||
|
||||
void IOSPlatform::onDestroy() {
|
||||
if(!_requestExit) {
|
||||
// ios exit process is special because it needs to wait for ts layer to destroy resources.
|
||||
// The timer cannot be used here.
|
||||
while (!_quitLoop) {
|
||||
runTask();
|
||||
}
|
||||
}
|
||||
UniversalPlatform::onDestroy();
|
||||
}
|
||||
|
||||
ISystemWindow *IOSPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) {
|
||||
return ccnew SystemWindow(windowId, externalHandle);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
4
cocos/platform/ios/Untitled.xcworkspace/contents.xcworkspacedata
generated
Normal file
4
cocos/platform/ios/Untitled.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
</Workspace>
|
||||
BIN
cocos/platform/ios/Untitled.xcworkspace/xcuserdata/gem.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
BIN
cocos/platform/ios/Untitled.xcworkspace/xcuserdata/gem.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
Binary file not shown.
42
cocos/platform/ios/View.h
Normal file
42
cocos/platform/ios/View.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#import <UIKit/UIView.h>
|
||||
|
||||
#ifdef CC_USE_METAL
|
||||
#import <MetalKit/MetalKit.h>
|
||||
#endif
|
||||
|
||||
@interface View : UIView
|
||||
|
||||
@property (nonatomic, assign) BOOL preventTouch;
|
||||
#ifdef CC_USE_METAL
|
||||
@property (nonatomic, assign) id<MTLDevice> device;
|
||||
#endif
|
||||
|
||||
- (void)setPreventTouchEvent:(BOOL)flag;
|
||||
|
||||
@end
|
||||
121
cocos/platform/ios/View.mm
Normal file
121
cocos/platform/ios/View.mm
Normal file
@@ -0,0 +1,121 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#import "View.h"
|
||||
|
||||
#include <UIKit/UIScreen.h>
|
||||
#import "platform/ios/AppDelegateBridge.h"
|
||||
#include "platform/ios/IOSPlatform.h"
|
||||
|
||||
namespace {
|
||||
} // namespace
|
||||
|
||||
@implementation View {
|
||||
cc::IOSPlatform *_platform;
|
||||
}
|
||||
|
||||
@synthesize preventTouch;
|
||||
|
||||
#ifdef CC_USE_METAL
|
||||
+ (Class)layerClass {
|
||||
return [CAMetalLayer class];
|
||||
}
|
||||
#else
|
||||
+ (Class)layerClass {
|
||||
return [CAEAGLLayer class];
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)dispatchTouchEvent:(cc::TouchEvent::Type)type withEvent:(NSSet *)touches {
|
||||
cc::TouchEvent touchEvent;
|
||||
touchEvent.windowId = 1;
|
||||
touchEvent.type = type;
|
||||
for (UITouch *touch in touches) {
|
||||
touchEvent.touches.push_back({static_cast<float>([touch locationInView:[touch view]].x),
|
||||
static_cast<float>([touch locationInView:[touch view]].y),
|
||||
static_cast<int>((intptr_t)touch)});
|
||||
}
|
||||
CC_ASSERT_NOT_NULL(_platform);
|
||||
cc::events::Touch::broadcast(touchEvent);
|
||||
}
|
||||
|
||||
- (id)initWithFrame:(CGRect)frame {
|
||||
_platform = reinterpret_cast<cc::IOSPlatform *>(cc::BasePlatform::getPlatform());
|
||||
#ifdef CC_USE_METAL
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
self.preventTouch = FALSE;
|
||||
|
||||
float pixelRatio = [[UIScreen mainScreen] nativeScale];
|
||||
CGSize size = CGSizeMake(static_cast<int>(frame.size.width * pixelRatio),
|
||||
static_cast<int>(frame.size.height * pixelRatio));
|
||||
self.contentScaleFactor = pixelRatio;
|
||||
// Config metal layer
|
||||
CAMetalLayer *layer = (CAMetalLayer *)self.layer;
|
||||
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
|
||||
layer.device = self.device = MTLCreateSystemDefaultDevice();
|
||||
layer.drawableSize = size;
|
||||
}
|
||||
#else
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
self.preventTouch = FALSE;
|
||||
}
|
||||
#endif
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
if (self.preventTouch)
|
||||
return;
|
||||
|
||||
[self dispatchTouchEvent:cc::TouchEvent::Type::BEGAN withEvent:touches];
|
||||
}
|
||||
|
||||
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
if (self.preventTouch)
|
||||
return;
|
||||
|
||||
[self dispatchTouchEvent:cc::TouchEvent::Type::MOVED withEvent:touches];
|
||||
}
|
||||
|
||||
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
if (self.preventTouch)
|
||||
return;
|
||||
|
||||
[self dispatchTouchEvent:cc::TouchEvent::Type::ENDED withEvent:touches];
|
||||
}
|
||||
|
||||
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
||||
if (self.preventTouch)
|
||||
return;
|
||||
|
||||
[self dispatchTouchEvent:cc::TouchEvent::Type::CANCELLED withEvent:touches];
|
||||
}
|
||||
|
||||
- (void)setPreventTouchEvent:(BOOL)flag {
|
||||
self.preventTouch = flag;
|
||||
}
|
||||
|
||||
@end
|
||||
24
cocos/platform/ios/WarpCocosContent.h
Normal file
24
cocos/platform/ios/WarpCocosContent.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface WarpCocosContent : NSObject
|
||||
|
||||
// 渲染视图
|
||||
@property (nonatomic, assign) UIView *renderView;
|
||||
|
||||
// 用于动态创建游戏视图控制器的 Block
|
||||
@property (nonatomic, copy) UIViewController* (^gameVCBlock)(void);
|
||||
|
||||
// 单例方法
|
||||
+ (instancetype)shareInstance;
|
||||
|
||||
// 设置渲染视图
|
||||
- (void)setRenderView:(UIView *)renderView;
|
||||
|
||||
// 获取当前游戏视图控制器
|
||||
- (UIViewController *)getGameViewController;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
28
cocos/platform/ios/WarpCocosContent.mm
Normal file
28
cocos/platform/ios/WarpCocosContent.mm
Normal file
@@ -0,0 +1,28 @@
|
||||
#import "WarpCocosContent.h"
|
||||
|
||||
@implementation WarpCocosContent
|
||||
|
||||
// 单例对象
|
||||
+ (instancetype)shareInstance {
|
||||
static WarpCocosContent *instance = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
instance = [[WarpCocosContent alloc] init];
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
// 设置渲染视图
|
||||
- (void)setRenderView:(UIView *)renderView {
|
||||
_renderView = renderView;
|
||||
}
|
||||
|
||||
// 获取当前游戏视图控制器
|
||||
- (UIViewController *)getGameViewController {
|
||||
if (self.gameVCBlock) {
|
||||
return self.gameVCBlock();
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
49
cocos/platform/ios/modules/Accelerometer.h
Normal file
49
cocos/platform/ios/modules/Accelerometer.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IAccelerometer.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class Accelerometer : public IAccelerometer {
|
||||
public:
|
||||
/**
|
||||
* @brief To enable or disable accelerometer.
|
||||
*/
|
||||
void setAccelerometerEnabled(bool isEnabled) override;
|
||||
|
||||
/**
|
||||
* @brief Sets the interval of accelerometer.
|
||||
*/
|
||||
void setAccelerometerInterval(float interval) override;
|
||||
|
||||
/**
|
||||
* @brief Gets the motion value of current device.
|
||||
*/
|
||||
const MotionValue &getDeviceMotionValue() override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
159
cocos/platform/ios/modules/Accelerometer.mm
Normal file
159
cocos/platform/ios/modules/Accelerometer.mm
Normal file
@@ -0,0 +1,159 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/modules/Accelerometer.h"
|
||||
#import <UIKit/UIKit.h>
|
||||
// Accelerometer
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#import <CoreMotion/CoreMotion.h>
|
||||
#include <CoreText/CoreText.h>
|
||||
#include "platform/interfaces/modules/IAccelerometer.h"
|
||||
|
||||
static const float g = 9.80665;
|
||||
static const float radToDeg = (180 / M_PI);
|
||||
|
||||
@interface CCMotionDispatcher : NSObject <UIAccelerometerDelegate> {
|
||||
CMMotionManager *_motionManager;
|
||||
cc::IAccelerometer::MotionValue _motionValue;
|
||||
float _interval; // unit: seconds
|
||||
bool _enabled;
|
||||
}
|
||||
|
||||
+ (id)sharedMotionDispatcher;
|
||||
- (id)init;
|
||||
- (void)setMotionEnabled:(bool)isEnabled;
|
||||
- (void)setMotionInterval:(float)interval;
|
||||
|
||||
@end
|
||||
|
||||
@implementation CCMotionDispatcher
|
||||
|
||||
static CCMotionDispatcher *__motionDispatcher = nullptr;
|
||||
|
||||
+ (id)sharedMotionDispatcher {
|
||||
if (__motionDispatcher == nil) {
|
||||
__motionDispatcher = [[CCMotionDispatcher alloc] init];
|
||||
}
|
||||
|
||||
return __motionDispatcher;
|
||||
}
|
||||
|
||||
- (id)init {
|
||||
if ((self = [super init])) {
|
||||
_enabled = false;
|
||||
_interval = 1.0f / 60.0f;
|
||||
_motionManager = [[CMMotionManager alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
__motionDispatcher = nullptr;
|
||||
[_motionManager release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (void)setMotionEnabled:(bool)enabled {
|
||||
if (_enabled == enabled)
|
||||
return;
|
||||
|
||||
bool isDeviceMotionAvailable = _motionManager.isDeviceMotionAvailable;
|
||||
if (enabled) {
|
||||
// Has Gyro? (iPhone4 and newer)
|
||||
if (isDeviceMotionAvailable) {
|
||||
[_motionManager startDeviceMotionUpdates];
|
||||
_motionManager.deviceMotionUpdateInterval = _interval;
|
||||
}
|
||||
// Only basic accelerometer data
|
||||
else {
|
||||
[_motionManager startAccelerometerUpdates];
|
||||
_motionManager.accelerometerUpdateInterval = _interval;
|
||||
}
|
||||
} else {
|
||||
// Has Gyro? (iPhone4 and newer)
|
||||
if (isDeviceMotionAvailable) {
|
||||
[_motionManager stopDeviceMotionUpdates];
|
||||
}
|
||||
// Only basic accelerometer data
|
||||
else {
|
||||
[_motionManager stopAccelerometerUpdates];
|
||||
}
|
||||
}
|
||||
_enabled = enabled;
|
||||
}
|
||||
|
||||
- (void)setMotionInterval:(float)interval {
|
||||
_interval = interval;
|
||||
if (_enabled) {
|
||||
if (_motionManager.isDeviceMotionAvailable) {
|
||||
_motionManager.deviceMotionUpdateInterval = _interval;
|
||||
} else {
|
||||
_motionManager.accelerometerUpdateInterval = _interval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (const cc::IAccelerometer::MotionValue &)getMotionValue {
|
||||
if (_motionManager.isDeviceMotionAvailable) {
|
||||
CMDeviceMotion *motion = _motionManager.deviceMotion;
|
||||
_motionValue.accelerationX = motion.userAcceleration.x * g;
|
||||
_motionValue.accelerationY = motion.userAcceleration.y * g;
|
||||
_motionValue.accelerationZ = motion.userAcceleration.z * g;
|
||||
|
||||
_motionValue.accelerationIncludingGravityX = (motion.userAcceleration.x + motion.gravity.x) * g;
|
||||
_motionValue.accelerationIncludingGravityY = (motion.userAcceleration.y + motion.gravity.y) * g;
|
||||
_motionValue.accelerationIncludingGravityZ = (motion.userAcceleration.z + motion.gravity.z) * g;
|
||||
|
||||
_motionValue.rotationRateAlpha = motion.rotationRate.x * radToDeg;
|
||||
_motionValue.rotationRateBeta = motion.rotationRate.y * radToDeg;
|
||||
_motionValue.rotationRateGamma = motion.rotationRate.z * radToDeg;
|
||||
} else {
|
||||
CMAccelerometerData *acc = _motionManager.accelerometerData;
|
||||
_motionValue.accelerationIncludingGravityX = acc.acceleration.x * g;
|
||||
_motionValue.accelerationIncludingGravityY = acc.acceleration.y * g;
|
||||
_motionValue.accelerationIncludingGravityZ = acc.acceleration.z * g;
|
||||
}
|
||||
|
||||
return _motionValue;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
//
|
||||
|
||||
namespace cc {
|
||||
void Accelerometer::setAccelerometerEnabled(bool isEnabled) {
|
||||
[[CCMotionDispatcher sharedMotionDispatcher] setMotionEnabled:isEnabled];
|
||||
}
|
||||
|
||||
void Accelerometer::setAccelerometerInterval(float interval) {
|
||||
[[CCMotionDispatcher sharedMotionDispatcher] setMotionInterval:interval];
|
||||
}
|
||||
|
||||
const Accelerometer::MotionValue &Accelerometer::getDeviceMotionValue() {
|
||||
return [[CCMotionDispatcher sharedMotionDispatcher] getMotionValue];
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
37
cocos/platform/ios/modules/Battery.h
Normal file
37
cocos/platform/ios/modules/Battery.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IBattery.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class Battery : public IBattery {
|
||||
public:
|
||||
Battery() = default;
|
||||
float getBatteryLevel() const override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
37
cocos/platform/ios/modules/Battery.mm
Normal file
37
cocos/platform/ios/modules/Battery.mm
Normal file
@@ -0,0 +1,37 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/modules/Battery.h"
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
namespace cc {
|
||||
|
||||
float Battery::getBatteryLevel() const {
|
||||
// set batteryMonitoringEnabled value to YES otherwise batteryLevel is always -1
|
||||
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
|
||||
return [UIDevice currentDevice].batteryLevel;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
36
cocos/platform/ios/modules/Network.h
Normal file
36
cocos/platform/ios/modules/Network.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/INetwork.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class Network : public INetwork {
|
||||
public:
|
||||
NetworkType getNetworkType() const override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
57
cocos/platform/ios/modules/Network.mm
Normal file
57
cocos/platform/ios/modules/Network.mm
Normal file
@@ -0,0 +1,57 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/modules/Network.h"
|
||||
#include <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
#include "platform/apple/Reachability.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
INetwork::NetworkType Network::getNetworkType() const {
|
||||
static Reachability *__reachability = nullptr;
|
||||
if (__reachability == nullptr) {
|
||||
__reachability = Reachability::createForInternetConnection();
|
||||
__reachability->addRef();
|
||||
}
|
||||
|
||||
NetworkType ret = NetworkType::NONE;
|
||||
Reachability::NetworkStatus status = __reachability->getCurrentReachabilityStatus();
|
||||
switch (status) {
|
||||
case Reachability::NetworkStatus::REACHABLE_VIA_WIFI:
|
||||
ret = NetworkType::LAN;
|
||||
break;
|
||||
case Reachability::NetworkStatus::REACHABLE_VIA_WWAN:
|
||||
ret = NetworkType::WWAN;
|
||||
break;
|
||||
default:
|
||||
ret = NetworkType::NONE;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
50
cocos/platform/ios/modules/Screen.h
Normal file
50
cocos/platform/ios/modules/Screen.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IScreen.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class Screen : public IScreen {
|
||||
public:
|
||||
int getDPI() const override;
|
||||
float getDevicePixelRatio() const override;
|
||||
void setKeepScreenOn(bool value) override;
|
||||
Orientation getDeviceOrientation() const override;
|
||||
Vec4 getSafeAreaEdge() const override;
|
||||
/**
|
||||
* @brief Get current display stats.
|
||||
* @return bool, is displaying stats or not.
|
||||
*/
|
||||
bool isDisplayStats() override;
|
||||
|
||||
/**
|
||||
* @brief set display stats information.
|
||||
*/
|
||||
void setDisplayStats(bool isShow) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
117
cocos/platform/ios/modules/Screen.mm
Normal file
117
cocos/platform/ios/modules/Screen.mm
Normal file
@@ -0,0 +1,117 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/modules/Screen.h"
|
||||
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#import <CoreMotion/CoreMotion.h>
|
||||
#include <CoreText/CoreText.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#include "base/Macros.h"
|
||||
#include "cocos/bindings/jswrapper/SeApi.h"
|
||||
#include "cocos/platform/ios/WarpCocosContent.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
int Screen::getDPI() const {
|
||||
static int dpi = -1;
|
||||
|
||||
if (dpi == -1) {
|
||||
float scale = [[UIScreen mainScreen] scale];
|
||||
|
||||
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
||||
dpi = 132 * scale;
|
||||
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
|
||||
dpi = 163 * scale;
|
||||
} else {
|
||||
dpi = 160 * scale;
|
||||
}
|
||||
}
|
||||
return dpi;
|
||||
}
|
||||
|
||||
float Screen::getDevicePixelRatio() const {
|
||||
return [[UIScreen mainScreen] nativeScale];
|
||||
}
|
||||
|
||||
void Screen::setKeepScreenOn(bool value) {
|
||||
[[UIApplication sharedApplication] setIdleTimerDisabled:(BOOL)value];
|
||||
}
|
||||
|
||||
Screen::Orientation Screen::getDeviceOrientation() const {
|
||||
Orientation orientation = Orientation::PORTRAIT;
|
||||
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
|
||||
case UIInterfaceOrientationLandscapeRight:
|
||||
orientation = Orientation::LANDSCAPE_RIGHT;
|
||||
break;
|
||||
|
||||
case UIInterfaceOrientationLandscapeLeft:
|
||||
orientation = Orientation::LANDSCAPE_LEFT;
|
||||
break;
|
||||
|
||||
case UIInterfaceOrientationPortraitUpsideDown:
|
||||
orientation = Orientation::PORTRAIT_UPSIDE_DOWN;
|
||||
break;
|
||||
|
||||
case UIInterfaceOrientationPortrait:
|
||||
orientation = Orientation::PORTRAIT;
|
||||
break;
|
||||
default:
|
||||
CC_ABORT();
|
||||
break;
|
||||
}
|
||||
|
||||
return orientation;
|
||||
}
|
||||
|
||||
Vec4 Screen::getSafeAreaEdge() const {
|
||||
//UIView *screenView = UIApplication.sharedApplication.delegate.window.rootViewController.view;
|
||||
|
||||
UIView * screenView = WarpCocosContent.shareInstance.renderView;
|
||||
if (@available(iOS 11.0, *)) {
|
||||
UIEdgeInsets safeAreaEdge = screenView.safeAreaInsets;
|
||||
return cc::Vec4(safeAreaEdge.top, safeAreaEdge.left, safeAreaEdge.bottom, safeAreaEdge.right);
|
||||
}
|
||||
// If running on iOS devices lower than 11.0, return ZERO Vec4.
|
||||
return cc::Vec4();
|
||||
}
|
||||
|
||||
bool Screen::isDisplayStats() {
|
||||
se::AutoHandleScope hs;
|
||||
se::Value ret;
|
||||
char commandBuf[100] = "cc.debug.isDisplayStats();";
|
||||
se::ScriptEngine::getInstance()->evalString(commandBuf, 100, &ret);
|
||||
return ret.toBoolean();
|
||||
}
|
||||
|
||||
void Screen::setDisplayStats(bool isShow) {
|
||||
se::AutoHandleScope hs;
|
||||
char commandBuf[100] = {0};
|
||||
sprintf(commandBuf, "cc.debug.setDisplayStats(%s);", isShow ? "true" : "false");
|
||||
se::ScriptEngine::getInstance()->evalString(commandBuf);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
65
cocos/platform/ios/modules/System.h
Normal file
65
cocos/platform/ios/modules/System.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/ISystem.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class System : public ISystem {
|
||||
public:
|
||||
/**
|
||||
* @brief Get target system type.
|
||||
*/
|
||||
OSType getOSType() const override;
|
||||
/**
|
||||
* @brief Get target device model.
|
||||
*/
|
||||
ccstd::string getDeviceModel() const override;
|
||||
/**
|
||||
* @brief Get current language config.
|
||||
* @return Current language config.
|
||||
*/
|
||||
LanguageType getCurrentLanguage() const override;
|
||||
/**
|
||||
* @brief Get current language iso 639-1 code.
|
||||
* @return Current language iso 639-1 code.
|
||||
*/
|
||||
ccstd::string getCurrentLanguageCode() const override;
|
||||
/**
|
||||
* @brief Get system version.
|
||||
* @return system version.
|
||||
*/
|
||||
ccstd::string getSystemVersion() const override;
|
||||
/**
|
||||
* @brief Open url in default browser.
|
||||
* @param String with url to open.
|
||||
* @return True if the resource located by the URL was successfully opened; otherwise false.
|
||||
*/
|
||||
bool openURL(const ccstd::string& url) override;
|
||||
void copyTextToClipboard(const std::string& text) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
109
cocos/platform/ios/modules/System.mm
Normal file
109
cocos/platform/ios/modules/System.mm
Normal file
@@ -0,0 +1,109 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/modules/System.h"
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#include <sys/utsname.h>
|
||||
#include <algorithm>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
|
||||
namespace cc {
|
||||
using OSType = System::OSType;
|
||||
|
||||
OSType System::getOSType() const {
|
||||
return OSType::IPHONE;
|
||||
}
|
||||
|
||||
ccstd::string System::getDeviceModel() const {
|
||||
struct utsname systemInfo;
|
||||
uname(&systemInfo);
|
||||
return systemInfo.machine;
|
||||
}
|
||||
|
||||
System::LanguageType System::getCurrentLanguage() const {
|
||||
// get the current language and country config
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
|
||||
NSString *currentLanguage = [languages objectAtIndex:0];
|
||||
|
||||
// get the current language code.(such as English is "en", Chinese is "zh" and so on)
|
||||
NSDictionary *temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
|
||||
NSString *languageCode = [temp objectForKey:NSLocaleLanguageCode];
|
||||
|
||||
if ([languageCode isEqualToString:@"zh"]) return LanguageType::CHINESE;
|
||||
if ([languageCode isEqualToString:@"en"]) return LanguageType::ENGLISH;
|
||||
if ([languageCode isEqualToString:@"fr"]) return LanguageType::FRENCH;
|
||||
if ([languageCode isEqualToString:@"it"]) return LanguageType::ITALIAN;
|
||||
if ([languageCode isEqualToString:@"de"]) return LanguageType::GERMAN;
|
||||
if ([languageCode isEqualToString:@"es"]) return LanguageType::SPANISH;
|
||||
if ([languageCode isEqualToString:@"nl"]) return LanguageType::DUTCH;
|
||||
if ([languageCode isEqualToString:@"ru"]) return LanguageType::RUSSIAN;
|
||||
if ([languageCode isEqualToString:@"ko"]) return LanguageType::KOREAN;
|
||||
if ([languageCode isEqualToString:@"ja"]) return LanguageType::JAPANESE;
|
||||
if ([languageCode isEqualToString:@"hu"]) return LanguageType::HUNGARIAN;
|
||||
if ([languageCode isEqualToString:@"pt"]) return LanguageType::PORTUGUESE;
|
||||
if ([languageCode isEqualToString:@"ar"]) return LanguageType::ARABIC;
|
||||
if ([languageCode isEqualToString:@"nb"]) return LanguageType::NORWEGIAN;
|
||||
if ([languageCode isEqualToString:@"pl"]) return LanguageType::POLISH;
|
||||
if ([languageCode isEqualToString:@"tr"]) return LanguageType::TURKISH;
|
||||
if ([languageCode isEqualToString:@"uk"]) return LanguageType::UKRAINIAN;
|
||||
if ([languageCode isEqualToString:@"ro"]) return LanguageType::ROMANIAN;
|
||||
if ([languageCode isEqualToString:@"bg"]) return LanguageType::BULGARIAN;
|
||||
if ([languageCode isEqualToString:@"hi"]) return LanguageType::HINDI;
|
||||
return LanguageType::ENGLISH;
|
||||
}
|
||||
|
||||
ccstd::string System::getCurrentLanguageCode() const {
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
|
||||
NSString *currentLanguage = [languages objectAtIndex:0];
|
||||
return [currentLanguage UTF8String];
|
||||
}
|
||||
|
||||
ccstd::string System::getSystemVersion() const {
|
||||
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
|
||||
return [systemVersion UTF8String];
|
||||
}
|
||||
|
||||
bool System::openURL(const ccstd::string &url) {
|
||||
NSString *msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
|
||||
NSURL *nsUrl = [NSURL URLWithString:msg];
|
||||
__block BOOL flag = false;
|
||||
[[UIApplication sharedApplication] openURL:nsUrl
|
||||
options:@{}
|
||||
completionHandler:^(BOOL success) {
|
||||
flag = success;
|
||||
}];
|
||||
return flag;
|
||||
}
|
||||
|
||||
void System::copyTextToClipboard(const std::string& text) {
|
||||
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
|
||||
pasteboard.string = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
58
cocos/platform/ios/modules/SystemWindow.h
Normal file
58
cocos/platform/ios/modules/SystemWindow.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "platform/interfaces/modules/ISystemWindow.h"
|
||||
|
||||
@class UIWindow;
|
||||
|
||||
namespace cc {
|
||||
class SystemWindow : public ISystemWindow {
|
||||
public:
|
||||
SystemWindow(uint32_t windowId, void* externalHandle);
|
||||
~SystemWindow() override;
|
||||
|
||||
void closeWindow() override;
|
||||
uintptr_t getWindowHandle() const override;
|
||||
|
||||
Size getViewSize() const override;
|
||||
/*
|
||||
@brief enable/disable(lock) the cursor, default is enabled
|
||||
*/
|
||||
void setCursorEnabled(bool value) override;
|
||||
|
||||
uint32_t getWindowId() const override { return _windowId; }
|
||||
UIWindow* getUIWindow() const { return _window; }
|
||||
|
||||
private:
|
||||
int32_t _width{0};
|
||||
int32_t _height{0};
|
||||
|
||||
uint32_t _windowId{0};
|
||||
void* _externalHandle{nullptr};
|
||||
UIWindow* _window{nullptr};
|
||||
};
|
||||
} // namespace cc
|
||||
62
cocos/platform/ios/modules/SystemWindow.mm
Normal file
62
cocos/platform/ios/modules/SystemWindow.mm
Normal file
@@ -0,0 +1,62 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/modules/SystemWindow.h"
|
||||
#import <UIKit/UIKit.h>
|
||||
#include "platform/BasePlatform.h"
|
||||
#include "platform/ios/IOSPlatform.h"
|
||||
#include "platform/interfaces/modules/IScreen.h"
|
||||
#include "platform/ios/WarpCocosContent.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
|
||||
: _windowId(windowId)
|
||||
, _externalHandle(externalHandle) {
|
||||
}
|
||||
|
||||
SystemWindow::~SystemWindow() = default;
|
||||
|
||||
void SystemWindow::setCursorEnabled(bool value) {
|
||||
}
|
||||
|
||||
void SystemWindow::closeWindow() {
|
||||
// Force quit as there's no API to exit UIApplication
|
||||
IOSPlatform* platform = dynamic_cast<IOSPlatform*>(BasePlatform::getPlatform());
|
||||
platform->requestExit();
|
||||
}
|
||||
|
||||
uintptr_t SystemWindow::getWindowHandle() const {
|
||||
//return reinterpret_cast<uintptr_t>(UIApplication.sharedApplication.delegate.window.rootViewController.view);
|
||||
return reinterpret_cast<uintptr_t>(WarpCocosContent.shareInstance.renderView);
|
||||
}
|
||||
|
||||
SystemWindow::Size SystemWindow::getViewSize() const {
|
||||
auto dpr = BasePlatform::getPlatform()->getInterface<IScreen>()->getDevicePixelRatio();
|
||||
CGRect bounds = [[UIScreen mainScreen] bounds];
|
||||
return Size{static_cast<float>(bounds.size.width * dpr), static_cast<float>(bounds.size.height * dpr)};
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
53
cocos/platform/ios/modules/SystemWindowManager.h
Normal file
53
cocos/platform/ios/modules/SystemWindowManager.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/std/container/unordered_map.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
|
||||
@class UIWindow;
|
||||
|
||||
namespace cc {
|
||||
|
||||
class ISystemWindow;
|
||||
|
||||
class SystemWindowManager : public ISystemWindowManager {
|
||||
public:
|
||||
explicit SystemWindowManager() = default;
|
||||
|
||||
int init() override { return 0; }
|
||||
void processEvent() override {}
|
||||
|
||||
ISystemWindow *createWindow(const ISystemWindowInfo &info) override;
|
||||
ISystemWindow *getWindow(uint32_t windowId) const override;
|
||||
const SystemWindowMap &getWindows() const override { return _windows; }
|
||||
|
||||
ISystemWindow *getWindowFromUIWindow(UIWindow *window) const;
|
||||
|
||||
private:
|
||||
uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID
|
||||
SystemWindowMap _windows;
|
||||
};
|
||||
} // namespace cc
|
||||
67
cocos/platform/ios/modules/SystemWindowManager.mm
Normal file
67
cocos/platform/ios/modules/SystemWindowManager.mm
Normal file
@@ -0,0 +1,67 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "SystemWindowManager.h"
|
||||
#include "platform/BasePlatform.h"
|
||||
#include "platform/SDLHelper.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
#include "platform/ios/modules/SystemWindow.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const {
|
||||
if (windowId == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto iter = _windows.find(windowId);
|
||||
if (iter != _windows.end()) {
|
||||
return iter->second.get();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ISystemWindow *SystemWindowManager::createWindow(const cc::ISystemWindowInfo &info) {
|
||||
ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle);
|
||||
if (window) {
|
||||
window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags);
|
||||
_windows[_nextWindowId] = std::shared_ptr<ISystemWindow>(window);
|
||||
_nextWindowId++;
|
||||
}
|
||||
return window;
|
||||
}
|
||||
|
||||
ISystemWindow *SystemWindowManager::getWindowFromUIWindow(UIWindow *window) const {
|
||||
for (const auto &iter : _windows) {
|
||||
SystemWindow *sysWindow = static_cast<SystemWindow *>(iter.second.get());
|
||||
UIWindow *nsWindow = sysWindow->getUIWindow();
|
||||
if (nsWindow == window) {
|
||||
return sysWindow;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
43
cocos/platform/ios/modules/Vibrator.h
Normal file
43
cocos/platform/ios/modules/Vibrator.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IVibrator.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class Vibrator : public IVibrator {
|
||||
public:
|
||||
/**
|
||||
* Vibrate for the specified amount of time.
|
||||
* If vibrate is not supported, then invoking this method has no effect.
|
||||
* Some platforms limit to a maximum duration of 5 seconds.
|
||||
* Duration is ignored on iOS due to API limitations.
|
||||
* @param duration The duration in seconds.
|
||||
*/
|
||||
void vibrate(float duration) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
36
cocos/platform/ios/modules/Vibrator.mm
Normal file
36
cocos/platform/ios/modules/Vibrator.mm
Normal file
@@ -0,0 +1,36 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/ios/modules/Vibrator.h"
|
||||
|
||||
#include "base/Macros.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
void Vibrator::vibrate(float duration) {
|
||||
CC_UNUSED_PARAM(duration);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user