no message

This commit is contained in:
gem
2025-02-18 15:21:31 +08:00
commit 2d133e56d7
1980 changed files with 465595 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
/****************************************************************************
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 <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification;
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)theApplication;
@end

View File

@@ -0,0 +1,52 @@
/****************************************************************************
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 "AppDelegate.h"
#include "base/std/container/string.h"
#include "engine/EngineEvents.h"
#include "platform/mac/MacPlatform.h"
@interface AppDelegate () {
NSWindow* _window;
cc::MacPlatform* _platform;
}
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
_platform = dynamic_cast<cc::MacPlatform*>(cc::BasePlatform::getPlatform());
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
return _platform->readyToExit() ? NSTerminateNow : NSTerminateLater;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)theApplication {
return YES;
}
@end

View File

@@ -0,0 +1,454 @@
/****************************************************************************
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.
****************************************************************************/
#include "KeyCodeHelper.h"
#include <mutex>
// Copy from glfw3.h
/* The unknown key */
#define GLFW_KEY_UNKNOWN -1
/* Printable keys */
#define GLFW_KEY_SPACE 32
#define GLFW_KEY_APOSTROPHE 39 /* ' */
#define GLFW_KEY_COMMA 44 /* , */
#define GLFW_KEY_MINUS 45 /* - */
#define GLFW_KEY_PERIOD 46 /* . */
#define GLFW_KEY_SLASH 47 /* / */
#define GLFW_KEY_0 48
#define GLFW_KEY_1 49
#define GLFW_KEY_2 50
#define GLFW_KEY_3 51
#define GLFW_KEY_4 52
#define GLFW_KEY_5 53
#define GLFW_KEY_6 54
#define GLFW_KEY_7 55
#define GLFW_KEY_8 56
#define GLFW_KEY_9 57
#define GLFW_KEY_SEMICOLON 59 /* ; */
#define GLFW_KEY_EQUAL 61 /* = */
#define GLFW_KEY_A 65
#define GLFW_KEY_B 66
#define GLFW_KEY_C 67
#define GLFW_KEY_D 68
#define GLFW_KEY_E 69
#define GLFW_KEY_F 70
#define GLFW_KEY_G 71
#define GLFW_KEY_H 72
#define GLFW_KEY_I 73
#define GLFW_KEY_J 74
#define GLFW_KEY_K 75
#define GLFW_KEY_L 76
#define GLFW_KEY_M 77
#define GLFW_KEY_N 78
#define GLFW_KEY_O 79
#define GLFW_KEY_P 80
#define GLFW_KEY_Q 81
#define GLFW_KEY_R 82
#define GLFW_KEY_S 83
#define GLFW_KEY_T 84
#define GLFW_KEY_U 85
#define GLFW_KEY_V 86
#define GLFW_KEY_W 87
#define GLFW_KEY_X 88
#define GLFW_KEY_Y 89
#define GLFW_KEY_Z 90
#define GLFW_KEY_LEFT_BRACKET 91 /* [ */
#define GLFW_KEY_BACKSLASH 92 /* \ */
#define GLFW_KEY_RIGHT_BRACKET 93 /* ] */
#define GLFW_KEY_GRAVE_ACCENT 96 /* ` */
#define GLFW_KEY_WORLD_1 161 /* non-US #1 */
#define GLFW_KEY_WORLD_2 162 /* non-US #2 */
/* Function keys */
#define GLFW_KEY_ESCAPE 256
#define GLFW_KEY_ENTER 257
#define GLFW_KEY_TAB 258
#define GLFW_KEY_BACKSPACE 259
#define GLFW_KEY_INSERT 260
#define GLFW_KEY_DELETE 261
#define GLFW_KEY_RIGHT 262
#define GLFW_KEY_LEFT 263
#define GLFW_KEY_DOWN 264
#define GLFW_KEY_UP 265
#define GLFW_KEY_PAGE_UP 266
#define GLFW_KEY_PAGE_DOWN 267
#define GLFW_KEY_HOME 268
#define GLFW_KEY_END 269
#define GLFW_KEY_CAPS_LOCK 280
#define GLFW_KEY_SCROLL_LOCK 281
#define GLFW_KEY_NUM_LOCK 282
#define GLFW_KEY_PRINT_SCREEN 283
#define GLFW_KEY_PAUSE 284
#define GLFW_KEY_F1 290
#define GLFW_KEY_F2 291
#define GLFW_KEY_F3 292
#define GLFW_KEY_F4 293
#define GLFW_KEY_F5 294
#define GLFW_KEY_F6 295
#define GLFW_KEY_F7 296
#define GLFW_KEY_F8 297
#define GLFW_KEY_F9 298
#define GLFW_KEY_F10 299
#define GLFW_KEY_F11 300
#define GLFW_KEY_F12 301
#define GLFW_KEY_F13 302
#define GLFW_KEY_F14 303
#define GLFW_KEY_F15 304
#define GLFW_KEY_F16 305
#define GLFW_KEY_F17 306
#define GLFW_KEY_F18 307
#define GLFW_KEY_F19 308
#define GLFW_KEY_F20 309
#define GLFW_KEY_F21 310
#define GLFW_KEY_F22 311
#define GLFW_KEY_F23 312
#define GLFW_KEY_F24 313
#define GLFW_KEY_F25 314
#define GLFW_KEY_KP_0 320
#define GLFW_KEY_KP_1 321
#define GLFW_KEY_KP_2 322
#define GLFW_KEY_KP_3 323
#define GLFW_KEY_KP_4 324
#define GLFW_KEY_KP_5 325
#define GLFW_KEY_KP_6 326
#define GLFW_KEY_KP_7 327
#define GLFW_KEY_KP_8 328
#define GLFW_KEY_KP_9 329
#define GLFW_KEY_KP_DECIMAL 330
#define GLFW_KEY_KP_DIVIDE 331
#define GLFW_KEY_KP_MULTIPLY 332
#define GLFW_KEY_KP_SUBTRACT 333
#define GLFW_KEY_KP_ADD 334
#define GLFW_KEY_KP_ENTER 335
#define GLFW_KEY_KP_EQUAL 336
#define GLFW_KEY_LEFT_SHIFT 340
#define GLFW_KEY_LEFT_CONTROL 341
#define GLFW_KEY_LEFT_ALT 342
#define GLFW_KEY_LEFT_SUPER 343
#define GLFW_KEY_RIGHT_SHIFT 344
#define GLFW_KEY_RIGHT_CONTROL 345
#define GLFW_KEY_RIGHT_ALT 346
#define GLFW_KEY_RIGHT_SUPER 347
#define GLFW_KEY_MENU 348
#define GLFW_KEY_LAST GLFW_KEY_MENU
namespace {
// Modifier Key State
bool stateShiftLeft = false;
bool stateShiftRight = false;
bool stateControlLeft = false;
bool stateControlRight = false;
bool stateOptionLeft = false;
bool stateOptionRight = false;
// Refer to https://github.com/glfw/glfw/blob/master/src/cocoa_window.m.
int keyCodes[0xff + 1] = {-1};
void init() {
keyCodes[0x1D] = GLFW_KEY_0;
keyCodes[0x12] = GLFW_KEY_1;
keyCodes[0x13] = GLFW_KEY_2;
keyCodes[0x14] = GLFW_KEY_3;
keyCodes[0x15] = GLFW_KEY_4;
keyCodes[0x17] = GLFW_KEY_5;
keyCodes[0x16] = GLFW_KEY_6;
keyCodes[0x1A] = GLFW_KEY_7;
keyCodes[0x1C] = GLFW_KEY_8;
keyCodes[0x19] = GLFW_KEY_9;
keyCodes[0x00] = GLFW_KEY_A;
keyCodes[0x0B] = GLFW_KEY_B;
keyCodes[0x08] = GLFW_KEY_C;
keyCodes[0x02] = GLFW_KEY_D;
keyCodes[0x0E] = GLFW_KEY_E;
keyCodes[0x03] = GLFW_KEY_F;
keyCodes[0x05] = GLFW_KEY_G;
keyCodes[0x04] = GLFW_KEY_H;
keyCodes[0x22] = GLFW_KEY_I;
keyCodes[0x26] = GLFW_KEY_J;
keyCodes[0x28] = GLFW_KEY_K;
keyCodes[0x25] = GLFW_KEY_L;
keyCodes[0x2E] = GLFW_KEY_M;
keyCodes[0x2D] = GLFW_KEY_N;
keyCodes[0x1F] = GLFW_KEY_O;
keyCodes[0x23] = GLFW_KEY_P;
keyCodes[0x0C] = GLFW_KEY_Q;
keyCodes[0x0F] = GLFW_KEY_R;
keyCodes[0x01] = GLFW_KEY_S;
keyCodes[0x11] = GLFW_KEY_T;
keyCodes[0x20] = GLFW_KEY_U;
keyCodes[0x09] = GLFW_KEY_V;
keyCodes[0x0D] = GLFW_KEY_W;
keyCodes[0x07] = GLFW_KEY_X;
keyCodes[0x10] = GLFW_KEY_Y;
keyCodes[0x06] = GLFW_KEY_Z;
keyCodes[0x27] = GLFW_KEY_APOSTROPHE;
keyCodes[0x2A] = GLFW_KEY_BACKSLASH;
keyCodes[0x2B] = GLFW_KEY_COMMA;
keyCodes[0x18] = GLFW_KEY_EQUAL;
keyCodes[0x32] = GLFW_KEY_GRAVE_ACCENT;
keyCodes[0x21] = GLFW_KEY_LEFT_BRACKET;
keyCodes[0x1B] = GLFW_KEY_MINUS;
keyCodes[0x2F] = GLFW_KEY_PERIOD;
keyCodes[0x1E] = GLFW_KEY_RIGHT_BRACKET;
keyCodes[0x29] = GLFW_KEY_SEMICOLON;
keyCodes[0x2C] = GLFW_KEY_SLASH;
keyCodes[0x0A] = GLFW_KEY_WORLD_1;
keyCodes[0x33] = GLFW_KEY_BACKSPACE;
keyCodes[0x39] = GLFW_KEY_CAPS_LOCK;
keyCodes[0x75] = GLFW_KEY_DELETE;
keyCodes[0x7D] = GLFW_KEY_DOWN;
keyCodes[0x77] = GLFW_KEY_END;
keyCodes[0x24] = GLFW_KEY_ENTER;
keyCodes[0x35] = GLFW_KEY_ESCAPE;
keyCodes[0x7A] = GLFW_KEY_F1;
keyCodes[0x78] = GLFW_KEY_F2;
keyCodes[0x63] = GLFW_KEY_F3;
keyCodes[0x76] = GLFW_KEY_F4;
keyCodes[0x60] = GLFW_KEY_F5;
keyCodes[0x61] = GLFW_KEY_F6;
keyCodes[0x62] = GLFW_KEY_F7;
keyCodes[0x64] = GLFW_KEY_F8;
keyCodes[0x65] = GLFW_KEY_F9;
keyCodes[0x6D] = GLFW_KEY_F10;
keyCodes[0x67] = GLFW_KEY_F11;
keyCodes[0x6F] = GLFW_KEY_F12;
keyCodes[0x69] = GLFW_KEY_F13;
keyCodes[0x6B] = GLFW_KEY_F14;
keyCodes[0x71] = GLFW_KEY_F15;
keyCodes[0x6A] = GLFW_KEY_F16;
keyCodes[0x40] = GLFW_KEY_F17;
keyCodes[0x4F] = GLFW_KEY_F18;
keyCodes[0x50] = GLFW_KEY_F19;
keyCodes[0x5A] = GLFW_KEY_F20;
keyCodes[0x73] = GLFW_KEY_HOME;
keyCodes[0x72] = GLFW_KEY_INSERT;
keyCodes[0x7B] = GLFW_KEY_LEFT;
keyCodes[0x3A] = GLFW_KEY_LEFT_ALT;
keyCodes[0x3B] = GLFW_KEY_LEFT_CONTROL;
keyCodes[0x38] = GLFW_KEY_LEFT_SHIFT;
keyCodes[0x37] = GLFW_KEY_LEFT_SUPER;
keyCodes[0x6E] = GLFW_KEY_MENU;
keyCodes[0x47] = GLFW_KEY_NUM_LOCK;
keyCodes[0x79] = GLFW_KEY_PAGE_DOWN;
keyCodes[0x74] = GLFW_KEY_PAGE_UP;
keyCodes[0x7C] = GLFW_KEY_RIGHT;
keyCodes[0x3D] = GLFW_KEY_RIGHT_ALT;
keyCodes[0x3E] = GLFW_KEY_RIGHT_CONTROL;
keyCodes[0x3C] = GLFW_KEY_RIGHT_SHIFT;
keyCodes[0x36] = GLFW_KEY_RIGHT_SUPER;
keyCodes[0x31] = GLFW_KEY_SPACE;
keyCodes[0x30] = GLFW_KEY_TAB;
keyCodes[0x7E] = GLFW_KEY_UP;
keyCodes[0x52] = GLFW_KEY_KP_0;
keyCodes[0x53] = GLFW_KEY_KP_1;
keyCodes[0x54] = GLFW_KEY_KP_2;
keyCodes[0x55] = GLFW_KEY_KP_3;
keyCodes[0x56] = GLFW_KEY_KP_4;
keyCodes[0x57] = GLFW_KEY_KP_5;
keyCodes[0x58] = GLFW_KEY_KP_6;
keyCodes[0x59] = GLFW_KEY_KP_7;
keyCodes[0x5B] = GLFW_KEY_KP_8;
keyCodes[0x5C] = GLFW_KEY_KP_9;
keyCodes[0x45] = GLFW_KEY_KP_ADD;
keyCodes[0x41] = GLFW_KEY_KP_DECIMAL;
keyCodes[0x4B] = GLFW_KEY_KP_DIVIDE;
keyCodes[0x4C] = GLFW_KEY_KP_ENTER;
keyCodes[0x51] = GLFW_KEY_KP_EQUAL;
keyCodes[0x43] = GLFW_KEY_KP_MULTIPLY;
keyCodes[0x4E] = GLFW_KEY_KP_SUBTRACT;
}
} // namespace
void updateModifierKeyState(int keyCodeInWeb) {
if (keyCodeInWeb == 16) { // shift left
stateShiftLeft = !stateShiftLeft;
} else if (keyCodeInWeb == 20016) { // shift right
stateShiftRight = !stateShiftRight;
} else if (keyCodeInWeb == 17) { // ctrl left
stateControlLeft = !stateControlLeft;
} else if (keyCodeInWeb == 20017) { // ctrl right
stateControlRight = !stateControlRight;
} else if (keyCodeInWeb == 18) { // alt left
stateOptionLeft = !stateOptionLeft;
} else if (keyCodeInWeb == 20018) { // alt right
stateOptionRight = !stateOptionRight;
}
}
cc::KeyboardEvent::Action getModifierKeyAction(int keyCodeInWeb) {
if (keyCodeInWeb == 16) { // shift left
if (stateShiftLeft) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 20016) { // shift right
if (stateShiftRight) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 17) { // ctrl left
if (stateControlLeft) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 20017) { // ctrl right
if (stateControlRight) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 18) { // alt left
if (stateOptionLeft) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
} else if (keyCodeInWeb == 20018) { // alt right
if (stateOptionRight) {
return cc::KeyboardEvent::Action::PRESS;
} else {
return cc::KeyboardEvent::Action::RELEASE;
}
}
return cc::KeyboardEvent::Action::UNKNOWN;
}
// Refer to: https://github.com/cocos-creator/cocos2d-x-lite/blob/v2.4.0/cocos/platform/desktop/CCGLView-desktop.cpp.
int translateKeycode(int keyCode) {
std::once_flag flag;
std::call_once(flag, init);
if (keyCode < 0 || keyCode > 0xff)
return -1;
int key = keyCodes[keyCode];
int keyInWeb = -1;
if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)
keyInWeb = key;
else if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z)
keyInWeb = key;
else if (key >= GLFW_KEY_F1 && key <= GLFW_KEY_F12)
keyInWeb = key - 178;
else if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_9)
keyInWeb = key - 272 + 10000; // For indicating number in Numberpad, needs to be converted in JS.
else if (key == GLFW_KEY_ESCAPE)
keyInWeb = 27;
else if (key == GLFW_KEY_MINUS)
keyInWeb = 189;
else if (key == GLFW_KEY_EQUAL)
keyInWeb = 187;
else if (key == GLFW_KEY_BACKSLASH)
keyInWeb = 220;
else if (key == GLFW_KEY_GRAVE_ACCENT)
keyInWeb = 192;
else if (key == GLFW_KEY_BACKSPACE)
keyInWeb = 8;
else if (key == GLFW_KEY_ENTER)
keyInWeb = 13;
else if (key == GLFW_KEY_LEFT_BRACKET)
keyInWeb = 219;
else if (key == GLFW_KEY_RIGHT_BRACKET)
keyInWeb = 221;
else if (key == GLFW_KEY_SEMICOLON)
keyInWeb = 186;
else if (key == GLFW_KEY_APOSTROPHE)
keyInWeb = 222;
else if (key == GLFW_KEY_TAB)
keyInWeb = 9;
else if (key == GLFW_KEY_LEFT_CONTROL)
keyInWeb = 17;
else if (key == GLFW_KEY_RIGHT_CONTROL)
keyInWeb = 17 + 20000; // For indicating Left/Right control, needs to be converted in JS.
else if (key == GLFW_KEY_LEFT_SHIFT)
keyInWeb = 16;
else if (key == GLFW_KEY_RIGHT_SHIFT)
keyInWeb = 16 + 20000; // For indicating Left/Right shift, needs to be converted in JS.
else if (key == GLFW_KEY_LEFT_ALT)
keyInWeb = 18;
else if (key == GLFW_KEY_RIGHT_ALT)
keyInWeb = 18 + 20000; // For indicating Left/Right alt, needs to be converted in JS.
else if (key == GLFW_KEY_LEFT_SUPER)
keyInWeb = 91;
else if (key == GLFW_KEY_RIGHT_SUPER)
keyInWeb = 93;
else if (key == GLFW_KEY_UP)
keyInWeb = 38;
else if (key == GLFW_KEY_DOWN)
keyInWeb = 40;
else if (key == GLFW_KEY_LEFT)
keyInWeb = 37;
else if (key == GLFW_KEY_RIGHT)
keyInWeb = 39;
else if (key == GLFW_KEY_MENU)
keyInWeb = 93 + 20000;
else if (key == GLFW_KEY_KP_ENTER)
keyInWeb = 13 + 20000; // For indicating numpad enter, needs to be converted in JS.
else if (key == GLFW_KEY_KP_ADD)
keyInWeb = 107;
else if (key == GLFW_KEY_KP_SUBTRACT)
keyInWeb = 109;
else if (key == GLFW_KEY_KP_MULTIPLY)
keyInWeb = 106;
else if (key == GLFW_KEY_KP_DIVIDE)
keyInWeb = 111;
else if (key == GLFW_KEY_NUM_LOCK)
keyInWeb = 12;
else if (key == GLFW_KEY_F13)
keyInWeb = 124;
else if (key == GLFW_KEY_BACKSPACE)
keyInWeb = 8;
else if (key == GLFW_KEY_HOME)
keyInWeb = 36;
else if (key == GLFW_KEY_PAGE_UP)
keyInWeb = 33;
else if (key == GLFW_KEY_PAGE_DOWN)
keyInWeb = 34;
else if (key == GLFW_KEY_END)
keyInWeb = 35;
else if (key == GLFW_KEY_COMMA)
keyInWeb = 188;
else if (key == GLFW_KEY_PERIOD)
keyInWeb = 190;
else if (key == GLFW_KEY_SLASH)
keyInWeb = 191;
else if (key == GLFW_KEY_SPACE)
keyInWeb = 32;
else if (key == GLFW_KEY_DELETE)
keyInWeb = 46;
else if (key == GLFW_KEY_KP_DECIMAL)
keyInWeb = 110;
else if (key == GLFW_KEY_CAPS_LOCK)
keyInWeb = 20;
return keyInWeb;
}

