src/platform/qt/input/InputItem.h (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef QGBA_INPUT_ITEM
7#define QGBA_INPUT_ITEM
8
9#include "GamepadAxisEvent.h"
10#include "GamepadHatEvent.h"
11
12#include <functional>
13
14#include <QAction>
15
16namespace QGBA {
17
18class InputItem : public QObject {
19Q_OBJECT
20
21public:
22 typedef QPair<std::function<void ()>, std::function<void ()>> Functions;
23
24 InputItem(QAction* action, const QString& name, QMenu* parent = nullptr);
25 InputItem(Functions functions, const QString& visibleName, const QString& name,
26 QMenu* parent = nullptr);
27 InputItem(const QString& visibleName, const QString& name, QMenu* parent = nullptr);
28
29 InputItem();
30 InputItem(InputItem&);
31 InputItem(const InputItem&);
32
33 QAction* action() { return m_action; }
34 const QAction* action() const { return m_action; }
35 Functions functions() const { return m_functions; }
36
37 QMenu* menu() { return m_menu; }
38 const QMenu* menu() const { return m_menu; }
39
40 const QString& visibleName() const { return m_visibleName; }
41 const QString& name() const { return m_name; }
42
43 int shortcut() const { return m_shortcut; }
44 void setShortcut(int sequence);
45 void clearShortcut();
46 bool hasShortcut() { return m_shortcut > -2; }
47
48 int button() const { return m_button; }
49 void setButton(int button);
50 void clearButton();
51 bool hasButton() { return m_button > -2; }
52
53 int axis() const { return m_axis; }
54 GamepadAxisEvent::Direction direction() const { return m_direction; }
55 void setAxis(int axis, GamepadAxisEvent::Direction direction);
56 bool hasAxis() { return m_axis > -2; }
57
58 bool operator==(const InputItem& other) const {
59 return m_name == other.m_name;
60 }
61
62public slots:
63 void trigger(bool = true);
64
65signals:
66 void shortcutBound(InputItem*, int shortcut);
67 void buttonBound(InputItem*, int button);
68 void axisBound(InputItem*, int axis, GamepadAxisEvent::Direction);
69 void childAdded(InputItem* parent, InputItem* child);
70
71private:
72 QAction* m_action = nullptr;
73 Functions m_functions;
74
75 QMenu* m_menu = nullptr;
76 QString m_name;
77 QString m_visibleName;
78
79 int m_shortcut = -2;
80 int m_button = -2;
81 int m_axis = -2;
82 GamepadAxisEvent::Direction m_direction = GamepadAxisEvent::NEUTRAL;
83};
84
85}
86
87Q_DECLARE_METATYPE(QGBA::InputItem)
88
89#endif