Make these event watchers

This commit is contained in:
irisz64
2025-05-27 08:42:41 +02:00
parent 67c761d34b
commit ab44f6d980
2 changed files with 86 additions and 89 deletions

View File

@@ -2,6 +2,9 @@
#include <log.hpp> #include <log.hpp>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
bool QueryDevices(void *userdata, SDL_Event *event);
bool PollGamepad(void *userdata, SDL_Event *event);
InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) { InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) {
int i = 0; int i = 0;
for(auto& kb : kbButtons) { for(auto& kb : kbButtons) {
@@ -11,6 +14,8 @@ InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) {
devices.addItem({"Keyboard/Mouse"}); devices.addItem({"Keyboard/Mouse"});
SDL_InitSubSystem(SDL_INIT_GAMEPAD); SDL_InitSubSystem(SDL_INIT_GAMEPAD);
SDL_AddEventWatch(QueryDevices, this);
SDL_AddEventWatch(PollGamepad, this);
} }
bool InputSettings::render() { bool InputSettings::render() {
@@ -32,9 +37,6 @@ bool InputSettings::render() {
whichGrabbing = i; whichGrabbing = i;
} }
QueryDevices();
PollGamepad();
if((i % 2 == 0) || i == 0) // only go down every 2 buttons... just... i like it this way if((i % 2 == 0) || i == 0) // only go down every 2 buttons... just... i like it this way
ImGui::SameLine(); ImGui::SameLine();
@@ -56,99 +58,97 @@ std::array<SDL_Keycode, 18> InputSettings::GetMappedKeys() {
return ret; return ret;
} }
void InputSettings::QueryDevices() noexcept { bool QueryDevices(void *userdata, SDL_Event *event) {
if (!devices.isEnabled()) auto _this = (InputSettings*)userdata;
return; if (!_this->devices.isEnabled())
return false;
SDL_Event e; switch (event->type) {
while (SDL_PollEvent(&e)) { case SDL_EVENT_GAMEPAD_ADDED:
switch (e.type) { {
case SDL_EVENT_GAMEPAD_ADDED: const auto index = event->gdevice.which;
{
const auto index = e.gdevice.which;
const auto gamepad = SDL_OpenGamepad(index); const auto gamepad = SDL_OpenGamepad(index);
Util::info("Found controller!"); Util::info("Found controller!");
const auto serial = SDL_GetGamepadSerial(gamepad); const auto serial = SDL_GetGamepadSerial(gamepad);
const auto name = SDL_GetGamepadName(gamepad); const auto name = SDL_GetGamepadName(gamepad);
const auto path = SDL_GetGamepadPath(gamepad); const auto path = SDL_GetGamepadPath(gamepad);
if (name) { if (name) {
if (!gamepadIndexes.contains(index)) { if (!_this->gamepadIndexes.contains(index)) {
gamepadIndexes[index] = name; _this->gamepadIndexes[index] = name;
}
devices.addItem({name});
} else if (serial) {
if (!gamepadIndexes.contains(index)) {
gamepadIndexes[index] = serial;
}
devices.addItem({serial});
} else if (path) {
if (!gamepadIndexes.contains(index)) {
gamepadIndexes[index] = path;
}
devices.addItem({path});
} }
_this->devices.addItem({name});
SDL_CloseGamepad(gamepad); } else if (serial) {
if (!_this->gamepadIndexes.contains(index)) {
_this->gamepadIndexes[index] = serial;
}
_this->devices.addItem({serial});
} else if (path) {
if (!_this->gamepadIndexes.contains(index)) {
_this->gamepadIndexes[index] = path;
}
_this->devices.addItem({path});
} }
break;
case SDL_EVENT_GAMEPAD_REMOVED:
{
const auto index = e.gdevice.which;
if (gamepadIndexes.contains(index)) SDL_CloseGamepad(gamepad);
devices.removeItem(gamepadIndexes[index]);
}
break;
} }
return true;
case SDL_EVENT_GAMEPAD_REMOVED:
{
const auto index = event->gdevice.which;
if (_this->gamepadIndexes.contains(index))
_this->devices.removeItem(_this->gamepadIndexes[index]);
}
return true;
default:
return false;
} }
} }
void InputSettings::PollGamepad() noexcept { bool PollGamepad(void *userdata, SDL_Event *event) {
SDL_Event e; auto _this = (InputSettings*)userdata;
while (SDL_PollEvent(&e)) { switch (event->type) {
switch (e.type) { case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_DOWN: {
{ if (!_this->selectedDeviceIsNotKeyboard)
if (!selectedDeviceIsNotKeyboard) return false;
return;
const auto k = SDL_GetGamepadStringForButton(static_cast<SDL_GamepadButton>(event->gbutton.button));
const auto k = SDL_GetGamepadStringForButton(static_cast<SDL_GamepadButton>(e.gbutton.button)); JSONSetField<std::string>(_this->settings, "input", _this->kbButtons[_this->whichGrabbing].getName(), k);
JSONSetField<std::string>(settings, "input", kbButtons[whichGrabbing].getName(), k); _this->kbButtons[_this->whichGrabbing].setLabel(k);
kbButtons[whichGrabbing].setLabel(k); _this->devices.setEnabled(true);
devices.setEnabled(true); for (auto &kbButton : _this->kbButtons) {
for (auto &kbButton : kbButtons) { kbButton.setEnabled(true);
kbButton.setEnabled(true);
}
grabbing = false;
whichGrabbing = -1;
modified = true;
} }
break;
case SDL_EVENT_KEY_DOWN: _this->grabbing = false;
{ _this->whichGrabbing = -1;
if (selectedDeviceIsNotKeyboard)
return; _this->modified = true;
const auto k = SDL_GetKeyName(e.key.key);
JSONSetField<std::string>(settings, "input", kbButtons[whichGrabbing].getName(), k);
kbButtons[whichGrabbing].setLabel(k);
devices.setEnabled(true);
for (auto &kbButton : kbButtons) {
kbButton.setEnabled(true);
}
grabbing = false;
whichGrabbing = -1;
modified = true;
}
break;
default:
break;
} }
return true;
case SDL_EVENT_KEY_DOWN:
{
if (_this->selectedDeviceIsNotKeyboard)
return false;
const auto k = SDL_GetKeyName(event->key.key);
JSONSetField<std::string>(_this->settings, "input", _this->kbButtons[_this->whichGrabbing].getName(), k);
_this->kbButtons[_this->whichGrabbing].setLabel(k);
_this->devices.setEnabled(true);
for (auto &kbButton : _this->kbButtons) {
kbButton.setEnabled(true);
}
_this->grabbing = false;
_this->whichGrabbing = -1;
_this->modified = true;
}
return true;
default:
return false;
} }
} }

View File

@@ -6,12 +6,10 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
class InputSettings final { class InputSettings final {
public:
bool grabbing = false; bool grabbing = false;
int whichGrabbing = -1; int whichGrabbing = -1;
void QueryDevices() noexcept;
void PollGamepad() noexcept;
std::unordered_map<u32, std::string> gamepadIndexes{}; std::unordered_map<u32, std::string> gamepadIndexes{};
std::array<gui::PushButton, 18> kbButtons = { std::array<gui::PushButton, 18> kbButtons = {
gui::PushButton{"", "A"}, gui::PushButton{"", "A"},
@@ -54,7 +52,6 @@ class InputSettings final {
//std::unique_ptr<QLabel> devicesLabel = std::make_unique<QLabel>("Device:"); //std::unique_ptr<QLabel> devicesLabel = std::make_unique<QLabel>("Device:");
//std::unique_ptr<QComboBox> devices = std::make_unique<QComboBox>(); //std::unique_ptr<QComboBox> devices = std::make_unique<QComboBox>();
//Q_OBJECT //Q_OBJECT
public:
bool render(); bool render();
bool selectedDeviceIsNotKeyboard = false; bool selectedDeviceIsNotKeyboard = false;
explicit InputSettings(nlohmann::json &); explicit InputSettings(nlohmann::json &);