feat: game keymap finish

This commit is contained in:
rankun 2019-08-31 08:49:34 +08:00
commit 62a9b70a37
5 changed files with 27 additions and 33 deletions

View file

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

View file

@ -60,7 +60,7 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
return; return;
} }
KeyMap::KeyMapNode node = m_keyMap.getKeyMapNode(from->key()); KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNode(from->key());
// 处理特殊按键:可以在按键映射和普通映射间切换的按键 // 处理特殊按键:可以在按键映射和普通映射间切换的按键
if (m_needSwitchGameAgain if (m_needSwitchGameAgain
&& KeyMap::KMT_CLICK == node.type && KeyMap::KMT_CLICK == node.type
@ -200,7 +200,8 @@ void InputConvertGame::processSteerWheel(KeyMap::KeyMapNode &node, const QKeyEve
} }
} else if (QEvent::KeyRelease == from->type()){ } else if (QEvent::KeyRelease == from->type()){
if (0 == keysNum) { if (0 == keysNum) {
sendTouchUpEvent(getTouchID(node.steerWheel.firstPressKey), node.steerWheel.centerPos); int id = getTouchID(node.steerWheel.firstPressKey);
sendTouchUpEvent(id, node.steerWheel.centerPos);
detachTouchID(node.steerWheel.firstPressKey); detachTouchID(node.steerWheel.firstPressKey);
node.steerWheel.firstPressKey = 0; node.steerWheel.firstPressKey = 0;
} else if (1 == keysNum) { } else if (1 == keysNum) {
@ -328,7 +329,7 @@ void InputConvertGame::processKeyClick(QPointF clickPos, bool clickTwice, bool s
bool InputConvertGame::processMouseClick(const QMouseEvent *from) bool InputConvertGame::processMouseClick(const QMouseEvent *from)
{ {
KeyMap::KeyMapNode node = m_keyMap.getKeyMapNode(from->button()); KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNode(from->button());
if (KeyMap::KMT_INVALID == node.type) { if (KeyMap::KMT_INVALID == node.type) {
return false; return false;
} }

View file

@ -14,7 +14,7 @@ QString KeyMap::s_keyMapPath = "";
KeyMap::KeyMap(QObject *parent) KeyMap::KeyMap(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
loadKeyMap("");
} }
KeyMap::~KeyMap() KeyMap::~KeyMap()
@ -37,7 +37,6 @@ const QString& KeyMap::getKeyMapPath()
void KeyMap::loadKeyMap(const QString &json) void KeyMap::loadKeyMap(const QString &json)
{ {
QString errorString; QString errorString;
QByteArray allData;
QJsonParseError jsonError; QJsonParseError jsonError;
QJsonDocument jsonDoc; QJsonDocument jsonDoc;
QJsonObject rootObj; QJsonObject rootObj;
@ -46,7 +45,7 @@ void KeyMap::loadKeyMap(const QString &json)
QMetaEnum metaEnumMouseButtons = QMetaEnum::fromType<Qt::MouseButtons>(); QMetaEnum metaEnumMouseButtons = QMetaEnum::fromType<Qt::MouseButtons>();
QMetaEnum metaEnumKeyMapType = QMetaEnum::fromType<KeyMap::KeyMapType>(); QMetaEnum metaEnumKeyMapType = QMetaEnum::fromType<KeyMap::KeyMapType>();
jsonDoc = QJsonDocument::fromJson(allData, &jsonError); jsonDoc = QJsonDocument::fromJson(json.toUtf8(), &jsonError);
if(jsonError.error != QJsonParseError::NoError) if(jsonError.error != QJsonParseError::NoError)
{ {
@ -255,44 +254,35 @@ parseError:
return; return;
} }
KeyMap::KeyMapNode KeyMap::getKeyMapNode(int key) KeyMap::KeyMapNode& KeyMap::getKeyMapNode(int key)
{ {
KeyMapNode retNode; for (auto& itemNode : m_keyMapNodes) {
bool find = false; switch (itemNode.type) {
for (auto& node : m_keyMapNodes) {
switch (node.type) {
case KMT_CLICK: case KMT_CLICK:
if (node.click.keyNode.key == key) { if (itemNode.click.keyNode.key == key) {
retNode = node; return itemNode;
find = true;
} }
break; break;
case KMT_CLICK_TWICE: case KMT_CLICK_TWICE:
if (node.clickTwice.keyNode.key == key) { if (itemNode.clickTwice.keyNode.key == key) {
retNode = node; return itemNode;
find = true;
} }
break; break;
case KMT_STEER_WHEEL: case KMT_STEER_WHEEL:
if (node.steerWheel.leftKey == key if (itemNode.steerWheel.leftKey == key
|| node.steerWheel.rightKey == key || itemNode.steerWheel.rightKey == key
|| node.steerWheel.upKey == key || itemNode.steerWheel.upKey == key
|| node.steerWheel.downKey == key || itemNode.steerWheel.downKey == key
) { ) {
retNode = node; return itemNode;
find = true;
} }
break; break;
default: default:
break; break;
} }
if (find) {
break;
}
} }
return retNode; return m_invalidNode;
} }
int KeyMap::getSwitchKey() int KeyMap::getSwitchKey()

View file

@ -74,7 +74,7 @@ public:
virtual ~KeyMap(); virtual ~KeyMap();
void loadKeyMap(const QString &json); void loadKeyMap(const QString &json);
KeyMap::KeyMapNode getKeyMapNode(int key); KeyMap::KeyMapNode& getKeyMapNode(int key);
int getSwitchKey(); int getSwitchKey();
MouseMoveMap getMouseMoveMap(); MouseMoveMap getMouseMoveMap();
bool enableMouseMoveMap(); bool enableMouseMoveMap();
@ -83,6 +83,7 @@ public:
private: private:
QVector<KeyMapNode> m_keyMapNodes; QVector<KeyMapNode> m_keyMapNodes;
KeyMapNode m_invalidNode;
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;

View file

@ -364,10 +364,10 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="10" colspan="2"> <item row="3" column="9" colspan="4">
<widget class="QCheckBox" name="gameCheck"> <widget class="QCheckBox" name="gameCheck">
<property name="text"> <property name="text">
<string>Game for Peace</string> <string>custom map</string>
</property> </property>
</widget> </widget>
</item> </item>