Settings work

This commit is contained in:
Simone
2024-01-23 13:03:34 +01:00
parent e3cc520fa5
commit 46bb91ec31
9 changed files with 87 additions and 7 deletions

View File

@@ -0,0 +1,48 @@
#include <AudioSettings.hpp>
#include <QLabel>
#include <QVBoxLayout>
AudioSettings::AudioSettings(nlohmann::json& settings) : settings(settings), QWidget(nullptr) {
lockChannels->setChecked(JSONGetField<bool>(settings, "audio", "lock"));
volumeL->setValue(JSONGetField<float>(settings, "audio", "volumeL") * 100);
volumeR->setValue(JSONGetField<float>(settings, "audio", "volumeR") * 100);
volumeL->setRange(0, 100);
volumeR->setRange(0, 100);
connect(lockChannels, &QCheckBox::stateChanged, this, [&]() {
JSONSetField(settings, "audio", "lock", lockChannels->isChecked());
emit modified();
});
connect(volumeL, &QSlider::valueChanged, this, [&]() {
JSONSetField(settings, "audio", "volumeL", float(volumeL->value()) / 100.f);
if (lockChannels->isChecked()) {
volumeR->setValue(volumeL->value());
JSONSetField(settings, "audio", "volumeR", float(volumeL->value()) / 100.f);
}
emit modified();
});
connect(volumeR, &QSlider::valueChanged, this, [&]() {
if (!lockChannels->isChecked()) {
JSONSetField(settings, "audio", "volumeR", float(volumeR->value()) / 100.f);
}
emit modified();
});
QLabel* labelLock = new QLabel("Lock channels:");
QLabel* labelL = new QLabel("Volume L");
QLabel* labelR = new QLabel("Volume R");
QVBoxLayout* mainLayout = new QVBoxLayout;
QHBoxLayout* volLayout = new QHBoxLayout;
mainLayout->addWidget(labelLock);
mainLayout->addWidget(lockChannels);
volLayout->addWidget(labelL);
volLayout->addWidget(volumeL);
volLayout->addWidget(labelR);
volLayout->addWidget(volumeR);
mainLayout->addLayout(volLayout);
mainLayout->addStretch();
setLayout(mainLayout);
}

View File

@@ -0,0 +1,16 @@
#pragma once
#include <QWidget>
#include <QSlider>
#include <QCheckBox>
#include <JSONUtils.hpp>
class AudioSettings : public QWidget {
QCheckBox* lockChannels = new QCheckBox;
Q_OBJECT
public:
QSlider* volumeL = new QSlider(Qt::Horizontal), * volumeR = new QSlider(Qt::Horizontal);
AudioSettings(nlohmann::json&);
nlohmann::json& settings;
Q_SIGNALS:
void modified();
};

View File

@@ -64,7 +64,9 @@ add_executable(kaizen-qt
SettingsWindow.cpp SettingsWindow.cpp
CPUSettings.hpp CPUSettings.hpp
CPUSettings.cpp CPUSettings.cpp
JSONUtils.hpp) JSONUtils.hpp
AudioSettings.hpp
AudioSettings.cpp)
target_link_libraries(kaizen-qt PUBLIC Qt6::Core Qt6::Gui Qt6::Widgets fmt mio nlohmann_json nfd parallel-rdp backend) target_link_libraries(kaizen-qt PUBLIC Qt6::Core Qt6::Gui Qt6::Widgets fmt mio nlohmann_json nfd parallel-rdp backend)

View File

