游戏按键转换初步

This commit is contained in:
Barry 2018-11-09 09:31:38 +08:00
parent d0480188e7
commit 2f74f9c068
11 changed files with 173 additions and 81 deletions

View file

@ -21,7 +21,9 @@ void Controller::setDeviceSocket(DeviceSocket *deviceSocket)
void Controller::postControlEvent(ControlEvent *controlEvent)
{
QCoreApplication::postEvent(this, controlEvent);
if (controlEvent) {
QCoreApplication::postEvent(this, controlEvent);
}
}
void Controller::test(QRect rc)
@ -33,7 +35,7 @@ void Controller::test(QRect rc)
bool Controller::event(QEvent *event)
{
if (event->type() == ControlEvent::Control) {
if (event && event->type() == ControlEvent::Control) {
ControlEvent* controlEvent = dynamic_cast<ControlEvent*>(event);
if (controlEvent) {
sendControl(controlEvent->serializeData());

View file

@ -1,12 +1,14 @@
HEADERS += \
$$PWD/controlevent.h \
$$PWD/controller.h \
$$PWD/inputconvert.h \
$$PWD/inputconvertbase.h
$$PWD/inputconvertbase.h \
$$PWD/inputconvertgame.h \
$$PWD/inputconvertnormal.h
SOURCES += \
$$PWD/controlevent.cpp \
$$PWD/controller.cpp \
$$PWD/inputconvert.cpp \
$$PWD/inputconvertbase.cpp
$$PWD/inputconvertbase.cpp \
$$PWD/inputconvertgame.cpp \
$$PWD/inputconvertnormal.cpp

View file

@ -1,22 +0,0 @@
#ifndef INPUTCONVERT_H
#define INPUTCONVERT_H
#include "inputconvertbase.h"
class InputConvert : public InputConvertBase
{
public:
InputConvert();
virtual ~InputConvert();
ControlEvent* mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize);
ControlEvent* wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize);
ControlEvent* keyEvent(const QKeyEvent* from);
private:
AndroidMotioneventButtons convertMouseButtons(Qt::MouseButtons buttonState);
AndroidKeycode convertKeyCode(int key, Qt::KeyboardModifiers modifiers);
AndroidMetastate convertMetastate(Qt::KeyboardModifiers modifiers);
};
#endif // INPUTCONVERT_H

View file

@ -10,3 +10,15 @@ InputConvertBase::~InputConvertBase()
}
void InputConvertBase::setDeviceSocket(DeviceSocket *deviceSocket)
{
m_controller.setDeviceSocket(deviceSocket);
}
void InputConvertBase::sendControlEvent(ControlEvent *event)
{
if (event) {
m_controller.postControlEvent(event);
}
}

View file

@ -6,6 +6,7 @@
#include <QKeyEvent>
#include "controlevent.h"
#include "controller.h"
class InputConvertBase
{
@ -15,9 +16,16 @@ public:
// the frame size may be different from the real device size, so we need the size
// to which the absolute position apply, to scale it accordingly
virtual ControlEvent* mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
virtual ControlEvent* wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
virtual ControlEvent* keyEvent(const QKeyEvent* from) = 0;
virtual void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
virtual void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
virtual void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
void setDeviceSocket(DeviceSocket* deviceSocket);
protected:
void sendControlEvent(ControlEvent* event);
private:
Controller m_controller;
};
#endif // INPUTCONVERTBASE_H

View file

@ -0,0 +1,66 @@
#include "inputconvertgame.h"
InputConvertGame::InputConvertGame()
{
}
InputConvertGame::~InputConvertGame()
{
}
void InputConvertGame::mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize)
{
}
void InputConvertGame::wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize)
{
}
void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize)
{
if (!from || from->isAutoRepeat()) {
return;
}
// action
AndroidMotioneventAction action;
switch (from->type()) {
case QEvent::KeyPress:
action = AMOTION_EVENT_ACTION_DOWN;
break;
case QEvent::KeyRelease:
action = AMOTION_EVENT_ACTION_UP;
break;
default:
return;
}
// pos
QPointF pos;
pos.setX(showSize.width() * 0.25f);
pos.setY(showSize.height() * 0.5f);
// convert pos
pos.setX(pos.x() * frameSize.width() / showSize.width());
pos.setY(pos.y() * frameSize.height() / showSize.height());
// set data
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_MOUSE);
if (!controlEvent) {
return;
}
controlEvent->setMouseEventData(action, AMOTION_EVENT_BUTTON_PRIMARY, QRect(pos.toPoint(), frameSize));
sendControlEvent(controlEvent);
if (QEvent::KeyPress == from->type()) {
ControlEvent* controlEvent2 = new ControlEvent(ControlEvent::CET_MOUSE);
if (!controlEvent2) {
return;
}
pos.setY(pos.y() - 50);
controlEvent2->setMouseEventData(AMOTION_EVENT_ACTION_MOVE, AMOTION_EVENT_BUTTON_PRIMARY, QRect(pos.toPoint(), frameSize));
sendControlEvent(controlEvent2);
}
}

View file

@ -0,0 +1,17 @@
#ifndef INPUTCONVERTGAME_H
#define INPUTCONVERTGAME_H
#include "inputconvertbase.h"
class InputConvertGame : public InputConvertBase
{
public:
InputConvertGame();
virtual ~InputConvertGame();
void mouseEvent(const QMouseEvent* 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);
};
#endif // INPUTCONVERTGAME_H

View file

@ -1,19 +1,19 @@
#include "inputconvert.h"
#include "inputconvertnormal.h"
InputConvert::InputConvert()
InputConvertNormal::InputConvertNormal()
{
}
InputConvert::~InputConvert()
InputConvertNormal::~InputConvertNormal()
{
}
ControlEvent* InputConvert::mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize)
void InputConvertNormal::mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize)
{
if (!from) {
return Q_NULLPTR;
return;
}
// action
@ -29,7 +29,7 @@ ControlEvent* InputConvert::mouseEvent(const QMouseEvent* from, const QSize& fra
action = AMOTION_EVENT_ACTION_MOVE;
break;
default:
return Q_NULLPTR;
return;
}
// pos
@ -41,16 +41,16 @@ ControlEvent* InputConvert::mouseEvent(const QMouseEvent* from, const QSize& fra
// set data
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_MOUSE);
if (!controlEvent) {
return Q_NULLPTR;
return;
}
controlEvent->setMouseEventData(action, convertMouseButtons(from->buttons()), QRect(pos.toPoint(), frameSize));
return controlEvent;
sendControlEvent(controlEvent);
}
ControlEvent *InputConvert::wheelEvent(const QWheelEvent *from, const QSize& frameSize, const QSize& showSize)
void InputConvertNormal::wheelEvent(const QWheelEvent *from, const QSize& frameSize, const QSize& showSize)
{
if (!from) {
return Q_NULLPTR;
return;
}
// delta
@ -74,16 +74,16 @@ ControlEvent *InputConvert::wheelEvent(const QWheelEvent *from, const QSize& fra
// set data
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_SCROLL);
if (!controlEvent) {
return Q_NULLPTR;
return;
}
controlEvent->setScrollEventData(QRect(pos.toPoint(), frameSize), hScroll, vScroll);
return controlEvent;
sendControlEvent(controlEvent);
}
ControlEvent *InputConvert::keyEvent(const QKeyEvent *from)
void InputConvertNormal::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize)
{
if (!from) {
return Q_NULLPTR;
return;
}
// action
@ -96,25 +96,25 @@ ControlEvent *InputConvert::keyEvent(const QKeyEvent *from)
action = AKEY_EVENT_ACTION_UP;
break;
default:
return Q_NULLPTR;
return;
}
// key code
AndroidKeycode keyCode = convertKeyCode(from->key(), from->modifiers());
if (AKEYCODE_UNKNOWN == keyCode) {
return Q_NULLPTR;
return;
}
// set data
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_KEYCODE);
if (!controlEvent) {
return Q_NULLPTR;
return;
}
controlEvent->setKeycodeEventData(action, keyCode, convertMetastate(from->modifiers()));
return controlEvent;
sendControlEvent(controlEvent);
}
AndroidMotioneventButtons InputConvert::convertMouseButtons(Qt::MouseButtons buttonState)
AndroidMotioneventButtons InputConvertNormal::convertMouseButtons(Qt::MouseButtons buttonState)
{
quint32 buttons = 0;
if (buttonState & Qt::LeftButton) {
@ -135,7 +135,7 @@ AndroidMotioneventButtons InputConvert::convertMouseButtons(Qt::MouseButtons but
return (AndroidMotioneventButtons)buttons;
}
AndroidKeycode InputConvert::convertKeyCode(int key, Qt::KeyboardModifiers modifiers)
AndroidKeycode InputConvertNormal::convertKeyCode(int key, Qt::KeyboardModifiers modifiers)
{
AndroidKeycode keyCode = AKEYCODE_UNKNOWN;
// functional keys
@ -309,7 +309,7 @@ AndroidKeycode InputConvert::convertKeyCode(int key, Qt::KeyboardModifiers modif
return keyCode;
}
AndroidMetastate InputConvert::convertMetastate(Qt::KeyboardModifiers modifiers)
AndroidMetastate InputConvertNormal::convertMetastate(Qt::KeyboardModifiers modifiers)
{
int metastate = AMETA_NONE;

View file

@ -0,0 +1,22 @@
#ifndef INPUTCONVERT_H
#define INPUTCONVERT_H
#include "inputconvertbase.h"
class InputConvertNormal : public InputConvertBase
{
public:
InputConvertNormal();
virtual ~InputConvertNormal();
void mouseEvent(const QMouseEvent* 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);
private:
AndroidMotioneventButtons convertMouseButtons(Qt::MouseButtons buttonState);
AndroidKeycode convertKeyCode(int key, Qt::KeyboardModifiers modifiers);
AndroidMetastate convertMetastate(Qt::KeyboardModifiers modifiers);
};
#endif // INPUTCONVERT_H

View file

@ -37,7 +37,7 @@ VideoForm::VideoForm(QWidget *parent) :
m_decoder.startDecode();
// init controller
m_controller.setDeviceSocket(m_server->getDeviceSocket());
m_inputConvert.setDeviceSocket(m_server->getDeviceSocket());
}
});
@ -98,48 +98,33 @@ void VideoForm::updateShowSize(const QSize &newSize)
void VideoForm::mousePressEvent(QMouseEvent *event)
{
ControlEvent* controlEvent = m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), size());
if (controlEvent) {
m_controller.postControlEvent(controlEvent);
}
m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), size());
}
void VideoForm::mouseReleaseEvent(QMouseEvent *event)
{
ControlEvent* controlEvent = m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), size());
if (controlEvent) {
m_controller.postControlEvent(controlEvent);
}
m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), size());
}
void VideoForm::mouseMoveEvent(QMouseEvent *event)
{
ControlEvent* controlEvent = m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), size());
if (controlEvent) {
m_controller.postControlEvent(controlEvent);
}
m_inputConvert.mouseEvent(event, ui->videoWidget->frameSize(), size());
}
void VideoForm::wheelEvent(QWheelEvent *event)
{
ControlEvent* controlEvent = m_inputConvert.wheelEvent(event, ui->videoWidget->frameSize(), size());
if (controlEvent) {
m_controller.postControlEvent(controlEvent);
}
m_inputConvert.wheelEvent(event, ui->videoWidget->frameSize(), size());
}
void VideoForm::keyPressEvent(QKeyEvent *event)
{
ControlEvent* controlEvent = m_inputConvert.keyEvent(event);
if (controlEvent) {
m_controller.postControlEvent(controlEvent);
}
qDebug() << "keyPressEvent" << event->isAutoRepeat();
m_inputConvert.keyEvent(event, ui->videoWidget->frameSize(), size());
}
void VideoForm::keyReleaseEvent(QKeyEvent *event)
{
ControlEvent* controlEvent = m_inputConvert.keyEvent(event);
if (controlEvent) {
m_controller.postControlEvent(controlEvent);
}
qDebug() << "keyReleaseEvent" << event->isAutoRepeat();
m_inputConvert.keyEvent(event, ui->videoWidget->frameSize(), size());
}

View file

@ -6,8 +6,8 @@
#include "server.h"
#include "decoder.h"
#include "frames.h"
#include "controller.h"
#include "inputconvert.h"
#include "inputconvertnormal.h"
#include "inputconvertgame.h"
namespace Ui {
class videoForm;
@ -38,8 +38,8 @@ private:
Server* m_server = Q_NULLPTR;
Decoder m_decoder;
Frames m_frames;
Controller m_controller;
InputConvert m_inputConvert;
//InputConvertNormal m_inputConvert;
InputConvertGame m_inputConvert;
};
#endif // VIDEOFORM_H