diff --git a/src/frontend/ImGuiImpl/Combobox.hpp b/src/frontend/ImGuiImpl/Combobox.hpp index 8d97e7e7..50f0fce5 100644 --- a/src/frontend/ImGuiImpl/Combobox.hpp +++ b/src/frontend/ImGuiImpl/Combobox.hpp @@ -70,6 +70,8 @@ struct Combobox { void setEnabled(bool v) { enabled = v; } bool isEnabled() { return enabled; } + const std::string& getCurrentlySelected() { return items[current_index].getLabel(); } + void setCurrentIndex(int v) { current_index = v; } int getCurrentIndex() { return current_index; } private: diff --git a/src/frontend/ImGuiImpl/PopupWindow.hpp b/src/frontend/ImGuiImpl/PopupWindow.hpp index 24654f3c..aae98348 100644 --- a/src/frontend/ImGuiImpl/PopupWindow.hpp +++ b/src/frontend/ImGuiImpl/PopupWindow.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/src/frontend/ImGuiImpl/StatusBar.hpp b/src/frontend/ImGuiImpl/StatusBar.hpp index 778fb5ab..c8431e87 100644 --- a/src/frontend/ImGuiImpl/StatusBar.hpp +++ b/src/frontend/ImGuiImpl/StatusBar.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include diff --git a/src/frontend/InputSettings.cpp b/src/frontend/InputSettings.cpp index c262dcab..cf5dc268 100644 --- a/src/frontend/InputSettings.cpp +++ b/src/frontend/InputSettings.cpp @@ -10,24 +10,17 @@ InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) { } devices.addItem({"Keyboard/Mouse"}); - - /* TODO: GAMEPAD STUFF IDK HOW TO HANDLE YET - connect(devices.get(), &QComboBox::currentTextChanged, this, [&](const QString &text) { - JSONSetField(settings, "input", "Device", text.toStdString()); - emit modified(); - }); - SDL_InitSubSystem(SDL_INIT_GAMEPAD); - - connect(&refresh, &QTimer::timeout, this, &InputSettings::QueryDevices); - refresh.start(16); - - connect(&pollGamepad, &QTimer::timeout, this, &InputSettings::PollGamepad); - pollGamepad.start(16); - */ } bool InputSettings::render() { + if(devices.render()) { + auto currentlySelectedDevice = devices.getCurrentlySelected(); + JSONSetField(settings, "input", "Device", currentlySelectedDevice); + selectedDeviceIsNotKeyboard = currentlySelectedDevice == "Keyboard/Mouse"; + modified = true; + } + int i = 0; for(auto& kb : kbButtons) { if(kb.render()) { @@ -35,10 +28,12 @@ bool InputSettings::render() { for (auto &otherKb : kbButtons) { otherKb.setEnabled(false); } + + whichGrabbing = i; } - // TODO: ACTUALLY GRAB THE PRESSED INPUT - // ... + QueryDevices(); + PollGamepad(); if(i % 2 != 0) // only go down every 2 buttons... just... i like it this way ImGui::SameLine(); @@ -47,28 +42,6 @@ bool InputSettings::render() { } } -/* TODO: RECREATE THIS IN SDL -void InputSettings::keyPressEvent(QKeyEvent *e) { - if (grabbing) { - const auto k = QKeySequence(e->key()).toString().toStdString(); - JSONSetField(settings, "input", buttonLabels[whichGrabbing]->text().toStdString(), k); - kbButtons[whichGrabbing].setText(k.c_str()); - devices.setEnabled(true); - for (const auto &kbButton : kbButtons) { - kbButton.setEnabled(true); - } - - grabbing = false; - whichGrabbing = -1; - - if (devices.currentText() == "Keyboard/Mouse") { - releaseKeyboard(); - emit modified(); - } - } -} -*/ - std::array InputSettings::GetMappedKeys() { std::array ret{}; @@ -130,21 +103,20 @@ void InputSettings::QueryDevices() noexcept { } } -/* void InputSettings::PollGamepad() noexcept { - if (!selectedDeviceIsNotKeyboard) - return; - 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(e.gbutton.button)); - JSONSetField(settings, "input", buttonLabels[whichGrabbing]->text().toStdString(), k); - kbButtons[whichGrabbing].setText(k); + JSONSetField(settings, "input", kbButtons[whichGrabbing].getName(), k); + kbButtons[whichGrabbing].setLabel(k); devices.setEnabled(true); - for (const auto &kbButton : kbButtons) { + for (auto &kbButton : kbButtons) { kbButton.setEnabled(true); } @@ -154,9 +126,27 @@ void InputSettings::PollGamepad() noexcept { modified = true; } break; + case SDL_EVENT_KEY_DOWN: + { + if (selectedDeviceIsNotKeyboard) + return; + + const auto k = SDL_GetKeyName(e.key.key); + JSONSetField(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; } } } -*/