diff --git a/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp b/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp index 83d074e..29d4d75 100644 --- a/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp +++ b/QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp @@ -68,7 +68,7 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c return; } - KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNode(from->key()); + KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNodeKey(from->key()); // 处理特殊按键:可以在按键映射和普通映射间切换的按键 if (m_needSwitchGameAgain && KeyMap::KMT_CLICK == node.type @@ -225,13 +225,13 @@ int InputConvertGame::updateSteerWheelKeysPress(KeyMap::KeyMapNode &node, const } else if (QEvent::KeyRelease == from->type()) { keyPress = false; } - if (from->key() == node.steerWheel.upKey) { + if (from->key() == node.steerWheel.up.key) { node.steerWheel.upKeyPressed = keyPress; - } else if (from->key() == node.steerWheel.rightKey) { + } else if (from->key() == node.steerWheel.right.key) { node.steerWheel.rightKeyPressed = keyPress; - } else if (from->key() == node.steerWheel.downKey) { + } else if (from->key() == node.steerWheel.down.key) { node.steerWheel.downKeyPressed = keyPress; - } else if (from->key() == node.steerWheel.leftKey) { + } else if (from->key() == node.steerWheel.left.key) { node.steerWheel.leftKeyPressed = keyPress; } @@ -243,33 +243,33 @@ int InputConvertGame::updateSteerWheelKeysPress(KeyMap::KeyMapNode &node, const if (node.steerWheel.upKeyPressed) { count++; if (Qt::Key_unknown == keyPress1) { - keyPress1 = node.steerWheel.upKey; + keyPress1 = node.steerWheel.up.key; } else if (Qt::Key_unknown == keyPress2) { - keyPress2 = node.steerWheel.upKey; + keyPress2 = node.steerWheel.up.key; } } if (node.steerWheel.rightKeyPressed) { count++; if (Qt::Key_unknown == keyPress1) { - keyPress1 = node.steerWheel.rightKey; + keyPress1 = node.steerWheel.right.key; } else if (Qt::Key_unknown == keyPress2) { - keyPress2 = node.steerWheel.rightKey; + keyPress2 = node.steerWheel.right.key; } } if (node.steerWheel.downKeyPressed) { count++; if (Qt::Key_unknown == keyPress1) { - keyPress1 = node.steerWheel.downKey; + keyPress1 = node.steerWheel.down.key; } else if (Qt::Key_unknown == keyPress2) { - keyPress2 = node.steerWheel.downKey; + keyPress2 = node.steerWheel.down.key; } } if (node.steerWheel.leftKeyPressed) { count++; if (Qt::Key_unknown == keyPress1) { - keyPress1 = node.steerWheel.leftKey; + keyPress1 = node.steerWheel.left.key; } else if (Qt::Key_unknown == keyPress2) { - keyPress2 = node.steerWheel.leftKey; + keyPress2 = node.steerWheel.left.key; } } return count; @@ -283,24 +283,24 @@ void InputConvertGame::steerWheelMove(KeyMap::KeyMapNode &node, int keysNum, int QPointF movePos = node.steerWheel.centerPos; switch (keysNum) { case 2: - if (keyPress2 == node.steerWheel.upKey) { - movePos.setY(movePos.y() - node.steerWheel.upOffset); - } else if (keyPress2 == node.steerWheel.rightKey) { - movePos.setX(movePos.x() + node.steerWheel.rightOffset); - } else if (keyPress2 == node.steerWheel.downKey) { - movePos.setY(movePos.y() + node.steerWheel.downOffset); - } else if (keyPress2 == node.steerWheel.leftKey) { - movePos.setX(movePos.x() - node.steerWheel.leftOffset); + if (keyPress2 == node.steerWheel.up.key) { + movePos.setY(movePos.y() - node.steerWheel.up.offset); + } else if (keyPress2 == node.steerWheel.right.key) { + movePos.setX(movePos.x() + node.steerWheel.right.offset); + } else if (keyPress2 == node.steerWheel.down.key) { + movePos.setY(movePos.y() + node.steerWheel.down.offset); + } else if (keyPress2 == node.steerWheel.left.key) { + movePos.setX(movePos.x() - node.steerWheel.left.offset); } case 1: - if (keyPress1 == node.steerWheel.upKey) { - movePos.setY(movePos.y() - node.steerWheel.upOffset); - } else if (keyPress1 == node.steerWheel.rightKey) { - movePos.setX(movePos.x() + node.steerWheel.rightOffset); - } else if (keyPress1 == node.steerWheel.downKey) { - movePos.setY(movePos.y() + node.steerWheel.downOffset); - } else if (keyPress1 == node.steerWheel.leftKey) { - movePos.setX(movePos.x() - node.steerWheel.leftOffset); + if (keyPress1 == node.steerWheel.up.key) { + movePos.setY(movePos.y() - node.steerWheel.up.offset); + } else if (keyPress1 == node.steerWheel.right.key) { + movePos.setX(movePos.x() + node.steerWheel.right.offset); + } else if (keyPress1 == node.steerWheel.down.key) { + movePos.setY(movePos.y() + node.steerWheel.down.offset); + } else if (keyPress1 == node.steerWheel.left.key) { + movePos.setX(movePos.x() - node.steerWheel.left.offset); } break; } @@ -333,7 +333,7 @@ void InputConvertGame::processKeyClick(QPointF clickPos, bool clickTwice, bool s bool InputConvertGame::processMouseClick(const QMouseEvent *from) { - KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNode(from->button()); + KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNodeMouse(from->button()); if (KeyMap::KMT_INVALID == node.type) { return false; } @@ -373,9 +373,9 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from) m_mouseMoveLastConverPos.setY(m_mouseMoveLastConverPos.y() + distance.y() / m_showSize.height()); if (m_mouseMoveLastConverPos.x() < 0.1 - || m_mouseMoveLastConverPos.x() > 0.8 + || m_mouseMoveLastConverPos.x() > 0.9 || m_mouseMoveLastConverPos.y() < 0.1 - || m_mouseMoveLastConverPos.y() > 0.8) { + || m_mouseMoveLastConverPos.y() > 0.9) { mouseMoveStopTouch(); mouseMoveStartTouch(from); } @@ -389,8 +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(static_cast(m_showSize.width()*mouseMoveStartPos.x()), + static_cast(m_showSize.height()*mouseMoveStartPos.y())); QPoint posOffset = from->localPos().toPoint() - localPos; QPoint globalPos = from->globalPos(); @@ -404,7 +404,7 @@ void InputConvertGame::moveCursorTo(const QMouseEvent *from, const QPoint &pos) QPoint globalPos = from->globalPos(); globalPos -= posOffset; - qWarning() <<"move to: "<(); - QMetaEnum metaEnumMouseButtons = QMetaEnum::fromType(); - QMetaEnum metaEnumKeyMapType = QMetaEnum::fromType(); - jsonDoc = QJsonDocument::fromJson(json.toUtf8(), &jsonError); if(jsonError.error != QJsonParseError::NoError) @@ -56,20 +52,13 @@ void KeyMap::loadKeyMap(const QString &json) // switchKey rootObj = jsonDoc.object(); if (rootObj.contains("switchKey") && rootObj.value("switchKey").isString()) { - 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){ + QPair p = getItemKey(rootObj, "switchKey"); + if(p.first == AT_INVALID){ errorString = QString("json error: switchKey invalid"); goto parseError; } - if(key != -1){ - m_switchType = AT_KEY; - m_switchKey = key; - }else{ - m_switchType = AT_MOUSE; - m_switchKey = btn; - } + m_switchType = p.first; + m_switchKey = p.second; } else { errorString = QString("json error: no find switchKey"); goto parseError; @@ -114,106 +103,73 @@ void KeyMap::loadKeyMap(const QString &json) goto parseError; } - KeyMap::KeyMapType type = (KeyMap::KeyMapType)metaEnumKeyMapType.keyToValue(node.value("type").toString().toStdString().c_str()); + KeyMap::KeyMapType type = getItemType(node, "type"); switch (type) { case KeyMap::KMT_CLICK: { // safe check - if (!node.contains("key") || !node.value("key").isString() - || !node.contains("pos") || !node.value("pos").isObject() - || !node.value("pos").toObject().contains("x") || !node.value("pos").toObject().value("x").isDouble() - || !node.value("pos").toObject().contains("y") || !node.value("pos").toObject().value("y").isDouble() - || !node.contains("switchMap") || !node.value("switchMap").isBool() - ) { + if (!checkForClick(node)) { qWarning() << "json error: keyMapNodes node format error"; break; } - - Qt::Key key = (Qt::Key)metaEnumKey.keyToValue(node.value("key").toString().toStdString().c_str()); - Qt::MouseButtons btn = (Qt::MouseButtons)metaEnumMouseButtons.keyToValue(node.value("key").toString().toStdString().c_str()); - if (-1 == key && -1 == btn) { + QPair key = getItemKey(node, "key"); + if(key.first == AT_INVALID){ qWarning() << "json error: keyMapNodes node invalid key: " << node.value("key").toString(); break; } - KeyMapNode keyMapNode; keyMapNode.type = type; - if (key != -1) { - keyMapNode.click.keyNode.key = key; - } else { - keyMapNode.click.keyNode.key = btn; - } - keyMapNode.click.keyNode.pos = QPointF(node.value("pos").toObject().value("x").toDouble(), - node.value("pos").toObject().value("y").toDouble()); - keyMapNode.click.switchMap = node.value("switchMap").toBool(); + keyMapNode.click.keyNode.type = key.first; + keyMapNode.click.keyNode.key = key.second; + keyMapNode.click.keyNode.pos = getItemPos(node, "pos"); + keyMapNode.click.switchMap = getItemSwitchMap(node, "switchMap"); m_keyMapNodes.push_back(keyMapNode); } break; case KeyMap::KMT_CLICK_TWICE: { // safe check - if (!node.contains("key") || !node.value("key").isString() - || !node.contains("pos") || !node.value("pos").isObject() - || !node.value("pos").toObject().contains("x") || !node.value("pos").toObject().value("x").isDouble() - || !node.value("pos").toObject().contains("y") || !node.value("pos").toObject().value("y").isDouble() - ) { + if (!checkForClickDouble(node)) { qWarning() << "json error: keyMapNodes node format error"; break; } - Qt::Key key = (Qt::Key)metaEnumKey.keyToValue(node.value("key").toString().toStdString().c_str()); - Qt::MouseButtons btn = (Qt::MouseButtons)metaEnumMouseButtons.keyToValue(node.value("key").toString().toStdString().c_str()); - if (-1 == key && -1 == btn) { + QPair key = getItemKey(node, "key"); + if(key.first == AT_INVALID){ qWarning() << "json error: keyMapNodes node invalid key: " << node.value("key").toString(); break; } - KeyMapNode keyMapNode; keyMapNode.type = type; - if (key != -1) { - keyMapNode.clickTwice.keyNode.key = key; - } else { - keyMapNode.clickTwice.keyNode.key = btn; - } - keyMapNode.clickTwice.keyNode.pos = QPointF(node.value("pos").toObject().value("x").toDouble(), - node.value("pos").toObject().value("y").toDouble()); + keyMapNode.click.keyNode.type = key.first; + keyMapNode.click.keyNode.key = key.second; + keyMapNode.click.keyNode.pos = getItemPos(node, "pos"); + keyMapNode.click.switchMap = getItemSwitchMap(node, "switchMap"); m_keyMapNodes.push_back(keyMapNode); } break; case KeyMap::KMT_STEER_WHEEL: { // safe check - if (!node.contains("leftKey") || !node.value("leftKey").isString() - || !node.contains("rightKey") || !node.value("rightKey").isString() - || !node.contains("upKey") || !node.value("upKey").isString() - || !node.contains("downKey") || !node.value("downKey").isString() - || !node.contains("leftOffset") || !node.value("leftOffset").isDouble() - || !node.contains("rightOffset") || !node.value("rightOffset").isDouble() - || !node.contains("upOffset") || !node.value("upOffset").isDouble() - || !node.contains("downOffset") || !node.value("downOffset").isDouble() - || !node.contains("centerPos") || !node.value("centerPos").isObject() - || !node.value("centerPos").toObject().contains("x") || !node.value("centerPos").toObject().value("x").isDouble() - || !node.value("centerPos").toObject().contains("y") || !node.value("centerPos").toObject().value("y").isDouble() - ) { + if(!checkForSteerWhell(node)){ qWarning() << "json error: keyMapNodes node format error"; break; } - - Qt::Key leftKey = (Qt::Key)metaEnumKey.keyToValue(node.value("leftKey").toString().toStdString().c_str()); - Qt::MouseButtons leftBtn = (Qt::MouseButtons)metaEnumMouseButtons.keyToValue(node.value("leftKey").toString().toStdString().c_str()); - Qt::Key rightKey = (Qt::Key)metaEnumKey.keyToValue(node.value("rightKey").toString().toStdString().c_str()); - Qt::MouseButtons rightBtn = (Qt::MouseButtons)metaEnumMouseButtons.keyToValue(node.value("rightKey").toString().toStdString().c_str()); - Qt::Key upKey = (Qt::Key)metaEnumKey.keyToValue(node.value("upKey").toString().toStdString().c_str()); - Qt::MouseButtons upBtn = (Qt::MouseButtons)metaEnumMouseButtons.keyToValue(node.value("upKey").toString().toStdString().c_str()); - Qt::Key downKey = (Qt::Key)metaEnumKey.keyToValue(node.value("downKey").toString().toStdString().c_str()); - Qt::MouseButtons downBtn = (Qt::MouseButtons)metaEnumMouseButtons.keyToValue(node.value("downKey").toString().toStdString().c_str()); - - if ((-1 == leftKey && -1 == leftBtn) - || (-1 == rightKey && -1 == rightBtn) - || (-1 == upKey && -1 == upBtn) - || (-1 == downKey && -1 == downBtn) - ) { - qWarning() << "json error: keyMapNodes node invalid key: " << node.value("key").toString(); + QPair leftKey = getItemKey(node, "leftKey"); + QPair rightKey = getItemKey(node, "rightKey"); + QPair upKey = getItemKey(node, "upKey"); + QPair downKey = getItemKey(node, "downKey"); + if(leftKey.first == AT_INVALID || rightKey.first == AT_INVALID + || upKey.first == AT_INVALID || downKey.first == AT_INVALID) + { + if(leftKey.first == AT_INVALID) + qWarning() << "json error: keyMapNodes node invalid key: " << node.value("leftKey").toString(); + if(rightKey.first == AT_INVALID) + qWarning() << "json error: keyMapNodes node invalid key: " << node.value("rightKey").toString(); + if(upKey.first == AT_INVALID) + qWarning() << "json error: keyMapNodes node invalid key: " << node.value("upKey").toString(); + if(downKey.first == AT_INVALID) + qWarning() << "json error: keyMapNodes node invalid key: " << node.value("downKey").toString(); break; } @@ -226,41 +182,45 @@ void KeyMap::loadKeyMap(const QString &json) keyMapNode.steerWheel.pressKeysNum = 0; keyMapNode.steerWheel.firstPressKey = 0; - if (leftKey != -1) { - keyMapNode.steerWheel.leftKey = leftKey; - } else { - keyMapNode.steerWheel.leftKey = leftBtn; - } - if (rightKey != -1) { - keyMapNode.steerWheel.rightKey = rightKey; - } else { - keyMapNode.steerWheel.rightKey = rightBtn; - } - if (upKey != -1) { - keyMapNode.steerWheel.upKey = upKey; - } else { - keyMapNode.steerWheel.upKey = upBtn; - } - if (downKey != -1) { - keyMapNode.steerWheel.downKey = downKey; - } else { - keyMapNode.steerWheel.downKey = downBtn; - } - keyMapNode.steerWheel.leftOffset = node.value("leftOffset").toDouble(); - keyMapNode.steerWheel.rightOffset = node.value("rightOffset").toDouble(); - keyMapNode.steerWheel.upOffset = node.value("upOffset").toDouble(); - keyMapNode.steerWheel.downOffset = node.value("downOffset").toDouble(); - keyMapNode.steerWheel.centerPos = QPointF(node.value("centerPos").toObject().value("x").toDouble(), - node.value("centerPos").toObject().value("y").toDouble()); + keyMapNode.steerWheel.left = { leftKey.first, leftKey.second, getItemNumber(node, "leftOffset") }; + keyMapNode.steerWheel.right = { rightKey.first, rightKey.second, getItemNumber(node, "rightOffset") }; + keyMapNode.steerWheel.up = { upKey.first, upKey.second, getItemNumber(node, "upOffset") }; + keyMapNode.steerWheel.down = { downKey.first, downKey.second, getItemNumber(node, "downOffset") }; + + keyMapNode.steerWheel.centerPos = getItemPos(node, "centerPos"); m_keyMapNodes.push_back(keyMapNode); } break; + case KeyMap::KMT_DRAG: + { + // safe check + if(!checkForDrag(node)){ + qWarning() << "json error: keyMapNodes node format error"; + break; + } + + QPair key = getItemKey(node, "key"); + if(key.first == AT_INVALID){ + qWarning() << "json error: keyMapNodes node invalid key: " << node.value("key").toString(); + break; + } + KeyMapNode keyMapNode; + keyMapNode.type = type; + keyMapNode.drag.type = key.first; + keyMapNode.drag.key = key.second; + keyMapNode.drag.startPos = getItemPos(node, "startPos"); + keyMapNode.drag.endPos = getItemPos(node, "endPos"); + m_keyMapNodes.push_back(keyMapNode); + break; + } default: qWarning() << "json error: keyMapNodes invalid node type:" << node.value("type").toString(); break; } } } + // this must be called after m_keyMapNodes is stable + makeReverseMap(); qWarning() << "Script updated."; parseError: @@ -272,33 +232,20 @@ parseError: KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key) { - for (auto& itemNode : m_keyMapNodes) { - switch (itemNode.type) { - case KMT_CLICK: - if (itemNode.click.keyNode.key == key) { - return itemNode; - } - break; - case KMT_CLICK_TWICE: - if (itemNode.clickTwice.keyNode.key == key) { - return itemNode; - } - break; - case KMT_STEER_WHEEL: - if (itemNode.steerWheel.leftKey == key - || itemNode.steerWheel.rightKey == key - || itemNode.steerWheel.upKey == key - || itemNode.steerWheel.downKey == key - ) { - return itemNode; - } - break; - default: - break; - } - } + auto p = rmapKey.value(key, &m_invalidNode); + if(p == &m_invalidNode) + return *rmapMouse.value(key, &m_invalidNode); + return *p; +} - return m_invalidNode; +KeyMap::KeyMapNode& KeyMap::getKeyMapNodeKey(int key) +{ + return *rmapKey.value(key, &m_invalidNode); +} + +KeyMap::KeyMapNode& KeyMap::getKeyMapNodeMouse(int key) +{ + return *rmapMouse.value(key, &m_invalidNode); } bool KeyMap::isSwitchOnKeyboard() @@ -320,3 +267,140 @@ bool KeyMap::enableMouseMoveMap() { return !m_mouseMoveMap.startPos.isNull(); } + +void KeyMap::makeReverseMap() +{ + rmapKey.clear(); + rmapMouse.clear(); + for(int i = 0 ;i < m_keyMapNodes.size(); ++i) { + auto& node = m_keyMapNodes[i]; + switch (node.type) { + case KMT_CLICK: + { + QMultiHash& m = node.click.keyNode.type == AT_KEY ? rmapKey : rmapMouse; + m.insert(node.click.keyNode.key, &node); + } + break; + case KMT_CLICK_TWICE: + { + QMultiHash& m = node.clickTwice.keyNode.type == AT_KEY ? rmapKey : rmapMouse; + m.insert(node.clickTwice.keyNode.key, &node); + } + break; + case KMT_STEER_WHEEL: + { + QMultiHash& ml = node.steerWheel.left.type == AT_KEY ? rmapKey : rmapMouse; + ml.insert(node.steerWheel.left.key, &node); + QMultiHash& mr = node.steerWheel.right.type == AT_KEY ? rmapKey : rmapMouse; + mr.insert(node.steerWheel.right.key, &node); + QMultiHash& mu = node.steerWheel.up.type == AT_KEY ? rmapKey : rmapMouse; + mu.insert(node.steerWheel.up.key, &node); + QMultiHash& md = node.steerWheel.down.type == AT_KEY ? rmapKey : rmapMouse; + md.insert(node.steerWheel.down.key, &node); + } + break; + case KMT_DRAG: + { + QMultiHash& m = node.drag.type == AT_KEY ? rmapKey : rmapMouse; + m.insert(node.drag.key, &node); + } + break; + default: + break; + } + } +} + +// ---- check and get of json item ---- + +bool KeyMap::checkItemKey(const QJsonObject& node, QString name) +{ + return node.contains(name) && node.value(name).isString(); +} + +bool KeyMap::checkItemPos(const QJsonObject& node, QString name) +{ + if(node.contains(name) && node.value(name).isObject()){ + QJsonObject pos = node.value(name).toObject(); + return pos.contains("x") && pos.value("x").isDouble() + && pos.contains("y") && pos.value("y").isDouble(); + } + return false; +} + +bool KeyMap::checkItemDouble(const QJsonObject& node, QString name) +{ + return node.contains(name) && node.value(name).isDouble(); +} + +bool KeyMap::checkItemSwitchMap(const QJsonObject& node, QString name) +{ + return !node.contains(name) || node.value(name).isBool(); +} + +KeyMap::KeyMapType KeyMap::getItemType(const QJsonObject& node, QString name) +{ + QString value = node.value(name).toString(); + return static_cast(m_metaEnumKeyMapType.keyToValue(value.toStdString().c_str())); +} + +QPair KeyMap::getItemKey(const QJsonObject& node, QString name) +{ + QString value = node.value(name).toString(); + int key = m_metaEnumKey.keyToValue(value.toStdString().c_str()); + int btn = m_metaEnumMouseButtons.keyToValue(value.toStdString().c_str()); + if(key == -1 && btn == -1){ + return {AT_INVALID, -1}; + }else if(key != -1){ + return {AT_KEY, key}; + }else{ + return {AT_MOUSE, btn}; + } +} + +QPointF KeyMap::getItemPos(const QJsonObject& node, QString name) +{ + QJsonObject pos = node.value(name).toObject(); + return QPointF(pos.value("x").toDouble(), pos.value("y").toDouble()); +} + +double KeyMap::getItemNumber(const QJsonObject& node, QString name) +{ + return node.value(name).toDouble(); +} + +bool KeyMap::getItemSwitchMap(const QJsonObject& node, QString name) +{ + return node.value(name).toBool(false); +} + + +// ---- check for key-map node ---- + +bool KeyMap::checkForClick(const QJsonObject& node) +{ + return checkItemKey(node, "key") && checkItemPos(node, "pos") + && checkItemSwitchMap(node, "switchMap"); +} + +bool KeyMap::checkForClickDouble(const QJsonObject& node) +{ + return checkForClick(node); +} + +bool KeyMap::checkForSteerWhell(const QJsonObject& node) +{ + return checkItemKey(node, "leftKey") && checkItemKey(node, "rightKey") + && checkItemKey(node, "upKey") && checkItemKey(node, "downKey") + && checkItemDouble(node, "leftOffset") && checkItemDouble(node, "rightOffset") + && checkItemDouble(node, "upOffset") && checkItemDouble(node, "downOffset") + && checkItemPos(node, "centerPos"); +} + +bool KeyMap::checkForDrag(const QJsonObject& node) +{ + return checkItemKey(node, "key") + && checkItemPos(node, "startPos") && checkItemPos(node, "endPos") + && checkItemSwitchMap(node, "switchMap"); +} + diff --git a/QtScrcpy/device/controller/inputconvert/keymap/keymap.h b/QtScrcpy/device/controller/inputconvert/keymap/keymap.h index 59000be..450fb0a 100644 --- a/QtScrcpy/device/controller/inputconvert/keymap/keymap.h +++ b/QtScrcpy/device/controller/inputconvert/keymap/keymap.h @@ -4,7 +4,11 @@ #include #include #include +#include +#include +#include +class QJsonObject; class KeyMap : public QObject { @@ -15,16 +19,19 @@ public: KMT_CLICK = 0, KMT_CLICK_TWICE, KMT_STEER_WHEEL, + KMT_DRAG, }; Q_ENUM(KeyMapType) enum ActionType { + AT_INVALID = -1, AT_KEY = 0, AT_MOUSE = 1, }; Q_ENUM(ActionType) struct KeyNode { + ActionType type = AT_INVALID; int key = Qt::Key_unknown; QPointF pos = QPointF(0, 0); }; @@ -40,20 +47,15 @@ public: KeyNode keyNode; } clickTwice; struct { - // 方向盘矩形中心位置 - QPointF centerPos = {0.0f, 0.0f}; + // 方向盘矩形中心位置 + QPointF centerPos = {0.0, 0.0}; - // 方向盘矩形四个方向偏移量 - float leftOffset = 0.0f; - float rightOffset = 0.0f; - float upOffset = 0.0f; - float downOffset = 0.0f; - - // 方向盘矩形四个方向按键 - int leftKey = Qt::Key_unknown; - int rightKey = Qt::Key_unknown; - int upKey = Qt::Key_unknown; - int downKey = Qt::Key_unknown; + struct DirInfo{ + ActionType type = AT_KEY; // keyboard/mouse + int key = Qt::Key_unknown; // key/button + double offset = 0.0; + }; + DirInfo left, right, up, down; // 辅助变量 // 方向键的按下状态 @@ -66,6 +68,12 @@ public: // 第一次按下的键 int firstPressKey = 0; } steerWheel; + struct { + ActionType type = AT_KEY; + int key = Qt::Key_unknown; + QPointF startPos = QPointF(0, 0); + QPointF endPos = QPointF(0, 0); + } drag; }; KeyMapNode() {} ~KeyMapNode() {} @@ -81,6 +89,8 @@ public: void loadKeyMap(const QString &json); KeyMap::KeyMapNode& getKeyMapNode(int key); + KeyMap::KeyMapNode& getKeyMapNodeKey(int key); + KeyMap::KeyMapNode& getKeyMapNodeMouse(int key); bool isSwitchOnKeyboard(); int getSwitchKey(); MouseMoveMap getMouseMoveMap(); @@ -88,6 +98,28 @@ public: static const QString& getKeyMapPath(); +private: + // set up the reverse map from key/event event to keyMapNode + void makeReverseMap(); + + // parse json of the mapping script + bool checkItemKey(const QJsonObject& node, QString name="key"); + bool checkItemPos(const QJsonObject& node, QString name="pos"); + bool checkItemDouble(const QJsonObject& node, QString name); + bool checkItemSwitchMap(const QJsonObject& node, QString name="switchMap"); + + KeyMapType getItemType(const QJsonObject& node, QString name="type"); + QPair getItemKey(const QJsonObject& node, QString name="key"); + QPointF getItemPos(const QJsonObject& node, QString name="pos"); + double getItemNumber(const QJsonObject& node, QString name); + bool getItemSwitchMap(const QJsonObject& node, QString name="switchMap"); + +private: + bool checkForClick(const QJsonObject& node); + bool checkForClickDouble(const QJsonObject& node); + bool checkForSteerWhell(const QJsonObject& node); + bool checkForDrag(const QJsonObject& node); + private: QVector m_keyMapNodes; KeyMapNode m_invalidNode; @@ -95,6 +127,14 @@ private: int m_switchKey = Qt::Key_QuoteLeft; MouseMoveMap m_mouseMoveMap; static QString s_keyMapPath; + + // mapping of key/mouse event name to index + QMetaEnum m_metaEnumKey = QMetaEnum::fromType(); + QMetaEnum m_metaEnumMouseButtons = QMetaEnum::fromType(); + QMetaEnum m_metaEnumKeyMapType = QMetaEnum::fromType(); + // reverse map of key/mouse event + QMultiHash rmapKey; + QMultiHash rmapMouse; }; #endif // KEYMAP_H