feat: update key map

This commit is contained in:
rankun 2019-08-22 14:16:51 +08:00
commit 2c94e458c6
4 changed files with 356 additions and 190 deletions

View file

@ -9,7 +9,10 @@
InputConvertGame::InputConvertGame(Controller* controller) InputConvertGame::InputConvertGame(Controller* controller)
: InputConvertNormal(controller) : InputConvertNormal(controller)
{ {
m_keyMap.loadKeyMapNode();
if (m_keyMap.enableMouseMoveMap()) {
m_mouseMoveLastConverPos = m_keyMap.getMouseMoveMap().startPos;
}
} }
InputConvertGame::~InputConvertGame() InputConvertGame::~InputConvertGame()
@ -22,11 +25,14 @@ void InputConvertGame::mouseEvent(const QMouseEvent *from, const QSize &frameSiz
if (m_gameMap) { if (m_gameMap) {
updateSize(frameSize, showSize); updateSize(frameSize, showSize);
// mouse move if (m_keyMap.enableMouseMoveMap()) {
if (processMouseMove(from)) { // mouse move
return; if (processMouseMove(from)) {
return;
}
} }
// mouse click // mouse click
if (processMouseClick(from)) { if (processMouseClick(from)) {
return; return;
@ -47,8 +53,8 @@ void InputConvertGame::wheelEvent(const QWheelEvent *from, const QSize &frameSiz
void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize) void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize)
{ {
switch (from->key()) { // 处理开关按键
case Qt::Key_QuoteLeft: if (m_keyMap.getSwitchKey() == from->key()) {
if (QEvent::KeyPress == from->type()) { if (QEvent::KeyPress == from->type()) {
if (!switchGameMap()) { if (!switchGameMap()) {
m_needSwitchGameAgain = false; m_needSwitchGameAgain = false;
@ -57,12 +63,16 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
return; return;
} }
if (m_needSwitchGameAgain) { KeyMap::KeyMapNode node = m_keyMap.getKeyMapNode(from->key());
// 处理特殊按键:可以在按键映射和普通映射间切换的按键
if (m_needSwitchGameAgain
&& KeyMap::KMT_CLICK == node.type
&& node.click.switchMap) {
updateSize(frameSize, showSize); updateSize(frameSize, showSize);
// Qt::Key_Tab Qt::Key_M // Qt::Key_Tab Qt::Key_M
if (processKeyClick(from)) { processKeyClick(node.click.keyNode.pos, false,
return; node.click.switchMap, from);
} return;
} }
if (m_gameMap) { if (m_gameMap) {
@ -71,14 +81,20 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
return; return;
} }
// steer wheel // 处理方向盘
if (isSteerWheelKeys(from)) { if (KeyMap::KMT_STEER_WHEEL == node.type) {
processSteerWheel(from); processSteerWheel(node, from);
return; return;
} }
// key click // 处理普通按键
if (processKeyClick(from)) { switch (node.type) {
case KeyMap::KMT_CLICK:
processKeyClick(node.click.keyNode.pos, false,
node.click.switchMap, from);
return;
case KeyMap::KMT_CLICK_TWICE:
processKeyClick(node.clickTwice.keyNode.pos, true, false, from);
return; return;
} }
} else { } else {
@ -159,49 +175,39 @@ int InputConvertGame::getTouchID(int key)
return -1; return -1;
} }
bool InputConvertGame::isSteerWheelKeys(const QKeyEvent *from) void InputConvertGame::processSteerWheel(KeyMap::KeyMapNode &node, const QKeyEvent *from)
{
for (int key : m_steerWheelKeys) {
if (key == from->key()) {
return true;
}
}
return false;
}
void InputConvertGame::processSteerWheel(const QKeyEvent *from)
{ {
int keyPress1 = -1; int keyPress1 = Qt::Key_unknown;
int keyPress2 = -1; int keyPress2 = Qt::Key_unknown;
int keysNum = updateSteerWheelKeysPress(from, keyPress1, keyPress2); int keysNum = updateSteerWheelKeysPress(node, from, keyPress1, keyPress2);
bool needMove = false; bool needMove = false;
if (QEvent::KeyPress == from->type()) { if (QEvent::KeyPress == from->type()) {
if (1 == keysNum) { if (1 == keysNum) {
m_steerWheelFirstTouchKey = from->key(); node.steerWheel.firstPressKey = from->key();
int id = attachTouchID(m_steerWheelFirstTouchKey); int id = attachTouchID(node.steerWheel.firstPressKey);
if (-1 == id) { if (-1 == id) {
return; return;
} }
sendTouchDownEvent(id, m_steerWheelPos); sendTouchDownEvent(id, node.steerWheel.centerPos);
needMove = true; needMove = true;
} else if (2 == keysNum) { } else if (2 == keysNum) {
needMove = true; needMove = true;
} }
} else if (QEvent::KeyRelease == from->type()){ } else if (QEvent::KeyRelease == from->type()){
if (0 == keysNum) { if (0 == keysNum) {
sendTouchUpEvent(getTouchID(m_steerWheelFirstTouchKey), m_steerWheelPos); sendTouchUpEvent(getTouchID(node.steerWheel.firstPressKey), node.steerWheel.centerPos);
detachTouchID(m_steerWheelFirstTouchKey); detachTouchID(node.steerWheel.firstPressKey);
m_steerWheelFirstTouchKey = 0; node.steerWheel.firstPressKey = 0;
} else if (1 == keysNum) { } else if (1 == keysNum) {
needMove = true; needMove = true;
} }
} }
if (needMove) { if (needMove) {
steerWheelMove(keysNum, keyPress1, keyPress2); steerWheelMove(node, keysNum, keyPress1, keyPress2);
} }
} }
int InputConvertGame::updateSteerWheelKeysPress(const QKeyEvent *from, int& keyPress1, int& keyPress2) int InputConvertGame::updateSteerWheelKeysPress(KeyMap::KeyMapNode &node, const QKeyEvent *from, int& keyPress1, int& keyPress2)
{ {
bool keyPress = false; bool keyPress = false;
if (QEvent::KeyPress == from->type()) { if (QEvent::KeyPress == from->type()) {
@ -209,146 +215,93 @@ int InputConvertGame::updateSteerWheelKeysPress(const QKeyEvent *from, int& keyP
} else if (QEvent::KeyRelease == from->type()) { } else if (QEvent::KeyRelease == from->type()) {
keyPress = false; keyPress = false;
} }
if (from->key() == m_steerWheelKeys[SWD_UP]) { if (from->key() == node.steerWheel.upKey) {
m_steerWheelKeysPress[SWD_UP] = keyPress; node.steerWheel.upKeyPressed = keyPress;
} else if (from->key() == m_steerWheelKeys[SWD_RIGHT]) { } else if (from->key() == node.steerWheel.rightKey) {
m_steerWheelKeysPress[SWD_RIGHT] = keyPress; node.steerWheel.rightKeyPressed = keyPress;
} else if (from->key() == m_steerWheelKeys[SWD_DOWN]) { } else if (from->key() == node.steerWheel.downKey) {
m_steerWheelKeysPress[SWD_DOWN] = keyPress; node.steerWheel.downKeyPressed = keyPress;
} else if (from->key() == m_steerWheelKeys[SWD_LEFT]) { } else if (from->key() == node.steerWheel.leftKey) {
m_steerWheelKeysPress[SWD_LEFT] = keyPress; node.steerWheel.leftKeyPressed = keyPress;
} }
int count = 0; int count = 0;
keyPress1 = -1; keyPress1 = Qt::Key_unknown;
keyPress2 = -1; keyPress2 = Qt::Key_unknown;
for (int i = 0; i < 4; i++) {
if (true == m_steerWheelKeysPress[i]) {
count++;
if (-1 == keyPress1) { // 上右下左的顺序统计按键数量,并记录前两个按键码
keyPress1 = i; if (node.steerWheel.upKeyPressed) {
} else if (-1 == keyPress2) { count++;
keyPress2 = i; if (Qt::Key_unknown == keyPress1) {
} keyPress1 = node.steerWheel.upKey;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.upKey;
}
}
if (node.steerWheel.rightKeyPressed) {
count++;
if (Qt::Key_unknown == keyPress1) {
keyPress1 = node.steerWheel.rightKey;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.rightKey;
}
}
if (node.steerWheel.downKeyPressed) {
count++;
if (Qt::Key_unknown == keyPress1) {
keyPress1 = node.steerWheel.downKey;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.downKey;
}
}
if (node.steerWheel.leftKeyPressed) {
count++;
if (Qt::Key_unknown == keyPress1) {
keyPress1 = node.steerWheel.leftKey;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.leftKey;
} }
} }
return count; return count;
} }
void InputConvertGame::steerWheelMove(int keysNum, int keyPress1, int keyPress2) void InputConvertGame::steerWheelMove(KeyMap::KeyMapNode &node, int keysNum, int keyPress1, int keyPress2)
{ {
if (1 != keysNum && 2 != keysNum) { if (1 != keysNum && 2 != keysNum) {
return; return;
} }
QPointF movePos = m_steerWheelPos; QPointF movePos = node.steerWheel.centerPos;
switch (keysNum) { switch (keysNum) {
case 2: case 2:
if (keyPress2 == SWD_UP) { if (keyPress2 == node.steerWheel.upKey) {
movePos.setY(movePos.y() - m_steerWheelOffset.top()); movePos.setY(movePos.y() - node.steerWheel.upOffset);
} else if (keyPress2 == SWD_RIGHT) { } else if (keyPress2 == node.steerWheel.rightKey) {
movePos.setX(movePos.x() + m_steerWheelOffset.right()); movePos.setX(movePos.x() + node.steerWheel.rightOffset);
} else if (keyPress2 == SWD_DOWN) { } else if (keyPress2 == node.steerWheel.downKey) {
movePos.setY(movePos.y() + m_steerWheelOffset.bottom()); movePos.setY(movePos.y() + node.steerWheel.downOffset);
} else if (keyPress2 == SWD_LEFT) { } else if (keyPress2 == node.steerWheel.leftKey) {
movePos.setX(movePos.x() - m_steerWheelOffset.left()); movePos.setX(movePos.x() - node.steerWheel.leftOffset);
} }
case 1: case 1:
if (keyPress1 == SWD_UP) { if (keyPress1 == node.steerWheel.upKey) {
movePos.setY(movePos.y() - m_steerWheelOffset.top()); movePos.setY(movePos.y() - node.steerWheel.upOffset);
} else if (keyPress1 == SWD_RIGHT) { } else if (keyPress1 == node.steerWheel.rightKey) {
movePos.setX(movePos.x() + m_steerWheelOffset.right()); movePos.setX(movePos.x() + node.steerWheel.rightOffset);
} else if (keyPress1 == SWD_DOWN) { } else if (keyPress1 == node.steerWheel.downKey) {
movePos.setY(movePos.y() + m_steerWheelOffset.bottom()); movePos.setY(movePos.y() + node.steerWheel.downOffset);
} else if (keyPress1 == SWD_LEFT) { } else if (keyPress1 == node.steerWheel.leftKey) {
movePos.setX(movePos.x() - m_steerWheelOffset.left()); movePos.setX(movePos.x() - node.steerWheel.leftOffset);
} }
break; break;
} }
sendTouchMoveEvent(getTouchID(m_steerWheelFirstTouchKey), movePos); sendTouchMoveEvent(getTouchID(node.steerWheel.firstPressKey), movePos);
} }
bool InputConvertGame::processKeyClick(const QKeyEvent *from) void InputConvertGame::processKeyClick(QPointF clickPos, bool clickTwice, bool switchMap, const QKeyEvent *from)
{ {
QPointF clickPos; if (switchMap && QEvent::KeyRelease == from->type()) {
bool clickTwice = false; m_needSwitchGameAgain = !m_needSwitchGameAgain;
switch (from->key()) { switchGameMap();
case Qt::Key_Space: // 跳
clickPos = QPointF(0.96f, 0.7f);
break;
case Qt::Key_M: // 地图
if (QEvent::KeyRelease == from->type()) {
m_needSwitchGameAgain = !m_needSwitchGameAgain;
switchGameMap();
}
clickPos = QPointF(0.98f, 0.03f);
break;
case Qt::Key_Tab: // 背包
if (QEvent::KeyRelease == from->type()) {
m_needSwitchGameAgain = !m_needSwitchGameAgain;
switchGameMap();
}
clickPos = QPointF(0.06f, 0.9f);
break;
case Qt::Key_Z: // 趴
clickPos = QPointF(0.95f, 0.9f);
break;
case Qt::Key_C: // 蹲
clickPos = QPointF(0.86f, 0.92f);
break;
case Qt::Key_R: // 换弹
clickPos = QPointF(0.795f, 0.93f);
break;
case Qt::Key_Alt: // 小眼睛
clickPos = QPointF(0.8f, 0.31f);
break;
case Qt::Key_F: // 捡东西1
clickPos = QPointF(0.7f, 0.34f);
break;
case Qt::Key_G: // 捡东西2
clickPos = QPointF(0.7f, 0.44f);
break;
case Qt::Key_H: // 捡东西3
clickPos = QPointF(0.7f, 0.54f);
break;
case Qt::Key_1: // 换枪1
clickPos = QPointF(0.45f, 0.9f);
break;
case Qt::Key_2: // 换枪2
clickPos = QPointF(0.55f, 0.9f);
break;
case Qt::Key_3: // 手雷
clickPos = QPointF(0.67f, 0.92f);
break;
case Qt::Key_4: // 快速打药
clickPos = QPointF(0.33f, 0.95f);
break;
case Qt::Key_5: // 下车
clickPos = QPointF(0.92f, 0.4f);
break;
case Qt::Key_6: // 救人
clickPos = QPointF(0.49f, 0.63f);
break;
case Qt::Key_Shift: // 车加速
clickPos = QPointF(0.82f, 0.8f);
break;
case Qt::Key_X: // 开关门
clickPos = QPointF(0.7f, 0.7f);
break;
case Qt::Key_T: // 舔包
clickPos = QPointF(0.72f, 0.26f);
break;
case Qt::Key_Q: // 左探头
clickTwice = true;
clickPos = QPointF(0.12f, 0.35f);
break;
case Qt::Key_E: // 右探头
clickTwice = true;
clickPos = QPointF(0.2, 0.35f);
break;
default:
return false;
break;
} }
if (QEvent::KeyPress == from->type()) { if (QEvent::KeyPress == from->type()) {
@ -366,25 +319,20 @@ bool InputConvertGame::processKeyClick(const QKeyEvent *from)
sendTouchUpEvent(getTouchID(from->key()), clickPos); sendTouchUpEvent(getTouchID(from->key()), clickPos);
detachTouchID(from->key()); detachTouchID(from->key());
} }
return true;
} }
bool InputConvertGame::processMouseClick(const QMouseEvent *from) bool InputConvertGame::processMouseClick(const QMouseEvent *from)
{ {
QPointF clickPos; KeyMap::KeyMapNode node = m_keyMap.getKeyMapNode(from->button());
if (Qt::LeftButton == from->button()) { if (KeyMap::KMT_INVALID == node.type) {
clickPos = QPointF(0.86f, 0.72f);
} else if (Qt::RightButton == from->button()){
clickPos = QPointF(0.96f, 0.52f);
} else {
return false; return false;
} }
if (QEvent::MouseButtonPress == from->type() || QEvent::MouseButtonDblClick == from->type()) { if (QEvent::MouseButtonPress == from->type() || QEvent::MouseButtonDblClick == from->type()) {
int id = attachTouchID(from->button()); int id = attachTouchID(from->button());
sendTouchDownEvent(id, clickPos); sendTouchDownEvent(id, node.click.keyNode.pos);
} else if (QEvent::MouseButtonRelease == from->type()) { } else if (QEvent::MouseButtonRelease == from->type()) {
sendTouchUpEvent(getTouchID(from->button()), clickPos); sendTouchUpEvent(getTouchID(from->button()), node.click.keyNode.pos);
detachTouchID(from->button()); detachTouchID(from->button());
} else { } else {
return false; return false;
@ -405,7 +353,7 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
if (!m_mouseMoveLastPos.isNull()) { if (!m_mouseMoveLastPos.isNull()) {
QPointF distance = from->localPos() - m_mouseMoveLastPos; QPointF distance = from->localPos() - m_mouseMoveLastPos;
distance /= 10; distance /= m_keyMap.getMouseMoveMap().speedRatio;
mouseMoveStartTouch(from); mouseMoveStartTouch(from);
startMouseMoveTimer(); startMouseMoveTimer();
@ -429,7 +377,8 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
void InputConvertGame::moveCursorToStart(const QMouseEvent *from) void InputConvertGame::moveCursorToStart(const QMouseEvent *from)
{ {
QPoint localPos = QPoint(m_showSize.width()*m_mouseMoveStartPos.x(), m_showSize.height()*m_mouseMoveStartPos.y()); QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos;
QPoint localPos = QPoint(m_showSize.width()*mouseMoveStartPos.x(), m_showSize.height()*mouseMoveStartPos.y());
QPoint posOffset = from->localPos().toPoint() - localPos; QPoint posOffset = from->localPos().toPoint() - localPos;
QPoint globalPos = from->globalPos(); QPoint globalPos = from->globalPos();
@ -464,10 +413,11 @@ void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from)
{ {
Q_UNUSED(from); Q_UNUSED(from);
if (!m_mouseMovePress) { if (!m_mouseMovePress) {
QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos;
//moveCursorToStart(from); //moveCursorToStart(from);
int id = attachTouchID(Qt::ExtraButton24); int id = attachTouchID(Qt::ExtraButton24);
sendTouchDownEvent(id, m_mouseMoveStartPos); sendTouchDownEvent(id, mouseMoveStartPos);
m_mouseMoveLastConverPos = m_mouseMoveStartPos; m_mouseMoveLastConverPos = mouseMoveStartPos;
m_mouseMovePress = true; m_mouseMovePress = true;
} }
} }

View file

@ -2,7 +2,9 @@
#define INPUTCONVERTGAME_H #define INPUTCONVERTGAME_H
#include <QPointF> #include <QPointF>
#include "inputconvertnormal.h" #include "inputconvertnormal.h"
#include "keymap.h"
#define MULTI_TOUCH_MAX_NUM 10 #define MULTI_TOUCH_MAX_NUM 10
class InputConvertGame : public InputConvertNormal class InputConvertGame : public InputConvertNormal
@ -30,13 +32,12 @@ protected:
int getTouchID(int key); int getTouchID(int key);
// steer wheel // steer wheel
bool isSteerWheelKeys(const QKeyEvent* from); void processSteerWheel(KeyMap::KeyMapNode &node, const QKeyEvent* from);
void processSteerWheel(const QKeyEvent* from); int updateSteerWheelKeysPress(KeyMap::KeyMapNode &node, const QKeyEvent* from, int& keyPress1, int& keyPress2);
int updateSteerWheelKeysPress(const QKeyEvent* from, int& keyPress1, int& keyPress2); void steerWheelMove(KeyMap::KeyMapNode &node, int keysNum, int keyPress1, int keyPress2);
void steerWheelMove(int keysNum, int keyPress1, int keyPress2);
// click // click
bool processKeyClick(const QKeyEvent* from); void processKeyClick(QPointF clickPos, bool clickTwice, bool switchMap, const QKeyEvent* from);
// mouse // mouse
bool processMouseClick(const QMouseEvent* from); bool processMouseClick(const QMouseEvent* from);
@ -67,24 +68,17 @@ private:
QSize m_showSize; QSize m_showSize;
bool m_gameMap = false; bool m_gameMap = false;
int multiTouchID[MULTI_TOUCH_MAX_NUM] = { 0 }; int multiTouchID[MULTI_TOUCH_MAX_NUM] = { 0 };
QPointF m_steerWheelPos = {0.16f, 0.75f}; // mouse move
QRectF m_steerWheelOffset = {QPointF(0.1f, 0.27f), QPointF(0.1f, 0.2f)}; QPointF m_mouseMoveLastConverPos;
// order by SteerWheelDirection(up right down left)
int m_steerWheelKeys[4] = {Qt::Key_W, Qt::Key_D, Qt::Key_S, Qt::Key_A};
bool m_steerWheelKeysPress[4] = { false };
int m_steerWheelKeysNum = 0;
int m_steerWheelFirstTouchKey = 0;
// mouse move
QPointF m_mouseMoveStartPos = {0.57f, 0.26f};
QPointF m_mouseMoveLastConverPos = m_mouseMoveStartPos;
QPointF m_mouseMoveLastPos = {0.0f, 0.0f}; QPointF m_mouseMoveLastPos = {0.0f, 0.0f};
bool m_mouseMovePress = false; bool m_mouseMovePress = false;
int m_mouseMoveTimer = 0; int m_mouseMoveTimer = 0;
bool m_needSwitchGameAgain = false; bool m_needSwitchGameAgain = false;
KeyMap m_keyMap;
}; };
#endif // INPUTCONVERTGAME_H #endif // INPUTCONVERTGAME_H

View file

@ -9,11 +9,162 @@ void KeyMap::loadKeyMapNode()
{ {
KeyMapNode node; KeyMapNode node;
node.type = KMT_CLICK; node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_J; node.click.keyNode.key = Qt::Key_Space; // 跳
node.click.keyNode.pos = QPointF(0.5f, 0.5f); node.click.keyNode.pos = QPointF(0.96f, 0.7f);
node.click.quitMap = false; node.click.switchMap = false;
m_keyMapNodes.push_back(node); m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_M; // 地图
node.click.keyNode.pos = QPointF(0.98f, 0.03f);
node.click.switchMap = true;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_Tab; // 背包
node.click.keyNode.pos = QPointF(0.06f, 0.9f);
node.click.switchMap = true;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_Z; // 趴
node.click.keyNode.pos = QPointF(0.95f, 0.9f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_C; // 蹲
node.click.keyNode.pos = QPointF(0.86f, 0.92f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_R; // 换弹
node.click.keyNode.pos = QPointF(0.795f, 0.93f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_Alt; // 小眼睛
node.click.keyNode.pos = QPointF(0.8f, 0.31f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_F; // 捡东西1
node.click.keyNode.pos = QPointF(0.7f, 0.34f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_G; // 捡东西2
node.click.keyNode.pos = QPointF(0.7f, 0.44f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_H; // 捡东西3
node.click.keyNode.pos = QPointF(0.7f, 0.54f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_1; // 换枪1
node.click.keyNode.pos = QPointF(0.45f, 0.9f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_2; // 换枪2
node.click.keyNode.pos = QPointF(0.55f, 0.9f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_3; // 手雷
node.click.keyNode.pos = QPointF(0.67f, 0.92f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_4; // 快速打药
node.click.keyNode.pos = QPointF(0.33f, 0.95f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_5; // 下车
node.click.keyNode.pos = QPointF(0.92f, 0.4f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_6; // 救人
node.click.keyNode.pos = QPointF(0.49f, 0.63f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_Shift; // 车加速
node.click.keyNode.pos = QPointF(0.82f, 0.8f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_X; // 开关门
node.click.keyNode.pos = QPointF(0.7f, 0.7f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::Key_T; // 舔包
node.click.keyNode.pos = QPointF(0.72f, 0.26f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::LeftButton; // 开枪
node.click.keyNode.pos = QPointF(0.86f, 0.72f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
node.type = KMT_CLICK;
node.click.keyNode.key = Qt::RightButton; // 开镜
node.click.keyNode.pos = QPointF(0.96f, 0.52f);
node.click.switchMap = false;
m_keyMapNodes.push_back(node);
KeyMapNode node2;
node2.type = KMT_CLICK_TWICE;
node2.clickTwice.keyNode.key = Qt::Key_Q; // 左探头
node2.clickTwice.keyNode.pos = QPointF(0.12f, 0.35f);
m_keyMapNodes.push_back(node2);
node2.type = KMT_CLICK_TWICE;
node2.clickTwice.keyNode.key = Qt::Key_E; // 右探头
node2.clickTwice.keyNode.pos = QPointF(0.2, 0.35f);
m_keyMapNodes.push_back(node2);
// 方向盘
KeyMapNode node3;
node3.type = KMT_STEER_WHEEL;
node3.steerWheel.centerPos = {0.16f, 0.75f};
node3.steerWheel.leftOffset = 0.1f;
node3.steerWheel.rightOffset = 0.1f;
node3.steerWheel.upOffset = 0.27f;
node3.steerWheel.downOffset = 0.2f;
node3.steerWheel.leftKey = Qt::Key_A;
node3.steerWheel.rightKey = Qt::Key_D;
node3.steerWheel.upKey = Qt::Key_W;
node3.steerWheel.downKey = Qt::Key_S;
m_keyMapNodes.push_back(node3);
m_mouseMoveMap.startPos = QPointF(0.57f, 0.26f);
m_mouseMoveMap.speedRatio = 10;
m_switchKey = Qt::Key_QuoteLeft;
} }
KeyMap::KeyMapNode KeyMap::getKeyMapNode(int key) KeyMap::KeyMapNode KeyMap::getKeyMapNode(int key)
@ -28,6 +179,24 @@ KeyMap::KeyMapNode KeyMap::getKeyMapNode(int key)
find = true; find = true;
} }
break; break;
case KMT_CLICK_TWICE:
if (node.clickTwice.keyNode.key == key) {
retNode = node;
find = true;
}
break;
case KMT_STEER_WHEEL:
if (node.steerWheel.leftKey == key
|| node.steerWheel.rightKey == key
|| node.steerWheel.upKey == key
|| node.steerWheel.downKey == key
) {
retNode = node;
find = true;
}
break;
default:
break;
} }
if (find) { if (find) {
@ -37,3 +206,18 @@ KeyMap::KeyMapNode KeyMap::getKeyMapNode(int key)
return retNode; return retNode;
} }
int KeyMap::getSwitchKey()
{
return m_switchKey;
}
KeyMap::MouseMoveMap KeyMap::getMouseMoveMap()
{
return m_mouseMoveMap;
}
bool KeyMap::enableMouseMoveMap()
{
return !m_mouseMoveMap.startPos.isNull();
}

View file

@ -2,6 +2,7 @@
#define KEYMAP_H #define KEYMAP_H
#include <QPointF> #include <QPointF>
#include <QVector> #include <QVector>
#include <QRectF>
class KeyMap class KeyMap
@ -11,6 +12,7 @@ public:
KMT_INVALID = -1, KMT_INVALID = -1,
KMT_CLICK = 0, KMT_CLICK = 0,
KMT_CLICK_TWICE, KMT_CLICK_TWICE,
KMT_STEER_WHEEL,
}; };
struct KeyNode { struct KeyNode {
@ -23,24 +25,60 @@ public:
union { union {
struct { struct {
KeyNode keyNode; KeyNode keyNode;
bool quitMap = false; bool switchMap = false;
} click; } click;
struct { struct {
KeyNode keyNode; KeyNode keyNode;
bool quitMap = false;
} clickTwice; } clickTwice;
struct {
// 方向盘矩形中心位置
QPointF centerPos = {0.0f, 0.0f};
// 方向盘矩形四个方向偏移量
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;
// 辅助变量
// 方向键的按下状态
bool leftKeyPressed = false;
bool rightKeyPressed = false;
bool upKeyPressed = false;
bool downKeyPressed = false;
// 按下方向键的数量
int pressKeysNum = 0;
// 第一次按下的键
int firstPressKey = 0;
} steerWheel;
}; };
KeyMapNode() {} KeyMapNode() {}
~KeyMapNode() {} ~KeyMapNode() {}
}; };
struct MouseMoveMap {
QPointF startPos = {0.0f, 0.0f};
int speedRatio = 1;
};
KeyMap(); KeyMap();
void loadKeyMapNode(); void loadKeyMapNode();
KeyMap::KeyMapNode getKeyMapNode(int key); KeyMap::KeyMapNode getKeyMapNode(int key);
int getSwitchKey();
MouseMoveMap getMouseMoveMap();
bool enableMouseMoveMap();
private: private:
QVector<KeyMapNode> m_keyMapNodes; QVector<KeyMapNode> m_keyMapNodes;
int m_switchKey = Qt::Key_QuoteLeft;
MouseMoveMap m_mouseMoveMap;
}; };