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

@@ -144,6 +144,11 @@ endif()
target_link_libraries(kaizen PUBLIC imgui SDL3::SDL3 SDL3::SDL3-static cflags::cflags ${MIO_LIB} parallel-rdp capstone backend) target_link_libraries(kaizen PUBLIC imgui SDL3::SDL3 SDL3::SDL3-static cflags::cflags ${MIO_LIB} parallel-rdp capstone backend)
target_compile_definitions(kaizen PUBLIC SDL_MAIN_HANDLED) target_compile_definitions(kaizen PUBLIC SDL_MAIN_HANDLED)
if (${CMAKE_BUILD_TYPE} MATCHES Debug)
target_compile_options(kaizen PUBLIC -fsanitize=undefined -fsanitize=address)
target_link_options(kaizen PUBLIC -fsanitize=undefined -fsanitize=address)
endif ()
file(COPY resources/ DESTINATION ${PROJECT_BINARY_DIR}/resources/) file(COPY resources/ DESTINATION ${PROJECT_BINARY_DIR}/resources/)
file(REMOVE file(REMOVE
${PROJECT_BINARY_DIR}/resources/mario.png ${PROJECT_BINARY_DIR}/resources/mario.png

View File

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

View File

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

View File

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

View File

@@ -1,12 +1,8 @@
#pragma once #pragma once
#include <Options.hpp> #include <SettingsTab.hpp>
class CPUSettings final { struct CPUSettings final : SettingsTab {
int selectedCpuTypeIndex = 0; int selectedCpuTypeIndex = 0;
bool modified = false; void render() override;
public:
bool render();
explicit CPUSettings(); 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 <log.hpp>
#include <Options.hpp> #include <Options.hpp>
#include <imgui.h> #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) { SettingsWindow::SettingsWindow(gui::NativeWindow& window) : window(window) {
savesPath = Options::GetInstance().GetValue<std::string>("general", "savePath"); savesPath = Options::GetInstance().GetValue<std::string>("general", "savePath");
} }
bool SettingsWindow::render() { bool SettingsWindow::render() {
ImVec2 center = ImGui::GetMainViewport()->GetCenter(); const ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if(!ImGui::BeginPopupModal("Settings", &isOpen)) if(!ImGui::BeginPopupModal("Settings", &isOpen))
@@ -17,46 +46,14 @@ bool SettingsWindow::render() {
if(!ImGui::BeginTabBar("SettingsTabBar")) if(!ImGui::BeginTabBar("SettingsTabBar"))
return false; return false;
if(ImGui::BeginTabItem("General")) { for (auto& [name, tab] : tabs) {
if(ImGui::Button("Pick...")) { if (ImGui::BeginTabItem(name.c_str())) {
SDL_ShowOpenFolderDialog([](void *userdata, const char * const *filelist, int filter) { tab->render();
SettingsWindow* settings = (SettingsWindow*)userdata; if (tab->modified && !applyEnabled)
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())
applyEnabled = true; applyEnabled = true;
}
ImGui::EndTabItem();
}
if(ImGui::BeginTabItem("Audio")) { ImGui::EndTabItem();
if(audioSettings.render()) {
if(audioSettings.getModified())
applyEnabled = true;
} }
ImGui::EndTabItem();
} }
ImGui::EndTabBar(); ImGui::EndTabBar();
@@ -65,6 +62,10 @@ bool SettingsWindow::render() {
if(ImGui::Button("Apply")) { if(ImGui::Button("Apply")) {
applyEnabled = false; applyEnabled = false;
Options::GetInstance().Apply(); Options::GetInstance().Apply();
for (auto &tab : tabs | std::views::values) {
tab->modified = false;
}
} }
ImGui::EndDisabled(); ImGui::EndDisabled();

View File

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