View File

@@ -0,0 +1,31 @@
/****************************************************************************
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 "cocos/bindings/event/EventDispatcher.h"
extern int translateKeycode(int);
extern void updateModifierKeyState(int keyCodeInWeb);
extern cc::KeyboardEvent::Action getModifierKeyAction(int keyCodeInWeb);

View File

@@ -0,0 +1,67 @@
/****************************************************************************
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 SystemWindowManager;
class MacPlatform : public UniversalPlatform {
public:
MacPlatform() = default;
/**
* Destructor of WindowPlatform.
*/
~MacPlatform() override;
/**
* Implementation of Windows platform initialization.
*/
int32_t init() override;
/**
* @brief Start base platform initialization.
*/
int32_t run(int argc, const char **argv) override;
ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override;
bool readyToExit();
void exit() override;
/**
* @brief Implement the main logic of the base platform.
*/
int32_t loop() override;
void setFps(int32_t fps) override;
void onPause() override;
void onResume() override;
void onClose() override;
void pollEvent() override;
private:
bool _readyToExit{false};
std::shared_ptr<SystemWindowManager> _windowManager{nullptr};
};
} // namespace cc

View File

@@ -0,0 +1,153 @@
/****************************************************************************
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/mac/MacPlatform.h"
#include "platform/interfaces/OSInterface.h"
#include "platform/mac/AppDelegate.h"
#include "modules/Accelerometer.h"
#include "modules/Battery.h"
#include "modules/Network.h"
#include "modules/System.h"
#include "modules/Vibrator.h"
#include "platform/SDLHelper.h"
#if defined(CC_SERVER_MODE)
#include "platform/empty/modules/Screen.h"
#include "platform/empty/modules/SystemWindow.h"
#include "platform/empty/modules/SystemWindowManager.h"
#else
#include "modules/Screen.h"
#include "modules/SystemWindow.h"
#include "modules/SystemWindowManager.h"
#endif
#import <AppKit/AppKit.h>
#include "base/memory/Memory.h"
extern int cocos_main(int argc, const char **argv);
namespace cc {
MacPlatform::~MacPlatform() {
}
int32_t MacPlatform::init() {
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>());
_windowManager = std::make_shared<SystemWindowManager>();
registerInterface(_windowManager);
registerInterface(std::make_shared<Vibrator>());
return _windowManager->init();
}
bool MacPlatform::readyToExit() {
return _readyToExit;
}
void MacPlatform::exit() {
if(!_readyToExit) {
[[NSApplication sharedApplication] replyToApplicationShouldTerminate:true];
_readyToExit = true;
}
}
int32_t MacPlatform::loop(void) {
#if CC_EDITOR
runTask();
return 1;
#else
while(!_readyToExit) {
pollEvent();
runTask();
}
onDestroy();
return 0;
#endif
}
int32_t MacPlatform::run(int argc, const char **argv) {
#if defined(CC_SERVER_MODE)
cocos_main(argc, argv);
while (true) {
runTask();
}
return 0;
#else
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
argc = static_cast<int>(arguments.count);
std::vector<const char*> argVec;
argVec.reserve(argc);
for (id arg in arguments) {
argVec.emplace_back([arg UTF8String]);
}
id delegate = [[AppDelegate alloc] init];
[NSApp setDelegate:delegate];
if(cocos_main(argc, argVec.data()) != 0) {
return -1;
}
return loop();
#endif
}
void MacPlatform::setFps(int32_t fps) {
if(fps != getFps()) {
UniversalPlatform::setFps(fps);
}
}
void MacPlatform::onPause() {
cc::WindowEvent ev;
ev.type = cc::WindowEvent::Type::HIDDEN;
cc::events::WindowEvent::broadcast(ev);
}
void MacPlatform::onResume() {
cc::WindowEvent ev;
ev.type = cc::WindowEvent::Type::SHOW;
cc::events::WindowEvent::broadcast(ev);
}
void MacPlatform::onClose() {
cc::WindowEvent ev;
ev.type = cc::WindowEvent::Type::CLOSE;
cc::events::WindowEvent::broadcast(ev);
}
cc::ISystemWindow *MacPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) {
return ccnew SystemWindow(windowId, externalHandle);
}
void MacPlatform::pollEvent() {
_windowManager->processEvent();
}
} // namespace cc

34
cocos/platform/mac/View.h Normal file
View File

@@ -0,0 +1,34 @@
/****************************************************************************
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 <AppKit/NSView.h>
#import <MetalKit/MetalKit.h>
@interface View : NSView <CALayerDelegate>
- (int)getWindowId;
@property (nonatomic, assign) id<MTLDevice> device;
@end

184
cocos/platform/mac/View.mm Normal file
View File

@@ -0,0 +1,184 @@
/****************************************************************************
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"
#import <AppKit/NSEvent.h>
#import <AppKit/NSScreen.h>
#import <AppKit/NSTouch.h>
#import <QuartzCore/QuartzCore.h>
#import "KeyCodeHelper.h"
#import "application/ApplicationManager.h"
#import "cocos/bindings/event/EventDispatcher.h"
#import "platform/mac/AppDelegate.h"
#import "platform/mac/modules/SystemWindow.h"
#import "platform/mac/modules/SystemWindowManager.h"
#include "SDL2/SDL.h"
static int MetalViewEventWatch(void* userData, SDL_Event*event) {
if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
@autoreleasepool {
auto *view = (__bridge View *)userData;
if ([view getWindowId] == event->window.windowID) {
[view viewDidChangeBackingProperties];
}
}
}
return 0;
}
@implementation View {
cc::MouseEvent _mouseEvent;
cc::KeyboardEvent _keyboardEvent;
}
- (CALayer *)makeBackingLayer {
CAMetalLayer *layer = [CAMetalLayer layer];
layer.delegate = self;
layer.autoresizingMask = true;
layer.needsDisplayOnBoundsChange = true;
return layer;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
if (self = [super initWithFrame:frameRect]) {
// View is used as a subview, so the resize message needs to be handled manually.
SDL_AddEventWatch(MetalViewEventWatch, (__bridge void*)(self));
[self.window makeFirstResponder:self];
int pixelRatio = [[NSScreen mainScreen] backingScaleFactor];
CGSize size = CGSizeMake(frameRect.size.width * pixelRatio, frameRect.size.height * pixelRatio);
// Create CAMetalLayer
self.wantsLayer = YES;
// Config metal layer
CAMetalLayer *layer = (CAMetalLayer *)self.layer;
layer.drawableSize = size;
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
layer.device = self.device = [MTLCreateSystemDefaultDevice() autorelease];
layer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
self.layerContentsPlacement = NSViewLayerContentsPlacementScaleProportionallyToFill;
// Add tracking area to receive mouse move events.
NSRect rect = {0, 0, size.width, size.height};
NSTrackingArea *trackingArea = [[[NSTrackingArea alloc] initWithRect:rect
options:(NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect)
owner:self
userInfo:nil] autorelease];
[self addTrackingArea:trackingArea];
}
return self;
}
- (void)drawInMTKView:(MTKView *)view {
//cc::Application::getInstance()->tick();
}
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size {
cc::WindowEvent ev;
ev.windowId = [self getWindowId];
ev.type = cc::WindowEvent::Type::RESIZED;
ev.width = static_cast<int>(size.width);
ev.height = static_cast<int>(size.height);
cc::events::WindowEvent::broadcast(ev);
}
- (void)displayLayer:(CALayer *)layer {
//cc::Application::getInstance()->tick();
}
- (void)setFrameSize:(NSSize)newSize {
CAMetalLayer *layer = (CAMetalLayer *)self.layer;
CGSize nativeSize = [self convertSizeToBacking:newSize];
[super setFrameSize:newSize];
layer.drawableSize = nativeSize;
}
- (void)viewDidChangeBackingProperties {
[super viewDidChangeBackingProperties];
CAMetalLayer *layer = (CAMetalLayer *)self.layer;
layer.contentsScale = self.window.backingScaleFactor;
auto size = [[self.window contentView] frame].size;
auto width = size.width * self.window.backingScaleFactor;
auto height = size.height * self.window.backingScaleFactor;
if (width > 0 && height > 0) {
[super setFrameSize:size];
layer.drawableSize = CGSizeMake(width, height);
}
}
- (void)flagsChanged:(NSEvent *)event {
int keyCode = translateKeycode(event.keyCode);
updateModifierKeyState(keyCode);
auto action = getModifierKeyAction(keyCode);
// NOTE: in some cases, flagsChanged event may return some wrong keyCodes
// For example:
// - when you long press the capslock key, you may get the keyCode -1
// - when you press ctrl + space, you may get the keyCode 65
if (action == cc::KeyboardEvent::Action::UNKNOWN) {
return;
}
_keyboardEvent.windowId = [self getWindowId];
_keyboardEvent.key = keyCode;
_keyboardEvent.action = action;
[self setModifierFlags:event];
cc::events::Keyboard::broadcast(_keyboardEvent);
}
- (void)setModifierFlags:(NSEvent *)event {
NSEventModifierFlags modifierFlags = event.modifierFlags;
if (modifierFlags & NSEventModifierFlagShift)
_keyboardEvent.shiftKeyActive = true;
else
_keyboardEvent.shiftKeyActive = false;
if (modifierFlags & NSEventModifierFlagControl)
_keyboardEvent.ctrlKeyActive = true;
else
_keyboardEvent.ctrlKeyActive = false;
if (modifierFlags & NSEventModifierFlagOption)
_keyboardEvent.altKeyActive = true;
else
_keyboardEvent.altKeyActive = false;
if (modifierFlags & NSEventModifierFlagCommand)
_keyboardEvent.metaKeyActive = true;
else
_keyboardEvent.metaKeyActive = false;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (int)getWindowId {
auto *windowMgr = CC_GET_PLATFORM_INTERFACE(cc::SystemWindowManager);
auto *window = windowMgr->getWindowFromNSWindow([self window]);
return window->getWindowId();
}
@end

View 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:
/**
* To enable or disable accelerometer.
*/
void setAccelerometerEnabled(bool isEnabled) override;
/**
* Sets the interval of accelerometer.
*/
void setAccelerometerInterval(float interval) override;
/**
* Gets the motion value of current device.
*/
const MotionValue &getDeviceMotionValue() override;
};
} // namespace cc

View File

@@ -0,0 +1,40 @@
/****************************************************************************
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/mac/modules/Accelerometer.h"
namespace cc {
void Accelerometer::setAccelerometerEnabled(bool isEnabled) {
}
void Accelerometer::setAccelerometerInterval(float interval) {
}
const Accelerometer::MotionValue& Accelerometer::getDeviceMotionValue() {
static MotionValue motionValue;
return motionValue;
}
} // namespace cc

View 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

View File

@@ -0,0 +1,34 @@
/****************************************************************************
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/mac/modules/Battery.h"
namespace cc {
float Battery::getBatteryLevel() const {
return 1.0F;
}
} // namespace cc

View 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

View File

@@ -0,0 +1,58 @@
/****************************************************************************
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/mac/modules/Network.h"
#import <AppKit/AppKit.h>
#include <Cocoa/Cocoa.h>
#include <Foundation/Foundation.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

View 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

View File

@@ -0,0 +1,88 @@
/****************************************************************************
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/mac/modules/Screen.h"
#import <AppKit/AppKit.h>
#include <Cocoa/Cocoa.h>
#include <Foundation/Foundation.h>
#include "base/Macros.h"
#include "cocos/bindings/jswrapper/SeApi.h"
#include "platform/interfaces/modules/ISystemWindowManager.h"
#include "platform/mac/modules/SystemWindow.h"
#include "application/ApplicationManager.h"
namespace cc {
int Screen::getDPI() const {
NSScreen *screen = [NSScreen mainScreen];
NSDictionary *description = [screen deviceDescription];
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
return ((displayPixelSize.width / displayPhysicalSize.width) * 25.4f);
}
float Screen::getDevicePixelRatio() const {
#if CC_EDITOR
auto* global = se::ScriptEngine::getInstance()->getGlobalObject();
se::Value devicePixelRatioVal;
global->getProperty("devicePixelRatio", &devicePixelRatioVal);
return devicePixelRatioVal.isNumber() ? devicePixelRatioVal.toFloat() : 1.F;
#else
auto* systemWindow = static_cast<SystemWindow*>(CC_GET_MAIN_SYSTEM_WINDOW());
auto* nsWindow = systemWindow->getNSWindow();
return [nsWindow backingScaleFactor];
#endif
}
void Screen::setKeepScreenOn(bool value) {
CC_UNUSED_PARAM(value);
}
Screen::Orientation Screen::getDeviceOrientation() const {
return Orientation::PORTRAIT;
}
Vec4 Screen::getSafeAreaEdge() const {
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

View File

@@ -0,0 +1,66 @@
/****************************************************************************
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

View File

@@ -0,0 +1,107 @@
/****************************************************************************
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/mac/modules/System.h"
#import <AppKit/AppKit.h>
#include <sys/utsname.h>
#include <algorithm>
#include <mutex>
#include <sstream>
namespace cc {
using OSType = System::OSType;
OSType System::getOSType() const {
return OSType::MAC;
}
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 {
NSOperatingSystemVersion v = NSProcessInfo.processInfo.operatingSystemVersion;
char version[50] = {0};
snprintf(version, sizeof(version), "%d.%d.%d", (int)v.majorVersion, (int)v.minorVersion, (int)v.patchVersion);
return version;
}
bool System::openURL(const ccstd::string &url) {
NSString *msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
NSURL *nsUrl = [NSURL URLWithString:msg];
return [[NSWorkspace sharedWorkspace] openURL:nsUrl];
}
void System::copyTextToClipboard(const std::string &text) {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
NSString *tmp = [NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding];
[pasteboard setString:tmp forType:NSPasteboardTypeString];
}
} // namespace cc

View File

@@ -0,0 +1,72 @@
/****************************************************************************
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 <AppKit/AppKit.h>
#include "platform/interfaces/modules/ISystemWindow.h"
struct SDL_Window;
namespace cc {
class SDLHelper;
class CC_DLL SystemWindow : public ISystemWindow {
friend class SystemWindowManager;
public:
explicit SystemWindow(uint32_t windowId, void* externalHandle);
~SystemWindow() override;
bool createWindow(const char* title,
int w, int h, int flags) override;
bool createWindow(const char* title,
int x, int y, int w,
int h, int flags) override;
void closeWindow() override;
virtual uint32_t getWindowId() const override { return _windowId; }
uintptr_t getWindowHandle() const override;
Size getViewSize() const override;
void setViewSize(uint32_t w, uint32_t h) override {
_width = w;
_height = h;
}
/*
@brief enable/disable(lock) the cursor, default is enabled
*/
void setCursorEnabled(bool value) override;
NSWindow* getNSWindow() const;
private:
SDL_Window* getSDLWindow() const { return _window; }
void initWindowProperty(SDL_Window* window, const char *title, int x, int y, int w, int h);
uint32_t _width{0};
uint32_t _height{0};
uint32_t _windowId{0};
uintptr_t _windowHandle{0};
SDL_Window* _window{nullptr};
};
} // namespace cc

