diff --git a/src/inputcontrol/inputconvertgame.cpp b/src/inputcontrol/inputconvertgame.cpp index 29258d7..2275bcd 100644 --- a/src/inputcontrol/inputconvertgame.cpp +++ b/src/inputcontrol/inputconvertgame.cpp @@ -12,18 +12,26 @@ InputConvertGame::~InputConvertGame() void InputConvertGame::mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize) { + updateSize(frameSize, showSize); } void InputConvertGame::wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize) { + updateSize(frameSize, showSize); } void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize) { + updateSize(frameSize, showSize); if (!from || from->isAutoRepeat()) { return; } + if (isSteerWheelKeys(from)) { + processSteerWheel(from); + return; + } + AndroidMotioneventAction action; // pos QPointF pos; @@ -79,17 +87,135 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c } return; -/* - if (QEvent::KeyPress == from->type()) { - ControlEvent* controlEvent2 = new ControlEvent(ControlEvent::CET_MOUSE); - if (!controlEvent2) { +} + +void InputConvertGame::updateSize(const QSize &frameSize, const QSize &showSize) +{ + m_frameSize = frameSize; + m_showSize = showSize; +} + +void InputConvertGame::sendTouchDownEvent(int id, QPointF pos) +{ + sendTouchEvent(id, pos, AMOTION_EVENT_ACTION_DOWN); +} + +void InputConvertGame::sendTouchMoveEvent(int id, QPointF pos) +{ + sendTouchEvent(id, pos, AMOTION_EVENT_ACTION_MOVE); +} + +void InputConvertGame::sendTouchUpEvent(int id, QPointF pos) +{ + sendTouchEvent(id, pos, AMOTION_EVENT_ACTION_UP); +} + +void InputConvertGame::sendTouchEvent(int id, QPointF pos, AndroidMotioneventAction action) +{ + if (0 > id || MULTI_TOUCH_MAX_NUM-1 < id) { + return; + } + ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_TOUCH); + if (!controlEvent) { + return; + } + controlEvent->setTouchEventData(id, action, QRect(calcAbsolutePos(pos).toPoint(), m_frameSize)); + sendControlEvent(controlEvent); +} + +QPointF InputConvertGame::calcAbsolutePos(QPointF relativePos) +{ + QPointF absolutePos; + absolutePos.setX(m_showSize.width() * relativePos.x()); + absolutePos.setY(m_showSize.height() * relativePos.y()); + // convert pos + absolutePos.setX(absolutePos.x() * m_frameSize.width() / m_showSize.width()); + absolutePos.setY(absolutePos.y() * m_frameSize.height() / m_showSize.height()); + return absolutePos; +} + +int InputConvertGame::attachTouchID(int key) +{ + for (int i = 0; i < MULTI_TOUCH_MAX_NUM; i++) { + if (0 == multiTouchID[i]) { + multiTouchID[i] = key; + return i; + } + } + return -1; +} + +void InputConvertGame::detachTouchID(int key) +{ + for (int i = 0; i < MULTI_TOUCH_MAX_NUM; i++) { + if (key == multiTouchID[i]) { + multiTouchID[i] = 0; return; } - pos.setY(pos.y() - 50); - action &= AMOTION_EVENT_ACTION_POINTER_INDEX_MASK; - action |= AMOTION_EVENT_ACTION_MOVE; - controlEvent2->setMouseEventData((AndroidMotioneventAction)action, AMOTION_EVENT_BUTTON_PRIMARY, QRect(pos.toPoint(), frameSize)); - sendControlEvent(controlEvent2); } - */ +} + +int InputConvertGame::getTouchID(int key) +{ + for (int i = 0; i < MULTI_TOUCH_MAX_NUM; i++) { + if (key == multiTouchID[i]) { + return i; + } + } + return -1; +} + +bool InputConvertGame::isSteerWheelKeys(const QKeyEvent *from) +{ + for (int key : m_steerWheelKeys) { + if (key == from->key()) { + return true; + } + } + return false; +} +#include +void InputConvertGame::processSteerWheel(const QKeyEvent *from) +{ + QPointF movePos = m_steerWheelPos; + int moveKey = 0; + if (QEvent::KeyPress == from->type()) { + m_steerWheelKeysNum++; + if (1 == m_steerWheelKeysNum) { + int id = attachTouchID(from->key()); + if (-1 == id) { + return; + } + m_steerWheelFirstTouchKey = from->key(); + sendTouchDownEvent(id, m_steerWheelPos); + moveKey = from->key(); + } else if (2 == m_steerWheelKeysNum) { + moveKey = from->key(); + } + + } else if (QEvent::KeyRelease == from->type()){ + m_steerWheelKeysNum--; + if (0 == m_steerWheelKeysNum) { + sendTouchUpEvent(getTouchID(m_steerWheelFirstTouchKey), m_steerWheelPos); + detachTouchID(from->key()); + m_steerWheelFirstTouchKey = 0; + return; + } else if (1 == m_steerWheelKeysNum) { + moveKey = m_steerWheelFirstTouchKey; + } + } + + if (moveKey == m_steerWheelKeys[SWD_UP]) { + movePos.setY(movePos.y() - m_steerWheelOffset); + } else if (moveKey == m_steerWheelKeys[SWD_RIGHT]) { + movePos.setX(movePos.x() + m_steerWheelOffset); + } else if (moveKey == m_steerWheelKeys[SWD_DOWN]) { + movePos.setY(movePos.y() + m_steerWheelOffset); + } else if (moveKey == m_steerWheelKeys[SWD_LEFT]) { + movePos.setX(movePos.x() - m_steerWheelOffset); + } + if (0 != moveKey) { + qDebug() << "move pos" << movePos; + sendTouchMoveEvent(getTouchID(m_steerWheelFirstTouchKey), movePos); + } } diff --git a/src/inputcontrol/inputconvertgame.h b/src/inputcontrol/inputconvertgame.h index fa79d86..8f98ef5 100644 --- a/src/inputcontrol/inputconvertgame.h +++ b/src/inputcontrol/inputconvertgame.h @@ -1,8 +1,10 @@ #ifndef INPUTCONVERTGAME_H #define INPUTCONVERTGAME_H +#include #include "inputconvertbase.h" +#define MULTI_TOUCH_MAX_NUM 10 class InputConvertGame : public InputConvertBase { public: @@ -12,6 +14,44 @@ public: void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize); void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize); void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize); + +protected: + void updateSize(const QSize& frameSize, const QSize& showSize); + void sendTouchDownEvent(int id, QPointF pos); + void sendTouchMoveEvent(int id, QPointF pos); + void sendTouchUpEvent(int id, QPointF pos); + void sendTouchEvent(int id, QPointF pos, AndroidMotioneventAction action); + QPointF calcAbsolutePos(QPointF relativePos); + + // multi touch id + int attachTouchID(int key); + void detachTouchID(int key); + int getTouchID(int key); + + // steer wheel + bool isSteerWheelKeys(const QKeyEvent* from); + void processSteerWheel(const QKeyEvent* from); + +private: + enum SteerWheelDirection { + SWD_UP = 0, + SWD_RIGHT, + SWD_DOWN, + SWD_LEFT, + }; + +private: + QSize m_frameSize; + QSize m_showSize; + + int multiTouchID[MULTI_TOUCH_MAX_NUM] = { 0 }; + + QPointF m_steerWheelPos = {0.16f, 0.75f}; + float m_steerWheelOffset = 0.1f; + // order by SteerWheelDirection(up right down left) + int m_steerWheelKeys[4] = {Qt::Key_W, Qt::Key_D, Qt::Key_S, Qt::Key_A}; + int m_steerWheelKeysNum = 0; + int m_steerWheelFirstTouchKey = 0; }; #endif // INPUTCONVERTGAME_H diff --git a/src/videoform.cpp b/src/videoform.cpp index 1a31d7f..1efbae4 100644 --- a/src/videoform.cpp +++ b/src/videoform.cpp @@ -88,6 +88,7 @@ void VideoForm::updateShowSize(const QSize &newSize) // 窗口居中 move(screenRect.center() - geometry().center()); + qDebug() << screenRect.center() << geometry().center(); } if (showSize != size()) {