optimize the processing of stear wheel and mouse move

This commit is contained in:
Tian Zhou 2019-12-09 03:10:12 -05:00
commit 0cb58260a1
4 changed files with 171 additions and 226 deletions

View file

@ -62,7 +62,7 @@ void InputConvertGame::mouseEvent(const QMouseEvent *from, const QSize &frameSiz
if (m_gameMap) { if (m_gameMap) {
updateSize(frameSize, showSize); updateSize(frameSize, showSize);
// mouse move // mouse move
if (m_keyMap.enableMouseMoveMap()) { if (m_keyMap.isValidMouseMoveMap()) {
if (processMouseMove(from)) { if (processMouseMove(from)) {
return; return;
} }
@ -97,7 +97,7 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
return; return;
} }
KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNodeKey(from->key()); const KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNodeKey(from->key());
// 处理特殊按键:可以在按键映射和普通映射间切换的按键 // 处理特殊按键:可以在按键映射和普通映射间切换的按键
if (m_needSwitchGameAgain if (m_needSwitchGameAgain
&& KeyMap::KMT_CLICK == node.type && KeyMap::KMT_CLICK == node.type
@ -140,8 +140,15 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
void InputConvertGame::loadKeyMap(const QString &json) void InputConvertGame::loadKeyMap(const QString &json)
{ {
m_keyMap.loadKeyMap(json); m_keyMap.loadKeyMap(json);
if (m_keyMap.enableMouseMoveMap()) { if (m_keyMap.isValidMouseMoveMap()) {
m_mouseMoveLastConverPos = m_keyMap.getMouseMoveMap().startPos; m_ctrlMouseMove.valid = true;
m_ctrlMouseMove.touching = false;
m_ctrlMouseMove.startPosRel = m_keyMap.getMouseMoveMap().startPos;
m_ctrlMouseMove.startPosPixel = calcFrameAbsolutePos(m_ctrlMouseMove.startPosRel);
}
if(m_keyMap.isValidSteerWheelMap()){
m_ctrlSteerWheel.valid = true;
m_ctrlMouseMove.touching = false;
} }
} }
@ -149,6 +156,9 @@ void InputConvertGame::updateSize(const QSize &frameSize, const QSize &showSize)
{ {
m_frameSize = frameSize; m_frameSize = frameSize;
m_showSize = showSize; m_showSize = showSize;
if(m_ctrlMouseMove.valid){
m_ctrlMouseMove.startPosPixel = calcScreenAbsolutePos(m_ctrlMouseMove.startPosRel);
}
} }
void InputConvertGame::sendTouchDownEvent(int id, QPointF pos) void InputConvertGame::sendTouchDownEvent(int id, QPointF pos)
@ -187,6 +197,14 @@ QPointF InputConvertGame::calcFrameAbsolutePos(QPointF relativePos)
return absolutePos; return absolutePos;
} }
QPointF InputConvertGame::calcScreenAbsolutePos(QPointF relativePos)
{
QPointF absolutePos;
absolutePos.setX(m_showSize.width() * relativePos.x());
absolutePos.setY(m_showSize.height() * relativePos.y());
return absolutePos;
}
int InputConvertGame::attachTouchID(int key) int InputConvertGame::attachTouchID(int key)
{ {
//QMetaEnum map = QMetaEnum::fromType<Qt::Key>(); //QMetaEnum map = QMetaEnum::fromType<Qt::Key>();
@ -225,129 +243,63 @@ int InputConvertGame::getTouchID(int key)
// -------- steer wheel event -------- // -------- steer wheel event --------
void InputConvertGame::processSteerWheel(KeyMap::KeyMapNode &node, const QKeyEvent *from) void InputConvertGame::processSteerWheel(const KeyMap::KeyMapNode &node, const QKeyEvent *from)
{ {
int keyPress1 = Qt::Key_unknown; int key = from->key();
int keyPress2 = Qt::Key_unknown; bool flag = from->type() == QEvent::KeyPress;
int keysNum = updateSteerWheelKeysPress(node, from, keyPress1, keyPress2); // identify keys
bool needMove = false; if(key == node.steerWheel.up.key){
if (QEvent::KeyPress == from->type()) { m_ctrlSteerWheel.pressedUp = flag;
if (1 == keysNum) { }else if(key == node.steerWheel.right.key){
node.steerWheel.firstPressKey = from->key(); m_ctrlSteerWheel.pressedRight = flag;
int id = attachTouchID(node.steerWheel.firstPressKey); }else if(key == node.steerWheel.down.key){
if (-1 == id) { m_ctrlSteerWheel.pressedDown = flag;
return; }else{ // left
} m_ctrlSteerWheel.pressedLeft = flag;
}
QPointF offset(0.0, 0.0);
int nPressed = 0;
if(m_ctrlSteerWheel.pressedUp){
++nPressed;
offset.ry() -= node.steerWheel.up.offset;
}
if(m_ctrlSteerWheel.pressedRight){
++nPressed;
offset.rx() += node.steerWheel.right.offset;
}
if(m_ctrlSteerWheel.pressedDown){
++nPressed;
offset.ry() += node.steerWheel.down.offset;
}
if(m_ctrlSteerWheel.pressedLeft){
++nPressed;
offset.rx() -= node.steerWheel.left.offset;
}
// action
//qDebug()<<nPressed<<"-"<<char(from->key())<<"-"<<from->type()<<"-"<<offset;
if(nPressed == 0){ // release all
int id = getTouchID(m_ctrlSteerWheel.touchKey);
sendTouchUpEvent(id, node.steerWheel.centerPos + m_ctrlSteerWheel.lastOffset);
detachTouchID(m_ctrlSteerWheel.touchKey);
}else{
int id;
if(nPressed == 1 && flag){ // first press
m_ctrlSteerWheel.touchKey = from->key();
id = attachTouchID(m_ctrlSteerWheel.touchKey);
sendTouchDownEvent(id, node.steerWheel.centerPos); sendTouchDownEvent(id, node.steerWheel.centerPos);
needMove = true; }else{
} else if (2 == keysNum) { id = getTouchID(m_ctrlSteerWheel.touchKey);
needMove = true;
}
} else if (QEvent::KeyRelease == from->type()){
if (0 == keysNum) {
int id = getTouchID(node.steerWheel.firstPressKey);
sendTouchUpEvent(id, node.steerWheel.centerPos);
detachTouchID(node.steerWheel.firstPressKey);
node.steerWheel.firstPressKey = 0;
} else if (1 == keysNum) {
needMove = true;
} }
sendTouchMoveEvent(id, node.steerWheel.centerPos + offset);
} }
if (needMove) { m_ctrlSteerWheel.lastOffset = offset;
steerWheelMove(node, keysNum, keyPress1, keyPress2); return;
}
}
int InputConvertGame::updateSteerWheelKeysPress(KeyMap::KeyMapNode &node, const QKeyEvent *from, int& keyPress1, int& keyPress2)
{
bool keyPress = false;
if (QEvent::KeyPress == from->type()) {
keyPress = true;
} else if (QEvent::KeyRelease == from->type()) {
keyPress = false;
}
if (from->key() == node.steerWheel.up.key) {
node.steerWheel.upKeyPressed = keyPress;
} else if (from->key() == node.steerWheel.right.key) {
node.steerWheel.rightKeyPressed = keyPress;
} else if (from->key() == node.steerWheel.down.key) {
node.steerWheel.downKeyPressed = keyPress;
} else if (from->key() == node.steerWheel.left.key) {
node.steerWheel.leftKeyPressed = keyPress;
}
int count = 0;
keyPress1 = Qt::Key_unknown;
keyPress2 = Qt::Key_unknown;
// 上右下左的顺序统计按键数量,并记录前两个按键码
if (node.steerWheel.upKeyPressed) {
count++;
if (Qt::Key_unknown == keyPress1) {
keyPress1 = node.steerWheel.up.key;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.up.key;
}
}
if (node.steerWheel.rightKeyPressed) {
count++;
if (Qt::Key_unknown == keyPress1) {
keyPress1 = node.steerWheel.right.key;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.right.key;
}
}
if (node.steerWheel.downKeyPressed) {
count++;
if (Qt::Key_unknown == keyPress1) {
keyPress1 = node.steerWheel.down.key;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.down.key;
}
}
if (node.steerWheel.leftKeyPressed) {
count++;
if (Qt::Key_unknown == keyPress1) {
keyPress1 = node.steerWheel.left.key;
} else if (Qt::Key_unknown == keyPress2) {
keyPress2 = node.steerWheel.left.key;
}
}
return count;
}
void InputConvertGame::steerWheelMove(KeyMap::KeyMapNode &node, int keysNum, int keyPress1, int keyPress2)
{
if (1 != keysNum && 2 != keysNum) {
return;
}
QPointF movePos = node.steerWheel.centerPos;
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);
}
if(keysNum > 1) {
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);
}
}
sendTouchMoveEvent(getTouchID(node.steerWheel.firstPressKey), movePos);
} }
// -------- key event -------- // -------- key event --------
void InputConvertGame::processKeyClick(QPointF clickPos, bool clickTwice, bool switchMap, const QKeyEvent *from) void InputConvertGame::processKeyClick(
const QPointF& clickPos, bool clickTwice, bool switchMap, const QKeyEvent *from)
{ {
if (switchMap && QEvent::KeyRelease == from->type()) { if (switchMap && QEvent::KeyRelease == from->type()) {
m_needSwitchGameAgain = !m_needSwitchGameAgain; m_needSwitchGameAgain = !m_needSwitchGameAgain;
@ -371,7 +323,7 @@ void InputConvertGame::processKeyClick(QPointF clickPos, bool clickTwice, bool s
} }
} }
void InputConvertGame::processKeyDrag(QPointF startPos, QPointF endPos, const QKeyEvent* from) void InputConvertGame::processKeyDrag(const QPointF& startPos, QPointF endPos, const QKeyEvent* from)
{ {
if(QEvent::KeyPress == from->type()){ if(QEvent::KeyPress == from->type()){
int id = attachTouchID(from->key()); int id = attachTouchID(from->key());
@ -386,7 +338,7 @@ void InputConvertGame::processKeyDrag(QPointF startPos, QPointF endPos, const QK
bool InputConvertGame::processMouseClick(const QMouseEvent *from) bool InputConvertGame::processMouseClick(const QMouseEvent *from)
{ {
KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNodeMouse(from->button()); const KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNodeMouse(from->button());
if (KeyMap::KMT_INVALID == node.type) { if (KeyMap::KMT_INVALID == node.type) {
return false; return false;
} }
@ -408,18 +360,13 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
if (QEvent::MouseMove != from->type()) { if (QEvent::MouseMove != from->type()) {
return false; return false;
} }
QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos; if(m_ctrlMouseMove.touching){
//qreal xbound = qMin(mmsp.x()-0.05, 0.95-mmsp.x());//m_keyMap.getMouseMoveMap().speedRatio;
//qreal ybound = qMin(mmsp.y()-0.05, 0.95-mmsp.y());//m_keyMap.getMouseMoveMap().speedRatio;
if(m_mouseMoving){
QPointF mousePos = from->localPos(); QPointF mousePos = from->localPos();
mousePos.rx() /= m_showSize.width(); mousePos.rx() /= m_showSize.width();
mousePos.ry() /= m_showSize.height(); mousePos.ry() /= m_showSize.height();
QPointF offset = mousePos - mouseMoveStartPos; QPointF offset = mousePos - m_ctrlMouseMove.startPosRel;
//qDebug()<<from->localPos()<<" - "<<m_mouseMoveLastConverPos<<" - "<<offset<<" - "<<offset.manhattanLength(); //qDebug()<<from->localPos()<<" - "<<m_mouseMoveLastConverPos<<" - "<<offset<<" - "<<offset.manhattanLength();
//if (qAbs(offset.x()) > xbound || qAbs(offset.y()) > ybound)
if(mousePos.x()<0.05 || mousePos.x()>0.95 || mousePos.y()<0.05 || mousePos.y()>0.95) if(mousePos.x()<0.05 || mousePos.x()>0.95 || mousePos.y()<0.05 || mousePos.y()>0.95)
{ {
//qDebug()<<"reset"; //qDebug()<<"reset";
@ -427,9 +374,10 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
mouseMoveStartTouch(from); mouseMoveStartTouch(from);
} }
offset /= m_keyMap.getMouseMoveMap().speedRatio; offset /= m_keyMap.getMouseMoveMap().speedRatio;
m_mouseMoveLastConverPos = mouseMoveStartPos + offset; m_ctrlMouseMove.lastPosRel = m_ctrlMouseMove.startPosRel + offset;
mouseMoveMovingTouch(m_mouseMoveLastConverPos); mouseMoveMovingTouch(m_ctrlMouseMove.lastPosRel);
}else{ }else{
m_ctrlMouseMove.touching = true;
mouseMoveStartTouch(from); mouseMoveStartTouch(from);
int left = from->globalX() - from->x(); int left = from->globalX() - from->x();
int top = from->globalY() - from->y(); int top = from->globalY() - from->y();
@ -438,24 +386,37 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
return true; return true;
} }
void InputConvertGame::moveCursorToStart(const QMouseEvent *from) void InputConvertGame::moveCursorTo(const QMouseEvent *from, const QPoint &localPosPixel)
{ {
QPointF mouseMoveStartPosF = m_keyMap.getMouseMoveMap().startPos; QPoint posOffset = from->pos() - localPosPixel;
QPointF mmspLocal = QPointF(
m_showSize.width()*mouseMoveStartPosF.x(),
m_showSize.height()*mouseMoveStartPosF.y());
moveCursorTo(from, mmspLocal.toPoint());
}
void InputConvertGame::moveCursorTo(const QMouseEvent *from, const QPoint &localPos)
{
QPoint posOffset = from->pos() - localPos;
QPoint globalPos = from->globalPos(); QPoint globalPos = from->globalPos();
globalPos -= posOffset; globalPos -= posOffset;
//qDebug()<<"move cursor to "<<globalPos<<" offset "<<posOffset; //qDebug()<<"move cursor to "<<globalPos<<" offset "<<posOffset;
QCursor::setPos(globalPos); QCursor::setPos(globalPos);
} }
void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from)
{
moveCursorTo(from, m_ctrlMouseMove.startPosPixel.toPoint());
int id = attachTouchID(m_ctrlMouseMove.touchKey);
sendTouchDownEvent(id, m_ctrlMouseMove.startPosRel);
m_ctrlMouseMove.lastPosRel = m_ctrlMouseMove.startPosRel;
m_ctrlMouseMove.touching = true;
}
void InputConvertGame::mouseMoveMovingTouch(const QPointF& target)
{
sendTouchMoveEvent(getTouchID(m_ctrlMouseMove.touchKey), target);
}
void InputConvertGame::mouseMoveStopTouch()
{
int id = getTouchID(m_ctrlMouseMove.touchKey);
sendTouchUpEvent(id, m_ctrlMouseMove.lastPosRel);
detachTouchID(m_ctrlMouseMove.touchKey);
m_ctrlMouseMove.touching = false;
}
void InputConvertGame::startMouseMoveTimer() void InputConvertGame::startMouseMoveTimer()
{ {
stopMouseMoveTimer(); stopMouseMoveTimer();
@ -470,38 +431,9 @@ void InputConvertGame::stopMouseMoveTimer()
} }
} }
void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from)
{
if (!m_mouseMoving) {
QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos;
moveCursorToStart(from);
int id = attachTouchID(Qt::ExtraButton24);
sendTouchDownEvent(id, mouseMoveStartPos);
m_mouseMoveLastConverPos = mouseMoveStartPos;
m_mouseMoving = true;
}
}
void InputConvertGame::mouseMoveMovingTouch(const QPointF& target)
{
if (m_mouseMoving) {
sendTouchMoveEvent(getTouchID(Qt::ExtraButton24), target);
}
}
void InputConvertGame::mouseMoveStopTouch()
{
if (m_mouseMoving) {
int id = getTouchID(Qt::ExtraButton24);
sendTouchUpEvent(id, m_mouseMoveLastConverPos);
detachTouchID(Qt::ExtraButton24);
m_mouseMoving = false;
}
}
bool InputConvertGame::switchGameMap() bool InputConvertGame::switchGameMap()
{ {
m_gameMap = !m_gameMap; m_gameMap = !m_gameMap;
emit grabCursor(m_gameMap); emit grabCursor(m_gameMap);
if (m_gameMap) { if (m_gameMap) {
#ifdef QT_NO_DEBUG #ifdef QT_NO_DEBUG
@ -511,7 +443,8 @@ bool InputConvertGame::switchGameMap()
#endif #endif
//restrictMouse(); // called at the first run of processMouseMove() //restrictMouse(); // called at the first run of processMouseMove()
} else { } else {
mouseMoveStopTouch(); if(m_ctrlMouseMove.touching)
mouseMoveStopTouch();
QGuiApplication::restoreOverrideCursor(); QGuiApplication::restoreOverrideCursor();
freeMouse(); freeMouse();
} }

View file

@ -27,6 +27,7 @@ protected:
void sendTouchUpEvent(int id, QPointF pos); void sendTouchUpEvent(int id, QPointF pos);
void sendTouchEvent(int id, QPointF pos, AndroidMotioneventAction action); void sendTouchEvent(int id, QPointF pos, AndroidMotioneventAction action);
QPointF calcFrameAbsolutePos(QPointF relativePos); QPointF calcFrameAbsolutePos(QPointF relativePos);
QPointF calcScreenAbsolutePos(QPointF relativePos);
// multi touch id // multi touch id
int attachTouchID(int key); int attachTouchID(int key);
@ -34,50 +35,60 @@ protected:
int getTouchID(int key); int getTouchID(int key);
// steer wheel // steer wheel
void processSteerWheel(KeyMap::KeyMapNode &node, const QKeyEvent* from); void processSteerWheel(const KeyMap::KeyMapNode &node, const QKeyEvent* from);
int updateSteerWheelKeysPress(KeyMap::KeyMapNode &node, const QKeyEvent* from, int& keyPress1, int& keyPress2);
void steerWheelMove(KeyMap::KeyMapNode &node, int keysNum, int keyPress1, int keyPress2);
// click // click
void processKeyClick(QPointF clickPos, bool clickTwice, bool switchMap, const QKeyEvent* from); void processKeyClick(const QPointF& clickPos, bool clickTwice, bool switchMap, const QKeyEvent* from);
// drag // drag
void processKeyDrag(QPointF startPos, QPointF endPos, const QKeyEvent* from); void processKeyDrag(const QPointF& startPos, QPointF endPos, const QKeyEvent* from);
// mouse // mouse
bool processMouseClick(const QMouseEvent* from); bool processMouseClick(const QMouseEvent* from);
bool processMouseMove(const QMouseEvent* from); bool processMouseMove(const QMouseEvent* from);
void moveCursorToStart(const QMouseEvent* from); void moveCursorTo(const QMouseEvent* from, const QPoint& localPosPixel);
void moveCursorTo(const QMouseEvent* from, const QPoint& localPos);
void startMouseMoveTimer();
void stopMouseMoveTimer();
void mouseMoveStartTouch(const QMouseEvent* from); void mouseMoveStartTouch(const QMouseEvent* from);
void mouseMoveMovingTouch(const QPointF& target); void mouseMoveMovingTouch(const QPointF& target);
void mouseMoveStopTouch(); void mouseMoveStopTouch();
void startMouseMoveTimer();
void stopMouseMoveTimer();
bool switchGameMap(); bool switchGameMap();
protected: protected:
void timerEvent(QTimerEvent *event); void timerEvent(QTimerEvent *event);
private:
enum SteerWheelDirection {
SWD_UP = 0,
SWD_RIGHT,
SWD_DOWN,
SWD_LEFT,
};
private: private:
QSize m_frameSize; QSize m_frameSize;
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 };
// steer wheel
struct{
bool valid = false;
bool touching = false;
int touchKey = Qt::Key_unknown; // the first key pressed
int nKeyPressed = 0;
bool pressedUp = false, pressedDown = false;
bool pressedLeft = false, pressedRight = false;
QPointF centerPos;
QPointF lastOffset;
} m_ctrlSteerWheel;
// mouse move // mouse move
QPointF m_mouseMoveLastConverPos; struct{
bool m_mouseMoving = false; bool valid = false;
bool touching = false;
const int touchKey = Qt::ExtraButton24;
QPointF startPosRel; // in [0, 1)
QPointF startPosPixel; // in [0, size)
QPointF lastPosRel;
//QPointF lastPosPixel;
} m_ctrlMouseMove;
int m_mouseMoveTimer = 0; int m_mouseMoveTimer = 0;
bool m_needSwitchGameAgain = false; bool m_needSwitchGameAgain = false;

View file

@ -175,19 +175,18 @@ void KeyMap::loadKeyMap(const QString &json)
KeyMapNode keyMapNode; KeyMapNode keyMapNode;
keyMapNode.type = type; keyMapNode.type = type;
keyMapNode.steerWheel.leftKeyPressed = false;
keyMapNode.steerWheel.rightKeyPressed = false;
keyMapNode.steerWheel.upKeyPressed = false;
keyMapNode.steerWheel.downKeyPressed = false;
keyMapNode.steerWheel.pressKeysNum = 0;
keyMapNode.steerWheel.firstPressKey = 0;
keyMapNode.steerWheel.left = { leftKey.first, leftKey.second, getItemNumber(node, "leftOffset") }; keyMapNode.steerWheel.left = { leftKey.first, leftKey.second,
keyMapNode.steerWheel.right = { rightKey.first, rightKey.second, getItemNumber(node, "rightOffset") }; getItemNumber(node, "leftOffset") };
keyMapNode.steerWheel.up = { upKey.first, upKey.second, getItemNumber(node, "upOffset") }; keyMapNode.steerWheel.right = { rightKey.first, rightKey.second,
keyMapNode.steerWheel.down = { downKey.first, downKey.second, getItemNumber(node, "downOffset") }; 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"); keyMapNode.steerWheel.centerPos = getItemPos(node, "centerPos");
m_idxSteerWheel = m_keyMapNodes.size();
m_keyMapNodes.push_back(keyMapNode); m_keyMapNodes.push_back(keyMapNode);
} }
break; break;
@ -230,7 +229,7 @@ parseError:
return; return;
} }
KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key) const KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key)
{ {
auto p = rmapKey.value(key, &m_invalidNode); auto p = rmapKey.value(key, &m_invalidNode);
if(p == &m_invalidNode) if(p == &m_invalidNode)
@ -238,12 +237,12 @@ KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key)
return *p; return *p;
} }
KeyMap::KeyMapNode& KeyMap::getKeyMapNodeKey(int key) const KeyMap::KeyMapNode& KeyMap::getKeyMapNodeKey(int key)
{ {
return *rmapKey.value(key, &m_invalidNode); return *rmapKey.value(key, &m_invalidNode);
} }
KeyMap::KeyMapNode& KeyMap::getKeyMapNodeMouse(int key) const KeyMap::KeyMapNode& KeyMap::getKeyMapNodeMouse(int key)
{ {
return *rmapMouse.value(key, &m_invalidNode); return *rmapMouse.value(key, &m_invalidNode);
} }
@ -258,16 +257,26 @@ int KeyMap::getSwitchKey()
return m_switchKey; return m_switchKey;
} }
KeyMap::MouseMoveMap KeyMap::getMouseMoveMap() const KeyMap::MouseMoveMap& KeyMap::getMouseMoveMap()
{ {
return m_mouseMoveMap; return m_mouseMoveMap;
} }
bool KeyMap::enableMouseMoveMap() const KeyMap::KeyMapNode& KeyMap::getSteerWheelMap()
{
return m_keyMapNodes[m_idxSteerWheel];
}
bool KeyMap::isValidMouseMoveMap()
{ {
return !m_mouseMoveMap.startPos.isNull(); return !m_mouseMoveMap.startPos.isNull();
} }
bool KeyMap::isValidSteerWheelMap()
{
return m_idxSteerWheel != -1;
}
void KeyMap::makeReverseMap() void KeyMap::makeReverseMap()
{ {
rmapKey.clear(); rmapKey.clear();

View file

@ -47,26 +47,13 @@ public:
KeyNode keyNode; KeyNode keyNode;
} clickTwice; } clickTwice;
struct { struct {
// 方向盘矩形中心位置
QPointF centerPos = {0.0, 0.0}; QPointF centerPos = {0.0, 0.0};
struct DirInfo{ struct DirInfo{
ActionType type = AT_KEY; // keyboard/mouse ActionType type = AT_KEY; // keyboard/mouse
int key = Qt::Key_unknown; // key/button int key = Qt::Key_unknown; // key/button
double offset = 0.0; double offset = 0.0;
}; };
DirInfo left, right, up, down; DirInfo left, right, up, down;
// 辅助变量
// 方向键的按下状态
bool leftKeyPressed = false;
bool rightKeyPressed = false;
bool upKeyPressed = false;
bool downKeyPressed = false;
// 按下方向键的数量
int pressKeysNum = 0;
// 第一次按下的键
int firstPressKey = 0;
} steerWheel; } steerWheel;
struct { struct {
ActionType type = AT_KEY; ActionType type = AT_KEY;
@ -88,13 +75,16 @@ public:
virtual ~KeyMap(); virtual ~KeyMap();
void loadKeyMap(const QString &json); void loadKeyMap(const QString &json);
KeyMap::KeyMapNode& getKeyMapNode(int key); const KeyMap::KeyMapNode& getKeyMapNode(int key);
KeyMap::KeyMapNode& getKeyMapNodeKey(int key); const KeyMap::KeyMapNode& getKeyMapNodeKey(int key);
KeyMap::KeyMapNode& getKeyMapNodeMouse(int key); const KeyMap::KeyMapNode& getKeyMapNodeMouse(int key);
bool isSwitchOnKeyboard(); bool isSwitchOnKeyboard();
int getSwitchKey(); int getSwitchKey();
MouseMoveMap getMouseMoveMap();
bool enableMouseMoveMap(); bool isValidMouseMoveMap();
bool isValidSteerWheelMap();
const MouseMoveMap& getMouseMoveMap();
const KeyMapNode& getSteerWheelMap();
static const QString& getKeyMapPath(); static const QString& getKeyMapPath();
@ -128,6 +118,8 @@ private:
MouseMoveMap m_mouseMoveMap; MouseMoveMap m_mouseMoveMap;
static QString s_keyMapPath; static QString s_keyMapPath;
int m_idxSteerWheel = -1;
// mapping of key/mouse event name to index // mapping of key/mouse event name to index
QMetaEnum m_metaEnumKey = QMetaEnum::fromType<Qt::Key>(); QMetaEnum m_metaEnumKey = QMetaEnum::fromType<Qt::Key>();
QMetaEnum m_metaEnumMouseButtons = QMetaEnum::fromType<Qt::MouseButtons>(); QMetaEnum m_metaEnumMouseButtons = QMetaEnum::fromType<Qt::MouseButtons>();