Skip to content

Commit b9e9c97

Browse files
committed
游戏按键转换初步
1 parent 849bde6 commit b9e9c97

11 files changed

+173
-81
lines changed

src/inputcontrol/controller.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ void Controller::setDeviceSocket(DeviceSocket *deviceSocket)
2121

2222
void Controller::postControlEvent(ControlEvent *controlEvent)
2323
{
24-
QCoreApplication::postEvent(this, controlEvent);
24+
if (controlEvent) {
25+
QCoreApplication::postEvent(this, controlEvent);
26+
}
2527
}
2628

2729
void Controller::test(QRect rc)
@@ -33,7 +35,7 @@ void Controller::test(QRect rc)
3335

3436
bool Controller::event(QEvent *event)
3537
{
36-
if (event->type() == ControlEvent::Control) {
38+
if (event && event->type() == ControlEvent::Control) {
3739
ControlEvent* controlEvent = dynamic_cast<ControlEvent*>(event);
3840
if (controlEvent) {
3941
sendControl(controlEvent->serializeData());

src/inputcontrol/inputcontrol.pri

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
HEADERS += \
22
$$PWD/controlevent.h \
33
$$PWD/controller.h \
4-
$$PWD/inputconvert.h \
5-
$$PWD/inputconvertbase.h
4+
$$PWD/inputconvertbase.h \
5+
$$PWD/inputconvertgame.h \
6+
$$PWD/inputconvertnormal.h
67

78
SOURCES += \
89
$$PWD/controlevent.cpp \
910
$$PWD/controller.cpp \
10-
$$PWD/inputconvert.cpp \
11-
$$PWD/inputconvertbase.cpp
11+
$$PWD/inputconvertbase.cpp \
12+
$$PWD/inputconvertgame.cpp \
13+
$$PWD/inputconvertnormal.cpp
1214

src/inputcontrol/inputconvert.h

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/inputcontrol/inputconvertbase.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ InputConvertBase::~InputConvertBase()
1010

1111
}
1212

13+
void InputConvertBase::setDeviceSocket(DeviceSocket *deviceSocket)
14+
{
15+
m_controller.setDeviceSocket(deviceSocket);
16+
}
17+
18+
void InputConvertBase::sendControlEvent(ControlEvent *event)
19+
{
20+
if (event) {
21+
m_controller.postControlEvent(event);
22+
}
23+
}
24+

src/inputcontrol/inputconvertbase.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QKeyEvent>
77

88
#include "controlevent.h"
9+
#include "controller.h"
910

1011
class InputConvertBase
1112
{
@@ -15,9 +16,16 @@ class InputConvertBase
1516

1617
// the frame size may be different from the real device size, so we need the size
1718
// to which the absolute position apply, to scale it accordingly
18-
virtual ControlEvent* mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
19-
virtual ControlEvent* wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
20-
virtual ControlEvent* keyEvent(const QKeyEvent* from) = 0;
19+
virtual void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
20+
virtual void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
21+
virtual void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize) = 0;
22+
23+
void setDeviceSocket(DeviceSocket* deviceSocket);
24+
protected:
25+
void sendControlEvent(ControlEvent* event);
26+
27+
private:
28+
Controller m_controller;
2129
};
2230

2331
#endif // INPUTCONVERTBASE_H

src/inputcontrol/inputconvertgame.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "inputconvertgame.h"
2+
3+
InputConvertGame::InputConvertGame()
4+
{
5+
6+
}
7+
8+
InputConvertGame::~InputConvertGame()
9+
{
10+
11+
}
12+
13+
void InputConvertGame::mouseEvent(const QMouseEvent *from, const QSize &frameSize, const QSize &showSize)
14+
{
15+
}
16+
17+
void InputConvertGame::wheelEvent(const QWheelEvent *from, const QSize &frameSize, const QSize &showSize)
18+
{
19+
}
20+
21+
void InputConvertGame::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize)
22+
{
23+
if (!from || from->isAutoRepeat()) {
24+
return;
25+
}
26+
27+
// action
28+
AndroidMotioneventAction action;
29+
switch (from->type()) {
30+
case QEvent::KeyPress:
31+
action = AMOTION_EVENT_ACTION_DOWN;
32+
break;
33+
case QEvent::KeyRelease:
34+
action = AMOTION_EVENT_ACTION_UP;
35+
break;
36+
default:
37+
return;
38+
}
39+
40+
// pos
41+
QPointF pos;
42+
pos.setX(showSize.width() * 0.25f);
43+
pos.setY(showSize.height() * 0.5f);
44+
45+
// convert pos
46+
pos.setX(pos.x() * frameSize.width() / showSize.width());
47+
pos.setY(pos.y() * frameSize.height() / showSize.height());
48+
49+
// set data
50+
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_MOUSE);
51+
if (!controlEvent) {
52+
return;
53+
}
54+
controlEvent->setMouseEventData(action, AMOTION_EVENT_BUTTON_PRIMARY, QRect(pos.toPoint(), frameSize));
55+
sendControlEvent(controlEvent);
56+
57+
if (QEvent::KeyPress == from->type()) {
58+
ControlEvent* controlEvent2 = new ControlEvent(ControlEvent::CET_MOUSE);
59+
if (!controlEvent2) {
60+
return;
61+
}
62+
pos.setY(pos.y() - 50);
63+
controlEvent2->setMouseEventData(AMOTION_EVENT_ACTION_MOVE, AMOTION_EVENT_BUTTON_PRIMARY, QRect(pos.toPoint(), frameSize));
64+
sendControlEvent(controlEvent2);
65+
}
66+
}

