all repos — mgba @ 639c0bf0e9499870d122e1579184a99f3f8561cd

mGBA Game Boy Advance Emulator

src/platform/qt/ShortcutController.h (view raw)

  1/* Copyright (c) 2013-2015 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#pragma once
  7
  8#include "ActionMapper.h"
  9#include "GamepadAxisEvent.h"
 10
 11#include <QHash>
 12#include <QMap>
 13#include <QObject>
 14#include <QString>
 15
 16#include <memory>
 17
 18class QKeyEvent;
 19
 20namespace QGBA {
 21
 22class ConfigController;
 23class InputProfile;
 24
 25class Shortcut : public QObject {
 26Q_OBJECT
 27
 28public:
 29	Shortcut(Action* action);
 30
 31	Action* action() { return m_action; }
 32	const Action* action() const { return m_action; }
 33	int shortcut() const { return m_shortcut; }
 34	QString visibleName() const { return m_action ? m_action->visibleName() : QString(); }
 35	QString name() const { return m_action ? m_action->name() : QString(); }
 36	int button() const { return m_button; }
 37	int axis() const { return m_axis; }
 38	GamepadAxisEvent::Direction direction() const { return m_direction; }
 39
 40	bool operator==(const Shortcut& other) const {
 41		return m_action == other.m_action;
 42	}
 43
 44public slots:
 45	void setShortcut(int sequence);
 46	void setButton(int button);
 47	void setAxis(int axis, GamepadAxisEvent::Direction direction);
 48
 49signals:
 50	void shortcutChanged(int sequence);
 51	void buttonChanged(int button);
 52	void axisChanged(int axis, GamepadAxisEvent::Direction direction);
 53
 54private:
 55	Action* m_action = nullptr;
 56	int m_shortcut = 0;
 57	int m_button = -1;
 58	int m_axis = -1;
 59	GamepadAxisEvent::Direction m_direction;
 60};
 61
 62class ShortcutController : public QObject {
 63Q_OBJECT
 64
 65private:
 66	constexpr static const char* const KEY_SECTION = "shortcutKey";
 67	constexpr static const char* const BUTTON_SECTION = "shortcutButton";
 68	constexpr static const char* const AXIS_SECTION = "shortcutAxis";
 69	constexpr static const char* const BUTTON_PROFILE_SECTION = "shortcutProfileButton.";
 70	constexpr static const char* const AXIS_PROFILE_SECTION = "shortcutProfileAxis.";
 71
 72public:
 73	ShortcutController(QObject* parent = nullptr);
 74
 75	void setConfigController(ConfigController* controller);
 76	void setActionMapper(ActionMapper* actionMapper);
 77
 78	void setProfile(const QString& profile);
 79
 80	void updateKey(const QString& action, int keySequence);
 81	void updateButton(const QString& action, int button);
 82	void updateAxis(const QString& action, int axis, GamepadAxisEvent::Direction direction);
 83
 84	void clearKey(const QString& action);
 85	void clearButton(const QString& action);
 86	void clearAxis(const QString& action);
 87
 88	static int toModifierShortcut(const QString& shortcut);
 89	static bool isModifierKey(int key);
 90	static int toModifierKey(int key);
 91
 92	const Shortcut* shortcut(const QString& action) const;
 93	int indexIn(const QString& action) const;
 94	int count(const QString& menu = {}) const;
 95	QString parent(const QString& action) const;
 96	QString name(int index, const QString& parent = {}) const;
 97	QString visibleName(const QString& item) const;
 98
 99signals:
100	void shortcutAdded(const QString& name);
101	void menuCleared(const QString& name);
102
103public slots:
104	void loadProfile(const QString& profile);
105	void rebuildItems();
106
107protected:
108	bool eventFilter(QObject*, QEvent*) override;
109
110private:
111	void generateItem(const QString& itemName);
112	bool loadShortcuts(std::shared_ptr<Shortcut>);
113	void loadGamepadShortcuts(std::shared_ptr<Shortcut>);
114	void onSubitems(const QString& menu, std::function<void(std::shared_ptr<Shortcut>)> func);
115	void onSubitems(const QString& menu, std::function<void(const QString&)> func);
116	void updateKey(std::shared_ptr<Shortcut> item, int keySequence);
117
118	QHash<QString, std::shared_ptr<Shortcut>> m_items;
119	QHash<int, std::shared_ptr<Shortcut>> m_buttons;
120	QMap<std::pair<int, GamepadAxisEvent::Direction>, std::shared_ptr<Shortcut>> m_axes;
121	QHash<int, std::shared_ptr<Shortcut>> m_heldKeys;
122	ActionMapper* m_actions = nullptr;
123	ConfigController* m_config = nullptr;
124	QString m_profileName;
125	const InputProfile* m_profile = nullptr;
126};
127
128}