diff --git a/QtScrcpy/device/controller/controller.cpp b/QtScrcpy/device/controller/controller.cpp index 59e7fa4..4231b8c 100644 --- a/QtScrcpy/device/controller/controller.cpp +++ b/QtScrcpy/device/controller/controller.cpp @@ -12,13 +12,7 @@ Controller::Controller(QString gameScript, QObject* parent) : QObject(parent) m_receiver = new Receiver(this); Q_ASSERT(m_receiver); - if (!gameScript.isEmpty()) { - InputConvertGame* convertgame = new InputConvertGame(this); - convertgame->loadKeyMap(gameScript); - m_inputConvert = convertgame; - } else { - m_inputConvert = new InputConvertNormal(this); - } + updateScript(gameScript); Q_ASSERT(m_inputConvert); connect(m_inputConvert, &InputConvertBase::grabCursor, this, &Controller::grabCursor); } diff --git a/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp b/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp index 6c30759..83d074e 100644 --- a/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp +++ b/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp @@ -19,17 +19,25 @@ InputConvertGame::~InputConvertGame() void InputConvertGame::mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize) { + // 处理开关按键 + if (m_keyMap.isSwitchOnKeyboard() == false && + from->type() == QEvent::MouseButtonPress && + m_keyMap.getSwitchKey() == from->button()) + { + if (!switchGameMap()) { + m_needSwitchGameAgain = false; + } + return; + } + if (m_gameMap) { updateSize(frameSize, showSize); - + // mouse move if (m_keyMap.enableMouseMoveMap()) { - // mouse move if (processMouseMove(from)) { return; } } - - // mouse click if (processMouseClick(from)) { return; @@ -51,7 +59,7 @@ void InputConvertGame::wheelEvent(const QWheelEvent *from, const QSize &frameSiz void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize) { // 处理开关按键 - if (m_keyMap.getSwitchKey() == from->key()) { + if (m_keyMap.isSwitchOnKeyboard() && m_keyMap.getSwitchKey() == from->key()) { if (QEvent::KeyPress == from->type()) { if (!switchGameMap()) { m_needSwitchGameAgain = false; @@ -66,9 +74,8 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c && KeyMap::KMT_CLICK == node.type && node.click.switchMap) { updateSize(frameSize, showSize); - // Qt::Key_Tab Qt::Key_M - processKeyClick(node.click.keyNode.pos, false, - node.click.switchMap, from); + // Qt::Key_Tab Qt::Key_M for PUBG mobile + processKeyClick(node.click.keyNode.pos, false, node.click.switchMap, from); return; } @@ -78,17 +85,14 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c return; } + switch (node.type) { // 处理方向盘 - if (KeyMap::KMT_STEER_WHEEL == node.type) { + case KeyMap::KMT_STEER_WHEEL: processSteerWheel(node, from); return; - } - // 处理普通按键 - switch (node.type) { case KeyMap::KMT_CLICK: - processKeyClick(node.click.keyNode.pos, false, - node.click.switchMap, from); + processKeyClick(node.click.keyNode.pos, false, node.click.switchMap, from); return; case KeyMap::KMT_CLICK_TWICE: processKeyClick(node.clickTwice.keyNode.pos, true, false, from); @@ -350,10 +354,11 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from) { if (QEvent::MouseMove != from->type()) { return false; - } + } + //qWarning() << from->localPos() << " - " << from->globalPos(); if (checkCursorPos(from)) { - m_mouseMoveLastPos = QPointF(0.0f, 0.0f); + m_mouseMoveLastPos = QPointF(0.0, 0.0); return true; } @@ -362,7 +367,7 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from) distance /= m_keyMap.getMouseMoveMap().speedRatio; mouseMoveStartTouch(from); - startMouseMoveTimer(); + //startMouseMoveTimer(); m_mouseMoveLastConverPos.setX(m_mouseMoveLastConverPos.x() + distance.x() / m_showSize.width()); m_mouseMoveLastConverPos.setY(m_mouseMoveLastConverPos.y() + distance.y() / m_showSize.height()); @@ -374,7 +379,7 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from) mouseMoveStopTouch(); mouseMoveStartTouch(from); } - + //qWarning() << "move: "<localPos(); @@ -384,7 +389,8 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from) void InputConvertGame::moveCursorToStart(const QMouseEvent *from) { QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos; - QPoint localPos = QPoint(m_showSize.width()*mouseMoveStartPos.x(), m_showSize.height()*mouseMoveStartPos.y()); + QPoint localPos = QPoint(m_showSize.width()*mouseMoveStartPos.x(), + m_showSize.height()*mouseMoveStartPos.y()); QPoint posOffset = from->localPos().toPoint() - localPos; QPoint globalPos = from->globalPos(); @@ -398,6 +404,7 @@ void InputConvertGame::moveCursorTo(const QMouseEvent *from, const QPoint &pos) QPoint globalPos = from->globalPos(); globalPos -= posOffset; + qWarning() <<"move to: "<pos(); if (pos.x() < CURSOR_POS_CHECK) { pos.setX(m_showSize.width() - CURSOR_POS_CHECK); - moveCursor = true; } else if (pos.x() > m_showSize.width() - CURSOR_POS_CHECK) { pos.setX(CURSOR_POS_CHECK); - moveCursor = true; } else if (pos.y() < CURSOR_POS_CHECK) { pos.setY(m_showSize.height() - CURSOR_POS_CHECK); - moveCursor = true; } else if (pos.y() > m_showSize.height() - CURSOR_POS_CHECK) { pos.setY(CURSOR_POS_CHECK); - moveCursor = true; + }else{ + moveCursor = false; } if (moveCursor) { diff --git a/QtScrcpy/device/controller/inputconvert/inputconvertgame.h b/QtScrcpy/device/controller/inputconvert/inputconvertgame.h index 0869d05..5313f7a 100644 --- a/QtScrcpy/device/controller/inputconvert/inputconvertgame.h +++ b/QtScrcpy/device/controller/inputconvert/inputconvertgame.h @@ -74,7 +74,7 @@ private: // mouse move QPointF m_mouseMoveLastConverPos; - QPointF m_mouseMoveLastPos = {0.0f, 0.0f}; + QPointF m_mouseMoveLastPos = {0.0, 0.0}; bool m_mouseMovePress = false; int m_mouseMoveTimer = 0; diff --git a/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp b/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp index 4297485..ede3086 100644 --- a/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp +++ b/QtScrcpy/device/controller/inputconvert/keymap/keymap.cpp @@ -56,12 +56,20 @@ void KeyMap::loadKeyMap(const QString &json) // switchKey rootObj = jsonDoc.object(); if (rootObj.contains("switchKey") && rootObj.value("switchKey").isString()) { - Qt::Key key = (Qt::Key)metaEnumKey.keyToValue(rootObj.value("switchKey").toString().toStdString().c_str()); - if (-1 == key) { + QString name = rootObj.value("switchKey").toString(); + int key = metaEnumKey.keyToValue(name.toStdString().c_str()); + int btn = metaEnumMouseButtons.keyToValue(name.toStdString().c_str()); + if(key == -1 && btn == -1){ errorString = QString("json error: switchKey invalid"); goto parseError; } - m_switchKey = key; + if(key != -1){ + m_switchType = AT_KEY; + m_switchKey = key; + }else{ + m_switchType = AT_MOUSE; + m_switchKey = btn; + } } else { errorString = QString("json error: no find switchKey"); goto parseError; @@ -253,6 +261,7 @@ void KeyMap::loadKeyMap(const QString &json) } } } + qWarning() << "Script updated."; parseError: if (!errorString.isEmpty()) { @@ -292,6 +301,11 @@ KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key) return m_invalidNode; } +bool KeyMap::isSwitchOnKeyboard() +{ + return m_switchType == AT_KEY; +} + int KeyMap::getSwitchKey() { return m_switchKey; diff --git a/QtScrcpy/device/controller/inputconvert/keymap/keymap.h b/QtScrcpy/device/controller/inputconvert/keymap/keymap.h index 1b4883b..59000be 100644 --- a/QtScrcpy/device/controller/inputconvert/keymap/keymap.h +++ b/QtScrcpy/device/controller/inputconvert/keymap/keymap.h @@ -18,6 +18,12 @@ public: }; Q_ENUM(KeyMapType) + enum ActionType { + AT_KEY = 0, + AT_MOUSE = 1, + }; + Q_ENUM(ActionType) + struct KeyNode { int key = Qt::Key_unknown; QPointF pos = QPointF(0, 0); @@ -66,7 +72,7 @@ public: }; struct MouseMoveMap { - QPointF startPos = {0.0f, 0.0f}; + QPointF startPos = {0.0, 0.0}; int speedRatio = 1; }; @@ -75,6 +81,7 @@ public: void loadKeyMap(const QString &json); KeyMap::KeyMapNode& getKeyMapNode(int key); + bool isSwitchOnKeyboard(); int getSwitchKey(); MouseMoveMap getMouseMoveMap(); bool enableMouseMoveMap(); @@ -84,6 +91,7 @@ public: private: QVector m_keyMapNodes; KeyMapNode m_invalidNode; + ActionType m_switchType = AT_KEY; int m_switchKey = Qt::Key_QuoteLeft; MouseMoveMap m_mouseMoveMap; static QString s_keyMapPath;