View File

@@ -0,0 +1,138 @@
/****************************************************************************
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.
****************************************************************************/
#include "platform/mac/modules/SystemWindow.h"
#include "platform/mac/View.h"
#include "base/Log.h"
#include "base/Macros.h"
// SDL headers
#include <functional>
#include "SDL2/SDL.h"
#include "SDL2/SDL_main.h"
#include "SDL2/SDL_syswm.h"
#include "engine/EngineEvents.h"
#include "platform/SDLHelper.h"
#import <AppKit/NSView.h>
#import <AppKit/NSWindow.h>
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
#include "platform/interfaces/modules/IScreen.h"
#include "platform/BasePlatform.h"
namespace cc {
SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
: _windowId(windowId) {
if (externalHandle) {
_windowHandle = reinterpret_cast<uintptr_t>(externalHandle);
}
}
SystemWindow::~SystemWindow() {
_windowHandle = 0;
_windowId = 0;
}
void SystemWindow::initWindowProperty(SDL_Window* window, const char *title, int x, int y, int w, int h) {
CC_ASSERT(window != nullptr);
auto* nsWindow = reinterpret_cast<NSWindow*>(SDLHelper::getWindowHandle(window));
NSString *astring = [NSString stringWithUTF8String:title];
nsWindow.title = astring;
// contentView is created internally by sdl.
NSView *view = nsWindow.contentView;
auto* newView = [[View alloc] initWithFrame:view.frame];
[view addSubview:newView];
[nsWindow.contentView setWantsBestResolutionOpenGLSurface:YES];
[nsWindow makeKeyAndOrderFront:nil];
_windowHandle = reinterpret_cast<uintptr_t>(newView);
auto dpr = [nsWindow backingScaleFactor];
_width = w * dpr;
_height = h * dpr;
}
NSWindow* SystemWindow::getNSWindow() const {
CC_ASSERT(_window != nullptr);
return reinterpret_cast<NSWindow*>(SDLHelper::getWindowHandle(_window));
}
bool SystemWindow::createWindow(const char *title,
int w, int h, int flags) {
#if CC_EDITOR
return createWindow(title, 0, 0, w, h, flags);
#else
_window = SDLHelper::createWindow(title, w, h, flags);
if (!_window) {
return false;
}
Vec2 pos = SDLHelper::getWindowPosition(_window);
initWindowProperty(_window, title, pos.x, pos.y, w, h);
return true;
#endif
}
bool SystemWindow::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
#if CC_EDITOR
_width = w;
_height = h;
CAMetalLayer *layer = [[CAMetalLayer layer] retain];
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
layer.frame = CGRectMake(x, y, w, h);
[layer setAnchorPoint:CGPointMake(0.f, 0.f)];
_windowHandle = reinterpret_cast<uintptr_t>(layer);
return true;
#else
_window = SDLHelper::createWindow(title, x, y, w, h, flags);
if (!_window) {
return false;
}
initWindowProperty(_window, title, x, y, w, h);
return true;
#endif
}
void SystemWindow::closeWindow() {
#ifndef CC_SERVER_MODE
SDL_Event et;
et.type = SDL_QUIT;
SDL_PushEvent(&et);
#endif
}
uintptr_t SystemWindow::getWindowHandle() const {
return _windowHandle;
}
void SystemWindow::setCursorEnabled(bool value) {
SDLHelper::setCursorEnabled(value);
}
SystemWindow::Size SystemWindow::getViewSize() const {
return Size{static_cast<float>(_width), static_cast<float>(_height)};
}
} // namespace cc

