mouse button can be used as the switch-key

This commit is contained in:
Tian Zhou 2019-11-18 02:25:15 -05:00
parent 9e3b4aec41
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);
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);
}

View file

@ -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: "<<m_mouseMoveLastConverPos;
sendTouchMoveEvent(getTouchID(Qt::ExtraButton24), m_mouseMoveLastConverPos);
}
m_mouseMoveLastPos = from->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: "<<globalPos;
QCursor::setPos(globalPos);
}
@ -417,10 +424,10 @@ void InputConvertGame::stopMouseMoveTimer()
void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from)
{
Q_UNUSED(from);
Q_UNUSED(from)
if (!m_mouseMovePress) {
QPointF mouseMoveStartPos = m_keyMap.getMouseMoveMap().startPos;
//moveCursorToStart(from);
moveCursorToStart(from);
int id = attachTouchID(Qt::ExtraButton24);
sendTouchDownEvent(id, mouseMoveStartPos);
m_mouseMoveLastConverPos = mouseMoveStartPos;
@ -452,20 +459,18 @@ bool InputConvertGame::switchGameMap()
bool InputConvertGame::checkCursorPos(const QMouseEvent *from)
{
bool moveCursor = false;
bool moveCursor = true;
QPoint pos = from->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) {

View file

@ -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;

View file

@ -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;

View file

@ -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<KeyMapNode> m_keyMapNodes;
KeyMapNode m_invalidNode;
ActionType m_switchType = AT_KEY;
int m_switchKey = Qt::Key_QuoteLeft;
MouseMoveMap m_mouseMoveMap;
static QString s_keyMapPath;