@@ -12,7 +12,7 @@ EmuThread::EmuThread(std::unique_ptr<QtInstanceFactory>&& instance, std::unique_
n64::InitAudio(); n64::InitAudio();
while (true) { while (true) {
if (!core->pause) { if (!core->pause) {
core->Run(0.5, 0.5); core->Run(settings->getVolumeL(), settings->getVolumeR());
if(core->render) { if(core->render) {
UpdateScreenParallelRdp(core->cpu->mem.mmio.vi); UpdateScreenParallelRdp(core->cpu->mem.mmio.vi);
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <QThread> #include <QThread>
#include <Core.hpp> #include <Core.hpp>
#include <SettingsWindow.hpp>
struct QtInstanceFactory; struct QtInstanceFactory;
struct ParallelRdpWindowInfo; struct ParallelRdpWindowInfo;
@@ -20,6 +21,7 @@ public:
[[noreturn]] void run() noexcept override; [[noreturn]] void run() noexcept override;
n64::Core* core; n64::Core* core;
SettingsWindow* settings;
bool running = false; bool running = false;
void TogglePause() void TogglePause()

View File

@@ -16,8 +16,10 @@ static inline nlohmann::json JSONOpenOrCreate(const std::string& path) {
} else { } else {
auto file = std::fstream(path, std::fstream::in | std::fstream::out | std::fstream::trunc); auto file = std::fstream(path, std::fstream::in | std::fstream::out | std::fstream::trunc);
nlohmann::json json; nlohmann::json json;
json["audio"]["volume"] = 0.5; json["audio"]["volumeL"] = 0.5;
json["cpu"]["type"] = std::string("interpreter"); json["audio"]["volumeR"] = 0.5;
json["audio"]["lock"] = true;
json["cpu"]["type"] = "interpreter";
file << json; file << json;
file.close(); file.close();
@@ -32,6 +34,6 @@ static inline void JSONSetField(nlohmann::json& json, const std::string& field1,
} }
template <typename T> template <typename T>
static inline const T& JSONGetField(nlohmann::json& json, const std::string& field1, const std::string& field2) { static inline T JSONGetField(nlohmann::json& json, const std::string& field1, const std::string& field2) {
return json[field1][field2]; return json[field1][field2].get<T>();
} }

View File

@@ -22,6 +22,7 @@ KaizenQt::KaizenQt() noexcept : QWidget(nullptr) {
settingsWindow = new SettingsWindow; settingsWindow = new SettingsWindow;
settingsWindow->hide(); settingsWindow->hide();
emuThread->core = new n64::Core(); emuThread->core = new n64::Core();
emuThread->settings = settingsWindow;
} }
void KaizenQt::ConnectMainWindowSignalsToSlots() noexcept { void KaizenQt::ConnectMainWindowSignalsToSlots() noexcept {

View File

@@ -13,10 +13,11 @@ SettingsWindow::SettingsWindow() : QWidget(nullptr) {
setWindowTitle("Settings"); setWindowTitle("Settings");
cpuSettings = new CPUSettings(settings); cpuSettings = new CPUSettings(settings);
audioSettings = new AudioSettings(settings);
auto* tabs = new QTabWidget; auto* tabs = new QTabWidget;
tabs->addTab(cpuSettings, tr("CPU")); tabs->addTab(cpuSettings, tr("CPU"));
//tabs->addTab(new PermissionsTab, tr("Audio")); tabs->addTab(audioSettings, tr("Audio"));
apply->setEnabled(false); apply->setEnabled(false);
@@ -24,6 +25,10 @@ SettingsWindow::SettingsWindow() : QWidget(nullptr) {
apply->setEnabled(true); apply->setEnabled(true);
}); });
connect(audioSettings, &AudioSettings::modified, this, [&]() {
apply->setEnabled(true);
});
connect(apply, &QPushButton::pressed, this, [&]() { connect(apply, &QPushButton::pressed, this, [&]() {
apply->setEnabled(false); apply->setEnabled(false);
std::ofstream file("resources/settings.json"); std::ofstream file("resources/settings.json");

View File

@@ -3,13 +3,17 @@
#include <QTabWidget> #include <QTabWidget>
#include <QPushButton> #include <QPushButton>
#include <CPUSettings.hpp> #include <CPUSettings.hpp>
#include <AudioSettings.hpp>
class SettingsWindow : public QWidget { class SettingsWindow : public QWidget {
QPushButton* cancel = new QPushButton("Cancel"); QPushButton* cancel = new QPushButton("Cancel");
QPushButton* apply = new QPushButton("Apply"); QPushButton* apply = new QPushButton("Apply");
Q_OBJECT Q_OBJECT
public: public:
float getVolumeL() { return float(audioSettings->volumeL->value()) / 100.f; }
float getVolumeR() { return float(audioSettings->volumeR->value()) / 100.f; }
SettingsWindow(); SettingsWindow();
nlohmann::json settings; nlohmann::json settings;
CPUSettings* cpuSettings; CPUSettings* cpuSettings;
AudioSettings* audioSettings;
}; };