From 9be1356d6705df5227e7e5f19654b4aed2d6241e Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 23 Mar 2020 16:22:54 +0800 Subject: [PATCH] System adapter (#2318) * move View to engine on mac * move View to engine on iOS * add CC prefix for file names * handle input events in engine on Android --- .../cocos2d_libs.xcodeproj/project.pbxproj | 24 +++ native/cocos/platform/android/Android.mk | 3 +- native/cocos/platform/android/CCView.cpp | 145 ++++++++++++++ native/cocos/platform/android/CCView.h | 43 ++++ .../View.h => cocos/platform/ios/CCView.h} | 0 .../View.mm => cocos/platform/ios/CCView.mm} | 3 +- .../platform/mac/CCKeyCodeHelper.cpp} | 25 ++- native/cocos/platform/mac/CCKeyCodeHelper.h | 26 +++ native/cocos/platform/mac/CCView.h | 28 +++ native/cocos/platform/mac/CCView.mm | 168 ++++++++++++++++ .../app/jni/hellojavascript/main.cpp | 189 ++++-------------- .../HelloJavascript.xcodeproj/project.pbxproj | 12 -- .../proj.ios_mac/ios/AppDelegate.mm | 2 +- .../proj.ios_mac/ios/ViewController.mm | 4 - .../proj.ios_mac/mac/KeyCodeHelper.h | 3 - .../proj.ios_mac/mac/ViewController.mm | 154 +------------- 16 files changed, 506 insertions(+), 323 deletions(-) create mode 100644 native/cocos/platform/android/CCView.cpp create mode 100644 native/cocos/platform/android/CCView.h rename native/{templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/View.h => cocos/platform/ios/CCView.h} (100%) rename native/{templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/View.mm => cocos/platform/ios/CCView.mm} (99%) rename native/{templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.cpp => cocos/platform/mac/CCKeyCodeHelper.cpp} (91%) create mode 100644 native/cocos/platform/mac/CCKeyCodeHelper.h create mode 100644 native/cocos/platform/mac/CCView.h create mode 100644 native/cocos/platform/mac/CCView.mm delete mode 100644 native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.h diff --git a/native/build/cocos2d_libs.xcodeproj/project.pbxproj b/native/build/cocos2d_libs.xcodeproj/project.pbxproj index 4f78c5665d..a25a4ae2f4 100644 --- a/native/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/native/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -250,6 +250,12 @@ 461DCA5A20C7E4BA00B22827 /* JavaScriptObjCBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 461DCA5720C7E4B900B22827 /* JavaScriptObjCBridge.h */; }; 461DCA5B20C7E4BA00B22827 /* JavaScriptObjCBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 461DCA5720C7E4B900B22827 /* JavaScriptObjCBridge.h */; }; 4626B1FF202D9BD800BE30B9 /* CCApplication-mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4626B1FE202D9BD800BE30B9 /* CCApplication-mac.mm */; }; + 4649F62224289EBE00BEDA2A /* CCView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4649F62024289EBD00BEDA2A /* CCView.mm */; }; + 4649F62324289EBE00BEDA2A /* CCView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4649F62124289EBE00BEDA2A /* CCView.h */; }; + 4649F62824289F5100BEDA2A /* CCView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4649F62424289F5000BEDA2A /* CCView.mm */; }; + 4649F62924289F5100BEDA2A /* CCKeyCodeHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 4649F62524289F5000BEDA2A /* CCKeyCodeHelper.h */; }; + 4649F62A24289F5100BEDA2A /* CCKeyCodeHelper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4649F62624289F5000BEDA2A /* CCKeyCodeHelper.cpp */; }; + 4649F62B24289F5100BEDA2A /* CCView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4649F62724289F5000BEDA2A /* CCView.h */; }; 468A967F22F43F4C005034BE /* Class.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A52DB19205BCD9200350EE3 /* Class.cpp */; }; 468A968022F43F4F005034BE /* Object.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A52DB08205BCD9200350EE3 /* Object.cpp */; }; 468A968122F43F53005034BE /* ObjectWrap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A52DAFE205BCD9200350EE3 /* ObjectWrap.cpp */; }; @@ -1121,6 +1127,12 @@ 461DCA5620C7E4B900B22827 /* JavaScriptObjCBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JavaScriptObjCBridge.mm; sourceTree = ""; }; 461DCA5720C7E4B900B22827 /* JavaScriptObjCBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JavaScriptObjCBridge.h; sourceTree = ""; }; 4626B1FE202D9BD800BE30B9 /* CCApplication-mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "CCApplication-mac.mm"; sourceTree = ""; }; + 4649F62024289EBD00BEDA2A /* CCView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CCView.mm; sourceTree = ""; }; + 4649F62124289EBE00BEDA2A /* CCView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCView.h; sourceTree = ""; }; + 4649F62424289F5000BEDA2A /* CCView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CCView.mm; sourceTree = ""; }; + 4649F62524289F5000BEDA2A /* CCKeyCodeHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCKeyCodeHelper.h; sourceTree = ""; }; + 4649F62624289F5000BEDA2A /* CCKeyCodeHelper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCKeyCodeHelper.cpp; sourceTree = ""; }; + 4649F62724289F5000BEDA2A /* CCView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCView.h; sourceTree = ""; }; 468A968422F43F8F005034BE /* libv8_monolith.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_monolith.a; path = ../external/ios/libs/libv8_monolith.a; sourceTree = ""; }; 468A968622F440AC005034BE /* libuv_a.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libuv_a.a; path = ../external/ios/libs/libuv_a.a; sourceTree = ""; }; 469302292046AE05004A3D6C /* RefCounter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = RefCounter.hpp; sourceTree = ""; }; @@ -2159,6 +2171,8 @@ 503DD8CD1926736A00CD74DD /* ios */ = { isa = PBXGroup; children = ( + 4649F62124289EBE00BEDA2A /* CCView.h */, + 4649F62024289EBD00BEDA2A /* CCView.mm */, 46BFDFC123CC136500CC5F8E /* CCDevice-ios.mm */, 46BFDF9923C870A000CC5F8E /* CCApplication-ios.mm */, 051C016F21E2F85C00D4A347 /* CCModuleConfigIOS.debug.xcconfig */, @@ -2209,6 +2223,10 @@ 50ABBF4E1926664700A911A9 /* mac */ = { isa = PBXGroup; children = ( + 4649F62624289F5000BEDA2A /* CCKeyCodeHelper.cpp */, + 4649F62524289F5000BEDA2A /* CCKeyCodeHelper.h */, + 4649F62724289F5000BEDA2A /* CCView.h */, + 4649F62424289F5000BEDA2A /* CCView.mm */, 051C017221E2F86900D4A347 /* CCModuleConfigMac.debug.xcconfig */, 051C017121E2F86900D4A347 /* CCModuleConfigMac.release.xcconfig */, 4626B1FE202D9BD800BE30B9 /* CCApplication-mac.mm */, @@ -2767,6 +2785,7 @@ BA06A6AE23583DBD007740C5 /* GFXBindingLayout.h in Headers */, 1AAAC8F5205CB6E9005321B9 /* Export.h in Headers */, BA06A85623584B92007740C5 /* egl.h in Headers */, + 4649F62B24289F5100BEDA2A /* CCView.h in Headers */, BA06A6CE23583DBD007740C5 /* GFXCommandBuffer.h in Headers */, 1A28FF5B1F20AFAB007A1D9D /* NSURLRequest+SRWebSocketPrivate.h in Headers */, ED30577D1BEC76C90083C3ED /* crypt.h in Headers */, @@ -2826,6 +2845,7 @@ 1A28FF851F20AFAB007A1D9D /* SRSIMDHelpers.h in Headers */, 50ABBD401925AB0000A911A9 /* CCMath.h in Headers */, ED3057831BEC76C90083C3ED /* unzip.h in Headers */, + 4649F62924289F5100BEDA2A /* CCKeyCodeHelper.h in Headers */, BA06A6EA23583DBD007740C5 /* Assertion.h in Headers */, BA06A6B823583DBD007740C5 /* GFXWindow.h in Headers */, 46ACCBFE241F27A70096CC35 /* GFXObject.h in Headers */, @@ -3050,6 +3070,7 @@ BA06A68723583DBD007740C5 /* Memory.h in Headers */, 4008729720CE20C2002EB77B /* jsb_cocos2dx_network_manual.h in Headers */, 50ABBD411925AB0000A911A9 /* CCMath.h in Headers */, + 4649F62324289EBE00BEDA2A /* CCView.h in Headers */, 461786682052607E008256E1 /* jsb_websocket.hpp in Headers */, BAC32B68236C28C600F3D55F /* tommyhashtbl.h in Headers */, 4617865E2052607E008256E1 /* jsb_xmlhttprequest.hpp in Headers */, @@ -3335,6 +3356,7 @@ 469FFAD6240E555700581BFC /* GLES2Context.cc in Sources */, 4626B1FF202D9BD800BE30B9 /* CCApplication-mac.mm in Sources */, 5027253C190BF1B900AAF4ED /* cocos2d.cpp in Sources */, + 4649F62A24289F5100BEDA2A /* CCKeyCodeHelper.cpp in Sources */, 1AAAC8EB205CB6E9005321B9 /* AudioCache.mm in Sources */, 46AE3FF92092F3A600F3A228 /* inspector_socket.cc in Sources */, 50ABBD501925AB0000A911A9 /* Quaternion.cpp in Sources */, @@ -3386,6 +3408,7 @@ BA06A6CA23583DBD007740C5 /* GFXRenderPass.cc in Sources */, 469FFB00240E55FF00581BFC /* GLES2Buffer.cc in Sources */, BA06A6BA23583DBD007740C5 /* GFXDef.cc in Sources */, + 4649F62824289F5100BEDA2A /* CCView.mm in Sources */, 1ABAD24E20C29F3800BC71C0 /* CCCanvasRenderingContext2D-apple.mm in Sources */, ED30577E1BEC76C90083C3ED /* ioapi_mem.cpp in Sources */, BA06A6E823583DBD007740C5 /* UTFString.cc in Sources */, @@ -3569,6 +3592,7 @@ BA06A78623583DFE007740C5 /* GLES2Window.cc in Sources */, ED30578A1BEC77460083C3ED /* tinyxml2.cpp in Sources */, 1A29D76A205665BE00168D9A /* jsb_cocos2dx_auto.cpp in Sources */, + 4649F62224289EBE00BEDA2A /* CCView.mm in Sources */, BA06A6F523583DBD007740C5 /* Log.cc in Sources */, 46BFDFC223CC136500CC5F8E /* CCDevice-ios.mm in Sources */, BAC32B4E236C28C600F3D55F /* tommyhashtbl.c in Sources */, diff --git a/native/cocos/platform/android/Android.mk b/native/cocos/platform/android/Android.mk index cd3376a85f..5d707f516e 100644 --- a/native/cocos/platform/android/Android.mk +++ b/native/cocos/platform/android/Android.mk @@ -10,9 +10,10 @@ LOCAL_SRC_FILES := \ CCDevice-android.cpp \ CCFileUtils-android.cpp \ CCApplication-android.cpp \ +CCView.cpp \ CCCanvasRenderingContext2D-android.cpp \ jni/JniImp.cpp \ -jni/JniHelper.cpp \ +jni/JniHelper.cpp LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) diff --git a/native/cocos/platform/android/CCView.cpp b/native/cocos/platform/android/CCView.cpp new file mode 100644 index 0000000000..dd8b31f97b --- /dev/null +++ b/native/cocos/platform/android/CCView.cpp @@ -0,0 +1,145 @@ +/**************************************************************************** +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2013-2016 Chukong Technologies Inc. +Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + 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 "CCView.h" +#include +#include "scripting/js-bindings/event/EventDispatcher.h" + +namespace +{ + struct cocos2d::TouchEvent touchEvent; + struct cocos2d::KeyboardEvent keyboardEvent; + + // key values in web, refer to http://docs.cocos.com/creator/api/en/enums/KEY.html + std::unordered_map keyCodeMap = { + {AKEYCODE_BACK, 6}, + {AKEYCODE_ENTER, 13}, + {AKEYCODE_MENU, 18}, + {AKEYCODE_DPAD_UP, 1003}, + {AKEYCODE_DPAD_DOWN, 1004}, + {AKEYCODE_DPAD_LEFT, 1000}, + {AKEYCODE_DPAD_RIGHT, 1001}, + {AKEYCODE_DPAD_CENTER, 1005} + }; + + void dispatchTouchEvent(int index, AInputEvent* event, cocos2d::TouchEvent& touchEvent) { + int pointerID = AMotionEvent_getPointerId(event, index); + touchEvent.touches.push_back({ + AMotionEvent_getX(event, index), // x + AMotionEvent_getY(event, index), // y + pointerID}); + + cocos2d::EventDispatcher::dispatchTouchEvent(touchEvent); + } + + void dispatchTouchEvents(AInputEvent* event, cocos2d::TouchEvent& touchEvent) { + size_t pointerCount = AMotionEvent_getPointerCount(event); + for (size_t i = 0; i < pointerCount; ++i) { + touchEvent.touches.push_back({ + AMotionEvent_getX(event, i), + AMotionEvent_getY(event, i), + AMotionEvent_getPointerId(event, i) + }); + } + + cocos2d::EventDispatcher::dispatchTouchEvent(touchEvent); + touchEvent.touches.clear(); + } +} + +NS_CC_BEGIN + +void View::engineHandleCmd(struct android_app* app, int32_t cmd) +{ + // Handle CMD here if needed. +} + +/** + * Process the next input event. + */ +int32_t View::engineHandleInput(struct android_app* app, AInputEvent* event) { + int type = AInputEvent_getType(event); + + if(type == AINPUT_EVENT_TYPE_KEY){ + int action = AKeyEvent_getAction(event); + if (action == AKEY_EVENT_ACTION_MULTIPLE) + return 0; + + int keyCode = AKeyEvent_getKeyCode(event); + if (keyCodeMap.count(keyCode) > 0) + keyCode = keyCodeMap[keyCode]; + else + keyCode = 0; + + keyboardEvent.key = keyCode; + keyboardEvent.action = action == AKEY_EVENT_ACTION_DOWN + ? cocos2d::KeyboardEvent::Action::RELEASE + : cocos2d::KeyboardEvent::Action::PRESS; + cocos2d::EventDispatcher::dispatchKeyboardEvent(keyboardEvent); + + return 1; + } else if (type == AINPUT_EVENT_TYPE_MOTION){ + int action = AMotionEvent_getAction(event); + + switch (action & AMOTION_EVENT_ACTION_MASK) { + case AMOTION_EVENT_ACTION_DOWN: + touchEvent.type = cocos2d::TouchEvent::Type::BEGAN; + dispatchTouchEvent(0, event, touchEvent); + break; + case AMOTION_EVENT_ACTION_POINTER_DOWN: + touchEvent.type = cocos2d::TouchEvent::Type::BEGAN; + dispatchTouchEvent((action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT, + event, + touchEvent); + break; + case AMOTION_EVENT_ACTION_UP: + touchEvent.type = cocos2d::TouchEvent::Type::ENDED; + dispatchTouchEvent(0, event, touchEvent); + break; + case AMOTION_EVENT_ACTION_POINTER_UP: + touchEvent.type = cocos2d::TouchEvent::Type::ENDED; + dispatchTouchEvent((action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT, + event, + touchEvent); + break; + case AMOTION_EVENT_ACTION_MOVE: + touchEvent.type = cocos2d::TouchEvent::Type::MOVED; + dispatchTouchEvents(event, touchEvent); + break; + case AMOTION_EVENT_ACTION_CANCEL: + touchEvent.type = cocos2d::TouchEvent::Type::CANCELLED; + dispatchTouchEvents(event, touchEvent); + break; + default: + return 0; + } + + return 1; + } + + return 0; +} + +NS_CC_END diff --git a/native/cocos/platform/android/CCView.h b/native/cocos/platform/android/CCView.h new file mode 100644 index 0000000000..55802fcbd8 --- /dev/null +++ b/native/cocos/platform/android/CCView.h @@ -0,0 +1,43 @@ +/**************************************************************************** + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + 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/ccMacros.h" +#include + +struct android_app; +struct AInputEvent; + +NS_CC_BEGIN + +class TouchEvent; + +class View +{ +public: + static void engineHandleCmd(struct android_app* app, int32_t cmd); + static int32_t engineHandleInput(struct android_app* app, AInputEvent* event); +}; + +NS_CC_END diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/View.h b/native/cocos/platform/ios/CCView.h similarity index 100% rename from native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/View.h rename to native/cocos/platform/ios/CCView.h diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/View.mm b/native/cocos/platform/ios/CCView.mm similarity index 99% rename from native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/View.mm rename to native/cocos/platform/ios/CCView.mm index 493965a331..1390fa9778 100644 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/View.mm +++ b/native/cocos/platform/ios/CCView.mm @@ -21,8 +21,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#import "View.h" - +#import "CCView.h" #include "scripting/js-bindings/event/EventDispatcher.h" namespace diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.cpp b/native/cocos/platform/mac/CCKeyCodeHelper.cpp similarity index 91% rename from native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.cpp rename to native/cocos/platform/mac/CCKeyCodeHelper.cpp index a2be3fcdc7..486f2278fd 100644 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.cpp +++ b/native/cocos/platform/mac/CCKeyCodeHelper.cpp @@ -1,4 +1,27 @@ -#include "KeyCodeHelper.h" +/**************************************************************************** + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + 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 "CCKeyCodeHelper.h" #include // Copy from glfw3.h diff --git a/native/cocos/platform/mac/CCKeyCodeHelper.h b/native/cocos/platform/mac/CCKeyCodeHelper.h new file mode 100644 index 0000000000..aac722c0ed --- /dev/null +++ b/native/cocos/platform/mac/CCKeyCodeHelper.h @@ -0,0 +1,26 @@ +/**************************************************************************** + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + 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 + +extern int translateKeycode(int); diff --git a/native/cocos/platform/mac/CCView.h b/native/cocos/platform/mac/CCView.h new file mode 100644 index 0000000000..7fcb2c4c3f --- /dev/null +++ b/native/cocos/platform/mac/CCView.h @@ -0,0 +1,28 @@ +/**************************************************************************** + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + 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. + ****************************************************************************/ +#import + +@interface View : NSView + +@end diff --git a/native/cocos/platform/mac/CCView.mm b/native/cocos/platform/mac/CCView.mm new file mode 100644 index 0000000000..fb032c60b9 --- /dev/null +++ b/native/cocos/platform/mac/CCView.mm @@ -0,0 +1,168 @@ +/**************************************************************************** + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + 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. + ****************************************************************************/ +#import "CCView.h" +#import +#import +#import "CCKeyCodeHelper.h" +#import "scripting/js-bindings/event/EventDispatcher.h" + +@implementation View { + cocos2d::MouseEvent _mouseEvent; + cocos2d::KeyboardEvent _keyboardEvent; +} + +- (instancetype)initWithFrame:(NSRect)frameRect { + if (self = [super initWithFrame:frameRect]) { + [self.window makeFirstResponder:self]; + } + return self; +} + +- (void)keyDown:(NSEvent *)event { + _keyboardEvent.key = translateKeycode(event.keyCode); + _keyboardEvent.action = [event isARepeat] ? cocos2d::KeyboardEvent::Action::REPEAT + : cocos2d::KeyboardEvent::Action::PRESS; + [self setModifierFlags:event]; + cocos2d::EventDispatcher::dispatchKeyboardEvent(_keyboardEvent); +} + +- (void)keyUp:(NSEvent *)event { + _keyboardEvent.key = translateKeycode(event.keyCode); + _keyboardEvent.action = cocos2d::KeyboardEvent::Action::RELEASE; + [self setModifierFlags:event]; + cocos2d::EventDispatcher::dispatchKeyboardEvent(_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; +} + +- (void)mouseDown:(NSEvent *)event { + [self sendMouseEvent:0 + type:cocos2d::MouseEvent::Type::DOWN + event:event]; +} + +- (void)mouseUp:(NSEvent *)event { + [self sendMouseEvent:0 + type:cocos2d::MouseEvent::Type::UP + event:event]; +} + +- (void)mouseDragged:(NSEvent *)event { + [self mouseMoved:event]; +} + +- (void)mouseMoved:(NSEvent *)event { + [self sendMouseEvent:0 + type:cocos2d::MouseEvent::Type::MOVE + event:event]; +} + +- (void)otherMouseDown:(NSEvent *)event { + [self sendMouseEvent:[self translateButtonNumber:event.buttonNumber] + type:cocos2d::MouseEvent::Type::DOWN + event:event]; +} + +- (void)otherMouseUp:(NSEvent *)event { + [self sendMouseEvent:[self translateButtonNumber:event.buttonNumber] + type:cocos2d::MouseEvent::Type::UP + event:event]; +} + +- (int)translateButtonNumber:(int) buttonNumber { + if (buttonNumber == 1) // left + return 0; + else if (buttonNumber == 2) // right + return 2; + else + return 1; +} + +- (void)scrollWheel:(NSEvent *)event { + double deltaX = [event scrollingDeltaX]; + double deltaY = [event scrollingDeltaY]; + + if ([event hasPreciseScrollingDeltas]) { + deltaX *= 0.1; + deltaY *= 0.1; + } + + if (fabs(deltaX) > 0.0 || fabs(deltaY) > 0.0) { + _mouseEvent.type = cocos2d::MouseEvent::Type::WHEEL; + _mouseEvent.button = 0; + _mouseEvent.x = deltaX; + _mouseEvent.y = deltaY; + cocos2d::EventDispatcher::dispatchMouseEvent(_mouseEvent); + } +} + +- (void)rightMouseDown:(NSEvent *)event { + [self sendMouseEvent:2 + type:cocos2d::MouseEvent::Type::DOWN + event:event]; +} + +- (void)rightMouseUp:(NSEvent *)event { + [self sendMouseEvent:2 + type:cocos2d::MouseEvent::Type::UP + event:event]; +} + +- (BOOL)acceptsFirstResponder { + return YES; +} + +- (void)sendMouseEvent:(int)button type:(cocos2d::MouseEvent::Type)type event:(NSEvent*)event { + const NSRect contentRect = [self frame]; + const NSPoint pos = [event locationInWindow]; + + _mouseEvent.type = type; + _mouseEvent.button = button; + _mouseEvent.x = pos.x; + _mouseEvent.y = contentRect.size.height - pos.y; + cocos2d::EventDispatcher::dispatchMouseEvent(_mouseEvent); +} + +@end diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp b/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp index dda31570b0..6831aec04f 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp @@ -27,9 +27,8 @@ #include #include #include "platform/android/CCFileUtils-android.h" -#include "platform/android/jni/JniHelper.h" +#include "platform/android/CCView.h" #include "Game.h" -#include "scripting/js-bindings/event/EventDispatcher.h" #define LOG_TAG "main" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) @@ -37,167 +36,63 @@ namespace { Game *game = nullptr; - struct cocos2d::TouchEvent touchEvent; - struct cocos2d::KeyboardEvent keyboardEvent; /** - * Shared state for our app. - */ + * Shared state for our app. + */ struct SavedState { struct android_app* app = nullptr; int animating = 0; - int32_t width = 0; - int32_t height = 0; bool initialized = false; }; +} - void engineHandleCmd(struct android_app* app, int32_t cmd) +void handle_cmd(struct android_app* app, int32_t cmd) +{ + struct SavedState* state = (struct SavedState*)app->userData; + switch (cmd) { - struct SavedState* state = (struct SavedState*)app->userData; - switch (cmd) - { - case APP_CMD_INIT_WINDOW: - if (state->app->window) - { - state->width = ANativeWindow_getWidth(app->window); - state->height = ANativeWindow_getHeight(app->window); - game = new Game(state->width, state->height); - game->init(); - state->initialized = true; - } - break; - case APP_CMD_TERM_WINDOW: - state->animating = 0; - break; - case APP_CMD_GAINED_FOCUS: - state->animating = 1; - break; - case APP_CMD_LOST_FOCUS: - state->animating = 0; - break; - case APP_CMD_PAUSE: - if (state->initialized) - cocos2d::Application::getInstance()->onPause(); - break; - case APP_CMD_RESUME: - if (state->initialized) - cocos2d::Application::getInstance()->onResume(); - break; - default: - break; - } - } - - void dispatchTouchEvent(int index, AInputEvent* event, cocos2d::TouchEvent& touchEvent) { - int pointerID = AMotionEvent_getPointerId(event, index); - touchEvent.touches.push_back({ - AMotionEvent_getX(event, index), // x - AMotionEvent_getY(event, index), // y - pointerID}); - - cocos2d::EventDispatcher::dispatchTouchEvent(touchEvent); - } - - void dispatchTouchEvents(AInputEvent* event, cocos2d::TouchEvent& touchEvent) { - size_t pointerCount = AMotionEvent_getPointerCount(event); - for (size_t i = 0; i < pointerCount; ++i) { - touchEvent.touches.push_back({ - AMotionEvent_getX(event, i), - AMotionEvent_getY(event, i), - AMotionEvent_getPointerId(event, i) - }); - } - - cocos2d::EventDispatcher::dispatchTouchEvent(touchEvent); - touchEvent.touches.clear(); - } - - // key values in web, refer to http://docs.cocos.com/creator/api/en/enums/KEY.html - std::unordered_map keyCodeMap = { - {AKEYCODE_BACK, 6}, - {AKEYCODE_ENTER, 13}, - {AKEYCODE_MENU, 18}, - {AKEYCODE_DPAD_UP, 1003}, - {AKEYCODE_DPAD_DOWN, 1004}, - {AKEYCODE_DPAD_LEFT, 1000}, - {AKEYCODE_DPAD_RIGHT, 1001}, - {AKEYCODE_DPAD_CENTER, 1005} - }; - - /** - * Process the next input event. - */ - int32_t engineHandleInput(struct android_app* app, AInputEvent* event) { - int type = AInputEvent_getType(event); - - if(type == AINPUT_EVENT_TYPE_KEY){ - int action = AKeyEvent_getAction(event); - if (action == AKEY_EVENT_ACTION_MULTIPLE) - return 0; - - int keyCode = AKeyEvent_getKeyCode(event); - if (keyCodeMap.count(keyCode) > 0) - keyCode = keyCodeMap[keyCode]; - else - keyCode = 0; - - keyboardEvent.key = keyCode; - keyboardEvent.action = action == AKEY_EVENT_ACTION_DOWN - ? cocos2d::KeyboardEvent::Action::RELEASE - : cocos2d::KeyboardEvent::Action::PRESS; - cocos2d::EventDispatcher::dispatchKeyboardEvent(keyboardEvent); - - return 1; - } else if (type == AINPUT_EVENT_TYPE_MOTION){ - int action = AMotionEvent_getAction(event); - - switch (action & AMOTION_EVENT_ACTION_MASK) { - case AMOTION_EVENT_ACTION_DOWN: - touchEvent.type = cocos2d::TouchEvent::Type::BEGAN; - dispatchTouchEvent(0, event, touchEvent); - break; - case AMOTION_EVENT_ACTION_POINTER_DOWN: - touchEvent.type = cocos2d::TouchEvent::Type::BEGAN; - dispatchTouchEvent((action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT, - event, - touchEvent); - break; - case AMOTION_EVENT_ACTION_UP: - touchEvent.type = cocos2d::TouchEvent::Type::ENDED; - dispatchTouchEvent(0, event, touchEvent); - break; - case AMOTION_EVENT_ACTION_POINTER_UP: - touchEvent.type = cocos2d::TouchEvent::Type::ENDED; - dispatchTouchEvent((action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT, - event, - touchEvent); - break; - case AMOTION_EVENT_ACTION_MOVE: - touchEvent.type = cocos2d::TouchEvent::Type::MOVED; - dispatchTouchEvents(event, touchEvent); - break; - case AMOTION_EVENT_ACTION_CANCEL: - touchEvent.type = cocos2d::TouchEvent::Type::CANCELLED; - dispatchTouchEvents(event, touchEvent); - break; - default: - return 0; + case APP_CMD_INIT_WINDOW: + if (state->app->window) + { + auto width = ANativeWindow_getWidth(app->window); + auto height = ANativeWindow_getHeight(app->window); + game = new Game(width, height); + game->init(); + state->initialized = true; } - - return 1; - } - - return 0; + break; + case APP_CMD_TERM_WINDOW: + state->animating = false; + break; + case APP_CMD_GAINED_FOCUS: + state->animating = true; + break; + case APP_CMD_LOST_FOCUS: + state->animating = false; + break; + case APP_CMD_PAUSE: + if (state->initialized) + cocos2d::Application::getInstance()->onPause(); + break; + case APP_CMD_RESUME: + if (state->initialized) + cocos2d::Application::getInstance()->onResume(); + break; + default: + break; } + + cocos2d::View::engineHandleCmd(app, cmd); } void android_main(struct android_app* state) { struct SavedState savedState; memset(&savedState, 0, sizeof(savedState)); state->userData = &savedState; - state->onAppCmd = engineHandleCmd; - state->onInputEvent = engineHandleInput; + state->onAppCmd = handle_cmd; + state->onInputEvent = cocos2d::View::engineHandleInput; savedState.app = state; cocos2d::JniHelper::setAndroidApp(state); @@ -221,13 +116,9 @@ void android_main(struct android_app* state) { } if (state->destroyRequested != 0) - { return; - } if (savedState.animating) - { game->tick(); - } } } \ No newline at end of file diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/HelloJavascript.xcodeproj/project.pbxproj b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/HelloJavascript.xcodeproj/project.pbxproj index b4a5fea1a4..5ef79b774a 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/HelloJavascript.xcodeproj/project.pbxproj +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/HelloJavascript.xcodeproj/project.pbxproj @@ -31,13 +31,11 @@ 469FFB172410A76800581BFC /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 469FFB162410A76700581BFC /* AppDelegate.mm */; }; 469FFB1D2410AAAF00581BFC /* libGLESv2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 469FFB1B2410AAAF00581BFC /* libGLESv2.dylib */; }; 469FFB1E2410AAAF00581BFC /* libEGL.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 469FFB1C2410AAAF00581BFC /* libEGL.dylib */; }; - 469FFB262411054700581BFC /* KeyCodeHelper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 469FFB242411054700581BFC /* KeyCodeHelper.cpp */; }; 46A4DEFA2416429000BE0499 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 46A4DEF82416429000BE0499 /* LaunchScreen.storyboard */; }; 46BFDFA723CC0F4100CC5F8E /* Game.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFA623CC0F4100CC5F8E /* Game.cpp */; }; 46BFDFA823CC0F4100CC5F8E /* Game.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFA623CC0F4100CC5F8E /* Game.cpp */; }; 46BFDFBA23CC10E800CC5F8E /* ViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFB423CC10E700CC5F8E /* ViewController.mm */; }; 46BFDFBB23CC10E800CC5F8E /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFB623CC10E700CC5F8E /* AppDelegate.mm */; }; - 46BFDFBC23CC10E800CC5F8E /* View.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFB723CC10E700CC5F8E /* View.mm */; }; 46BFE04823CD5EA300CC5F8E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 46BFE04023CD5E7A00CC5F8E /* main.m */; }; 46BFE04923CD5EA700CC5F8E /* ViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46BFE03E23CD5E7A00CC5F8E /* ViewController.mm */; }; 46BFE04E23CD5F3800CC5F8E /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46BFE04C23CD5F3800CC5F8E /* Metal.framework */; }; @@ -140,16 +138,12 @@ 469FFB162410A76700581BFC /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = AppDelegate.mm; sourceTree = ""; }; 469FFB1B2410AAAF00581BFC /* libGLESv2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libGLESv2.dylib; path = ../../../../../external/mac/libs/libGLESv2.dylib; sourceTree = ""; }; 469FFB1C2410AAAF00581BFC /* libEGL.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libEGL.dylib; path = ../../../../../external/mac/libs/libEGL.dylib; sourceTree = ""; }; - 469FFB242411054700581BFC /* KeyCodeHelper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KeyCodeHelper.cpp; sourceTree = ""; }; - 469FFB252411054700581BFC /* KeyCodeHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KeyCodeHelper.h; sourceTree = ""; }; 46A4DEF92416429000BE0499 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = ios/Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 46BFDFA523CC0F4100CC5F8E /* Game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Game.h; sourceTree = ""; }; 46BFDFA623CC0F4100CC5F8E /* Game.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Game.cpp; sourceTree = ""; }; 46BFDFB423CC10E700CC5F8E /* ViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = ViewController.mm; path = ios/ViewController.mm; sourceTree = ""; }; 46BFDFB523CC10E700CC5F8E /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = ios/AppDelegate.h; sourceTree = ""; }; 46BFDFB623CC10E700CC5F8E /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = ios/AppDelegate.mm; sourceTree = ""; }; - 46BFDFB723CC10E700CC5F8E /* View.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = View.mm; path = ios/View.mm; sourceTree = ""; }; - 46BFDFB823CC10E700CC5F8E /* View.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = View.h; path = ios/View.h; sourceTree = ""; }; 46BFDFB923CC10E800CC5F8E /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ViewController.h; path = ios/ViewController.h; sourceTree = ""; }; 46BFE03C23CD5E7900CC5F8E /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 46BFE03D23CD5E7A00CC5F8E /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -290,8 +284,6 @@ 509D4ACA17EBB2BE00697056 /* mac */ = { isa = PBXGroup; children = ( - 469FFB242411054700581BFC /* KeyCodeHelper.cpp */, - 469FFB252411054700581BFC /* KeyCodeHelper.h */, 469FFB162410A76700581BFC /* AppDelegate.mm */, 46BFE03D23CD5E7A00CC5F8E /* AppDelegate.h */, 46BFE04023CD5E7A00CC5F8E /* main.m */, @@ -380,8 +372,6 @@ 46A4DEF82416429000BE0499 /* LaunchScreen.storyboard */, 46BFDFB523CC10E700CC5F8E /* AppDelegate.h */, 46BFDFB623CC10E700CC5F8E /* AppDelegate.mm */, - 46BFDFB823CC10E700CC5F8E /* View.h */, - 46BFDFB723CC10E700CC5F8E /* View.mm */, 46BFDFB923CC10E800CC5F8E /* ViewController.h */, 46BFDFB423CC10E700CC5F8E /* ViewController.mm */, 051C018221E32AE400D4A347 /* UserConfigIOS.debug.xcconfig */, @@ -539,7 +529,6 @@ 46BFE04823CD5EA300CC5F8E /* main.m in Sources */, 469FFB172410A76800581BFC /* AppDelegate.mm in Sources */, 46BFDFA823CC0F4100CC5F8E /* Game.cpp in Sources */, - 469FFB262411054700581BFC /* KeyCodeHelper.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -549,7 +538,6 @@ files = ( 46BFDFBA23CC10E800CC5F8E /* ViewController.mm in Sources */, 509D4AC817EBB2AB00697056 /* main.m in Sources */, - 46BFDFBC23CC10E800CC5F8E /* View.mm in Sources */, 22CC757121BA5B5C00FBF2F7 /* SDKWrapper.m in Sources */, 46BFDFBB23CC10E800CC5F8E /* AppDelegate.mm in Sources */, 46BFDFA723CC0F4100CC5F8E /* Game.cpp in Sources */, diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppDelegate.mm b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppDelegate.mm index 4a0b103b72..0f2a8cf8a8 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppDelegate.mm +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppDelegate.mm @@ -25,7 +25,7 @@ ****************************************************************************/ #import "AppDelegate.h" #import "ViewController.h" -#import "View.h" +#import "platform/ios/CCView.h" #include "SDKWrapper.h" #include "Game.h" diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/ViewController.mm b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/ViewController.mm index 3be2a42f3c..db3eec61ef 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/ViewController.mm +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/ViewController.mm @@ -23,11 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ - #import "ViewController.h" -#import "cocos2d.h" - -#import "View.h" @interface ViewController () diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.h b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.h deleted file mode 100644 index a2a538d466..0000000000 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/KeyCodeHelper.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -extern int translateKeycode(int); diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/ViewController.mm b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/ViewController.mm index e0e7182a30..85f9eeb239 100644 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/ViewController.mm +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/mac/ViewController.mm @@ -1,163 +1,17 @@ #import "ViewController.h" -#import -#import - -#import "Game.h" -#import "KeyCodeHelper.h" -#import "scripting/js-bindings/event/EventDispatcher.h" +#import "platform/mac/CCView.h" @implementation ViewController { - NSView* _view; - cocos2d::MouseEvent _mouseEvent; - cocos2d::KeyboardEvent _keyboardEvent; + View* _view; } - (instancetype)initWithSize:(NSRect)rect { - if ( self = [super init]) { - _view = [[NSView alloc] initWithFrame:rect]; + if (self = [super init]) { + _view = [[View alloc] initWithFrame:rect]; self.view = _view; } return self; } -- (void)setRepresentedObject:(id)representedObject { - [super setRepresentedObject:representedObject]; - - // Update the view, if already loaded. -} - -- (void)viewDidAppear -{ - // Make the view controller the window's first responder so that it can handle the Key events - [_view.window makeFirstResponder:self]; -} - -- (void)keyDown:(NSEvent *)event { - - _keyboardEvent.key = translateKeycode(event.keyCode); - _keyboardEvent.action = [event isARepeat] ? cocos2d::KeyboardEvent::Action::REPEAT - : cocos2d::KeyboardEvent::Action::PRESS; - [self setModifierFlags:event]; - cocos2d::EventDispatcher::dispatchKeyboardEvent(_keyboardEvent); -} - -- (void)keyUp:(NSEvent *)event { - _keyboardEvent.key = translateKeycode(event.keyCode); - _keyboardEvent.action = cocos2d::KeyboardEvent::Action::RELEASE; - [self setModifierFlags:event]; - cocos2d::EventDispatcher::dispatchKeyboardEvent(_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; -} - -- (void)mouseDown:(NSEvent *)event { - [self sendMouseEvent:0 - type:cocos2d::MouseEvent::Type::DOWN - event:event]; -} - -- (void)mouseUp:(NSEvent *)event { - [self sendMouseEvent:0 - type:cocos2d::MouseEvent::Type::UP - event:event]; -} - -- (void)mouseDragged:(NSEvent *)event { - [self mouseMoved:event]; -} - -- (void)mouseMoved:(NSEvent *)event { - [self sendMouseEvent:0 - type:cocos2d::MouseEvent::Type::MOVE - event:event]; -} - -- (void)otherMouseDown:(NSEvent *)event { - [self sendMouseEvent:[self translateButtonNumber:event.buttonNumber] - type:cocos2d::MouseEvent::Type::DOWN - event:event]; -} - -- (void)otherMouseUp:(NSEvent *)event { - [self sendMouseEvent:[self translateButtonNumber:event.buttonNumber] - type:cocos2d::MouseEvent::Type::UP - event:event]; -} - -- (int)translateButtonNumber:(int) buttonNumber { - if (buttonNumber == 1) // left - return 0; - else if (buttonNumber == 2) // right - return 2; - else - return 1; -} - -- (void)scrollWheel:(NSEvent *)event { - double deltaX = [event scrollingDeltaX]; - double deltaY = [event scrollingDeltaY]; - - if ([event hasPreciseScrollingDeltas]) { - deltaX *= 0.1; - deltaY *= 0.1; - } - - if (fabs(deltaX) > 0.0 || fabs(deltaY) > 0.0) { - _mouseEvent.type = cocos2d::MouseEvent::Type::WHEEL; - _mouseEvent.button = 0; - _mouseEvent.x = deltaX; - _mouseEvent.y = deltaY; - cocos2d::EventDispatcher::dispatchMouseEvent(_mouseEvent); - } -} - -- (void)rightMouseDown:(NSEvent *)event { - [self sendMouseEvent:2 - type:cocos2d::MouseEvent::Type::DOWN - event:event]; -} - -- (void)rightMouseUp:(NSEvent *)event { - [self sendMouseEvent:2 - type:cocos2d::MouseEvent::Type::UP - event:event]; -} - -- (BOOL)acceptsFirstResponder { - return YES; -} - -- (void)sendMouseEvent:(int)button type:(cocos2d::MouseEvent::Type)type event:(NSEvent*)event { - const NSRect contentRect = [self.view frame]; - const NSPoint pos = [event locationInWindow]; - - _mouseEvent.type = type; - _mouseEvent.button = button; - _mouseEvent.x = pos.x; - _mouseEvent.y = contentRect.size.height - pos.y; - cocos2d::EventDispatcher::dispatchMouseEvent(_mouseEvent); -} - @end