more work towards a clean code

This commit is contained in:
SimoZ64
2025-04-28 23:51:34 +02:00
parent f57e15f4de
commit b319255178
15 changed files with 163 additions and 213 deletions

View File

@@ -3,81 +3,14 @@
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
InputSettings::InputSettings(nlohmann::json &settings) : QWidget(nullptr), settings(settings) {
buttonLabels[0] = std::make_unique<QLabel>("A");
buttonLabels[1] = std::make_unique<QLabel>("B");
buttonLabels[2] = std::make_unique<QLabel>("Z");
buttonLabels[3] = std::make_unique<QLabel>("Start");
buttonLabels[4] = std::make_unique<QLabel>("L");
buttonLabels[5] = std::make_unique<QLabel>("R");
buttonLabels[6] = std::make_unique<QLabel>("Dpad Up");
buttonLabels[7] = std::make_unique<QLabel>("Dpad Down");
buttonLabels[8] = std::make_unique<QLabel>("Dpad Left");
buttonLabels[9] = std::make_unique<QLabel>("Dpad Right");
buttonLabels[10] = std::make_unique<QLabel>("C Up");
buttonLabels[11] = std::make_unique<QLabel>("C Down");
buttonLabels[12] = std::make_unique<QLabel>("C Left");
buttonLabels[13] = std::make_unique<QLabel>("C Right");
buttonLabels[14] = std::make_unique<QLabel>("Analog Up");
buttonLabels[15] = std::make_unique<QLabel>("Analog Down");
buttonLabels[16] = std::make_unique<QLabel>("Analog Left");
buttonLabels[17] = std::make_unique<QLabel>("Analog Right");
auto str = JSONGetField<std::string>(settings, "input", "A");
kbButtons[0] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "B");
kbButtons[1] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Z");
kbButtons[2] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Start");
kbButtons[3] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "L");
kbButtons[4] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "R");
kbButtons[5] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Dpad Up");
kbButtons[6] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Dpad Down");
kbButtons[7] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Dpad Left");
kbButtons[8] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Dpad Right");
kbButtons[9] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "C Up");
kbButtons[10] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "C Down");
kbButtons[11] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "C Left");
kbButtons[12] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "C Right");
kbButtons[13] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Analog Up");
kbButtons[14] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Analog Down");
kbButtons[15] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Analog Left");
kbButtons[16] = std::make_unique<QPushButton>(str.c_str());
str = JSONGetField<std::string>(settings, "input", "Analog Right");
kbButtons[17] = std::make_unique<QPushButton>(str.c_str());
for (int i = 0; i < 18; i++) {
connect(kbButtons[i].get(), &QPushButton::pressed, this, [&, i] {
devices->setEnabled(false);
for (const auto &kbButton : kbButtons) {
kbButton->setEnabled(false);
}
grabbing = true;
whichGrabbing = i;
if (devices->currentText() == "Keyboard/Mouse") {
grabKeyboard();
} else {
selectedDeviceIsNotKeyboard = true;
}
});
InputSettings::InputSettings(nlohmann::json &settings) : settings(settings) {
for(auto& kb : kbButtons) {
kb.setLabel(JSONGetField<std::string>(settings, "input", kb.getName()));
}
devices.addItem("Keyboard/Mouse");
/* TODO: GAMEPAD STUFF IDK HOW TO HANDLE YET
connect(devices.get(), &QComboBox::currentTextChanged, this, [&](const QString &text) {
JSONSetField<std::string>(settings, "input", "Device", text.toStdString());
emit modified();
@@ -90,62 +23,30 @@ InputSettings::InputSettings(nlohmann::json &settings) : QWidget(nullptr), setti
connect(&pollGamepad, &QTimer::timeout, this, &InputSettings::PollGamepad);
pollGamepad.start(16);
devices->addItem("Keyboard/Mouse");
deviceComboBoxLayout->addWidget(devicesLabel.get());
deviceComboBoxLayout->addWidget(devices.get());
mainLayout->addLayout(deviceComboBoxLayout.get());
AB->addWidget(buttonLabels[0].get());
AB->addWidget(kbButtons[0].get());
AB->addWidget(buttonLabels[1].get());
AB->addWidget(kbButtons[1].get());
mainLayout->addLayout(AB.get());
ZStart->addWidget(buttonLabels[2].get());
ZStart->addWidget(kbButtons[2].get());
ZStart->addWidget(buttonLabels[3].get());
ZStart->addWidget(kbButtons[3].get());
mainLayout->addLayout(ZStart.get());
LR->addWidget(buttonLabels[4].get());
LR->addWidget(kbButtons[4].get());
LR->addWidget(buttonLabels[5].get());
LR->addWidget(kbButtons[5].get());
mainLayout->addLayout(LR.get());
DupDdown->addWidget(buttonLabels[6].get());
DupDdown->addWidget(kbButtons[6].get());
DupDdown->addWidget(buttonLabels[7].get());
DupDdown->addWidget(kbButtons[7].get());
mainLayout->addLayout(DupDdown.get());
DleftDright->addWidget(buttonLabels[8].get());
DleftDright->addWidget(kbButtons[8].get());
DleftDright->addWidget(buttonLabels[9].get());
DleftDright->addWidget(kbButtons[9].get());
mainLayout->addLayout(DleftDright.get());
CupCdown->addWidget(buttonLabels[10].get());
CupCdown->addWidget(kbButtons[10].get());
CupCdown->addWidget(buttonLabels[11].get());
CupCdown->addWidget(kbButtons[11].get());
mainLayout->addLayout(CupCdown.get());
CleftCright->addWidget(buttonLabels[12].get());
CleftCright->addWidget(kbButtons[12].get());
CleftCright->addWidget(buttonLabels[13].get());
CleftCright->addWidget(kbButtons[13].get());
mainLayout->addLayout(CleftCright.get());
AupAdown->addWidget(buttonLabels[14].get());
AupAdown->addWidget(kbButtons[14].get());
AupAdown->addWidget(buttonLabels[15].get());
AupAdown->addWidget(kbButtons[15].get());
mainLayout->addLayout(AupAdown.get());
AleftAright->addWidget(buttonLabels[16].get());
AleftAright->addWidget(kbButtons[16].get());
AleftAright->addWidget(buttonLabels[17].get());
AleftAright->addWidget(kbButtons[17].get());
mainLayout->addLayout(AleftAright.get());
mainLayout->addStretch();
setLayout(mainLayout.get());
*/
}
bool InputSettings::render() {
int i = 0;
for(auto& kb : kbButtons) {
if(kb.render()) {
devices.setEnabled(false);
for (auto &otherKb : kbButtons) {
otherKb.setEnabled(false);
}
}
// TODO: ACTUALLY GRAB THE PRESSED INPUT
// ...
if(i % 2 != 0) // only go down every 2 buttons... just... i like it this way
ImGui::SameLine();
i++;
}
}
/* TODO: RECREATE THIS IN SDL
void InputSettings::keyPressEvent(QKeyEvent *e) {
if (grabbing) {
const auto k = QKeySequence(e->key()).toString().toStdString();
@@ -165,17 +66,21 @@ void InputSettings::keyPressEvent(QKeyEvent *e) {
}
}
}
*/
std::array<Qt::Key, 18> InputSettings::GetMappedKeys() const {
std::array<Qt::Key, 18> ret{};
std::array<SDL_Keycode, 18> InputSettings::GetMappedKeys() {
std::array<SDL_Keycode, 18> ret{};
int i = 0;
for (int i = 0; i < 18; i++) {
ret[i] = QKeySequence(kbButtons[i]->text().toUpper())[0].key();
for (const auto& kb : kbButtons) {
ret[i++] = SDL_GetKeyFromName(kb.getLabel().c_str());
}
return ret;
}
/*TODO: RECREATE THIS IN SDL
void InputSettings::QueryDevices() noexcept {
if (!devices->isEnabled())
return;
@@ -253,3 +158,4 @@ void InputSettings::PollGamepad() noexcept {
}
}
}
*/