From 7d94878173dfdf649edbc7a8f81b11704083e005 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 17 Sep 2013 22:34:20 +0800 Subject: [PATCH 1/4] [win32] glfw keyboard callback is enabled now. Enables KeyboardTest for win32. --- cocos2dx/platform/win32/CCEGLView.cpp | 24 ++++++++++++++----- .../Classes/KeyboardTest/KeyboardTest.cpp | 3 --- .../Classes/KeyboardTest/KeyboardTest.h | 3 --- .../TestCpp/Classes/KeypadTest/KeypadTest.cpp | 2 +- samples/Cpp/TestCpp/Classes/controller.cpp | 2 -- samples/Cpp/TestCpp/Classes/tests.h | 2 -- .../Cpp/TestCpp/proj.win32/TestCpp.vcxproj | 2 ++ .../proj.win32/TestCpp.vcxproj.filters | 9 +++++++ 8 files changed, 30 insertions(+), 17 deletions(-) diff --git a/cocos2dx/platform/win32/CCEGLView.cpp b/cocos2dx/platform/win32/CCEGLView.cpp index 4754b1892c..49b1690250 100644 --- a/cocos2dx/platform/win32/CCEGLView.cpp +++ b/cocos2dx/platform/win32/CCEGLView.cpp @@ -35,12 +35,20 @@ THE SOFTWARE. NS_CC_BEGIN -static std::map g_keyCodeMap;// = { +struct keyCodeItem +{ + int glfwKeyCode; + KeyboardEvent::KeyCode keyCode; +}; + +static std::map g_keyCodeMap; + +static keyCodeItem g_keyCodeStructArray[] = { /* The unknown key */ - // make_pair( GLFW_KEY_UNKNOWN , KeyboardEvent::KeyCode::KEY_NONE ), + { GLFW_KEY_UNKNOWN , KeyboardEvent::KeyCode::KEY_NONE }, /* Printable keys */ - /* + { GLFW_KEY_SPACE , KeyboardEvent::KeyCode::KEY_SPACE }, { GLFW_KEY_APOSTROPHE , KeyboardEvent::KeyCode::KEY_APOSTROPHE }, { GLFW_KEY_COMMA , KeyboardEvent::KeyCode::KEY_COMMA }, @@ -92,7 +100,7 @@ static std::map g_keyCodeMap;// = { { GLFW_KEY_WORLD_1 , KeyboardEvent::KeyCode::KEY_GRAVE }, { GLFW_KEY_WORLD_2 , KeyboardEvent::KeyCode::KEY_NONE }, - /* Function keys *//* + /* Function keys */ { GLFW_KEY_ESCAPE , KeyboardEvent::KeyCode::KEY_ESCAPE }, { GLFW_KEY_ENTER , KeyboardEvent::KeyCode::KEY_KP_ENTER }, { GLFW_KEY_TAB , KeyboardEvent::KeyCode::KEY_TAB }, @@ -163,8 +171,8 @@ static std::map g_keyCodeMap;// = { { GLFW_KEY_RIGHT_ALT , KeyboardEvent::KeyCode::KEY_ALT }, { GLFW_KEY_RIGHT_SUPER , KeyboardEvent::KeyCode::KEY_HYPER }, { GLFW_KEY_MENU , KeyboardEvent::KeyCode::KEY_MENU }, - { GLFW_KEY_LAST , KeyboardEvent::KeyCode::KEY_NONE }*/ -//}; + { GLFW_KEY_LAST , KeyboardEvent::KeyCode::KEY_NONE } +}; #if(_MSC_VER >= 1600) // Visual Studio 2010 or higher version. // Windows Touch define @@ -368,6 +376,10 @@ EGLView::EGLView() { CCASSERT(nullptr == s_pEglView, "EGLView is singleton, Should be inited only one time\n"); s_pEglView = this; + for (auto& item : g_keyCodeStructArray) + { + g_keyCodeMap.insert(std::make_pair(item.glfwKeyCode, item.keyCode)); + } strcpy(_viewName, "Cocos2dxWin32"); glfwSetErrorCallback(EGLViewEventHandler::OnGLFWError); glfwInit(); diff --git a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp index f5b2a55f30..53c1d8248b 100644 --- a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.cpp @@ -1,7 +1,5 @@ #include "KeyboardTest.h" -#ifdef CC_KEYBOARD_SUPPORT - KeyboardTest::KeyboardTest() { auto s = Director::getInstance()->getWinSize(); @@ -43,4 +41,3 @@ void KeyboardTestScene::runThisTest() layer->release(); } -#endif diff --git a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.h b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.h index 56f322f4fd..d3db8eb9d1 100644 --- a/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.h +++ b/samples/Cpp/TestCpp/Classes/KeyboardTest/KeyboardTest.h @@ -1,8 +1,6 @@ #ifndef _KEYBOARD_TEST_H_ #define _KEYBOARD_TEST_H_ -#ifdef CC_KEYBOARD_SUPPORT - #include "cocos2d.h" #include "../testBasic.h" @@ -25,6 +23,5 @@ public: virtual void runThisTest(); }; -#endif #endif diff --git a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp index 3f46ecca1d..3878a2f81b 100644 --- a/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp +++ b/samples/Cpp/TestCpp/Classes/KeypadTest/KeypadTest.cpp @@ -7,7 +7,7 @@ KeypadTest::KeypadTest() addChild(label, 0); label->setPosition( Point(s.width/2, s.height-50) ); - setKeypadEnabled(true); + setKeyboardEnabled(true); // create a label to display the tip string _label = LabelTTF::create("Please press any key...", "Arial", 22); diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index f3ad2162a7..5d9f13bb17 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -50,9 +50,7 @@ struct { { "FileUtilsTest", []() { return new FileUtilsTestScene(); } }, { "FontTest", []() { return new FontTestScene(); } }, { "IntervalTest", [](){return new IntervalTestScene(); } }, -#ifdef CC_KEYBOARD_SUPPORT { "KeyboardTest", []() { return new KeyboardTestScene(); } }, -#endif #if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA) { "KeypadTest", []() { return new KeypadTestScene(); } }, #endif diff --git a/samples/Cpp/TestCpp/Classes/tests.h b/samples/Cpp/TestCpp/Classes/tests.h index 724f95276f..851e6a3749 100644 --- a/samples/Cpp/TestCpp/Classes/tests.h +++ b/samples/Cpp/TestCpp/Classes/tests.h @@ -31,9 +31,7 @@ #include "EffectsAdvancedTest/EffectsAdvancedTest.h" #include "AccelerometerTest/AccelerometerTest.h" #include "KeypadTest/KeypadTest.h" -#ifdef CC_KEYBOARD_SUPPORT #include "KeyboardTest/KeyboardTest.h" -#endif #include "PerformanceTest/PerformanceTest.h" #include "ZwoptexTest/ZwoptexTest.h" #include "CocosDenshionTest/CocosDenshionTest.h" diff --git a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj index d8c38569e3..d87a186dec 100644 --- a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj +++ b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj @@ -177,6 +177,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\libwebsockets\win32\lib\*.*" "$(O + @@ -311,6 +312,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\libwebsockets\win32\lib\*.*" "$(O + diff --git a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters index cf47e0d936..3eef58db2b 100644 --- a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters +++ b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters @@ -295,6 +295,9 @@ {24f044ee-09a6-406b-98d7-8d5d759e5bb1} + + {8d7d37cd-5cc2-4a7d-9bd2-7b5c928adbb5} + @@ -685,6 +688,9 @@ Classes\ExtensionsTest\CocoStudioGUITest + + Classes\KeyboardTest + @@ -1252,5 +1258,8 @@ Classes\ExtensionsTest\CocoStudioGUITest + + Classes\KeyboardTest + \ No newline at end of file From aaf4a6a81b7ab9f73fd8e06a0723c69f23be17bb Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 17 Sep 2013 22:37:16 +0800 Subject: [PATCH 2/4] [win32] clear keycode map before init eglview. --- cocos2dx/platform/win32/CCEGLView.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cocos2dx/platform/win32/CCEGLView.cpp b/cocos2dx/platform/win32/CCEGLView.cpp index 49b1690250..8aec9c89f4 100644 --- a/cocos2dx/platform/win32/CCEGLView.cpp +++ b/cocos2dx/platform/win32/CCEGLView.cpp @@ -376,6 +376,7 @@ EGLView::EGLView() { CCASSERT(nullptr == s_pEglView, "EGLView is singleton, Should be inited only one time\n"); s_pEglView = this; + g_keyCodeMap.clear(); for (auto& item : g_keyCodeStructArray) { g_keyCodeMap.insert(std::make_pair(item.glfwKeyCode, item.keyCode)); From 513237d5f60445689e10288b94a3e30d3d7e19a0 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 17 Sep 2013 23:03:33 +0800 Subject: [PATCH 3/4] Fixing armature test crash. --- .../CocoStudio/Armature/utils/CCArmatureDataManager.cpp | 2 +- .../CocoStudio/Armature/utils/CCArmatureDataManager.h | 7 +++++-- .../CocoStudioArmatureTest/ArmatureScene.cpp | 4 +++- samples/Cpp/TestCpp/Classes/testBasic.cpp | 3 +++ samples/Cpp/TestCpp/Classes/testBasic.h | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.cpp b/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.cpp index b2fa7b8b68..e67f9a72e4 100644 --- a/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.cpp +++ b/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.cpp @@ -46,7 +46,7 @@ ArmatureDataManager *ArmatureDataManager::getInstance() return s_sharedArmatureDataManager; } -void ArmatureDataManager::purge() +void ArmatureDataManager::destoryInstance() { SpriteFrameCacheHelper::purge(); DataReaderHelper::purge(); diff --git a/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.h b/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.h index 6bef41bb13..5b7d534d44 100644 --- a/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.h +++ b/extensions/CocoStudio/Armature/utils/CCArmatureDataManager.h @@ -40,9 +40,12 @@ public: /** @deprecated Use getInstance() instead */ CC_DEPRECATED_ATTRIBUTE static ArmatureDataManager *sharedArmatureDataManager() { return ArmatureDataManager::getInstance(); } + /** @deprecated Use destoryInstance() instead */ + CC_DEPRECATED_ATTRIBUTE static void purge() { ArmatureDataManager::destoryInstance(); }; + static ArmatureDataManager *getInstance(); - - static void purge(); + static void destoryInstance(); + private: /** * @js ctor diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp index 7456204bd6..e5a62d7bac 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp @@ -163,7 +163,7 @@ void ArmatureTestLayer::onEnter() addChild(menu, 100); - setShaderProgram(ShaderCache::getInstance()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); + setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); } void ArmatureTestLayer::onExit() @@ -171,6 +171,8 @@ void ArmatureTestLayer::onExit() removeAllChildren(); backItem = restartItem = nextItem = NULL; + + Layer::onExit(); } std::string ArmatureTestLayer::title() diff --git a/samples/Cpp/TestCpp/Classes/testBasic.cpp b/samples/Cpp/TestCpp/Classes/testBasic.cpp index c1c7b22bad..5926c0a000 100644 --- a/samples/Cpp/TestCpp/Classes/testBasic.cpp +++ b/samples/Cpp/TestCpp/Classes/testBasic.cpp @@ -1,5 +1,6 @@ #include "testBasic.h" #include "controller.h" +#include "cocos-ext.h" TestScene::TestScene(bool bPortrait) { @@ -45,6 +46,8 @@ void TestScene::onEnter() Director::getInstance()->replaceScene(scene); scene->release(); } + + cocos2d::extension::armature::ArmatureDataManager::destoryInstance(); }); auto menu =Menu::create(menuItem, NULL); diff --git a/samples/Cpp/TestCpp/Classes/testBasic.h b/samples/Cpp/TestCpp/Classes/testBasic.h index f4be332a7d..020aab48bd 100644 --- a/samples/Cpp/TestCpp/Classes/testBasic.h +++ b/samples/Cpp/TestCpp/Classes/testBasic.h @@ -5,6 +5,7 @@ #include "VisibleRect.h" USING_NS_CC; + using namespace std; class TestScene : public Scene From e563c2799463eca49fd5580505681e792be835d9 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 17 Sep 2013 23:21:45 +0800 Subject: [PATCH 4/4] Keypad event fix for js and lua bindings. --- .../CCLayer.cpp | 17 ++++++++ .../layers_scenes_transitions_nodes/CCLayer.h | 9 ++-- cocos2dx/script_support/CCScriptSupport.h | 5 ++- .../project.pbxproj.REMOVED.git-id | 2 +- .../javascript/bindings/ScriptingCore.cpp | 41 +++++++++++++------ .../javascript/bindings/js/jsb_deprecated.js | 10 +++++ .../lua/cocos2dx_support/CCLuaEngine.cpp | 28 ++++++------- 7 files changed, 78 insertions(+), 34 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 6179bfd934..d48e923fd9 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -279,6 +279,23 @@ void Layer::onAcceleration(Acceleration* pAccelerationValue, Event* event) } } +void Layer::onKeyPressed(KeyboardEvent::KeyCode keyCode, Event* event) +{ + CC_UNUSED_PARAM(keyCode); + CC_UNUSED_PARAM(event); +} + +void Layer::onKeyReleased(KeyboardEvent::KeyCode keyCode, Event* event) +{ + CC_UNUSED_PARAM(event); + if(kScriptTypeNone != _scriptType) + { + KeypadScriptData data(keyCode, this); + ScriptEvent event(kKeypadEvent,&data); + ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + } +} + /// isKeyboardEnabled getter bool Layer::isKeyboardEnabled() const { diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index ffd4eafe2c..f75cc830a1 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -144,14 +144,15 @@ public: virtual bool isKeyboardEnabled() const; virtual void setKeyboardEnabled(bool value); - /** Please use onKeyPressed instead. */ + + /** Please use onKeyPressed instead. */ virtual void keyPressed(int keyCode) final {}; - /** Please use onKeyRelease instead. */ + /** Please use onKeyReleased instead. */ virtual void keyReleased(int keyCode) final {}; - virtual void onKeyPressed(KeyboardEvent::KeyCode keyCode, Event* event) {}; - virtual void onKeyReleased(KeyboardEvent::KeyCode keyCode, Event* event) {}; + virtual void onKeyPressed(KeyboardEvent::KeyCode keyCode, Event* event); + virtual void onKeyReleased(KeyboardEvent::KeyCode keyCode, Event* event); CC_DEPRECATED_ATTRIBUTE virtual bool isKeypadEnabled() const final { return false; }; CC_DEPRECATED_ATTRIBUTE virtual void setKeypadEnabled(bool value) final {}; diff --git a/cocos2dx/script_support/CCScriptSupport.h b/cocos2dx/script_support/CCScriptSupport.h index 65585da564..c84bd69462 100644 --- a/cocos2dx/script_support/CCScriptSupport.h +++ b/cocos2dx/script_support/CCScriptSupport.h @@ -28,6 +28,7 @@ #include "platform/CCCommon.h" #include "event_dispatcher/CCTouch.h" #include "event_dispatcher/CCTouchEvent.h" +#include "event_dispatcher/CCKeyboardEvent.h" #include "cocoa/CCSet.h" #include #include @@ -291,7 +292,7 @@ struct TouchScriptData struct KeypadScriptData { - int actionType; + KeyboardEvent::KeyCode actionType; void* nativeObject; // Constructor @@ -299,7 +300,7 @@ struct KeypadScriptData * @js NA * @lua NA */ - KeypadScriptData(int inActionType,void* inNativeObject) + KeypadScriptData(KeyboardEvent::KeyCode inActionType,void* inNativeObject) : actionType(inActionType),nativeObject(inNativeObject) { } diff --git a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id index 7cc40dd941..6e0a3e3729 100644 --- a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -ff8fe7ad5d9802d094d57853c10fe706d467e0b3 \ No newline at end of file +30df0cfd4f92ce0f01d24a90387aef16fa9e6fc8 \ No newline at end of file diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 08c19e4575..9b51d0b503 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -980,23 +980,40 @@ int ScriptingCore::handleKeypadEvent(void* data) if (NULL == keypadScriptData->nativeObject) return 0; - int action = keypadScriptData->actionType; + KeyboardEvent::KeyCode action = keypadScriptData->actionType; js_proxy_t * p = jsb_get_native_proxy(keypadScriptData->nativeObject); if (p) { -// FIXME: switch(action) -// { -// case kTypeBackClicked: -// executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "backClicked"); -// break; -// case kTypeMenuClicked: -// executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "menuClicked"); -// break; -// default: -// break; -// } + JSBool ret = JS_FALSE; + switch(action) + { + case KeyboardEvent::KeyCode::KEY_BACKSPACE: + ret = executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onBackClicked"); + if (!ret) + { + ret = executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "backClicked"); + if (ret) + { + CCLOG("backClicked will be deprecated, please use onBackClicked instead."); + } + } + break; + case KeyboardEvent::KeyCode::KEY_MENU: + ret = executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "onMenuClicked"); + if (!ret) + { + ret = executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "menuClicked"); + if (ret) + { + CCLOG("menuClicked will be deprecated, please use onMenuClicked instead."); + } + } + break; + default: + break; + } return 1; } diff --git a/scripting/javascript/bindings/js/jsb_deprecated.js b/scripting/javascript/bindings/js/jsb_deprecated.js index 89efd00284..4211bc12af 100644 --- a/scripting/javascript/bindings/js/jsb_deprecated.js +++ b/scripting/javascript/bindings/js/jsb_deprecated.js @@ -64,4 +64,14 @@ var cc = cc || {}; cc.log("cc.Menu.setHandlerPriority was deprecated, 3.0 uses new event dispatcher to dispatch touch event based on draw order, so setHandlerPriority is not needed now."); }; + cc.Layer.prototype.setKeypadEnabled = function() { + logW("cc.Layer.setKeypadEnabled", "cc.Layer.setKeyboardEnabled"); + return cc.Layer.prototype.setKeyboardEnabled.apply(this, arguments); + }; + + cc.Layer.prototype.isKeypadEnabled = function() { + logW("cc.Layer.isKeypadEnabled", "cc.Layer.isKeyboardEnabled"); + return cc.Layer.prototype.isKeyboardEnabled.apply(this, arguments); + }; + })(); diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp index 214a8acadf..ea380dcd76 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp @@ -400,22 +400,20 @@ int LuaEngine::handleKeypadEvent(void* data) if (0 == handler) return 0; - int action = keypadScriptData->actionType; + KeyboardEvent::KeyCode action = keypadScriptData->actionType; + + switch(action) + { + case KeyboardEvent::KeyCode::KEY_BACKSPACE: + _stack->pushString("backClicked"); + break; + case KeyboardEvent::KeyCode::KEY_MENU: + _stack->pushString("menuClicked"); + break; + default: + break; + } - //FIXME: -// switch (action) -// { -// case kTypeBackClicked: -// _stack->pushString("backClicked"); -// break; -// -// case kTypeMenuClicked: -// _stack->pushString("menuClicked"); -// break; -// -// default: -// return 0; -// } int ret = _stack->executeFunctionByHandler(handler, 1); _stack->clean(); return ret;