src/inputcontrol/inputconvertgame.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef INPUTCONVERTGAME_H
2+
#define INPUTCONVERTGAME_H
3+
4+
#include "inputconvertbase.h"
5+
6+
class InputConvertGame : public InputConvertBase
7+
{
8+
public:
9+
InputConvertGame();
10+
virtual ~InputConvertGame();
11+
12+
void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize);
13+
void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize);
14+
void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize);
15+
};
16+
17+
#endif // INPUTCONVERTGAME_H

src/inputcontrol/inputconvert.cpp renamed to src/inputcontrol/inputconvertnormal.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
#include "inputconvert.h"
1+
#include "inputconvertnormal.h"
22

3-
InputConvert::InputConvert()
3+
InputConvertNormal::InputConvertNormal()
44
{
55

66
}
77

8-
InputConvert::~InputConvert()
8+
InputConvertNormal::~InputConvertNormal()
99
{
1010

1111
}
1212

13-
ControlEvent* InputConvert::mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize)
13+
void InputConvertNormal::mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize)
1414
{
1515
if (!from) {
16-
return Q_NULLPTR;
16+
return;
1717
}
1818

1919
// action
@@ -29,7 +29,7 @@ ControlEvent* InputConvert::mouseEvent(const QMouseEvent* from, const QSize& fra
2929
action = AMOTION_EVENT_ACTION_MOVE;
3030
break;
3131
default:
32-
return Q_NULLPTR;
32+
return;
3333
}
3434

3535
// pos
@@ -41,16 +41,16 @@ ControlEvent* InputConvert::mouseEvent(const QMouseEvent* from, const QSize& fra
4141
// set data
4242
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_MOUSE);
4343
if (!controlEvent) {
44-
return Q_NULLPTR;
44+
return;
4545
}
4646
controlEvent->setMouseEventData(action, convertMouseButtons(from->buttons()), QRect(pos.toPoint(), frameSize));
47-
return controlEvent;
47+
sendControlEvent(controlEvent);
4848
}
4949

