mirror of
https://github.com/barry-ran/QtScrcpy.git
synced 2025-07-31 21:08:38 +00:00
游戏方向盘控制逻辑初步
This commit is contained in:
parent
c69c0b0c25
commit
395ce726cc
3 changed files with 177 additions and 10 deletions
|
@ -12,18 +12,26 @@ 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)
|
||||||
{
|
{
|
||||||
|
updateSize(frameSize, showSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputConvertGame::wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize)
|
void InputConvertGame::wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize)
|
||||||
{
|
{
|
||||||
|
updateSize(frameSize, showSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize)
|
void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize)
|
||||||
{
|
{
|
||||||
|
updateSize(frameSize, showSize);
|
||||||
if (!from || from->isAutoRepeat()) {
|
if (!from || from->isAutoRepeat()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isSteerWheelKeys(from)) {
|
||||||
|
processSteerWheel(from);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
AndroidMotioneventAction action;
|
AndroidMotioneventAction action;
|
||||||
// pos
|
// pos
|
||||||
QPointF pos;
|
QPointF pos;
|
||||||
|
@ -79,17 +87,135 @@ void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, c
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
/*
|
}
|
||||||
if (QEvent::KeyPress == from->type()) {
|
|
||||||
ControlEvent* controlEvent2 = new ControlEvent(ControlEvent::CET_MOUSE);
|
void InputConvertGame::updateSize(const QSize &frameSize, const QSize &showSize)
|
||||||
if (!controlEvent2) {
|
{
|
||||||
|
m_frameSize = frameSize;
|
||||||
|
m_showSize = showSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputConvertGame::sendTouchDownEvent(int id, QPointF pos)
|
||||||
|
{
|
||||||
|
sendTouchEvent(id, pos, AMOTION_EVENT_ACTION_DOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputConvertGame::sendTouchMoveEvent(int id, QPointF pos)
|
||||||
|
{
|
||||||
|
sendTouchEvent(id, pos, AMOTION_EVENT_ACTION_MOVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputConvertGame::sendTouchUpEvent(int id, QPointF pos)
|
||||||
|
{
|
||||||
|
sendTouchEvent(id, pos, AMOTION_EVENT_ACTION_UP);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputConvertGame::sendTouchEvent(int id, QPointF pos, AndroidMotioneventAction action)
|
||||||
|
{
|
||||||
|
if (0 > id || MULTI_TOUCH_MAX_NUM-1 < id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_TOUCH);
|
||||||
|
if (!controlEvent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
controlEvent->setTouchEventData(id, action, QRect(calcAbsolutePos(pos).toPoint(), m_frameSize));
|
||||||
|
sendControlEvent(controlEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
QPointF InputConvertGame::calcAbsolutePos(QPointF relativePos)
|
||||||
|
{
|
||||||
|
QPointF absolutePos;
|
||||||
|
absolutePos.setX(m_showSize.width() * relativePos.x());
|
||||||
|
absolutePos.setY(m_showSize.height() * relativePos.y());
|
||||||
|
// convert pos
|
||||||
|
absolutePos.setX(absolutePos.x() * m_frameSize.width() / m_showSize.width());
|
||||||
|
absolutePos.setY(absolutePos.y() * m_frameSize.height() / m_showSize.height());
|
||||||
|
return absolutePos;
|
||||||
|
}
|
||||||
|
|
||||||
|
int InputConvertGame::attachTouchID(int key)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < MULTI_TOUCH_MAX_NUM; i++) {
|
||||||
|
if (0 == multiTouchID[i]) {
|
||||||
|
multiTouchID[i] = key;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputConvertGame::detachTouchID(int key)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < MULTI_TOUCH_MAX_NUM; i++) {
|
||||||
|
if (key == multiTouchID[i]) {
|
||||||
|
multiTouchID[i] = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pos.setY(pos.y() - 50);
|
|
||||||
action &= AMOTION_EVENT_ACTION_POINTER_INDEX_MASK;
|
|
||||||
action |= AMOTION_EVENT_ACTION_MOVE;
|
|
||||||
controlEvent2->setMouseEventData((AndroidMotioneventAction)action, AMOTION_EVENT_BUTTON_PRIMARY, QRect(pos.toPoint(), frameSize));
|
|
||||||
sendControlEvent(controlEvent2);
|
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
int InputConvertGame::getTouchID(int key)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < MULTI_TOUCH_MAX_NUM; i++) {
|
||||||
|
if (key == multiTouchID[i]) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InputConvertGame::isSteerWheelKeys(const QKeyEvent *from)
|
||||||
|
{
|
||||||
|
for (int key : m_steerWheelKeys) {
|
||||||
|
if (key == from->key()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#include <QDebug>
|
||||||
|
void InputConvertGame::processSteerWheel(const QKeyEvent *from)
|
||||||
|
{
|
||||||
|
QPointF movePos = m_steerWheelPos;
|
||||||
|
int moveKey = 0;
|
||||||
|
if (QEvent::KeyPress == from->type()) {
|
||||||
|
m_steerWheelKeysNum++;
|
||||||
|
if (1 == m_steerWheelKeysNum) {
|
||||||
|
int id = attachTouchID(from->key());
|
||||||
|
if (-1 == id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_steerWheelFirstTouchKey = from->key();
|
||||||
|
sendTouchDownEvent(id, m_steerWheelPos);
|
||||||
|
moveKey = from->key();
|
||||||
|
} else if (2 == m_steerWheelKeysNum) {
|
||||||
|
moveKey = from->key();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (QEvent::KeyRelease == from->type()){
|
||||||
|
m_steerWheelKeysNum--;
|
||||||
|
if (0 == m_steerWheelKeysNum) {
|
||||||
|
sendTouchUpEvent(getTouchID(m_steerWheelFirstTouchKey), m_steerWheelPos);
|
||||||
|
detachTouchID(from->key());
|
||||||
|
m_steerWheelFirstTouchKey = 0;
|
||||||
|
return;
|
||||||
|
} else if (1 == m_steerWheelKeysNum) {
|
||||||
|
moveKey = m_steerWheelFirstTouchKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (moveKey == m_steerWheelKeys[SWD_UP]) {
|
||||||
|
movePos.setY(movePos.y() - m_steerWheelOffset);
|
||||||
|
} else if (moveKey == m_steerWheelKeys[SWD_RIGHT]) {
|
||||||
|
movePos.setX(movePos.x() + m_steerWheelOffset);
|
||||||
|
} else if (moveKey == m_steerWheelKeys[SWD_DOWN]) {
|
||||||
|
movePos.setY(movePos.y() + m_steerWheelOffset);
|
||||||
|
} else if (moveKey == m_steerWheelKeys[SWD_LEFT]) {
|
||||||
|
movePos.setX(movePos.x() - m_steerWheelOffset);
|
||||||
|
}
|
||||||
|
if (0 != moveKey) {
|
||||||
|
qDebug() << "move pos" << movePos;
|
||||||
|
sendTouchMoveEvent(getTouchID(m_steerWheelFirstTouchKey), movePos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
#ifndef INPUTCONVERTGAME_H
|
#ifndef INPUTCONVERTGAME_H
|
||||||
#define INPUTCONVERTGAME_H
|
#define INPUTCONVERTGAME_H
|
||||||
|
|
||||||
|
#include <QPointF>
|
||||||
#include "inputconvertbase.h"
|
#include "inputconvertbase.h"
|
||||||
|
|
||||||
|
#define MULTI_TOUCH_MAX_NUM 10
|
||||||
class InputConvertGame : public InputConvertBase
|
class InputConvertGame : public InputConvertBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -12,6 +14,44 @@ public:
|
||||||
void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize);
|
void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize);
|
||||||
void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize);
|
void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize);
|
||||||
void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize);
|
void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void updateSize(const QSize& frameSize, const QSize& showSize);
|
||||||
|
void sendTouchDownEvent(int id, QPointF pos);
|
||||||
|
void sendTouchMoveEvent(int id, QPointF pos);
|
||||||
|
void sendTouchUpEvent(int id, QPointF pos);
|
||||||
|
void sendTouchEvent(int id, QPointF pos, AndroidMotioneventAction action);
|
||||||
|
QPointF calcAbsolutePos(QPointF relativePos);
|
||||||
|
|
||||||
|
// multi touch id
|
||||||
|
int attachTouchID(int key);
|
||||||
|
void detachTouchID(int key);
|
||||||
|
int getTouchID(int key);
|
||||||
|
|
||||||
|
// steer wheel
|
||||||
|
bool isSteerWheelKeys(const QKeyEvent* from);
|
||||||
|
void processSteerWheel(const QKeyEvent* from);
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum SteerWheelDirection {
|
||||||
|
SWD_UP = 0,
|
||||||
|
SWD_RIGHT,
|
||||||
|
SWD_DOWN,
|
||||||
|
SWD_LEFT,
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
QSize m_frameSize;
|
||||||
|
QSize m_showSize;
|
||||||
|
|
||||||
|
int multiTouchID[MULTI_TOUCH_MAX_NUM] = { 0 };
|
||||||
|
|
||||||
|
QPointF m_steerWheelPos = {0.16f, 0.75f};
|
||||||
|
float m_steerWheelOffset = 0.1f;
|
||||||
|
// order by SteerWheelDirection(up right down left)
|
||||||
|
int m_steerWheelKeys[4] = {Qt::Key_W, Qt::Key_D, Qt::Key_S, Qt::Key_A};
|
||||||
|
int m_steerWheelKeysNum = 0;
|
||||||
|
int m_steerWheelFirstTouchKey = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INPUTCONVERTGAME_H
|
#endif // INPUTCONVERTGAME_H
|
||||||
|
|
|
@ -88,6 +88,7 @@ void VideoForm::updateShowSize(const QSize &newSize)
|
||||||
|
|
||||||
// 窗口居中
|
// 窗口居中
|
||||||
move(screenRect.center() - geometry().center());
|
move(screenRect.center() - geometry().center());
|
||||||
|
qDebug() << screenRect.center() << geometry().center();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (showSize != size()) {
|
if (showSize != size()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue