Merge pull request #3680 from Dhilan007/develop

1.android platform:handle key event and fix Accelerometer value error [ci skip]
This commit is contained in:
James Chen 2013-09-18 00:01:32 -07:00
commit 40a6f84585
3 changed files with 44 additions and 11 deletions

View File

@ -155,8 +155,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 {};

View File

@ -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);

View File

@ -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);
}
//------------------------------------------------------------------