Fix apply button not turning grey after clicking on it.

This commit is contained in:
IrisZ64
2025-12-14 16:21:21 +01:00
parent affb221c2c
commit 9a1089b70e
8 changed files with 87 additions and 64 deletions

View File

@@ -1,5 +1,6 @@
#include <AudioSettings.hpp>
#include <imgui.h>
#include <Options.hpp>
AudioSettings::AudioSettings() {
lockChannels = Options::GetInstance().GetValue<bool>("audio", "lock");
@@ -7,7 +8,7 @@ AudioSettings::AudioSettings() {
volumeR = Options::GetInstance().GetValue<float>("audio", "volumeR") * 100;
}
bool AudioSettings::render() {
void AudioSettings::render() {
if(ImGui::Checkbox("Lock channels:", &lockChannels)) {
Options::GetInstance().SetValue("audio", "lock", lockChannels);
if(lockChannels) {
@@ -34,6 +35,4 @@ bool AudioSettings::render() {
modified = true;
}
ImGui::EndDisabled();
return modified;
}

View File

@@ -1,14 +1,11 @@
#pragma once
#include <Options.hpp>
#include <SettingsTab.hpp>
class AudioSettings final {
struct AudioSettings final : SettingsTab {
bool lockChannels = false;
bool modified = false;
public:
float volumeL{};
float volumeR{};
explicit AudioSettings();
bool render();
void setModified(bool v) { modified = v; }
bool getModified() { return modified; }
void render() override;
};

View File

@@ -11,7 +11,7 @@ CPUSettings::CPUSettings() {
}
}
bool CPUSettings::render() {
void CPUSettings::render() {
const char* items[] = {
"Interpreter",
"Dynamic Recompiler"
@@ -40,6 +40,4 @@ bool CPUSettings::render() {
Options::GetInstance().SetValue<std::string>("cpu", "type", "jit");
}
}
return modified;
}

View File

@@ -1,12 +1,8 @@
#pragma once
#include <Options.hpp>
#include <SettingsTab.hpp>
class CPUSettings final {
struct CPUSettings final : SettingsTab {
int selectedCpuTypeIndex = 0;
bool modified = false;
public:
bool render();
void render() override;
explicit CPUSettings();
void setModified(bool v) { modified = v; }
bool getModified() { return modified; }
};

View File

@@ -0,0 +1,8 @@
#pragma once
struct SettingsTab {
virtual ~SettingsTab() = default;
virtual void render() = 0;
bool modified = false;
};

View File

@@ -2,13 +2,42 @@
#include <log.hpp>
#include <Options.hpp>
#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() {
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
const ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if(!ImGui::BeginPopupModal("Settings", &isOpen))
@@ -17,46 +46,14 @@ bool SettingsWindow::render() {
if(!ImGui::BeginTabBar("SettingsTabBar"))
return false;
if(ImGui::BeginTabItem("General")) {
if(ImGui::Button("Pick...")) {
SDL_ShowOpenFolderDialog([](void *userdata, const char * const *filelist, int filter) {
SettingsWindow* settings = (SettingsWindow*)userdata;
if (!filelist) {
panic("An error occured: {}", SDL_GetError());
return;
} else if (!*filelist) {
warn("The user did not select any file.");
warn("Most likely, the dialog was canceled.");
return;
}
settings->savesPath = *filelist;
Options::GetInstance().SetValue<std::string>("general", "savePath", settings->savesPath);
settings->applyEnabled = true;
}, this, window.getHandle(), nullptr, false);
}
ImGui::SameLine();
ImGui::BeginDisabled();
ImGui::InputText("Save Path", (char*)savesPath.c_str(), savesPath.length());
ImGui::EndDisabled();
ImGui::EndTabItem();
}
if(ImGui::BeginTabItem("Core")) {
if(cpuSettings.render()) {
if(cpuSettings.getModified())
for (auto& [name, tab] : tabs) {
if (ImGui::BeginTabItem(name.c_str())) {
tab->render();
if (tab->modified && !applyEnabled)
applyEnabled = true;
}
ImGui::EndTabItem();
}
if(ImGui::BeginTabItem("Audio")) {
if(audioSettings.render()) {
if(audioSettings.getModified())
applyEnabled = true;
ImGui::EndTabItem();
}
ImGui::EndTabItem();
}
ImGui::EndTabBar();
@@ -65,6 +62,10 @@ bool SettingsWindow::render() {
if(ImGui::Button("Apply")) {
applyEnabled = false;
Options::GetInstance().Apply();
for (auto &tab : tabs | std::views::values) {
tab->modified = false;
}
}
ImGui::EndDisabled();

View File

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