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

View File

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

View File

@ -383,8 +383,8 @@ void InputController::setGyroSensitivity(float sensitivity) {
#endif
}
GBAKey InputController::mapKeyboard(int key) const {
return static_cast<GBAKey>(mInputMapKey(&m_inputMap, KEYBOARD, key));
int InputController::mapKeyboard(int key) const {
return mInputMapKey(&m_inputMap, KEYBOARD, key);
}
void InputController::updateJoysticks() {
@ -408,8 +408,8 @@ int InputController::pollEvents() {
int i;
QReadLocker l(&m_eventsLock);
for (i = 0; i < numButtons; ++i) {
GBAKey key = static_cast<GBAKey>(mInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i));
if (key == GBA_KEY_NONE) {
int key = mInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i);
if (key == -1) {
continue;
}
if (hasPendingEvent(key)) {
@ -430,8 +430,8 @@ int InputController::pollEvents() {
for (i = 0; i < numAxes; ++i) {
int value = SDL_JoystickGetAxis(joystick, i);
enum GBAKey key = static_cast<GBAKey>(mInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value));
if (key != GBA_KEY_NONE) {
int key = mInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value);
if (key != -1) {
activeButtons |= 1 << key;
}
}
@ -557,16 +557,16 @@ void InputController::testGamepad(int type) {
bool newlyAboveThreshold = activeAxes.contains(axis);
if (newlyAboveThreshold) {
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this);
postPendingEvent(event->gbaKey());
postPendingEvent(event->platformKey());
sendGamepadEvent(event);
if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey());
clearPendingEvent(event->platformKey());
}
}
}
for (auto axis : oldAxes) {
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this);
clearPendingEvent(event->gbaKey());
clearPendingEvent(event->platformKey());
sendGamepadEvent(event);
}
@ -579,15 +579,15 @@ void InputController::testGamepad(int type) {
for (int button : activeButtons) {
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this);
postPendingEvent(event->gbaKey());
postPendingEvent(event->platformKey());
sendGamepadEvent(event);
if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey());
clearPendingEvent(event->platformKey());
}
}
for (int button : oldButtons) {
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this);
clearPendingEvent(event->gbaKey());
clearPendingEvent(event->platformKey());
sendGamepadEvent(event);
}
@ -596,15 +596,15 @@ void InputController::testGamepad(int type) {
for (auto& hat : activeHats) {
GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Down(), hat.first, hat.second, type, this);
postPendingEvent(event->gbaKey());
postPendingEvent(event->platformKey());
sendGamepadEvent(event);
if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey());
clearPendingEvent(event->platformKey());
}
}
for (auto& hat : oldHats) {
GamepadHatEvent* event = new GamepadHatEvent(GamepadHatEvent::Up(), hat.first, hat.second, type, this);
clearPendingEvent(event->gbaKey());
clearPendingEvent(event->platformKey());
sendGamepadEvent(event);
}
}
@ -622,15 +622,15 @@ void InputController::sendGamepadEvent(QEvent* event) {
QApplication::postEvent(focusWidget, event, Qt::HighEventPriority);
}
void InputController::postPendingEvent(GBAKey key) {
void InputController::postPendingEvent(int key) {
m_pendingEvents.insert(key);
}
void InputController::clearPendingEvent(GBAKey key) {
void InputController::clearPendingEvent(int key) {
m_pendingEvents.remove(key);
}
bool InputController::hasPendingEvent(GBAKey key) const {
bool InputController::hasPendingEvent(int key) const {
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
* License, v. 2.0. If a copy of the MPL was not distributed with this
@ -19,8 +19,8 @@
#include <memory>
#include <mgba/core/input.h>
#include <mgba/gba/interface.h>
#include <mgba/internal/gba/input.h>
#ifdef BUILD_SDL
#include "platform/sdl/sdl-events.h"
@ -63,7 +63,7 @@ public:
void saveProfile(uint32_t type, const QString& profile);
const char* profileForType(uint32_t type);
GBAKey mapKeyboard(int key) const;
int mapKeyboard(int key) const;
mInputMap* map() { return &m_inputMap; }
const mInputMap* map() const { return &m_inputMap; }
@ -132,9 +132,9 @@ private slots:
void teardownCam();
private:
void postPendingEvent(GBAKey);
void clearPendingEvent(GBAKey);
bool hasPendingEvent(GBAKey) const;
void postPendingEvent(int);
void clearPendingEvent(int);
bool hasPendingEvent(int) const;
void sendGamepadEvent(QEvent*);
struct InputControllerLux : GBALuminanceSource {
@ -180,7 +180,7 @@ private:
QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats;
QTimer m_gamepadTimer{nullptr};
QSet<GBAKey> m_pendingEvents;
QSet<int> m_pendingEvents;
QReadWriteLock m_eventsLock;
};

View File

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

View File

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

View File

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

View File

@ -16,11 +16,11 @@ GamepadAxisEvent::GamepadAxisEvent(int axis, Direction direction, bool isNew, in
, m_axis(axis)
, m_direction(direction)
, m_isNew(isNew)
, m_key(GBA_KEY_NONE)
, m_key(-1)
{
ignore();
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 <mgba/internal/gba/input.h>
namespace QGBA {
class InputController;
@ -26,7 +24,7 @@ public:
int axis() const { return m_axis; }
Direction direction() const { return m_direction; }
bool isNew() const { return m_isNew; }
GBAKey gbaKey() const { return m_key; }
int platformKey() const { return m_key; }
static enum Type Type();
@ -36,7 +34,7 @@ private:
int m_axis;
Direction m_direction;
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)
: QEvent(pressType)
, m_button(button)
, m_key(GBA_KEY_NONE)
, m_key(-1)
{
ignore();
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
* License, v. 2.0. If a copy of the MPL was not distributed with this
@ -7,8 +7,6 @@
#include <QEvent>
#include <mgba/internal/gba/input.h>
namespace QGBA {
class InputController;
@ -18,7 +16,7 @@ public:
GamepadButtonEvent(Type pressType, int button, int type, InputController* controller = nullptr);
int value() const { return m_button; }
GBAKey gbaKey() const { return m_key; }
int platformKey() const { return m_key; }
static Type Down();
static Type Up();
@ -28,7 +26,7 @@ private:
static Type s_upType;
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)
, m_hatId(hatId)
, m_direction(direction)
, m_key(GBA_KEY_NONE)
, m_key(-1)
{
ignore();
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 <mgba/internal/gba/input.h>
namespace QGBA {
class InputController;
@ -27,7 +25,7 @@ public:
int hatId() const { return m_hatId; }
Direction direction() const { return m_direction; }
GBAKey gbaKey() const { return m_key; }
int platformKey() const { return m_key; }
static Type Down();
static Type Up();
@ -38,7 +36,7 @@ private:
int m_hatId;
Direction m_direction;
GBAKey m_key;
int m_key;
};
}