Structure settings better

This commit is contained in:
irisz64
2025-12-15 09:37:10 +01:00
parent 9a1089b70e
commit 8f787522bb
9 changed files with 60 additions and 55 deletions

View File

@@ -0,0 +1,35 @@
#include <GeneralSettings.hpp>
#include <Options.hpp>
#include <imgui.h>
#include <log.hpp>
GeneralSettings::GeneralSettings(gui::NativeWindow& window) : window(window) {
savesPath = Options::GetInstance().GetValue<std::string>("general", "savePath");
}
void GeneralSettings::render() {
if(ImGui::Button("Pick...")) {
SDL_ShowOpenFolderDialog([](void *userdata, const char * const *filelist, int _) {
auto* general = static_cast<GeneralSettings*>(userdata);
if (!filelist) {
panic("An error occurred: {}", SDL_GetError());
}
if (!*filelist) {
warn("The user did not select any file.");
warn("Most likely, the dialog was canceled.");
general->modified = false;
return;
}
general->savesPath = *filelist;
Options::GetInstance().SetValue<std::string>("general", "savePath", general->savesPath);
general->modified = true;
}, this, window.getHandle(), nullptr, false);
}
ImGui::SameLine();
ImGui::BeginDisabled();
ImGui::InputText("Save Path", const_cast<char*>(savesPath.c_str()), savesPath.length());
ImGui::EndDisabled();
}

View File

@@ -0,0 +1,11 @@
#pragma once
#include <SettingsTab.hpp>
#include <NativeWindow.hpp>
struct GeneralSettings final : SettingsTab {
void render() override;
explicit GeneralSettings(gui::NativeWindow&);
private:
gui::NativeWindow& window;
std::string savesPath;
};

View File

@@ -4,38 +4,6 @@
#include <imgui.h>
#include <ranges>
void GeneralTab::render() {
if(ImGui::Button("Pick...")) {
SDL_ShowOpenFolderDialog([](void *userdata, const char * const *filelist, int _) {
auto* general = static_cast<GeneralTab*>(userdata);
auto& settings = general->settingsWindow;
if (!filelist) {
panic("An error occurred: {}", SDL_GetError());
}
if (!*filelist) {
warn("The user did not select any file.");
warn("Most likely, the dialog was canceled.");
general->modified = false;
return;
}
settings.savesPath = *filelist;
Options::GetInstance().SetValue<std::string>("general", "savePath", settings.savesPath);
general->modified = true;
}, this, settingsWindow.window.getHandle(), nullptr, false);
}
ImGui::SameLine();
ImGui::BeginDisabled();
ImGui::InputText("Save Path", const_cast<char*>(settingsWindow.savesPath.c_str()), settingsWindow.savesPath.length());
ImGui::EndDisabled();
}
SettingsWindow::SettingsWindow(gui::NativeWindow& window) : window(window) {
savesPath = Options::GetInstance().GetValue<std::string>("general", "savePath");
}
bool SettingsWindow::render() {
const ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));

View File

@@ -1,38 +1,26 @@
#pragma once
#include <AudioSettings.hpp>
#include <CPUSettings.hpp>
#include <GeneralSettings.hpp>
#include <NativeWindow.hpp>
#include <vector>
class SettingsWindow;
struct GeneralTab : SettingsTab {
explicit GeneralTab(SettingsWindow &window) : settingsWindow(window) {}
void render() override;
private:
SettingsWindow& settingsWindow;
};
class SettingsWindow final {
public:
CPUSettings* cpuSettings = new CPUSettings;
private:
AudioSettings* audioSettings = new AudioSettings;
GeneralTab* generalSettings = new GeneralTab(*this);
std::string savesPath;
bool applyEnabled = false;
gui::NativeWindow& window;
GeneralSettings* generalSettings;
CPUSettings* cpuSettings = new CPUSettings;
AudioSettings* audioSettings = new AudioSettings;
bool applyEnabled = false;
std::vector<std::pair<std::string, SettingsTab*>> tabs = {
{ "General", generalSettings },
{ "CPU", cpuSettings },
{ "Audio", audioSettings },
};
friend struct GeneralTab;
public:
bool isOpen = false;
bool render();
explicit SettingsWindow(gui::NativeWindow& window);
explicit SettingsWindow(gui::NativeWindow& window) : window(window), generalSettings(new GeneralSettings(window)) {}
[[nodiscard]] float getVolumeL() const { return audioSettings->volumeL / 100.f; }
[[nodiscard]] float getVolumeR() const { return audioSettings->volumeR / 100.f; }
};