mouse button can be used as the switch-key

This commit is contained in:
Tian Zhou 2019-11-18 02:25:15 -05:00
commit a1e04aa376
5 changed files with 59 additions and 38 deletions

View file

@ -12,13 +12,7 @@ Controller::Controller(QString gameScript, QObject* parent) : QObject(parent)
m_receiver = new Receiver(this); m_receiver = new Receiver(this);
Q_ASSERT(m_receiver); Q_ASSERT(m_receiver);
if (!gameScript.isEmpty()) { updateScript(gameScript);
InputConvertGame* convertgame = new InputConvertGame(this);
convertgame->loadKeyMap(gameScript);
m_inputConvert = convertgame;
} else {
m_inputConvert = new InputConvertNormal(this);
}
Q_ASSERT(m_inputConvert); Q_ASSERT(m_inputConvert);
connect(m_inputConvert, &InputConvertBase::grabCursor, this, &Controller::grabCursor); connect(m_inputConvert, &InputConvertBase::grabCursor, this, &Controller::grabCursor);
} }

View file

@ -19,17 +19,25 @@ InputConvertGame::~InputConvertGame()
void InputConvertGame::mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize) 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) { if (m_gameMap) {
updateSize(frameSize, showSize); updateSize(frameSize, showSize);
if (m_keyMap.enableMouseMoveMap()) {
// mouse move // mouse move
if (m_keyMap.enableMouseMoveMap()) {
if (processMouseMove(from)) { if (processMouseMove(from)) {
return; return;
} }
} }
// mouse click // mouse click
if (processMouseClick(from)) { if (processMouseClick(from)) {
return; 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) 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 (QEvent::KeyPress == from->type()) {
if (!switchGameMap()) { if (!switchGameMap()) {
m_needSwitchGameAgain = false; m_needSwitchGameAgain = false;
@ -66,9 +74,8 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
&& KeyMap::KMT_CLICK == node.type && KeyMap::KMT_CLICK == node.type
&& node.click.switchMap) { && node.click.switchMap) {
updateSize(frameSize, showSize); updateSize(frameSize, showSize);
// Qt::Key_Tab Qt::Key_M // Qt::Key_Tab Qt::Key_M for PUBG mobile
processKeyClick(node.click.keyNode.pos, false, processKeyClick(node.click.keyNode.pos, false, node.click.switchMap, from);
node.click.switchMap, from);
return; return;
} }
@ -78,17 +85,14 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
return; return;
} }
switch (node.type) {
// 处理方向盘 // 处理方向盘
if (KeyMap::KMT_STEER_WHEEL == node.type) { case KeyMap::KMT_STEER_WHEEL:
processSteerWheel(node, from); processSteerWheel(node, from);
return; return;
}
// 处理普通按键 // 处理普通按键
switch (node.type) {
case KeyMap::KMT_CLICK: case KeyMap::KMT_CLICK:
processKeyClick(node.click.keyNode.pos, false, processKeyClick(node.click.keyNode.pos, false, node.click.switchMap, from);
node.click.switchMap, from);
return; return;
case KeyMap::KMT_CLICK_TWICE: case KeyMap::KMT_CLICK_TWICE:
processKeyClick(node.clickTwice.keyNode.pos, true, false, from); processKeyClick(node.clickTwice.keyNode.pos, true, false, from);
@ -352,8 +356,9 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
return false; return false;
} }
//qWarning() << from->localPos() << " - " << from->globalPos();
if (checkCursorPos(from)) { if (checkCursorPos(from)) {
m_mouseMoveLastPos = QPointF(0.0f, 0.0f); m_mouseMoveLastPos = QPointF(0.0, 0.0);
return true; return true;
} }
@ -362,7 +367,7 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
distance /= m_keyMap.getMouseMoveMap().speedRatio; distance /= m_keyMap.getMouseMoveMap().speedRatio;
mouseMoveStartTouch(from); mouseMoveStartTouch(from);
startMouseMoveTimer(); //startMouseMoveTimer();
m_mouseMoveLastConverPos.setX(m_mouseMoveLastConverPos.x() + distance.x() / m_showSize.width()); m_mouseMoveLastConverPos.setX(m_mouseMoveLastConverPos.x() + distance.x() / m_showSize.width());
m_mouseMoveLastConverPos.setY(m_mouseMoveLastConverPos.y() + distance.y() / m_showSize.height()); m_mouseMoveLastConverPos.setY(m_mouseMoveLastConverPos.y() + distance.y() / m_showSize.height());
@ -374,7 +379,7 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
mouseMoveStopTouch(); mouseMoveStopTouch();
mouseMoveStartTouch(from); mouseMoveStartTouch(from);
} }
//qWarning() << "move: "<<m_mouseMoveLastConverPos;
sendTouchMoveEvent(getTouchID(Qt::ExtraButton24), m_mouseMoveLastConverPos); sendTouchMoveEvent(getTouchID(Qt::ExtraButton24), m_mouseMoveLastConverPos);
} }
m_mouseMoveLastPos = from->localPos(); m_mouseMoveLastPos = from->localPos();
@ -384,7 +389,8 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
void InputConvertGame::moveCursorToStart(const QMouseEvent *from) void InputConvertGame::moveCursorToStart(const QMouseEvent *from)
{ {
QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos; 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 posOffset = from->localPos().toPoint() - localPos;
QPoint globalPos = from->globalPos(); QPoint globalPos = from->globalPos();
@ -398,6 +404,7 @@ void InputConvertGame::moveCursorTo(const QMouseEvent *from, const QPoint &pos)
QPoint globalPos = from->globalPos(); QPoint globalPos = from->globalPos();
globalPos -= posOffset; globalPos -= posOffset;
qWarning() <<"move to: "<<globalPos;
QCursor::setPos(globalPos); QCursor::setPos(globalPos);
} }
@ -417,10 +424,10 @@ void InputConvertGame::stopMouseMoveTimer()
void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from) void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from)
{ {
Q_UNUSED(from); Q_UNUSED(from)
if (!m_mouseMovePress) { if (!m_mouseMovePress) {
QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos; QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos;
//moveCursorToStart(from); moveCursorToStart(from);
int id = attachTouchID(Qt::ExtraButton24); int id = attachTouchID(Qt::ExtraButton24);
sendTouchDownEvent(id, mouseMoveStartPos); sendTouchDownEvent(id, mouseMoveStartPos);
m_mouseMoveLastConverPos = mouseMoveStartPos; m_mouseMoveLastConverPos = mouseMoveStartPos;
@ -452,20 +459,18 @@ bool InputConvertGame::switchGameMap()
bool InputConvertGame::checkCursorPos(const QMouseEvent *from) bool InputConvertGame::checkCursorPos(const QMouseEvent *from)
{ {
bool moveCursor = false; bool moveCursor = true;
QPoint pos = from->pos(); QPoint pos = from->pos();
if (pos.x() < CURSOR_POS_CHECK) { if (pos.x() < CURSOR_POS_CHECK) {
pos.setX(m_showSize.width() - CURSOR_POS_CHECK); pos.setX(m_showSize.width() - CURSOR_POS_CHECK);
moveCursor = true;
} else if (pos.x() > m_showSize.width() - CURSOR_POS_CHECK) { } else if (pos.x() > m_showSize.width() - CURSOR_POS_CHECK) {
pos.setX(CURSOR_POS_CHECK); pos.setX(CURSOR_POS_CHECK);
moveCursor = true;
} else if (pos.y() < CURSOR_POS_CHECK) { } else if (pos.y() < CURSOR_POS_CHECK) {
pos.setY(m_showSize.height() - CURSOR_POS_CHECK); pos.setY(m_showSize.height() - CURSOR_POS_CHECK);
moveCursor = true;
} else if (pos.y() > m_showSize.height() - CURSOR_POS_CHECK) { } else if (pos.y() > m_showSize.height() - CURSOR_POS_CHECK) {
pos.setY(CURSOR_POS_CHECK); pos.setY(CURSOR_POS_CHECK);
moveCursor = true; }else{
moveCursor = false;
} }
if (moveCursor) { if (moveCursor) {

View file

@ -74,7 +74,7 @@ private:
// mouse move // mouse move
QPointF m_mouseMoveLastConverPos; QPointF m_mouseMoveLastConverPos;
QPointF m_mouseMoveLastPos = {0.0f, 0.0f}; QPointF m_mouseMoveLastPos = {0.0, 0.0};
bool m_mouseMovePress = false; bool m_mouseMovePress = false;
int m_mouseMoveTimer = 0; int m_mouseMoveTimer = 0;

View file

@ -56,12 +56,20 @@ void KeyMap::loadKeyMap(const QString &json)
// switchKey // switchKey
rootObj = jsonDoc.object(); rootObj = jsonDoc.object();
if (rootObj.contains("switchKey") && rootObj.value("switchKey").isString()) { if (rootObj.contains("switchKey") && rootObj.value("switchKey").isString()) {
Qt::Key key = (Qt::Key)metaEnumKey.keyToValue(rootObj.value("switchKey").toString().toStdString().c_str()); QString name = rootObj.value("switchKey").toString();
if (-1 == key) { 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"); errorString = QString("json error: switchKey invalid");
goto parseError; goto parseError;
} }
if(key != -1){
m_switchType = AT_KEY;
m_switchKey = key; m_switchKey = key;
}else{
m_switchType = AT_MOUSE;
m_switchKey = btn;
}
} else { } else {
errorString = QString("json error: no find switchKey"); errorString = QString("json error: no find switchKey");
goto parseError; goto parseError;
@ -253,6 +261,7 @@ void KeyMap::loadKeyMap(const QString &json)
} }
} }
} }
qWarning() << "Script updated.";
parseError: parseError:
if (!errorString.isEmpty()) { if (!errorString.isEmpty()) {
@ -292,6 +301,11 @@ KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key)
return m_invalidNode; return m_invalidNode;
} }
bool KeyMap::isSwitchOnKeyboard()
{
return m_switchType == AT_KEY;
}
int KeyMap::getSwitchKey() int KeyMap::getSwitchKey()
{ {
return m_switchKey; return m_switchKey;

View file

@ -18,6 +18,12 @@ public:
}; };
Q_ENUM(KeyMapType) Q_ENUM(KeyMapType)
enum ActionType {
AT_KEY = 0,
AT_MOUSE = 1,
};
Q_ENUM(ActionType)
struct KeyNode { struct KeyNode {
int key = Qt::Key_unknown; int key = Qt::Key_unknown;
QPointF pos = QPointF(0, 0); QPointF pos = QPointF(0, 0);
@ -66,7 +72,7 @@ public:
}; };
struct MouseMoveMap { struct MouseMoveMap {
QPointF startPos = {0.0f, 0.0f}; QPointF startPos = {0.0, 0.0};
int speedRatio = 1; int speedRatio = 1;
}; };
@ -75,6 +81,7 @@ public:
void loadKeyMap(const QString &json); void loadKeyMap(const QString &json);
KeyMap::KeyMapNode& getKeyMapNode(int key); KeyMap::KeyMapNode& getKeyMapNode(int key);
bool isSwitchOnKeyboard();
int getSwitchKey(); int getSwitchKey();
MouseMoveMap getMouseMoveMap(); MouseMoveMap getMouseMoveMap();
bool enableMouseMoveMap(); bool enableMouseMoveMap();
@ -84,6 +91,7 @@ public:
private: private:
QVector<KeyMapNode> m_keyMapNodes; QVector<KeyMapNode> m_keyMapNodes;
KeyMapNode m_invalidNode; KeyMapNode m_invalidNode;
ActionType m_switchType = AT_KEY;
int m_switchKey = Qt::Key_QuoteLeft; int m_switchKey = Qt::Key_QuoteLeft;
MouseMoveMap m_mouseMoveMap; MouseMoveMap m_mouseMoveMap;
static QString s_keyMapPath; static QString s_keyMapPath;