Qt: Start shaking out GBAKey

This commit is contained in:
Vicki Pfau 2023-01-18 22:53:11 -08:00
parent 8ec856e10c
commit 547c9269fa
13 changed files with 62 additions and 65 deletions

View File

@ -262,7 +262,7 @@ void GBAKeyEditor::refresh() {
#endif #endif
} }
void GBAKeyEditor::lookupBinding(const mInputMap* map, KeyEditor* keyEditor, GBAKey key) { void GBAKeyEditor::lookupBinding(const mInputMap* map, KeyEditor* keyEditor, int key) {
#ifdef BUILD_SDL #ifdef BUILD_SDL
if (m_type == SDL_BINDING_BUTTON) { if (m_type == SDL_BINDING_BUTTON) {
int value = mInputQueryBinding(map, m_type, key); int value = mInputQueryBinding(map, m_type, key);
@ -277,14 +277,14 @@ void GBAKeyEditor::lookupBinding(const mInputMap* map, KeyEditor* keyEditor, GBA
void GBAKeyEditor::lookupAxes(const mInputMap* map) { void GBAKeyEditor::lookupAxes(const mInputMap* map) {
mInputEnumerateAxes(map, m_type, [](int axis, const mInputAxis* description, void* user) { mInputEnumerateAxes(map, m_type, [](int axis, const mInputAxis* description, void* user) {
GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user); GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
if (description->highDirection != GBA_KEY_NONE) { if (description->highDirection != -1) {
KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->highDirection)); KeyEditor* key = self->keyById(description->highDirection);
if (key) { if (key) {
key->setValueAxis(axis, GamepadAxisEvent::POSITIVE); key->setValueAxis(axis, GamepadAxisEvent::POSITIVE);
} }
} }
if (description->lowDirection != GBA_KEY_NONE) { if (description->lowDirection != -1) {
KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->lowDirection)); KeyEditor* key = self->keyById(description->lowDirection);
if (key) { if (key) {
key->setValueAxis(axis, GamepadAxisEvent::NEGATIVE); key->setValueAxis(axis, GamepadAxisEvent::NEGATIVE);
} }
@ -297,25 +297,25 @@ void GBAKeyEditor::lookupHats(const mInputMap* map) {
int i = 0; int i = 0;
while (mInputQueryHat(map, m_type, i, &bindings)) { while (mInputQueryHat(map, m_type, i, &bindings)) {
if (bindings.up >= 0) { if (bindings.up >= 0) {
KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.up)); KeyEditor* key = keyById(bindings.up);
if (key) { if (key) {
key->setValueHat(i, GamepadHatEvent::UP); key->setValueHat(i, GamepadHatEvent::UP);
} }
} }
if (bindings.right >= 0) { if (bindings.right >= 0) {
KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.right)); KeyEditor* key = keyById(bindings.right);
if (key) { if (key) {
key->setValueHat(i, GamepadHatEvent::RIGHT); key->setValueHat(i, GamepadHatEvent::RIGHT);
} }
} }
if (bindings.down >= 0) { if (bindings.down >= 0) {
KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.down)); KeyEditor* key = keyById(bindings.down);
if (key) { if (key) {
key->setValueHat(i, GamepadHatEvent::DOWN); key->setValueHat(i, GamepadHatEvent::DOWN);
} }
} }
if (bindings.left >= 0) { if (bindings.left >= 0) {
KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.left)); KeyEditor* key = keyById(bindings.left);
if (key) { if (key) {
key->setValueHat(i, GamepadHatEvent::LEFT); key->setValueHat(i, GamepadHatEvent::LEFT);
} }
@ -325,7 +325,7 @@ void GBAKeyEditor::lookupHats(const mInputMap* map) {
} }
#endif #endif
void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) { void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, int key) {
InputMapper mapper = m_controller->mapper(m_type); InputMapper mapper = m_controller->mapper(m_type);
#ifdef BUILD_SDL #ifdef BUILD_SDL
if (m_type == SDL_BINDING_BUTTON && keyEditor->axis() >= 0) { if (m_type == SDL_BINDING_BUTTON && keyEditor->axis() >= 0) {
@ -361,7 +361,7 @@ void GBAKeyEditor::selectGamepad(int index) {
} }
#endif #endif
KeyEditor* GBAKeyEditor::keyById(GBAKey key) { KeyEditor* GBAKeyEditor::keyById(int key) {
switch (key) { switch (key) {
case GBA_KEY_UP: case GBA_KEY_UP:
return m_keyDU; return m_keyDU;

View File

@ -54,8 +54,8 @@ private:
void setLocation(QWidget* widget, qreal x, qreal y); void setLocation(QWidget* widget, qreal x, qreal y);
void lookupBinding(const mInputMap*, KeyEditor*, GBAKey); void lookupBinding(const mInputMap*, KeyEditor*, int key);
void bindKey(const KeyEditor*, GBAKey); void bindKey(const KeyEditor*, int key);
bool findFocus(KeyEditor* needle = nullptr); bool findFocus(KeyEditor* needle = nullptr);
@ -64,7 +64,7 @@ private:
void lookupHats(const mInputMap*); void lookupHats(const mInputMap*);
#endif #endif
KeyEditor* keyById(GBAKey); KeyEditor* keyById(int);
QComboBox* m_profileSelect = nullptr; QComboBox* m_profileSelect = nullptr;
QWidget* m_clear = nullptr; QWidget* m_clear = nullptr;

View File

@ -383,8 +383,8 @@ void InputController::setGyroSensitivity(float sensitivity) {
#endif #endif
} }
GBAKey InputController::mapKeyboard(int key) const { int InputController::mapKeyboard(int key) const {
return static_cast<GBAKey>(mInputMapKey(&m_inputMap, KEYBOARD, key)); return mInputMapKey(&m_inputMap, KEYBOARD, key);
} }
void InputController::updateJoysticks() { void InputController::updateJoysticks() {
@ -408,8 +408,8 @@ int InputController::pollEvents() {
int i; int i;
QReadLocker l(&m_eventsLock); QReadLocker l(&m_eventsLock);
for (i = 0; i < numButtons; ++i) { for (i = 0; i < numButtons; ++i) {
GBAKey key = static_cast<GBAKey>(mInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i)); int key = mInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i);
if (key == GBA_KEY_NONE) { if (key == -1) {
continue; continue;
} }
if (hasPendingEvent(key)) { if (hasPendingEvent(key)) {
@ -430,8 +430,8 @@ int InputController::pollEvents() {
for (i = 0; i < numAxes; ++i) { for (i = 0; i < numAxes; ++i) {
int value = SDL_JoystickGetAxis(joystick, i); int value = SDL_JoystickGetAxis(joystick, i);
enum GBAKey key = static_cast<GBAKey>(mInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value)); int key = mInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value);
if (key != GBA_KEY_NONE) { if (key != -1) {
activeButtons |= 1 << key; activeButtons |= 1 << key;
} }
} }
@ -557,16 +557,16 @@ void InputController::testGamepad(int type) {
bool newlyAboveThreshold = activeAxes.contains(axis); bool newlyAboveThreshold = activeAxes.contains(axis);
if (newlyAboveThreshold) { if (newlyAboveThreshold) {
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this); GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this);
postPendingEvent(event->gbaKey()); postPendingEvent(event->platformKey());
sendGamepadEvent(event); sendGamepadEvent(event);
if (!event->isAccepted()) { if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->platformKey());
} }
} }
} }
for (auto axis : oldAxes) { for (auto axis : oldAxes) {
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this); GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this);
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->platformKey());
sendGamepadEvent(event); sendGamepadEvent(event);
} }
@ -579,15 +579,15 @@ void InputController::testGamepad(int type) {
for (int button : activeButtons) { for (int button : activeButtons) {
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this); GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this);
postPendingEvent(event->gbaKey()); postPendingEvent(event->platformKey());
sendGamepadEvent(event); sendGamepadEvent(event);
if (!event->isAccepted()) { if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->platformKey());
} }
} }
for (int button : oldButtons) { for (int button : oldButtons) {
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this); GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this);
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->platformKey());
sendGamepadEvent(event); sendGamepadEvent(event);
} }
@ -596,15 +596,15 @@ void InputController::testGamepad(int type) {
for (auto& hat : activeHats) { for (auto& hat : activeHats) {
GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Down(), hat.first, hat.second, type, this); GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Down(), hat.first, hat.second, type, this);
postPendingEvent(event->gbaKey()); postPendingEvent(event->platformKey());
sendGamepadEvent(event); sendGamepadEvent(event);
if (!event->isAccepted()) { if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->platformKey());
} }
} }
for (auto& hat : oldHats) { for (auto& hat : oldHats) {
GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Up(), hat.first, hat.second, type, this); GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Up(), hat.first, hat.second, type, this);
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->platformKey());
sendGamepadEvent(event); sendGamepadEvent(event);
} }
} }
@ -622,15 +622,15 @@ void InputController::sendGamepadEvent(QEvent* event) {
QApplication::postEvent(focusWidget, event, Qt::HighEventPriority); QApplication::postEvent(focusWidget, event, Qt::HighEventPriority);
} }
void InputController::postPendingEvent(GBAKey key) { void InputController::postPendingEvent(int key) {
m_pendingEvents.insert(key); m_pendingEvents.insert(key);
} }
void InputController::clearPendingEvent(GBAKey key) { void InputController::clearPendingEvent(int key) {
m_pendingEvents.remove(key); m_pendingEvents.remove(key);
} }
bool InputController::hasPendingEvent(GBAKey key) const { bool InputController::hasPendingEvent(int key) const {
return m_pendingEvents.contains(key); return m_pendingEvents.contains(key);
} }