View 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 <AppKit/AppKit.h>
#include "base/std/container/unordered_map.h"
#include "platform/interfaces/modules/ISystemWindowManager.h"
struct SDL_Window;
namespace cc {
class ISystemWindow;
class SystemWindowManager : public ISystemWindowManager {
public:
SystemWindowManager() = default;
int init() override;
void processEvent() override;
ISystemWindow *createWindow(const ISystemWindowInfo &info) override;
ISystemWindow *getWindow(uint32_t windowId) const override;
const SystemWindowMap &getWindows() const override { return _windows; }
ISystemWindow *getWindowFromSDLWindow(SDL_Window *window) const;
ISystemWindow *getWindowFromNSWindow(NSWindow *window) const;
private:
uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID
SystemWindowMap _windows;
};
} // namespace cc

View File

@@ -0,0 +1,98 @@
/****************************************************************************
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.
****************************************************************************/
#include "SystemWindowManager.h"
#include "SDL2/SDL_events.h"
#include "platform/BasePlatform.h"
#include "platform/SDLHelper.h"
#include "platform/interfaces/modules/ISystemWindowManager.h"
#include "platform/mac/modules/SystemWindow.h"
namespace cc {
int SystemWindowManager::init() {
return SDLHelper::init();
}
void SystemWindowManager::processEvent() {
SDL_Event sdlEvent;
while (SDL_PollEvent(&sdlEvent) != 0) {
SDL_Window *sdlWindow = SDL_GetWindowFromID(sdlEvent.window.windowID);
// SDL_Event like SDL_QUIT does not associate a window
if (!sdlWindow) {
SDLHelper::dispatchSDLEvent(0, sdlEvent);
} else {
ISystemWindow *window = getWindowFromSDLWindow(sdlWindow);
CC_ASSERT(window);
uint32_t windowId = window->getWindowId();
SDLHelper::dispatchSDLEvent(windowId, sdlEvent);
}
}
}
ISystemWindow *SystemWindowManager::createWindow(const ISystemWindowInfo &info) {
ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle);
if (window) {
if (!info.externalHandle) {
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::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;
}
cc::ISystemWindow *SystemWindowManager::getWindowFromSDLWindow(SDL_Window *window) const {
for (const auto &iter : _windows) {
SystemWindow *sysWindow = static_cast<SystemWindow *>(iter.second.get());
SDL_Window *sdlWindow = sysWindow->getSDLWindow();
if (sdlWindow == window) {
return sysWindow;
}
}
return nullptr;
}
ISystemWindow *SystemWindowManager::getWindowFromNSWindow(NSWindow *window) const {
for (const auto &iter : _windows) {
SystemWindow *sysWindow = static_cast<SystemWindow *>(iter.second.get());
NSWindow *nsWindow = sysWindow->getNSWindow();
if (nsWindow == window) {
return sysWindow;
}
}
return nullptr;
}
} // namespace cc

View 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

View 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/mac/modules/Vibrator.h"
#include "base/Macros.h"
namespace cc {
void Vibrator::vibrate(float duration) {
CC_UNUSED_PARAM(duration);
}
} // namespace cc