Make these event watchers
This commit is contained in:
@@ -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,16 +58,15 @@ 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)) {
|
|
||||||
switch (e.type) {
|
|
||||||
case SDL_EVENT_GAMEPAD_ADDED:
|
case SDL_EVENT_GAMEPAD_ADDED:
|
||||||
{
|
{
|
||||||
const auto index = e.gdevice.which;
|
const auto index = event->gdevice.which;
|
||||||
|
|
||||||
const auto gamepad = SDL_OpenGamepad(index);
|
const auto gamepad = SDL_OpenGamepad(index);
|
||||||
Util::info("Found controller!");
|
Util::info("Found controller!");
|
||||||
@@ -74,81 +75,80 @@ void InputSettings::QueryDevices() noexcept {
|
|||||||
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});
|
_this->devices.addItem({name});
|
||||||
} else if (serial) {
|
} else if (serial) {
|
||||||
if (!gamepadIndexes.contains(index)) {
|
if (!_this->gamepadIndexes.contains(index)) {
|
||||||
gamepadIndexes[index] = serial;
|
_this->gamepadIndexes[index] = serial;
|
||||||
}
|
}
|
||||||
devices.addItem({serial});
|
_this->devices.addItem({serial});
|
||||||
} else if (path) {
|
} else if (path) {
|
||||||
if (!gamepadIndexes.contains(index)) {
|
if (!_this->gamepadIndexes.contains(index)) {
|
||||||
gamepadIndexes[index] = path;
|
_this->gamepadIndexes[index] = path;
|
||||||
}
|
}
|
||||||
devices.addItem({path});
|
_this->devices.addItem({path});
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_CloseGamepad(gamepad);
|
SDL_CloseGamepad(gamepad);
|
||||||
}
|
}
|
||||||
break;
|
return true;
|
||||||
case SDL_EVENT_GAMEPAD_REMOVED:
|
case SDL_EVENT_GAMEPAD_REMOVED:
|
||||||
{
|
{
|
||||||
const auto index = e.gdevice.which;
|
const auto index = event->gdevice.which;
|
||||||
|
|
||||||
if (gamepadIndexes.contains(index))
|
if (_this->gamepadIndexes.contains(index))
|
||||||
devices.removeItem(gamepadIndexes[index]);
|
_this->devices.removeItem(_this->gamepadIndexes[index]);
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
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 (!selectedDeviceIsNotKeyboard)
|
if (!_this->selectedDeviceIsNotKeyboard)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
const auto k = SDL_GetGamepadStringForButton(static_cast<SDL_GamepadButton>(e.gbutton.button));
|
const auto k = SDL_GetGamepadStringForButton(static_cast<SDL_GamepadButton>(event->gbutton.button));
|
||||||
JSONSetField<std::string>(settings, "input", kbButtons[whichGrabbing].getName(), k);
|
JSONSetField<std::string>(_this->settings, "input", _this->kbButtons[_this->whichGrabbing].getName(), k);
|
||||||
kbButtons[whichGrabbing].setLabel(k);
|
_this->kbButtons[_this->whichGrabbing].setLabel(k);
|
||||||
devices.setEnabled(true);
|
_this->devices.setEnabled(true);
|
||||||
for (auto &kbButton : kbButtons) {
|
for (auto &kbButton : _this->kbButtons) {
|
||||||
kbButton.setEnabled(true);
|
kbButton.setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
grabbing = false;
|
_this->grabbing = false;
|
||||||
whichGrabbing = -1;
|
_this->whichGrabbing = -1;
|
||||||
|
|
||||||
modified = true;
|
_this->modified = true;
|
||||||
}
|
}
|
||||||
break;
|
return true;
|
||||||
case SDL_EVENT_KEY_DOWN:
|
case SDL_EVENT_KEY_DOWN:
|
||||||
{
|
{
|
||||||
if (selectedDeviceIsNotKeyboard)
|
if (_this->selectedDeviceIsNotKeyboard)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
const auto k = SDL_GetKeyName(e.key.key);
|
const auto k = SDL_GetKeyName(event->key.key);
|
||||||
JSONSetField<std::string>(settings, "input", kbButtons[whichGrabbing].getName(), k);
|
JSONSetField<std::string>(_this->settings, "input", _this->kbButtons[_this->whichGrabbing].getName(), k);
|
||||||
kbButtons[whichGrabbing].setLabel(k);
|
_this->kbButtons[_this->whichGrabbing].setLabel(k);
|
||||||
devices.setEnabled(true);
|
_this->devices.setEnabled(true);
|
||||||
for (auto &kbButton : kbButtons) {
|
for (auto &kbButton : _this->kbButtons) {
|
||||||
kbButton.setEnabled(true);
|
kbButton.setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
grabbing = false;
|
_this->grabbing = false;
|
||||||
whichGrabbing = -1;
|
_this->whichGrabbing = -1;
|
||||||
|
|
||||||
modified = true;
|
_this->modified = true;
|
||||||
}
|
}
|
||||||
break;
|
return true;
|
||||||
default:
|
default:
|
||||||
break;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 &);
|
||||||
|
|||||||
Reference in New Issue
Block a user