mirror of https://github.com/mgba-emu/mgba.git
Qt: Hook up gamepad to scripting
This commit is contained in:
parent
dfe2f62f16
commit
0dd7cfd44a
|
@ -44,7 +44,7 @@ InputController::InputController(int playerId, QWidget* topLevel, QObject* paren
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
m_gamepadTimer.setInterval(50);
|
m_gamepadTimer.setInterval(15);
|
||||||
m_gamepadTimer.start();
|
m_gamepadTimer.start();
|
||||||
|
|
||||||
#ifdef BUILD_QT_MULTIMEDIA
|
#ifdef BUILD_QT_MULTIMEDIA
|
||||||
|
@ -336,6 +336,7 @@ void InputController::update() {
|
||||||
loadProfile(driver->type(), newProfile);
|
loadProfile(driver->type(), newProfile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
emit updated();
|
||||||
}
|
}
|
||||||
|
|
||||||
int InputController::pollEvents() {
|
int InputController::pollEvents() {
|
||||||
|
|
|
@ -56,6 +56,8 @@ public:
|
||||||
|
|
||||||
void addInputDriver(std::shared_ptr<InputDriver>);
|
void addInputDriver(std::shared_ptr<InputDriver>);
|
||||||
|
|
||||||
|
int playerId() const { return m_playerId; }
|
||||||
|
|
||||||
void setConfiguration(ConfigController* config);
|
void setConfiguration(ConfigController* config);
|
||||||
void saveConfiguration();
|
void saveConfiguration();
|
||||||
bool loadConfiguration(uint32_t type);
|
bool loadConfiguration(uint32_t type);
|
||||||
|
@ -103,6 +105,7 @@ public:
|
||||||
GBALuminanceSource* luminance() { return &m_lux; }
|
GBALuminanceSource* luminance() { return &m_lux; }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
void updated();
|
||||||
void profileLoaded(const QString& profile);
|
void profileLoaded(const QString& profile);
|
||||||
void luminanceValueChanged(int value);
|
void luminanceValueChanged(int value);
|
||||||
|
|
||||||
|
|
|
@ -627,6 +627,7 @@ void Window::consoleOpen() {
|
||||||
void Window::scriptingOpen() {
|
void Window::scriptingOpen() {
|
||||||
if (!m_scripting) {
|
if (!m_scripting) {
|
||||||
m_scripting = std::make_unique<ScriptingController>();
|
m_scripting = std::make_unique<ScriptingController>();
|
||||||
|
m_scripting->setInputController(&m_inputController);
|
||||||
m_shortcutController->setScriptingController(m_scripting.get());
|
m_shortcutController->setScriptingController(m_scripting.get());
|
||||||
if (m_controller) {
|
if (m_controller) {
|
||||||
m_scripting->setController(m_controller);
|
m_scripting->setController(m_controller);
|
||||||
|
|
|
@ -12,8 +12,10 @@
|
||||||
|
|
||||||
#include "CoreController.h"
|
#include "CoreController.h"
|
||||||
#include "Display.h"
|
#include "Display.h"
|
||||||
|
#include "input/Gamepad.h"
|
||||||
#include "input/GamepadButtonEvent.h"
|
#include "input/GamepadButtonEvent.h"
|
||||||
#include "input/GamepadHatEvent.h"
|
#include "input/GamepadHatEvent.h"
|
||||||
|
#include "InputController.h"
|
||||||
#include "scripting/ScriptingTextBuffer.h"
|
#include "scripting/ScriptingTextBuffer.h"
|
||||||
#include "scripting/ScriptingTextBufferModel.h"
|
#include "scripting/ScriptingTextBufferModel.h"
|
||||||
|
|
||||||
|
@ -48,12 +50,15 @@ ScriptingController::ScriptingController(QObject* parent)
|
||||||
m_bufferModel = new ScriptingTextBufferModel(this);
|
m_bufferModel = new ScriptingTextBufferModel(this);
|
||||||
QObject::connect(m_bufferModel, &ScriptingTextBufferModel::textBufferCreated, this, &ScriptingController::textBufferCreated);
|
QObject::connect(m_bufferModel, &ScriptingTextBufferModel::textBufferCreated, this, &ScriptingController::textBufferCreated);
|
||||||
|
|
||||||
|
mScriptGamepadInit(&m_gamepad);
|
||||||
|
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptingController::~ScriptingController() {
|
ScriptingController::~ScriptingController() {
|
||||||
clearController();
|
clearController();
|
||||||
mScriptContextDeinit(&m_scriptContext);
|
mScriptContextDeinit(&m_scriptContext);
|
||||||
|
mScriptGamepadDeinit(&m_gamepad);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptingController::setController(std::shared_ptr<CoreController> controller) {
|
void ScriptingController::setController(std::shared_ptr<CoreController> controller) {
|
||||||
|
@ -70,6 +75,14 @@ void ScriptingController::setController(std::shared_ptr<CoreController> controll
|
||||||
connect(m_controller.get(), &CoreController::stopping, this, &ScriptingController::clearController);
|
connect(m_controller.get(), &CoreController::stopping, this, &ScriptingController::clearController);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptingController::setInputController(InputController* input) {
|
||||||
|
if (m_inputController) {
|
||||||
|
m_inputController->disconnect(this);
|
||||||
|
}
|
||||||
|
m_inputController = input;
|
||||||
|
connect(m_inputController, &InputController::updated, this, &ScriptingController::updateGamepad);
|
||||||
|
}
|
||||||
|
|
||||||
bool ScriptingController::loadFile(const QString& path) {
|
bool ScriptingController::loadFile(const QString& path) {
|
||||||
VFileDevice vf(path, QIODevice::ReadOnly);
|
VFileDevice vf(path, QIODevice::ReadOnly);
|
||||||
if (!vf.isOpen()) {
|
if (!vf.isOpen()) {
|
||||||
|
@ -204,6 +217,7 @@ void ScriptingController::event(QObject* obj, QEvent* event) {
|
||||||
}
|
}
|
||||||
if (type == GamepadHatEvent::Type()) {
|
if (type == GamepadHatEvent::Type()) {
|
||||||
struct mScriptGamepadHatEvent ev{mSCRIPT_EV_TYPE_GAMEPAD_HAT};
|
struct mScriptGamepadHatEvent ev{mSCRIPT_EV_TYPE_GAMEPAD_HAT};
|
||||||
|
updateGamepad();
|
||||||
auto gamepadEvent = static_cast<GamepadHatEvent*>(event);
|
auto gamepadEvent = static_cast<GamepadHatEvent*>(event);
|
||||||
ev.pad = 0;
|
ev.pad = 0;
|
||||||
ev.hat = gamepadEvent->hatId();
|
ev.hat = gamepadEvent->hatId();
|
||||||
|
@ -212,6 +226,60 @@ void ScriptingController::event(QObject* obj, QEvent* event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptingController::updateGamepad() {
|
||||||
|
InputDriver* driver = m_inputController->gamepadDriver();
|
||||||
|
if (!driver) {
|
||||||
|
detachGamepad();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Gamepad* gamepad = driver->activeGamepad();
|
||||||
|
if (!gamepad) {
|
||||||
|
detachGamepad();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
attachGamepad();
|
||||||
|
|
||||||
|
QList<bool> buttons = gamepad->currentButtons();
|
||||||
|
int nButtons = gamepad->buttonCount();
|
||||||
|
mScriptGamepadSetButtonCount(&m_gamepad, nButtons);
|
||||||
|
for (int i = 0; i < nButtons; ++i) {
|
||||||
|
mScriptGamepadSetButton(&m_gamepad, i, buttons.at(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<int16_t> axes = gamepad->currentAxes();
|
||||||
|
int nAxes = gamepad->axisCount();
|
||||||
|
mScriptGamepadSetAxisCount(&m_gamepad, nAxes);
|
||||||
|
for (int i = 0; i < nAxes; ++i) {
|
||||||
|
mScriptGamepadSetAxis(&m_gamepad, i, axes.at(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<GamepadHatEvent::Direction> hats = gamepad->currentHats();
|
||||||
|
int nHats = gamepad->hatCount();
|
||||||
|
mScriptGamepadSetHatCount(&m_gamepad, nHats);
|
||||||
|
for (int i = 0; i < nHats; ++i) {
|
||||||
|
mScriptGamepadSetHat(&m_gamepad, i, hats.at(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptingController::attachGamepad() {
|
||||||
|
mScriptGamepad* pad = mScriptContextGamepadLookup(&m_scriptContext, 0);
|
||||||
|
if (pad == &m_gamepad) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pad) {
|
||||||
|
mScriptContextGamepadDetach(&m_scriptContext, 0);
|
||||||
|
}
|
||||||
|
mScriptContextGamepadAttach(&m_scriptContext, &m_gamepad);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptingController::detachGamepad() {
|
||||||
|
mScriptGamepad* pad = mScriptContextGamepadLookup(&m_scriptContext, 0);
|
||||||
|
if (pad != &m_gamepad) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mScriptContextGamepadDetach(&m_scriptContext, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptingController::init() {
|
void ScriptingController::init() {
|
||||||
mScriptContextInit(&m_scriptContext);
|
mScriptContextInit(&m_scriptContext);
|
||||||
mScriptContextAttachStdlib(&m_scriptContext);
|
mScriptContextAttachStdlib(&m_scriptContext);
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include <mgba/script/context.h>
|
#include <mgba/script/context.h>
|
||||||
|
#include <mgba/script/input.h>
|
||||||
#include <mgba/core/scripting.h>
|
#include <mgba/core/scripting.h>
|
||||||
|
|
||||||
#include "VFileDevice.h"
|
#include "VFileDevice.h"
|
||||||
|
@ -21,6 +22,7 @@ class QTextDocument;
|
||||||
namespace QGBA {
|
namespace QGBA {
|
||||||
|
|
||||||
class CoreController;
|
class CoreController;
|
||||||
|
class InputController;
|
||||||
class ScriptingTextBuffer;
|
class ScriptingTextBuffer;
|
||||||
class ScriptingTextBufferModel;
|
class ScriptingTextBufferModel;
|
||||||
|
|
||||||
|
@ -32,6 +34,7 @@ public:
|
||||||
~ScriptingController();
|
~ScriptingController();
|
||||||
|
|
||||||
void setController(std::shared_ptr<CoreController> controller);
|
void setController(std::shared_ptr<CoreController> controller);
|
||||||
|
void setInputController(InputController* controller);
|
||||||
|
|
||||||
bool loadFile(const QString& path);
|
bool loadFile(const QString& path);
|
||||||
bool load(VFileDevice& vf, const QString& name);
|
bool load(VFileDevice& vf, const QString& name);
|
||||||
|
@ -55,9 +58,15 @@ public slots:
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject*, QEvent*) override;
|
bool eventFilter(QObject*, QEvent*) override;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void updateGamepad();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
void attachGamepad();
|
||||||
|
void detachGamepad();
|
||||||
|
|
||||||
static uint32_t qtToScriptingKey(const QKeyEvent*);
|
static uint32_t qtToScriptingKey(const QKeyEvent*);
|
||||||
static uint16_t qtToScriptingModifiers(Qt::KeyboardModifiers);
|
static uint16_t qtToScriptingModifiers(Qt::KeyboardModifiers);
|
||||||
|
|
||||||
|
@ -71,7 +80,10 @@ private:
|
||||||
QHash<QString, mScriptEngineContext*> m_engines;
|
QHash<QString, mScriptEngineContext*> m_engines;
|
||||||
ScriptingTextBufferModel* m_bufferModel;
|
ScriptingTextBufferModel* m_bufferModel;
|
||||||
|
|
||||||
|
mScriptGamepad m_gamepad;
|
||||||
|
|
||||||
std::shared_ptr<CoreController> m_controller;
|
std::shared_ptr<CoreController> m_controller;
|
||||||
|
InputController* m_inputController = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue