/** * Process the next input event. */ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) { struct engine* engine = (struct engine*)app->userData; if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { int x = AMotionEvent_getX(event, 0); int y = AMotionEvent_getY(event, 0); if ((AMOTION_EVENT_ACTION_MASK & AMotionEvent_getAction( event )) == AMOTION_EVENT_ACTION_DOWN) { nuiAndroidBridge::androidMouse(0, 0, x, y); } else if ((AMOTION_EVENT_ACTION_MASK & AMotionEvent_getAction( event )) == AMOTION_EVENT_ACTION_UP) { nuiAndroidBridge::androidMouse(0, 1, x, y); } else if ((AMOTION_EVENT_ACTION_MASK & AMotionEvent_getAction( event )) == AMOTION_EVENT_ACTION_MOVE) { nuiAndroidBridge::androidMotion(x, y); } engine->animating = 1; engine->state.x = AMotionEvent_getX(event, 0); engine->state.y = AMotionEvent_getY(event, 0); return 1; } return 0; }
//-------------------------------------------------------------------------------- // TapDetector //-------------------------------------------------------------------------------- GESTURE_STATE TapDetector::Detect(const AInputEvent* motion_event) { if (AMotionEvent_getPointerCount(motion_event) > 1) { // Only support single touch return false; } int32_t action = AMotionEvent_getAction(motion_event); unsigned int flags = action & AMOTION_EVENT_ACTION_MASK; switch (flags) { case AMOTION_EVENT_ACTION_DOWN: down_pointer_id_ = AMotionEvent_getPointerId(motion_event, 0); down_x_ = AMotionEvent_getX(motion_event, 0); down_y_ = AMotionEvent_getY(motion_event, 0); break; case AMOTION_EVENT_ACTION_UP: { int64_t eventTime = AMotionEvent_getEventTime(motion_event); int64_t downTime = AMotionEvent_getDownTime(motion_event); if (eventTime - downTime <= TAP_TIMEOUT) { if (down_pointer_id_ == AMotionEvent_getPointerId(motion_event, 0)) { float x = AMotionEvent_getX(motion_event, 0) - down_x_; float y = AMotionEvent_getY(motion_event, 0) - down_y_; if (x * x + y * y < TOUCH_SLOP * TOUCH_SLOP * dp_factor_) { LOGI("TapDetector: Tap detected"); return GESTURE_STATE_ACTION; } } } break; } } return GESTURE_STATE_NONE; }
//-------------------------------------------------------------------------------- // DoubletapDetector //-------------------------------------------------------------------------------- GESTURE_STATE DoubletapDetector::Detect(const AInputEvent* motion_event) { if (AMotionEvent_getPointerCount(motion_event) > 1) { // Only support single double tap return false; } bool tap_detected = tap_detector_.Detect(motion_event); int32_t action = AMotionEvent_getAction(motion_event); unsigned int flags = action & AMOTION_EVENT_ACTION_MASK; switch (flags) { case AMOTION_EVENT_ACTION_DOWN: { int64_t eventTime = AMotionEvent_getEventTime(motion_event); if (eventTime - last_tap_time_ <= DOUBLE_TAP_TIMEOUT) { float x = AMotionEvent_getX(motion_event, 0) - last_tap_x_; float y = AMotionEvent_getY(motion_event, 0) - last_tap_y_; if (x * x + y * y < DOUBLE_TAP_SLOP * DOUBLE_TAP_SLOP * dp_factor_) { LOGI("DoubletapDetector: Doubletap detected"); return GESTURE_STATE_ACTION; } } break; } case AMOTION_EVENT_ACTION_UP: if (tap_detected) { last_tap_time_ = AMotionEvent_getEventTime(motion_event); last_tap_x_ = AMotionEvent_getX(motion_event, 0); last_tap_y_ = AMotionEvent_getY(motion_event, 0); } break; } return GESTURE_STATE_NONE; }
int32_t GLESApplication::handleInput(android_app *app, AInputEvent *event) { int32_t eventType = AInputEvent_getType(event); if (eventType == AINPUT_EVENT_TYPE_MOTION) { int32_t action = AMotionEvent_getAction(event); switch(action) { case AMOTION_EVENT_ACTION_DOWN: onTouchDown(AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0)); break; case AMOTION_EVENT_ACTION_MOVE: onTouchMove(AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0)); break; case AMOTION_EVENT_ACTION_UP: onTouchUp(AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0)); break; default: break; } } return 0; }
static int HandleInput(struct android_app* app, AInputEvent* event) { Android_App* myapp = (Android_App*) app->userData; if(myapp) { switch(AInputEvent_getType(event)) { case AINPUT_EVENT_TYPE_KEY: { switch(AKeyEvent_getKeyCode(event)) { case 4: // Back if(AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP) myapp->OnKeyPress(enumKEY_Back); break; case 24: // Volume up break; case 25: // Volume down break; } break; } case AINPUT_EVENT_TYPE_MOTION: // Handle touch events { switch(AMotionEvent_getAction(event)) { case AMOTION_EVENT_ACTION_DOWN: { Touch t = { AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0) }; myapp->OnTouchDown(&t, 1); break; } case AMOTION_EVENT_ACTION_MOVE: { Touch t = { AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0) }; myapp->OnTouchMoved(&t, 1); break; } case AMOTION_EVENT_ACTION_UP: { Touch t = { AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0) }; myapp->OnTouchUp(&t, 1); break; } } return 1; } } } return 1; }
bool NativeEngine::HandleInput(AInputEvent *event) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { int32_t action = AMotionEvent_getAction(event); size_t pointerIndex; size_t pointerId; size_t pointerCount; int x; int y; switch (action & AMOTION_EVENT_ACTION_MASK) { case AMOTION_EVENT_ACTION_DOWN: pointerId = AMotionEvent_getPointerId(event, 0); x = AMotionEvent_getX(event, 0); y = AMotionEvent_getY(event, 0); touchBegan(pointerId, Origami::maths::vec2(x, y)); break; case AMOTION_EVENT_ACTION_UP: pointerId = AMotionEvent_getPointerId(event, 0); x = AMotionEvent_getX(event, 0); y = AMotionEvent_getY(event, 0); touchended(pointerId, Origami::maths::vec2(x, y)); break; case AMOTION_EVENT_ACTION_POINTER_DOWN: pointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; pointerId = AMotionEvent_getPointerId(event, pointerIndex); x = AMotionEvent_getX(event, pointerIndex); y = AMotionEvent_getY(event, pointerIndex); touchBegan(pointerId, Origami::maths::vec2(x, y)); break; case AMOTION_EVENT_ACTION_POINTER_UP: pointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; pointerId = AMotionEvent_getPointerId(event, pointerIndex); x = AMotionEvent_getX(event, pointerIndex); y = AMotionEvent_getY(event, pointerIndex); touchended(pointerId, Origami::maths::vec2(x, y)); break; case AMOTION_EVENT_ACTION_MOVE: pointerCount = AMotionEvent_getPointerCount(event); for (size_t i = 0; i < pointerCount; ++i) { pointerId = AMotionEvent_getPointerId(event, i); x = AMotionEvent_getX(event, i); y = AMotionEvent_getY(event, i); touchMoved(pointerId, Origami::maths::vec2(x, y)); } break; } }
static bool CookEvent_Motion(AInputEvent *event, CookedEventCallback callback) { int src = AInputEvent_getSource(event); int action = AMotionEvent_getAction(event); int actionMasked = action & AMOTION_EVENT_ACTION_MASK; int ptrIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; struct CookedEvent ev; memset(&ev, 0, sizeof(ev)); if (actionMasked == AMOTION_EVENT_ACTION_DOWN || actionMasked == AMOTION_EVENT_ACTION_POINTER_DOWN) { ev.type = COOKED_EVENT_TYPE_POINTER_DOWN; } else if (actionMasked == AMOTION_EVENT_ACTION_UP || actionMasked == AMOTION_EVENT_ACTION_POINTER_UP) { ev.type = COOKED_EVENT_TYPE_POINTER_UP; } else { ev.type = COOKED_EVENT_TYPE_POINTER_MOVE; } ev.motionPointerId = AMotionEvent_getPointerId(event, ptrIndex); ev.motionIsOnScreen = (src == AINPUT_SOURCE_TOUCHSCREEN); ev.motionX = AMotionEvent_getX(event, ptrIndex); ev.motionY = AMotionEvent_getY(event, ptrIndex); if (ev.motionIsOnScreen) { // use screen size as the motion range ev.motionMinX = 0.0f; ev.motionMaxX = SceneManager::GetInstance()->GetScreenWidth(); ev.motionMinY = 0.0f; ev.motionMaxY = SceneManager::GetInstance()->GetScreenHeight(); } else { // look up motion range for this device _look_up_motion_range((int) AInputEvent_getDeviceId(event), (int)AInputEvent_getSource(event), &ev.motionMinX, &ev.motionMaxX, &ev.motionMinY, &ev.motionMaxY); } // deliver event callback(&ev); // deliver motion info about other pointers (for multi-touch) int ptrCount = AMotionEvent_getPointerCount(event); for (int i = 0; i < ptrCount; i++) { ev.type = COOKED_EVENT_TYPE_POINTER_MOVE; ev.motionX = AMotionEvent_getX(event, i); ev.motionY = AMotionEvent_getY(event, i); ev.motionPointerId = AMotionEvent_getPointerId(event, i); callback(&ev); } // If this is a touch-nav event, return false to indicate that we haven't handled it. // This will trigger translation of swipes to DPAD keys, which is what we want. // Otherwise, we say that we've handled it. return (src != SOURCE_TOUCH_NAVIGATION); }
bool CAndroidTouch::onTouchEvent(AInputEvent* event) { CXBMCApp::android_printf("%s", __PRETTY_FUNCTION__); if (event == NULL) return false; size_t numPointers = AMotionEvent_getPointerCount(event); if (numPointers <= 0) { CXBMCApp::android_printf(" => aborting touch event because there are no active pointers"); return false; } if (numPointers > TOUCH_MAX_POINTERS) numPointers = TOUCH_MAX_POINTERS; int32_t eventAction = AMotionEvent_getAction(event); int8_t touchAction = eventAction & AMOTION_EVENT_ACTION_MASK; size_t touchPointer = eventAction >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; CTouchInput::TouchEvent touchEvent = CTouchInput::TouchEventAbort; switch (touchAction) { case AMOTION_EVENT_ACTION_DOWN: case AMOTION_EVENT_ACTION_POINTER_DOWN: touchEvent = CTouchInput::TouchEventDown; break; case AMOTION_EVENT_ACTION_UP: case AMOTION_EVENT_ACTION_POINTER_UP: touchEvent = CTouchInput::TouchEventUp; break; case AMOTION_EVENT_ACTION_MOVE: touchEvent = CTouchInput::TouchEventMove; break; case AMOTION_EVENT_ACTION_OUTSIDE: case AMOTION_EVENT_ACTION_CANCEL: default: break; } float x = AMotionEvent_getX(event, touchPointer); float y = AMotionEvent_getY(event, touchPointer); float size = m_dpi / 16.0f; int64_t time = AMotionEvent_getEventTime(event); // first update all touch pointers for (unsigned int pointer = 0; pointer < numPointers; pointer++) CTouchInput::Get().Update(pointer, AMotionEvent_getX(event, pointer), AMotionEvent_getY(event, pointer), AMotionEvent_getEventTime(event), m_dpi / 16.0f); // now send the event return CTouchInput::Get().Handle(touchEvent, x, y, time, touchPointer, size); }
void WindowImplAndroid::processScrollEvent(AInputEvent* _event, ActivityStates* states) { // Prepare the java virtual machine jint lResult; jint lFlags = 0; JavaVM* lJavaVM = states->activity->vm; JNIEnv* lJNIEnv = states->activity->env; JavaVMAttachArgs lJavaVMAttachArgs; lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.name = "NativeThread"; lJavaVMAttachArgs.group = NULL; lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs); if (lResult == JNI_ERR) err() << "Failed to initialize JNI, couldn't get the unicode value" << std::endl; // Retrieve everything we need to create this MotionEvent in java jlong downTime = AMotionEvent_getDownTime(_event); jlong eventTime = AMotionEvent_getEventTime(_event); jint action = AMotionEvent_getAction(_event); jfloat x = AMotionEvent_getX(_event, 0); jfloat y = AMotionEvent_getY(_event, 0); jfloat pressure = AMotionEvent_getPressure(_event, 0); jfloat size = AMotionEvent_getSize(_event, 0); jint metaState = AMotionEvent_getMetaState(_event); jfloat xPrecision = AMotionEvent_getXPrecision(_event); jfloat yPrecision = AMotionEvent_getYPrecision(_event); jint deviceId = AInputEvent_getDeviceId(_event); jint edgeFlags = AMotionEvent_getEdgeFlags(_event); // Create the MotionEvent object in java trough its static constructor obtain() jclass ClassMotionEvent = lJNIEnv->FindClass("android/view/MotionEvent"); jmethodID StaticMethodObtain = lJNIEnv->GetStaticMethodID(ClassMotionEvent, "obtain", "(JJIFFFFIFFII)Landroid/view/MotionEvent;"); jobject ObjectMotionEvent = lJNIEnv->CallStaticObjectMethod(ClassMotionEvent, StaticMethodObtain, downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags); // Call its getAxisValue() method to get the delta value of our wheel move event jmethodID MethodGetAxisValue = lJNIEnv->GetMethodID(ClassMotionEvent, "getAxisValue", "(I)F"); jfloat delta = lJNIEnv->CallFloatMethod(ObjectMotionEvent, MethodGetAxisValue, 0x00000001); // Create and send our mouse wheel event Event event; event.type = Event::MouseWheelMoved; event.mouseWheel.delta = static_cast<double>(delta); event.mouseWheel.x = AMotionEvent_getX(_event, 0); event.mouseWheel.y = AMotionEvent_getY(_event, 0); forwardEvent(event); // Dettach this thread from the JVM lJavaVM->DetachCurrentThread(); }
int32_t cxAndroid::HandleMotionInput(AInputEvent* event) { cxInt action = AMotionEvent_getAction(event); cxInt atype = action & AMOTION_EVENT_ACTION_MASK; switch (atype) { case AMOTION_EVENT_ACTION_POINTER_DOWN:{ cxInt idx = action >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; cxTouchId id = AMotionEvent_getPointerId(event, idx); cxFloat x = AMotionEvent_getX(event, idx); cxFloat y = AMotionEvent_getY(event, idx); cxEngine::Instance()->Dispatch(id, cxTouchPoint::Began, x, y); break; } case AMOTION_EVENT_ACTION_POINTER_UP:{ cxInt idx = action >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; cxTouchId id = AMotionEvent_getPointerId(event, idx); cxFloat x = AMotionEvent_getX(event, idx); cxFloat y = AMotionEvent_getY(event, idx); cxEngine::Instance()->Dispatch(id, cxTouchPoint::Ended, x, y); break; } case AMOTION_EVENT_ACTION_MOVE:{ cxInt count = (cxInt)AMotionEvent_getPointerCount(event); for(cxInt i=0; i < count; i++){ cxTouchId id = AMotionEvent_getPointerId(event, i); cxFloat x = AMotionEvent_getX(event, i); cxFloat y =AMotionEvent_getY(event, i); cxEngine::Instance()->Dispatch(id, cxTouchPoint::Moved, x, y); } break; } case AMOTION_EVENT_ACTION_DOWN:{ cxInt count = (cxInt)AMotionEvent_getPointerCount(event); for(cxInt i=0; i < count; i++){ cxTouchId id = AMotionEvent_getPointerId(event, i); cxFloat x = AMotionEvent_getX(event, i); cxFloat y =AMotionEvent_getY(event, i); cxEngine::Instance()->Dispatch(id, cxTouchPoint::Began, x, y); } break; } case AMOTION_EVENT_ACTION_UP:{ cxInt count = (cxInt)AMotionEvent_getPointerCount(event); for(cxInt i=0; i < count; i++){ cxTouchId id = AMotionEvent_getPointerId(event, i); cxFloat x = AMotionEvent_getX(event, i); cxFloat y =AMotionEvent_getY(event, i); cxEngine::Instance()->Dispatch(id, cxTouchPoint::Ended, x, y); } break; } } return 0; }
int32_t Window::handle_input( AInputEvent* event ) { switch( AInputEvent_getType( event ) ) { case AINPUT_EVENT_TYPE_MOTION: { int action = AMotionEvent_getAction( event ); int index = action >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; action &= AMOTION_EVENT_ACTION_MASK; switch( action ) { case AMOTION_EVENT_ACTION_DOWN: case AMOTION_EVENT_ACTION_POINTER_DOWN: pointevent( "pointdown", index + 1, AMotionEvent_getX( event, 0 ), AMotionEvent_getY( event, 0 ) ); break; case AMOTION_EVENT_ACTION_UP: case AMOTION_EVENT_ACTION_CANCEL: pointevent( "pointup", AMotionEvent_getPointerId( event, 0 ) + 1, -1, -1 ); break; case AMOTION_EVENT_ACTION_POINTER_UP: pointevent( "pointup", index + 1, -1, -1 ); break; case AMOTION_EVENT_ACTION_MOVE: for( int i = 0; i < int( AMotionEvent_getPointerCount( event ) ); ++i ) { pointevent( "pointmove", AMotionEvent_getPointerId( event, i ) + 1, AMotionEvent_getX( event, i ), AMotionEvent_getY( event, i ) ); } break; } return 0; } break; } return 0; }
int32_t process_input(struct android_app* app, AInputEvent* event) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { const int32_t action = AMotionEvent_getAction(event); const int32_t pointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; const int32_t pointerCount = AMotionEvent_getPointerCount(event); const int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex); const float x = AMotionEvent_getX(event, pointerIndex); const float y = AMotionEvent_getY(event, pointerIndex); const int32_t actionMasked = (action & AMOTION_EVENT_ACTION_MASK); switch (actionMasked) { case AMOTION_EVENT_ACTION_DOWN: case AMOTION_EVENT_ACTION_POINTER_DOWN: { _queue.push_touch_event((int16_t)x, (int16_t)y, (uint8_t)pointerId, true); break; } case AMOTION_EVENT_ACTION_UP: case AMOTION_EVENT_ACTION_POINTER_UP: { _queue.push_touch_event((int16_t)x, (int16_t)y, (uint8_t)pointerId, false); break; } case AMOTION_EVENT_ACTION_OUTSIDE: case AMOTION_EVENT_ACTION_CANCEL: { _queue.push_touch_event((int16_t)x, (int16_t)y, (uint8_t)pointerId, false); break; } case AMOTION_EVENT_ACTION_MOVE: { for (int index = 0; index < pointerCount; index++) { const float xx = AMotionEvent_getX(event, index); const float yy = AMotionEvent_getY(event, index); const int32_t id = AMotionEvent_getPointerId(event, index); _queue.push_touch_event((int16_t)xx, (int16_t)yy, (uint8_t)id); } break; } } return 1; } else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY)
bool InputService::onTouchEvent(AInputEvent* pEvent) { #ifdef INPUTSERVICE_LOG_EVENTS packt_Log_debug("AMotionEvent_getAction=%d", AMotionEvent_getAction(pEvent)); packt_Log_debug("AMotionEvent_getFlags=%d", AMotionEvent_getFlags(pEvent)); packt_Log_debug("AMotionEvent_getMetaState=%d", AMotionEvent_getMetaState(pEvent)); packt_Log_debug("AMotionEvent_getEdgeFlags=%d", AMotionEvent_getEdgeFlags(pEvent)); packt_Log_debug("AMotionEvent_getDownTime=%lld", AMotionEvent_getDownTime(pEvent)); packt_Log_debug("AMotionEvent_getEventTime=%lld", AMotionEvent_getEventTime(pEvent)); packt_Log_debug("AMotionEvent_getXOffset=%f", AMotionEvent_getXOffset(pEvent)); packt_Log_debug("AMotionEvent_getYOffset=%f", AMotionEvent_getYOffset(pEvent)); packt_Log_debug("AMotionEvent_getXPrecision=%f", AMotionEvent_getXPrecision(pEvent)); packt_Log_debug("AMotionEvent_getYPrecision=%f", AMotionEvent_getYPrecision(pEvent)); packt_Log_debug("AMotionEvent_getPointerCount=%d", AMotionEvent_getPointerCount(pEvent)); packt_Log_debug("AMotionEvent_getRawX=%f", AMotionEvent_getRawX(pEvent, 0)); packt_Log_debug("AMotionEvent_getRawY=%f", AMotionEvent_getRawY(pEvent, 0)); packt_Log_debug("AMotionEvent_getX=%f", AMotionEvent_getX(pEvent, 0)); packt_Log_debug("AMotionEvent_getY=%f", AMotionEvent_getY(pEvent, 0)); packt_Log_debug("AMotionEvent_getPressure=%f", AMotionEvent_getPressure(pEvent, 0)); packt_Log_debug("AMotionEvent_getSize=%f", AMotionEvent_getSize(pEvent, 0)); packt_Log_debug("AMotionEvent_getOrientation=%f", AMotionEvent_getOrientation(pEvent, 0)); packt_Log_debug("AMotionEvent_getTouchMajor=%f", AMotionEvent_getTouchMajor(pEvent, 0)); packt_Log_debug("AMotionEvent_getTouchMinor=%f", AMotionEvent_getTouchMinor(pEvent, 0)); #endif const float TOUCH_MAX_RANGE = 65.0f; // In pixels. if (mRefPoint != NULL) { if (AMotionEvent_getAction(pEvent) == AMOTION_EVENT_ACTION_MOVE) { // Needs a conversion to proper coordinates // (origin at bottom/left). Only lMoveY needs it. float lMoveX = AMotionEvent_getX(pEvent, 0) - mRefPoint->mPosX; float lMoveY = mHeight - AMotionEvent_getY(pEvent, 0) - mRefPoint->mPosY; float lMoveRange = sqrt((lMoveX * lMoveX) + (lMoveY * lMoveY)); if (lMoveRange > TOUCH_MAX_RANGE) { float lCropFactor = TOUCH_MAX_RANGE / lMoveRange; lMoveX *= lCropFactor; lMoveY *= lCropFactor; } mHorizontal = lMoveX / TOUCH_MAX_RANGE; mVertical = lMoveY / TOUCH_MAX_RANGE; } else { mHorizontal = 0.0f; mVertical = 0.0f; } } return true; }
void recordCameraState(struct engine* engine, AInputEvent* event) { engine->m_lastMouseX = AMotionEvent_getX(event, 0); engine->m_lastMouseY = AMotionEvent_getY(event, 0); engine->m_lastPanAngle = engine->m_cameraController->getPanAngle(); engine->m_lastTiltAngle = engine->m_cameraController->getTiltAngle(); }
static int32_t input(struct android_app * app, AInputEvent * e) { int handled = 1; int kc; cvkey cvkc; int action; switch (AInputEvent_getType(e)) { case AINPUT_EVENT_TYPE_KEY: kc = AKeyEvent_getKeyCode(e); action = AKeyEvent_getAction(e); cvkc = mapkey(kc); handled = kc != CVK_NONE; if (handled) { if (action == AKEY_EVENT_ACTION_DOWN) cvInject(CVE_DOWN, cvkc, 0); if (action == AKEY_EVENT_ACTION_UP) cvInject(CVE_UP, cvkc, 0); } break; case AINPUT_EVENT_TYPE_MOTION: cvInject(CVE_MOTION, AMotionEvent_getX(e, 0), AMotionEvent_getY(e, 0)); break; default: handled = 0; } return handled; }
/** * Process the next input event. */ static int32_t EngineHandleInput(struct android_app* app, AInputEvent* event) { struct Engine* engine = (struct Engine*)app->userData; if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { int32_t x = AMotionEvent_getX(event, 0); int32_t y = AMotionEvent_getY(event, 0); if(abs((engine->state.x -x) + (engine->state.y - y)) > 4) { engine->state.x = x; engine->state.y = y; if(engine->state.x < engine->state.width/2 ) { scr_touch = 1; } else { scr_touch = 2; } } return 1; } return 0; }
void WindowImplAndroid::processPointerEvent(bool isDown, AInputEvent* _event, ActivityStates* states) { int32_t device = AInputEvent_getSource(_event); int32_t action = AMotionEvent_getAction(_event); int index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; int id = AMotionEvent_getPointerId(_event, index); float x = AMotionEvent_getX(_event, index); float y = AMotionEvent_getY(_event, index); Event event; if (isDown) { if (device == AINPUT_SOURCE_MOUSE) { event.type = Event::MouseButtonPressed; event.mouseButton.button = static_cast<Mouse::Button>(id); event.mouseButton.x = x; event.mouseButton.y = y; if (id >= 0 && id < Mouse::ButtonCount) states->isButtonPressed[id] = true; } else if (device == AINPUT_SOURCE_TOUCHSCREEN) { event.type = Event::TouchBegan; event.touch.finger = id; event.touch.x = x; event.touch.y = y; states->touchEvents[id] = Vector2i(event.touch.x, event.touch.y); } } else { if (device == AINPUT_SOURCE_MOUSE) { event.type = Event::MouseButtonReleased; event.mouseButton.button = static_cast<Mouse::Button>(id); event.mouseButton.x = x; event.mouseButton.y = y; if (id >= 0 && id < Mouse::ButtonCount) states->isButtonPressed[id] = false; } else if (device == AINPUT_SOURCE_TOUCHSCREEN) { event.type = Event::TouchEnded; event.touch.finger = id; event.touch.x = x; event.touch.y = y; states->touchEvents.erase(id); } } forwardEvent(event); }
bool CAndroidMouse::onMouseEvent(AInputEvent* event) { if (event == NULL) return false; int32_t eventAction = AMotionEvent_getAction(event); int8_t mouseAction = eventAction & AMOTION_EVENT_ACTION_MASK; size_t mousePointer = eventAction >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; CXBMCApp::android_printf("%s pointer:%i", __PRETTY_FUNCTION__, mousePointer); float x = AMotionEvent_getX(event, mousePointer); float y = AMotionEvent_getY(event, mousePointer); switch (mouseAction) { case AMOTION_EVENT_ACTION_UP: case AMOTION_EVENT_ACTION_DOWN: MouseButton(x,y,mouseAction); return true; default: MouseMove(x,y); return true; } return false; }
bool CAndroidMouse::onMouseEvent(AInputEvent* event) { if (event == NULL) return false; int32_t eventAction = AMotionEvent_getAction(event); int8_t mouseAction = eventAction & AMOTION_EVENT_ACTION_MASK; size_t mousePointerIdx = eventAction >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; int32_t mousePointerId = AMotionEvent_getPointerId(event, mousePointerIdx); #ifdef DEBUG_VERBOSE CXBMCApp::android_printf("%s idx:%i, id:%i", __PRETTY_FUNCTION__, mousePointerIdx, mousePointerId); #endif CPoint in(AMotionEvent_getX(event, mousePointerIdx), AMotionEvent_getY(event, mousePointerIdx)); CPoint out = in * m_droid2guiRatio; switch (mouseAction) { case AMOTION_EVENT_ACTION_UP: case AMOTION_EVENT_ACTION_DOWN: MouseButton(out.x, out.y, mouseAction, AMotionEvent_getButtonState(event)); return true; case AMOTION_EVENT_ACTION_SCROLL: MouseWheel(out.x, out.y, AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_VSCROLL, mousePointerIdx)); return true; default: MouseMove(out.x, out.y); return true; } return false; }
static bool _process_keys(bool isJoy, AInputEvent *event, CookedEventCallback callback) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) { int action = AKeyEvent_getAction(event); int code = _translate_keycode(AKeyEvent_getKeyCode(event)); bool handled = code >= 0; if (code >= 0 && action == AKEY_EVENT_ACTION_DOWN) { _report_key_state(code, true, callback); } else if (code >= 0 && action == AKEY_EVENT_ACTION_UP) { _report_key_state(code, false, callback); } return handled; } else if (isJoy) { // use joystick axes to emulate directional key events (we could leave this // up to the platform, but joystick-to-dpad conversion doesn't work // on NDK on older devices, so we implement manually for maximum compatibility) float x = AMotionEvent_getX(event, 0); float y = AMotionEvent_getY(event, 0); if (_getAxisValue) { // take the hat switches into account too, so that either the // regular axes or the hat axes can be used to navigate UIs x += _getAxisValue(event, AXIS_HAT_X, 0); y += _getAxisValue(event, AXIS_HAT_Y, 0); x = Clamp(x, -1.0f, 1.0f); y = Clamp(y, -1.0f, 1.0f); } _report_key_states_from_axes(x, y, callback); return true; } }
int32_t Engine::onMotionEvent(android_app* app, AInputEvent* event) { if (!this->loaded) return 0; if (!this->focused) return 0; size_t pointerCount = AMotionEvent_getPointerCount(event); this->updateUptime(); for (size_t i = 0; i < pointerCount; i++) { size_t pointerId = AMotionEvent_getPointerId(event, i); size_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK; size_t pointerIndex = i; if (action == AMOTION_EVENT_ACTION_POINTER_DOWN || action == AMOTION_EVENT_ACTION_POINTER_UP) { pointerIndex = (AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; pointerId = AMotionEvent_getPointerId(event, pointerIndex); } this->touchEventParamCache[0] = pointerId; this->touchEventParamCache[1] = action; this->touchEventParamCache[2] = AMotionEvent_getX(event, pointerIndex); this->touchEventParamCache[3] = AMotionEvent_getY(event, pointerIndex); this->touchEventParamCache[4] = this->uptime.time; this->touchEventParamCache[5] = this->uptime.millitm; this->touchEventParamCache[6] = AInputEvent_getDeviceId(event); this->touchEventParamCache[7] = AInputEvent_getSource(event); if (callSqFunction_Bool_Floats(this->sqvm, EMO_NAMESPACE, EMO_FUNC_MOTIONEVENT, this->touchEventParamCache, MOTION_EVENT_PARAMS_SIZE, false)) { return 1; } }
/** * Process the next input event. */ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) { struct engine* engine = (struct engine*)app->userData; if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { unsigned int flags = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK; // gpg-cpp: Sign in or out on tap if (flags == AMOTION_EVENT_ACTION_UP || flags == AMOTION_EVENT_ACTION_POINTER_UP) { LOGI("Motion"); if (!StateManager::GetGameServices()->IsAuthorized()) { LOGI("Signing in"); StateManager::BeginUserInitiatedSignIn(); } else { LOGI("Signing out"); StateManager::SignOut(); } } // Make things pretty engine->animating = 1; engine->state.x = AMotionEvent_getX(event, 0); engine->state.y = AMotionEvent_getY(event, 0); return 1; } return 0; }
/** * Process the next input event. */ static int32_t handle_motion_event(struct android_app* app, AInputEvent* event) { struct engine* engine = (struct engine*)app->userData; int i, c = AMotionEvent_getPointerCount(event); for( i = 0; i<c; i++) { int32_t action = AMotionEvent_getAction(event); uint32_t id = action >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; uint32_t index = AMotionEvent_getPointerId(event, i); float x = AMotionEvent_getX(event, i); float y = AMotionEvent_getY(event, i); action = action & AMOTION_EVENT_ACTION_MASK; if (action == AMOTION_EVENT_ACTION_DOWN) { /* Workaround, these are needed in order to dispatch GK_ON_MOUSE_DOWN. The problem is that the library assumes that the 'mouse' is moved over a panel, before it's pressed. In case of thouchscreen the 'press' event happens before the 'move' which results in wrong/invalid gkMouseTarget. The mouse down event is dispatched ON the gkMouseTarget. */ onWindowMouseMove(x,y); gkUpdateMouseTarget(gkMainPanel); gkCheckFocusedPanel(); onWindowMouseDown(x,y,index); }else if (action == AMOTION_EVENT_ACTION_MOVE) { onWindowMouseMove(x,y); }else if (action == AMOTION_EVENT_ACTION_UP) { onWindowMouseUp(x,y,index); } __android_log_print(ANDROID_LOG_INFO, "GK", "action: %d index: %d id: %d i: %d", action, index, id, i); } return 1; }
static int32_t handleInput(struct android_app* app, AInputEvent* event) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { float x = AMotionEvent_getX(event, 0); float y = AMotionEvent_getY(event, 0); LOGW("MOTION: x=%d y=%d", (int)x, (int)y); if( abs(x - lastPos.x) < 25) { CameraRot.x += (x - lastPos.x) * 0.01; } if( abs(y - lastPos.y)< 25) { CameraRot.y += (y - lastPos.y) * 0.01; } camera->setPosition( cos( CameraRot.x) * 20, sin( CameraRot.y) * 20 , -sin( CameraRot.x) * 20 ); camera->lookAt(0,0,0); lastPos.x = x; lastPos.y = y; if(textArea != NULL) { char text[300]; sprintf(text, "X:%d Y:%d", (int)x, (int)y); textArea->setCaption( text ); } /* This next big chunk basically sends a raycast straight down from the camera's position It then checks to see if it is under world geometry and if it is we move the camera back up */ //Ogre::Vector3 camPos = camera->getPosition(); //Ogre::Ray cameraRay(Ogre::Vector3(camPos.x, 5000.0f, camPos.z), Ogre::Vector3::NEGATIVE_UNIT_Y); //mRayScnQuery->setRay(cameraRay); //create a raycast straight out from the camera at the mouse's location Ogre::Ray mouseRay = camera->getCameraToViewportRay( x/float(vp->getActualWidth()), y/float(vp->getActualHeight())); mRayScnQuery->setRay(mouseRay); Ogre::RaySceneQueryResult& result = mRayScnQuery->execute(); Ogre::RaySceneQueryResult::iterator iter = result.begin(); if(iter != result.end() && iter->movable) { LOGW("SELECT: %s", iter->movable->getName().c_str()); } return true; return 1; } return 0; }
/* * Get X, Y positions and ID's for all pointers */ static void getTouchPos(AInputEvent *event, int ids[], float xs[], float ys[]) { int pointerCount = AMotionEvent_getPointerCount(event); for(int i = 0; i < pointerCount; ++i) { ids[i] = AMotionEvent_getPointerId(event, i); xs[i] = AMotionEvent_getX(event, i); ys[i] = AMotionEvent_getY(event, i); } }
//callback extern int32_t __android_handle_input(struct android_app* app, AInputEvent* event){ unsigned int action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK; unsigned int actionPointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_POINTER_DOWN) { if(fingerDown){ int count = AMotionEvent_getPointerCount(event); int i = 0; for(;i < count ; i++) { unsigned int pointerFingerId = AMotionEvent_getPointerId(event, i); fingerDown(app->userData,pointerFingerId, AMotionEvent_getX(event, i), AMotionEvent_getY(event, i), AMotionEvent_getPressure(event, i)); } } } else if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_POINTER_UP) { //up if(fingerUp){ int count = AMotionEvent_getPointerCount(event); int i = 0; for(;i < count ; i++) { unsigned int pointerFingerId = AMotionEvent_getPointerId(event, i); fingerUp(app->userData,pointerFingerId, AMotionEvent_getX(event, i), AMotionEvent_getY(event, i), AMotionEvent_getPressure(event, i)); } } } else if (action == AMOTION_EVENT_ACTION_MOVE) { //move if(fingerMove){ int count = AMotionEvent_getPointerCount(event); int i = 0; for(;i < count ; i++) { unsigned int pointerFingerId = AMotionEvent_getPointerId(event, i); fingerMove(app->userData,pointerFingerId, AMotionEvent_getX(event, i), AMotionEvent_getY(event, i), AMotionEvent_getPressure(event, i)); } } } else if (action == AMOTION_EVENT_ACTION_CANCEL) { } else { return 0; } }
s32 process_input(struct android_app* app, AInputEvent* event) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { const s32 action = AMotionEvent_getAction(event); const s32 pointer_index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; const s32 pointer_count = AMotionEvent_getPointerCount(event); const s32 pointer_id = AMotionEvent_getPointerId(event, pointer_index); const f32 x = AMotionEvent_getX(event, pointer_index); const f32 y = AMotionEvent_getY(event, pointer_index); const s32 actionMasked = (action & AMOTION_EVENT_ACTION_MASK); switch (actionMasked) { case AMOTION_EVENT_ACTION_DOWN: case AMOTION_EVENT_ACTION_POINTER_DOWN: _queue.push_touch_event((s16)x, (s16)y, (u8)pointer_id, true); break; case AMOTION_EVENT_ACTION_UP: case AMOTION_EVENT_ACTION_POINTER_UP: _queue.push_touch_event((s16)x, (s16)y, (u8)pointer_id, false); break; case AMOTION_EVENT_ACTION_OUTSIDE: case AMOTION_EVENT_ACTION_CANCEL: _queue.push_touch_event((s16)x, (s16)y, (u8)pointer_id, false); break; case AMOTION_EVENT_ACTION_MOVE: for (int index = 0; index < pointer_count; index++) { const f32 xx = AMotionEvent_getX(event, index); const f32 yy = AMotionEvent_getY(event, index); const s32 id = AMotionEvent_getPointerId(event, index); _queue.push_touch_event((s16)xx, (s16)yy, (u8)id); } break; } return 1; } else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY)
Pointer get_pointer_event(AInputEvent* event, int32_t index) { const Vec2i& point = rainbow::graphics::convert_to_flipped_view(Vec2i( AMotionEvent_getX(event, index), AMotionEvent_getY(event, index))); return Pointer(AMotionEvent_getPointerId(event, index), point.x, point.y, AMotionEvent_getEventTime(event)); }
static int32_t app_handle_input(struct android_app* app, AInputEvent* event) { if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { AMotionEvent_getX(event, 0); AMotionEvent_getY(event, 0); return 1; } return 0; }
/** *处理下一输入事件。 */ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) { struct engine* engine = (struct engine*)app->userData; if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { engine->state.x = AMotionEvent_getX(event, 0); engine->state.y = AMotionEvent_getY(event, 0); return 1; } return 0; }