View File

@ -1,4 +1,4 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau /* Copyright (c) 2013-2023 Jeffrey Pfau
* *
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -19,8 +19,8 @@
#include <memory> #include <memory>
#include <mgba/core/input.h>
#include <mgba/gba/interface.h> #include <mgba/gba/interface.h>
#include <mgba/internal/gba/input.h>
#ifdef BUILD_SDL #ifdef BUILD_SDL
#include "platform/sdl/sdl-events.h" #include "platform/sdl/sdl-events.h"
@ -63,7 +63,7 @@ public:
void saveProfile(uint32_t type, const QString& profile); void saveProfile(uint32_t type, const QString& profile);
const char* profileForType(uint32_t type); const char* profileForType(uint32_t type);
GBAKey mapKeyboard(int key) const; int mapKeyboard(int key) const;
mInputMap* map() { return &m_inputMap; } mInputMap* map() { return &m_inputMap; }
const mInputMap* map() const { return &m_inputMap; } const mInputMap* map() const { return &m_inputMap; }
@ -132,9 +132,9 @@ private slots:
void teardownCam(); void teardownCam();
private: private:
void postPendingEvent(GBAKey); void postPendingEvent(int);
void clearPendingEvent(GBAKey); void clearPendingEvent(int);
bool hasPendingEvent(GBAKey) const; bool hasPendingEvent(int) const;
void sendGamepadEvent(QEvent*); void sendGamepadEvent(QEvent*);
struct InputControllerLux : GBALuminanceSource { struct InputControllerLux : GBALuminanceSource {
@ -180,7 +180,7 @@ private:
QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats; QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats;
QTimer m_gamepadTimer{nullptr}; QTimer m_gamepadTimer{nullptr};
QSet<GBAKey> m_pendingEvents; QSet<int> m_pendingEvents;
QReadWriteLock m_eventsLock; QReadWriteLock m_eventsLock;
}; };

View File

@ -8,6 +8,7 @@
#include "input/GamepadAxisEvent.h" #include "input/GamepadAxisEvent.h"
#include <mgba/gba/interface.h> #include <mgba/gba/interface.h>
#include <mgba/internal/gba/input.h>
namespace QGBA { namespace QGBA {

View File

@ -17,6 +17,7 @@
#include <QPainter> #include <QPainter>
#include <mgba/core/serialize.h> #include <mgba/core/serialize.h>
#include <mgba/internal/gba/input.h>
#include <mgba-util/memory.h> #include <mgba-util/memory.h>
#include <mgba-util/vfs.h> #include <mgba-util/vfs.h>
@ -131,13 +132,13 @@ bool LoadSaveState::eventFilter(QObject* object, QEvent* event) {
if (event->type() == GamepadButtonEvent::Down() || event->type() == GamepadAxisEvent::Type()) { if (event->type() == GamepadButtonEvent::Down() || event->type() == GamepadAxisEvent::Type()) {
int column = m_currentFocus % 3; int column = m_currentFocus % 3;
int row = m_currentFocus - column; int row = m_currentFocus - column;
GBAKey key = GBA_KEY_NONE; int key = -1;
if (event->type() == GamepadButtonEvent::Down()) { if (event->type() == GamepadButtonEvent::Down()) {
key = static_cast<GamepadButtonEvent*>(event)->gbaKey(); key = static_cast<GamepadButtonEvent*>(event)->platformKey();
} else if (event->type() == GamepadAxisEvent::Type()) { } else if (event->type() == GamepadAxisEvent::Type()) {
GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event); GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
if (gae->isNew()) { if (gae->isNew()) {
key = gae->gbaKey(); key = gae->platformKey();
} else { } else {
return false; return false;
} }

View File

@ -76,6 +76,7 @@
#include <mgba/internal/gba/gba.h> #include <mgba/internal/gba/gba.h>
#endif #endif
#include <mgba/feature/commandline.h> #include <mgba/feature/commandline.h>
#include <mgba/internal/gba/input.h>
#include <mgba-util/vfs.h> #include <mgba-util/vfs.h>
#include <mgba-util/convolve.h> #include <mgba-util/convolve.h>
@ -631,8 +632,8 @@ void Window::keyPressEvent(QKeyEvent* event) {
QWidget::keyPressEvent(event); QWidget::keyPressEvent(event);
return; return;
} }
GBAKey key = m_inputController.mapKeyboard(event->key()); int key = m_inputController.mapKeyboard(event->key());
if (key == GBA_KEY_NONE) { if (key == -1) {
QWidget::keyPressEvent(event); QWidget::keyPressEvent(event);
return; return;
} }
@ -647,8 +648,8 @@ void Window::keyReleaseEvent(QKeyEvent* event) {
QWidget::keyReleaseEvent(event); QWidget::keyReleaseEvent(event);
return; return;
} }
GBAKey key = m_inputController.mapKeyboard(event->key()); int key = m_inputController.mapKeyboard(event->key());
if (key == GBA_KEY_NONE) { if (key == -1) {
QWidget::keyPressEvent(event); QWidget::keyPressEvent(event);
return; return;
} }

View File

@ -16,11 +16,11 @@ GamepadAxisEvent::GamepadAxisEvent(int axis, Direction direction, bool isNew, in
, m_axis(axis) , m_axis(axis)
, m_direction(direction) , m_direction(direction)
, m_isNew(isNew) , m_isNew(isNew)
, m_key(GBA_KEY_NONE) , m_key(-1)
{ {
ignore(); ignore();
if (controller) { if (controller) {
m_key = static_cast<GBAKey>(mInputMapAxis(controller->map(), type, axis, direction * INT_MAX)); m_key = mInputMapAxis(controller->map(), type, axis, direction * INT_MAX);
} }
} }

View File

@ -7,8 +7,6 @@
#include <QEvent> #include <QEvent>
#include <mgba/internal/gba/input.h>
namespace QGBA { namespace QGBA {
class InputController; class InputController;
@ -26,7 +24,7 @@ public:
int axis() const { return m_axis; } int axis() const { return m_axis; }
Direction direction() const { return m_direction; } Direction direction() const { return m_direction; }
bool isNew() const { return m_isNew; } bool isNew() const { return m_isNew; }
GBAKey gbaKey() const { return m_key; } int platformKey() const { return m_key; }
static enum Type Type(); static enum Type Type();
@ -36,7 +34,7 @@ private:
int m_axis; int m_axis;
Direction m_direction; Direction m_direction;
bool m_isNew; bool m_isNew;
GBAKey m_key; int m_key;
}; };
} }

View File

@ -15,11 +15,11 @@ QEvent::Type GamepadButtonEvent::s_upType = QEvent::None;
GamepadButtonEvent::GamepadButtonEvent(QEvent::Type pressType, int button, int type, InputController* controller) GamepadButtonEvent::GamepadButtonEvent(QEvent::Type pressType, int button, int type, InputController* controller)
: QEvent(pressType) : QEvent(pressType)
, m_button(button) , m_button(button)
, m_key(GBA_KEY_NONE) , m_key(-1)
{ {
ignore(); ignore();
if (controller) { if (controller) {
m_key = static_cast<GBAKey>(mInputMapKey(controller->map(), type, button)); m_key = mInputMapKey(controller->map(), type, button);
} }
} }

View File

@ -1,4 +1,4 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau /* Copyright (c) 2013-2023 Jeffrey Pfau
* *
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -7,8 +7,6 @@
#include <QEvent> #include <QEvent>
#include <mgba/internal/gba/input.h>
namespace QGBA { namespace QGBA {
class InputController; class InputController;
@ -18,7 +16,7 @@ public:
GamepadButtonEvent(Type pressType, int button, int type, InputController* controller = nullptr); GamepadButtonEvent(Type pressType, int button, int type, InputController* controller = nullptr);
int value() const { return m_button; } int value() const { return m_button; }
GBAKey gbaKey() const { return m_key; } int platformKey() const { return m_key; }
static Type Down(); static Type Down();
static Type Up(); static Type Up();
@ -28,7 +26,7 @@ private:
static Type s_upType; static Type s_upType;
int m_button; int m_button;
GBAKey m_key; int m_key;
}; };
} }

View File

@ -16,11 +16,11 @@ GamepadHatEvent::GamepadHatEvent(QEvent::Type pressType, int hatId, Direction di
: QEvent(pressType) : QEvent(pressType)
, m_hatId(hatId) , m_hatId(hatId)
, m_direction(direction) , m_direction(direction)
, m_key(GBA_KEY_NONE) , m_key(-1)
{ {
ignore(); ignore();
if (controller) { if (controller) {
m_key = static_cast<GBAKey>(mInputMapHat(controller->map(), type, hatId, direction)); m_key = mInputMapHat(controller->map(), type, hatId, direction);
} }
} }

View File

@ -7,8 +7,6 @@
#include <QEvent> #include <QEvent>
#include <mgba/internal/gba/input.h>
namespace QGBA { namespace QGBA {
class InputController; class InputController;
@ -27,7 +25,7 @@ public:
int hatId() const { return m_hatId; } int hatId() const { return m_hatId; }
Direction direction() const { return m_direction; } Direction direction() const { return m_direction; }
GBAKey gbaKey() const { return m_key; } int platformKey() const { return m_key; }
static Type Down(); static Type Down();
static Type Up(); static Type Up();
@ -38,7 +36,7 @@ private:
int m_hatId; int m_hatId;
Direction m_direction; Direction m_direction;
GBAKey m_key; int m_key;
}; };
} }