50-
ControlEvent *InputConvert::wheelEvent(const QWheelEvent *from, const QSize& frameSize, const QSize& showSize)
50+
void InputConvertNormal::wheelEvent(const QWheelEvent *from, const QSize& frameSize, const QSize& showSize)
5151
{
5252
if (!from) {
53-
return Q_NULLPTR;
53+
return;
5454
}
5555

5656
// delta
@@ -74,16 +74,16 @@ ControlEvent *InputConvert::wheelEvent(const QWheelEvent *from, const QSize& fra
7474
// set data
7575
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_SCROLL);
7676
if (!controlEvent) {
77-
return Q_NULLPTR;
77+
return;
7878
}
7979
controlEvent->setScrollEventData(QRect(pos.toPoint(), frameSize), hScroll, vScroll);
80-
return controlEvent;
80+
sendControlEvent(controlEvent);
8181
}
8282

83-
ControlEvent *InputConvert::keyEvent(const QKeyEvent *from)
83+
void InputConvertNormal::keyEvent(const QKeyEvent *from, const QSize& frameSize, const QSize& showSize)
8484
{
8585
if (!from) {
86-
return Q_NULLPTR;
86+
return;
8787
}
8888

8989
// action
@@ -96,25 +96,25 @@ ControlEvent *InputConvert::keyEvent(const QKeyEvent *from)
9696
action = AKEY_EVENT_ACTION_UP;
9797
break;
9898
default:
99-
return Q_NULLPTR;
99+
return;
100100
}
101101

102102
// key code
103103
AndroidKeycode keyCode = convertKeyCode(from->key(), from->modifiers());
104104
if (AKEYCODE_UNKNOWN == keyCode) {
105-
return Q_NULLPTR;
105+
return;
106106
}
107107

108108
// set data
109109
ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_KEYCODE);
110110
if (!controlEvent) {
111-
return Q_NULLPTR;
111+
return;
112112
}
113113
controlEvent->setKeycodeEventData(action, keyCode, convertMetastate(from->modifiers()));
114-
return controlEvent;
114+
sendControlEvent(controlEvent);
115115
}
116116

117-
AndroidMotioneventButtons InputConvert::convertMouseButtons(Qt::MouseButtons buttonState)
117+
AndroidMotioneventButtons InputConvertNormal::convertMouseButtons(Qt::MouseButtons buttonState)
118118
{
119119
quint32 buttons = 0;
120120
if (buttonState & Qt::LeftButton) {
@@ -135,7 +135,7 @@ AndroidMotioneventButtons InputConvert::convertMouseButtons(Qt::MouseButtons but
135135
return (AndroidMotioneventButtons)buttons;
136136
}
137137

138-
AndroidKeycode InputConvert::convertKeyCode(int key, Qt::KeyboardModifiers modifiers)
138+
AndroidKeycode InputConvertNormal::convertKeyCode(int key, Qt::KeyboardModifiers modifiers)
139139
{
140140
AndroidKeycode keyCode = AKEYCODE_UNKNOWN;
141141
// functional keys
@@ -309,7 +309,7 @@ AndroidKeycode InputConvert::convertKeyCode(int key, Qt::KeyboardModifiers modif
309309
return keyCode;
310310
}
311311

312-
AndroidMetastate InputConvert::convertMetastate(Qt::KeyboardModifiers modifiers)
312+
AndroidMetastate InputConvertNormal::convertMetastate(Qt::KeyboardModifiers modifiers)
313313
{
314314
int metastate = AMETA_NONE;
315315

src/inputcontrol/inputconvertnormal.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef INPUTCONVERT_H
2+
#define INPUTCONVERT_H
3+
4+
#include "inputconvertbase.h"
5+
6+
class InputConvertNormal : public InputConvertBase
7+
{
8+
public:
9+
InputConvertNormal();
10+
virtual ~InputConvertNormal();
11+
12+
void mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize);
13+
void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize);
14+
void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize);
15+
16+
private:
17+
AndroidMotioneventButtons convertMouseButtons(Qt::MouseButtons buttonState);
18+
AndroidKeycode convertKeyCode(int key, Qt::KeyboardModifiers modifiers);
19+
AndroidMetastate convertMetastate(Qt::KeyboardModifiers modifiers);
20+
};
21+
22+
#endif // INPUTCONVERT_H

0 commit comments

Comments
 (0)