Qt: Make Gamepad be a shared_ptr

This commit is contained in:
Vicki Pfau 2023-12-20 22:59:35 -08:00
parent 5bd5a8d998
commit ffacbcfeea
7 changed files with 22 additions and 20 deletions

View File

@ -345,7 +345,7 @@ void InputController::update() {
int InputController::pollEvents() {
int activeButtons = 0;
for (auto& pad : gamepads()) {
InputMapper im(mapper(pad));
InputMapper im(mapper(pad.get()));
activeButtons |= im.mapKeys(pad->currentButtons());
activeButtons |= im.mapAxes(pad->currentAxes());
activeButtons |= im.mapHats(pad->currentHats());
@ -358,7 +358,7 @@ int InputController::pollEvents() {
return activeButtons;
}
Gamepad* InputController::gamepad(uint32_t type) {
std::shared_ptr<Gamepad> InputController::gamepad(uint32_t type) {
auto driver = m_inputDrivers.value(type);
if (!driver) {
return nullptr;
@ -370,13 +370,13 @@ Gamepad* InputController::gamepad(uint32_t type) {
return driver->activeGamepad();
}
QList<Gamepad*> InputController::gamepads() {
QList<Gamepad*> pads;
QList<std::shared_ptr<Gamepad>> InputController::gamepads() {
QList<std::shared_ptr<Gamepad>> pads;
for (auto& driver : m_inputDrivers) {
if (!driver->supportsGamepads()) {
continue;
}
Gamepad* pad = driver->activeGamepad();
std::shared_ptr<Gamepad> pad = driver->activeGamepad();
if (pad) {
pads.append(pad);
}
@ -386,7 +386,7 @@ QList<Gamepad*> InputController::gamepads() {
QSet<int> InputController::activeGamepadButtons(uint32_t type) {
QSet<int> activeButtons;
Gamepad* pad = gamepad(type);
std::shared_ptr<Gamepad> pad = gamepad(type);
if (!pad) {
return {};
}
@ -401,7 +401,7 @@ QSet<int> InputController::activeGamepadButtons(uint32_t type) {
QSet<QPair<int, GamepadAxisEvent::Direction>> InputController::activeGamepadAxes(uint32_t type) {
QSet<QPair<int, GamepadAxisEvent::Direction>> activeAxes;
Gamepad* pad = gamepad(type);
std::shared_ptr<Gamepad> pad = gamepad(type);
if (!pad) {
return {};
}
@ -422,7 +422,7 @@ QSet<QPair<int, GamepadAxisEvent::Direction>> InputController::activeGamepadAxes
QSet<QPair<int, GamepadHatEvent::Direction>> InputController::activeGamepadHats(uint32_t type) {
QSet<QPair<int, GamepadHatEvent::Direction>> activeHats;
Gamepad* pad = gamepad(type);
std::shared_ptr<Gamepad> pad = gamepad(type);
if (!pad) {
return {};
}

View File

@ -143,8 +143,8 @@ private:
static int claimPlayer();
static void freePlayer(int);
Gamepad* gamepad(uint32_t type);
QList<Gamepad*> gamepads();
std::shared_ptr<Gamepad> gamepad(uint32_t type);
QList<std::shared_ptr<Gamepad>> gamepads();
QSet<int> activeGamepadButtons(uint32_t type);
QSet<QPair<int, GamepadAxisEvent::Direction>> activeGamepadAxes(uint32_t type);

View File

@ -36,7 +36,7 @@ QList<KeySource*> InputDriver::connectedKeySources() const {
return {};
}
QList<Gamepad*> InputDriver::connectedGamepads() const {
QList<std::shared_ptr<Gamepad>> InputDriver::connectedGamepads() const {
return {};
}
@ -57,8 +57,8 @@ KeySource* InputDriver::activeKeySource() {
return ks[activeKeySource];
}
Gamepad* InputDriver::activeGamepad() {
QList<Gamepad*> pads(connectedGamepads());
std::shared_ptr<Gamepad> InputDriver::activeGamepad() {
QList<std::shared_ptr<Gamepad>> pads(connectedGamepads());
int activeGamepad = activeGamepadIndex();
if (activeGamepad < 0 || activeGamepad >= pads.count()) {
return nullptr;

View File

@ -9,6 +9,8 @@
#include <QString>
#include <QObject>
#include <memory>
struct mRotationSource;
struct mRumble;
@ -42,13 +44,13 @@ public:
virtual bool update() = 0;
virtual QList<KeySource*> connectedKeySources() const;
virtual QList<Gamepad*> connectedGamepads() const;
virtual QList<std::shared_ptr<Gamepad>> connectedGamepads() const;
virtual int activeKeySourceIndex() const;
virtual int activeGamepadIndex() const;
KeySource* activeKeySource();
Gamepad* activeGamepad();
std::shared_ptr<Gamepad> activeGamepad();
virtual void setActiveKeySource(int);
virtual void setActiveGamepad(int);

View File

@ -139,10 +139,10 @@ bool SDLInputDriver::update() {
return true;
}
QList<Gamepad*> SDLInputDriver::connectedGamepads() const {
QList<Gamepad*> pads;
QList<std::shared_ptr<Gamepad>> SDLInputDriver::connectedGamepads() const {
QList<std::shared_ptr<Gamepad>> pads;
for (auto& pad : m_gamepads) {
pads.append(pad.get());
pads.append(pad);
}
return pads;
}

View File

@ -44,7 +44,7 @@ public:
bool update() override;
QList<Gamepad*> connectedGamepads() const override;
QList<std::shared_ptr<Gamepad>> connectedGamepads() const override;
int activeGamepadIndex() const override;
void setActiveGamepad(int) override;

View File

@ -264,7 +264,7 @@ void ScriptingController::updateGamepad() {
detachGamepad();
return;
}
Gamepad* gamepad = driver->activeGamepad();
std::shared_ptr<Gamepad> gamepad = driver->activeGamepad();
if (!gamepad) {
detachGamepad();
return;