Make these event watchers
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
#include <log.hpp>
|
||||
#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) {
|
||||
int i = 0;
|
||||
for(auto& kb : kbButtons) {
|
||||
@@ -11,6 +14,8 @@ InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) {
|
||||
|
||||
devices.addItem({"Keyboard/Mouse"});
|
||||
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
|
||||
SDL_AddEventWatch(QueryDevices, this);
|
||||
SDL_AddEventWatch(PollGamepad, this);
|
||||
}
|
||||
|
||||
bool InputSettings::render() {
|
||||
@@ -32,9 +37,6 @@ bool InputSettings::render() {
|
||||
whichGrabbing = i;
|
||||
}
|
||||
|
||||
QueryDevices();
|
||||
PollGamepad();
|
||||
|
||||
if((i % 2 == 0) || i == 0) // only go down every 2 buttons... just... i like it this way
|
||||
ImGui::SameLine();
|
||||
|
||||
@@ -56,99 +58,97 @@ std::array<SDL_Keycode, 18> InputSettings::GetMappedKeys() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
void InputSettings::QueryDevices() noexcept {
|
||||
if (!devices.isEnabled())
|
||||
return;
|
||||
bool QueryDevices(void *userdata, SDL_Event *event) {
|
||||
auto _this = (InputSettings*)userdata;
|
||||
if (!_this->devices.isEnabled())
|
||||
return false;
|
||||
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_EVENT_GAMEPAD_ADDED:
|
||||
{
|
||||
const auto index = e.gdevice.which;
|
||||
switch (event->type) {
|
||||
case SDL_EVENT_GAMEPAD_ADDED:
|
||||
{
|
||||
const auto index = event->gdevice.which;
|
||||
|
||||
const auto gamepad = SDL_OpenGamepad(index);
|
||||
Util::info("Found controller!");
|
||||
const auto serial = SDL_GetGamepadSerial(gamepad);
|
||||
const auto name = SDL_GetGamepadName(gamepad);
|
||||
const auto path = SDL_GetGamepadPath(gamepad);
|
||||
const auto gamepad = SDL_OpenGamepad(index);
|
||||
Util::info("Found controller!");
|
||||
const auto serial = SDL_GetGamepadSerial(gamepad);
|
||||
const auto name = SDL_GetGamepadName(gamepad);
|
||||
const auto path = SDL_GetGamepadPath(gamepad);
|
||||
|
||||
if (name) {
|
||||
if (!gamepadIndexes.contains(index)) {
|
||||
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});
|
||||
if (name) {
|
||||
if (!_this->gamepadIndexes.contains(index)) {
|
||||
_this->gamepadIndexes[index] = name;
|
||||
}
|
||||
|
||||
SDL_CloseGamepad(gamepad);
|
||||
_this->devices.addItem({name});
|
||||
} 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))
|
||||
devices.removeItem(gamepadIndexes[index]);
|
||||
}
|
||||
break;
|
||||
SDL_CloseGamepad(gamepad);
|
||||
}
|
||||
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 {
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
|
||||
{
|
||||
if (!selectedDeviceIsNotKeyboard)
|
||||
return;
|
||||
|
||||
const auto k = SDL_GetGamepadStringForButton(static_cast<SDL_GamepadButton>(e.gbutton.button));
|
||||
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;
|
||||
bool PollGamepad(void *userdata, SDL_Event *event) {
|
||||
auto _this = (InputSettings*)userdata;
|
||||
switch (event->type) {
|
||||
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
|
||||
{
|
||||
if (!_this->selectedDeviceIsNotKeyboard)
|
||||
return false;
|
||||
|
||||
const auto k = SDL_GetGamepadStringForButton(static_cast<SDL_GamepadButton>(event->gbutton.button));
|
||||
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);
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
{
|
||||
if (selectedDeviceIsNotKeyboard)
|
||||
return;
|
||||
|
||||
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;
|
||||
|
||||
_this->grabbing = false;
|
||||
_this->whichGrabbing = -1;
|
||||
|
||||
_this->modified = true;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,10 @@
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
class InputSettings final {
|
||||
public:
|
||||
bool grabbing = false;
|
||||
int whichGrabbing = -1;
|
||||
|
||||
void QueryDevices() noexcept;
|
||||
void PollGamepad() noexcept;
|
||||
|
||||
std::unordered_map<u32, std::string> gamepadIndexes{};
|
||||
std::array<gui::PushButton, 18> kbButtons = {
|
||||
gui::PushButton{"", "A"},
|
||||
@@ -54,7 +52,6 @@ class InputSettings final {
|
||||
//std::unique_ptr<QLabel> devicesLabel = std::make_unique<QLabel>("Device:");
|
||||
//std::unique_ptr<QComboBox> devices = std::make_unique<QComboBox>();
|
||||
//Q_OBJECT
|
||||
public:
|
||||
bool render();
|
||||
bool selectedDeviceIsNotKeyboard = false;
|
||||
explicit InputSettings(nlohmann::json &);
|
||||
|
||||
Reference in New Issue
Block a user