support action: drag

This commit is contained in:
Tian Zhou 2019-12-06 18:44:39 -05:00
parent cc7988907c
commit d5b4342018
4 changed files with 69 additions and 19 deletions

View file

@ -4,6 +4,37 @@
#include "inputconvertgame.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <windef.h>
// restrict mouse into a window
static void restrictMouse(const int left, const int right,
const int top, const int bottom)
{
RECT mainWinRect; // RECT is defined in <windef.h>
mainWinRect.left = static_cast<LONG>(left);
mainWinRect.right = static_cast<LONG>(right);
mainWinRect.top = static_cast<LONG>(top);
mainWinRect.bottom = static_cast<LONG>(bottom);
ClipCursor(&mainWinRect); // Windows API
}
static void freeMouse()
{
ClipCursor(nullptr);
}
#else
// dummy
static void restrictMouse(const int left, const int right,
const int top, const int bottom)
{}
static void freeMouse()
{}
#endif // _WINDOWS_
#define CURSOR_POS_CHECK 50
InputConvertGame::InputConvertGame(Controller* controller)
@ -97,6 +128,11 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
case KeyMap::KMT_CLICK_TWICE:
processKeyClick(node.clickTwice.keyNode.pos, true, false, from);
return;
case KeyMap::KMT_DRAG:
processKeyDrag(node.drag.startPos, node.drag.endPos, from);
return;
default:
break;
}
} else {
InputConvertNormal::keyEvent(from, frameSize, showSize);
@ -331,6 +367,17 @@ void InputConvertGame::processKeyClick(QPointF clickPos, bool clickTwice, bool s
}
}
void InputConvertGame::processKeyDrag(QPointF startPos, QPointF endPos, const QKeyEvent* from)
{
if(QEvent::KeyPress == from->type()){
int id = attachTouchID(from->key());
sendTouchDownEvent(id, startPos);
sendTouchMoveEvent(id, endPos);
sendTouchUpEvent(id, endPos);
detachTouchID(from->key());
}
}
bool InputConvertGame::processMouseClick(const QMouseEvent *from)
{
KeyMap::KeyMapNode& node = m_keyMap.getKeyMapNodeMouse(from->button());
@ -451,7 +498,7 @@ bool InputConvertGame::switchGameMap()
if (m_gameMap) {
QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
} else {
mouseMoveStopTouch();
mouseMoveStopTouch();
QGuiApplication::restoreOverrideCursor();
}
return m_gameMap;

View file

@ -41,6 +41,9 @@ protected:
// click
void processKeyClick(QPointF clickPos, bool clickTwice, bool switchMap, const QKeyEvent* from);
// drag
void processKeyDrag(QPointF startPos, QPointF endPos, const QKeyEvent* from);
// mouse
bool processMouseClick(const QMouseEvent* from);
bool processMouseMove(const QMouseEvent* from);

View file

@ -313,12 +313,12 @@ void KeyMap::makeReverseMap()
// ---- check and get of json item ----
bool KeyMap::checkItemKey(const QJsonObject& node, QString name)
bool KeyMap::checkItemKey(const QJsonObject& node, const QString& name)
{
return node.contains(name) && node.value(name).isString();
}
bool KeyMap::checkItemPos(const QJsonObject& node, QString name)
bool KeyMap::checkItemPos(const QJsonObject& node, const QString& name)
{
if(node.contains(name) && node.value(name).isObject()){
QJsonObject pos = node.value(name).toObject();
@ -328,23 +328,23 @@ bool KeyMap::checkItemPos(const QJsonObject& node, QString name)
return false;
}
bool KeyMap::checkItemDouble(const QJsonObject& node, QString name)
bool KeyMap::checkItemDouble(const QJsonObject& node, const QString& name)
{
return node.contains(name) && node.value(name).isDouble();
}
bool KeyMap::checkItemSwitchMap(const QJsonObject& node, QString name)
bool KeyMap::checkItemSwitchMap(const QJsonObject& node, const QString& name)
{
return !node.contains(name) || node.value(name).isBool();
}
KeyMap::KeyMapType KeyMap::getItemType(const QJsonObject& node, QString name)
KeyMap::KeyMapType KeyMap::getItemType(const QJsonObject& node, const QString& name)
{
QString value = node.value(name).toString();
return static_cast<KeyMap::KeyMapType>(m_metaEnumKeyMapType.keyToValue(value.toStdString().c_str()));
}
QPair<KeyMap::ActionType, int> KeyMap::getItemKey(const QJsonObject& node, QString name)
QPair<KeyMap::ActionType, int> KeyMap::getItemKey(const QJsonObject& node, const QString& name)
{
QString value = node.value(name).toString();
int key = m_metaEnumKey.keyToValue(value.toStdString().c_str());
@ -358,18 +358,18 @@ QPair<KeyMap::ActionType, int> KeyMap::getItemKey(const QJsonObject& node, QStri
}
}
QPointF KeyMap::getItemPos(const QJsonObject& node, QString name)
QPointF KeyMap::getItemPos(const QJsonObject& node, const QString& name)
{
QJsonObject pos = node.value(name).toObject();
return QPointF(pos.value("x").toDouble(), pos.value("y").toDouble());
}
double KeyMap::getItemNumber(const QJsonObject& node, QString name)
double KeyMap::getItemNumber(const QJsonObject& node, const QString& name)
{
return node.value(name).toDouble();
}
bool KeyMap::getItemSwitchMap(const QJsonObject& node, QString name)
bool KeyMap::getItemSwitchMap(const QJsonObject& node, const QString& name)
{
return node.value(name).toBool(false);
}

View file

@ -103,16 +103,16 @@ private:
void makeReverseMap();
// parse json of the mapping script
bool checkItemKey(const QJsonObject& node, QString name="key");
bool checkItemPos(const QJsonObject& node, QString name="pos");
bool checkItemDouble(const QJsonObject& node, QString name);
bool checkItemSwitchMap(const QJsonObject& node, QString name="switchMap");
bool checkItemKey(const QJsonObject& node, const QString& name="key");
bool checkItemPos(const QJsonObject& node, const QString& name="pos");
bool checkItemDouble(const QJsonObject& node, const QString& name);
bool checkItemSwitchMap(const QJsonObject& node, const QString& name="switchMap");
KeyMapType getItemType(const QJsonObject& node, QString name="type");
QPair<ActionType, int> getItemKey(const QJsonObject& node, QString name="key");
QPointF getItemPos(const QJsonObject& node, QString name="pos");
double getItemNumber(const QJsonObject& node, QString name);
bool getItemSwitchMap(const QJsonObject& node, QString name="switchMap");
KeyMapType getItemType(const QJsonObject& node, const QString& name="type");
QPair<ActionType, int> getItemKey(const QJsonObject& node, const QString& name="key");
QPointF getItemPos(const QJsonObject& node, const QString& name="pos");
double getItemNumber(const QJsonObject& node, const QString& name);
bool getItemSwitchMap(const QJsonObject& node, const QString& name="switchMap");
private:
bool checkForClick(const QJsonObject& node);