aboutsummaryrefslogtreecommitdiffstats
path: root/src/universalinput/qactionstore.h
blob: f16f07b8be0ef408f083fe6a052648374252796c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QACTIONSTORE_H
#define QACTIONSTORE_H

#include <QtUniversalInput/quniversalinput.h>

using namespace Qt::Literals::StringLiterals;

QT_BEGIN_NAMESPACE

class QActionStorePrivate;

class Q_UNIVERSALINPUT_EXPORT QActionStore : public QObject
{
    Q_OBJECT
    Q_ENUMS(Controller)
    Q_ENUMS(AxisDirection)
public:
    enum class Controller
    {
        All = -1,
        Device0 = 0,
        Device1 = 1,
        Device2 = 2,
        Device3 = 3,
        Device4 = 4,
        Device5 = 5,
        Device6 = 6,
        Device7 = 7,
        Device8 = 8,
        Device9 = 9,
        Device10 = 10,
        Device11 = 11,
        Device12 = 12,
        Device13 = 13,
        Device14 = 14,
        Device15 = 15,
        Device16 = 16,
        DeviceMAX = 17,
    };

    enum class AxisDirection
    {
        All = -1,
        Up = 0,
        Right = 1,
        Down = 2,
        Left = 3,
        Max = 4,
    };

    struct JoyButtonAction
    {
        Controller device = Controller::All;
        JoyButton button = JoyButton::Invalid;
        bool isPressed = false;
    };

    struct JoyAxisAction
    {
        Controller device = Controller::All;
        JoyAxis axis = JoyAxis::Invalid;
        AxisDirection direction = AxisDirection::Max;
        float deadzone = 0.5f;
    };

    struct KeyEventAction
    {
        Qt::Key key = Qt::Key_unknown;
        bool isPressed = false;
    };

    struct MouseButtonAction
    {
        Qt::MouseButton button = Qt::NoButton;
        bool isPressed = false;
    };

    struct Action
    {
        QString name = u""_s;
        QList<JoyButtonAction> buttons;
        QList<JoyAxisAction> axes;
        QList<KeyEventAction> keys;
        QList<MouseButtonAction> mouseButtons;
    };

    struct Q_UNIVERSALINPUT_EXPORT ActionBuilder
    {
        ActionBuilder(const QString &name);
        ActionBuilder &addAxis(Controller device, JoyAxis axis, AxisDirection direction, float deadzone);
        ActionBuilder &addButton(Controller device, JoyButton button, bool isPressed = true);
        ActionBuilder &addKey(Qt::Key key, bool isPressed = true);
        ActionBuilder &addMouseButton(Qt::MouseButton button, bool isPressed = true);

        Action build() const;

    private:
        Action m_action;
    };

    explicit QActionStore(QObject *parent = nullptr);
    ~QActionStore();

    void registerAction(const Action &action);
    void clearActions();

Q_SIGNALS:
    void actionEvent(const QString &action);
    void actionKeyEvent(const QString &action, Qt::Key key, bool isPressed);
    void actionMouseButtonEvent(const QString &action, Qt::MouseButton button, bool isPressed);
    void actionJoyButtonEvent(const QString &action, int device, JoyButton button, bool isPressed);
    void actionJoyAxisEvent(const QString &action, int device, JoyAxis axis, float value);

public Q_SLOTS:
    void sendKeyEvent(Qt::Key key, bool isPressed = true);
    void sendMouseButtonEvent(Qt::MouseButton button, bool isPressed = true);

private:
    Q_DECLARE_PRIVATE(QActionStore)
    Q_DISABLE_COPY(QActionStore)

private:
    Q_PRIVATE_SLOT(d_func(), void _q_handleJoyAxisEvent(int, JoyAxis, float))
    Q_PRIVATE_SLOT(d_func(), void _q_handleJoyButtonEvent(int, JoyButton, bool))
};

QT_END_NAMESPACE

Q_DECLARE_METATYPE(QActionStore *)
Q_DECLARE_METATYPE(QActionStore::Controller)
Q_DECLARE_METATYPE(QActionStore::AxisDirection)
Q_DECLARE_METATYPE(QActionStore::JoyButtonAction)
Q_DECLARE_METATYPE(QActionStore::JoyAxisAction)
Q_DECLARE_METATYPE(QActionStore::KeyEventAction)
Q_DECLARE_METATYPE(QActionStore::MouseButtonAction)
Q_DECLARE_METATYPE(QActionStore::Action)

#endif // QACTIONSTORE_H