From fd3a03a6eecef98c3665e40a5bcdff09974ef2d4 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Wed, 18 Sep 2013 12:04:35 +0800 Subject: [PATCH] 1.android platform:handle key event 2.android platform:fix Accelerometer value error --- .../layers_scenes_transitions_nodes/CCLayer.h | 4 +- cocos2dx/platform/android/nativeactivity.cpp | 37 ++++++++++++++++++- .../AccelerometerTest/AccelerometerTest.cpp | 14 +++---- .../TestCpp/Classes/KeypadTest/KeypadTest.cpp | 2 +- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index ffd4eafe2c..e7c6565744 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -153,8 +153,8 @@ public: 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 {}; + CC_DEPRECATED_ATTRIBUTE virtual bool isKeypadEnabled() const final { return isKeyboardEnabled(); }; + CC_DEPRECATED_ATTRIBUTE virtual void setKeypadEnabled(bool value) { setKeyboardEnabled(value); }; /** @deprecated Please override onKeyReleased and check the keycode of KeyboardEvent::KeyCode::Menu(KEY_BACKSPACE) instead. */ CC_DEPRECATED_ATTRIBUTE virtual void keyBackClicked() final {}; diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index aee9a1e0cf..323cb7811f 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -26,6 +26,7 @@ #include "textures/CCTextureCache.h" #include "event_dispatcher/CCEventDispatcher.h" #include "event_dispatcher/CCAccelerationEvent.h" +#include "event_dispatcher/CCKeyboardEvent.h" #include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h" @@ -388,6 +389,38 @@ static int32_t handle_touch_input(AInputEvent *event) { } } +/* +* Handle Key Inputs +*/ +static int32_t handle_key_input(AInputEvent *event) +{ + if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP) + { + switch (AKeyEvent_getKeyCode(event)) + { + case AKEYCODE_BACK: + { + cocos2d::KeyboardEvent event; + event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE; + cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); + LOGI("AKEYCODE_BACK"); + } + return 1; + case AKEYCODE_MENU: + { + cocos2d::KeyboardEvent event; + event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_MENU; + cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); + LOGI("AKEYCODE_MENU"); + } + return 1; + default: + break; + } + } + return 0; +} + /** * Process the next input event. */ @@ -404,6 +437,8 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) return handle_touch_input(event); } + else + return handle_key_input(event); return 0; } @@ -568,7 +603,7 @@ void android_main(struct android_app* state) { // ACONFIGURATION_ORIENTATION_SQUARE cocos2d::AccelerationEvent accEvent; accEvent.acc.x = event.acceleration.x; - accEvent.acc.y = -event.acceleration.y; + accEvent.acc.y = event.acceleration.y; accEvent.acc.z = event.acceleration.z; accEvent.acc.timestamp = 0; cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent); diff --git a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp index 4f06570737..73f1854947 100644 --- a/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp +++ b/samples/Cpp/TestCpp/Classes/AccelerometerTest/AccelerometerTest.cpp @@ -66,16 +66,14 @@ void AccelerometerTest::onAcceleration(Acceleration* acc, Event* event) auto ballSize = _ball->getContentSize(); - auto ptNow = _ball->getPosition(); - auto ptTemp = pDir->convertToUI(ptNow); + auto ptNow = _ball->getPosition(); - ptTemp.x += acc->x * 9.81f; - ptTemp.y -= acc->y * 9.81f; + ptNow.x -= acc->x ; + ptNow.y -= acc->y ; - auto ptNext = pDir->convertToGL(ptTemp); - FIX_POS(ptNext.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0)); - FIX_POS(ptNext.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0)); - _ball->setPosition(ptNext); + FIX_POS(ptNow.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0)); + FIX_POS(ptNow.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0)); + _ball->setPosition(ptNow); } //------------------------------------------------------------------ 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);