From ddd1c4f301833cac671b045ca7920bbde5b1e41e Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sat, 6 Mar 2021 17:41:32 +0000 Subject: [PATCH] Move ImGui Settings window(s) to a separate class. Signed-off-by: Andrea Odetti --- source/frontends/sdl/CMakeLists.txt | 2 + source/frontends/sdl/imgui/sdlimguiframe.cpp | 149 +---------------- source/frontends/sdl/imgui/sdlimguiframe.h | 13 +- source/frontends/sdl/imgui/sdlsettings.cpp | 162 +++++++++++++++++++ source/frontends/sdl/imgui/sdlsettings.h | 25 +++ 5 files changed, 196 insertions(+), 155 deletions(-) create mode 100644 source/frontends/sdl/imgui/sdlsettings.cpp create mode 100644 source/frontends/sdl/imgui/sdlsettings.h diff --git a/source/frontends/sdl/CMakeLists.txt b/source/frontends/sdl/CMakeLists.txt index 2a30dbaf..fd9201bd 100644 --- a/source/frontends/sdl/CMakeLists.txt +++ b/source/frontends/sdl/CMakeLists.txt @@ -49,10 +49,12 @@ target_sources(sa2 PRIVATE imgui/sdlimguiframe.cpp imgui/image.cpp imgui/settingshelper.cpp + imgui/sdlsettings.cpp imgui/sdlimguiframe.h imgui/image.h imgui/settingshelper.h + imgui/sdlsettings.h imgui/imconfig.h imgui/gles.h diff --git a/source/frontends/sdl/imgui/sdlimguiframe.cpp b/source/frontends/sdl/imgui/sdlimguiframe.cpp index 63516e82..9bf251c0 100644 --- a/source/frontends/sdl/imgui/sdlimguiframe.cpp +++ b/source/frontends/sdl/imgui/sdlimguiframe.cpp @@ -5,35 +5,12 @@ #include "frontends/common2/fileregistry.h" #include "frontends/common2/programoptions.h" #include "frontends/sdl/imgui/image.h" -#include "frontends/sdl/imgui/settingshelper.h" #include "Interface.h" #include "Core.h" -#include "CPU.h" -#include "CardManager.h" -#include "Speaker.h" -#include "Mockingboard.h" -#include "Registry.h" #include -namespace -{ - - void HelpMarker(const char* desc) - { - ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered()) - { - ImGui::BeginTooltip(); - ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); - ImGui::TextUnformatted(desc); - ImGui::PopTextWrapPos(); - ImGui::EndTooltip(); - } - } - -} namespace sa2 { @@ -84,14 +61,14 @@ namespace sa2 ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); - mySettings.iniFileLocation = common2::GetConfigFile("imgui.ini"); - if (mySettings.iniFileLocation.empty()) + myIniFileLocation = common2::GetConfigFile("imgui.ini"); + if (myIniFileLocation.empty()) { io.IniFilename = nullptr; } else { - io.IniFilename = mySettings.iniFileLocation.c_str(); + io.IniFilename = myIniFileLocation.c_str(); } //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls @@ -147,23 +124,7 @@ namespace sa2 const ImVec2 uv0(0, 1); const ImVec2 uv1(1, 0); - float menuBarHeight; - - if (ImGui::BeginMainMenuBar()) - { - menuBarHeight = ImGui::GetWindowHeight(); - if (ImGui::BeginMenu("System")) - { - ImGui::MenuItem("Settings", nullptr, &mySettings.showSettings); - ImGui::MenuItem("Demo", nullptr, &mySettings.showDemo); - ImGui::EndMenu(); - } - ImGui::EndMainMenuBar(); - } - else - { - menuBarHeight = 0.0; - } + const float menuBarHeight = mySettings.drawMenuBar(); if (mySettings.windowed) { @@ -182,113 +143,13 @@ namespace sa2 } } - void SDLImGuiFrame::ShowSettings() - { - if (mySettings.showSettings) - { - if (ImGui::Begin("Settings", &mySettings.showSettings)) - { - ImGuiIO& io = ImGui::GetIO(); - - if (ImGui::BeginTabBar("Settings")) - { - if (ImGui::BeginTabItem("General")) - { - ImGui::Checkbox("Apple Video windowed", &mySettings.windowed); - ImGui::SameLine(); HelpMarker("Show Apple Video in a separate window."); - - ImGui::Checkbox("Show Demo", &mySettings.showDemo); - ImGui::SameLine(); HelpMarker("Show Dear ImGui DemoWindow."); - - ImGui::Text("FPS: %d", int(io.Framerate)); - ImGui::EndTabItem(); - } - if (ImGui::BeginTabItem("Hardware")) - { - if (ImGui::BeginTable("Cards", 2, ImGuiTableFlags_RowBg)) - { - CardManager & manager = GetCardMgr(); - ImGui::TableSetupColumn("Slot"); - ImGui::TableSetupColumn("Card"); - ImGui::TableHeadersRow(); - - for (size_t slot = 0; slot < 8; ++slot) - { - ImGui::TableNextColumn(); - ImGui::Selectable(std::to_string(slot).c_str()); - ImGui::TableNextColumn(); - const SS_CARDTYPE card = manager.QuerySlot(slot);; - ImGui::Selectable(getCardName(card).c_str()); - } - ImGui::TableNextColumn(); - ImGui::Selectable("AUX"); - ImGui::TableNextColumn(); - const SS_CARDTYPE card = manager.QueryAux(); - ImGui::Selectable(getCardName(card).c_str()); - - ImGui::EndTable(); - } - ImGui::Separator(); - - if (ImGui::BeginTable("Type", 2, ImGuiTableFlags_RowBg)) - { - ImGui::TableNextColumn(); - ImGui::Selectable("Apple 2"); - ImGui::TableNextColumn(); - const eApple2Type a2e = GetApple2Type(); - ImGui::Selectable(getApple2Name(a2e).c_str()); - - ImGui::TableNextColumn(); - ImGui::Selectable("CPU"); - ImGui::TableNextColumn(); - const eCpuType cpu = GetMainCpu(); - ImGui::Selectable(getCPUName(cpu).c_str()); - - ImGui::EndTable(); - } - - ImGui::EndTabItem(); - } - - if (ImGui::BeginTabItem("Audio")) - { - const int volumeMax = GetPropertySheet().GetVolumeMax(); - if (ImGui::SliderInt("Speaker volume", &mySettings.speakerVolume, 0, volumeMax)) - { - SpkrSetVolume(volumeMax - mySettings.speakerVolume, volumeMax); - REGSAVE(TEXT(REGVALUE_SPKR_VOLUME), SpkrGetVolume()); - } - - if (ImGui::SliderInt("Mockingboard volume", &mySettings.mockingboardVolume, 0, volumeMax)) - { - MB_SetVolume(volumeMax - mySettings.mockingboardVolume, volumeMax); - REGSAVE(TEXT(REGVALUE_MB_VOLUME), MB_GetVolume()); - } - - ImGui::EndTabItem(); - } - - ImGui::EndTabBar(); - } - - } - ImGui::End(); - } - } - void SDLImGuiFrame::RenderPresent() { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplSDL2_NewFrame(myWindow.get()); ImGui::NewFrame(); - ShowSettings(); - - if (mySettings.showDemo) - { - ImGui::ShowDemoWindow(&mySettings.showDemo); - } - + mySettings.show(); DrawAppleVideo(); ImGui::Render(); diff --git a/source/frontends/sdl/imgui/sdlimguiframe.h b/source/frontends/sdl/imgui/sdlimguiframe.h index 28a786fe..5a8de7b4 100644 --- a/source/frontends/sdl/imgui/sdlimguiframe.h +++ b/source/frontends/sdl/imgui/sdlimguiframe.h @@ -1,6 +1,7 @@ #pragma once #include "frontends/sdl/sdlframe.h" +#include "frontends/sdl/imgui/sdlsettings.h" #include "frontends/sdl/imgui/gles.h" namespace common2 @@ -29,17 +30,6 @@ namespace sa2 void ClearBackground(); void DrawAppleVideo(); - void ShowSettings(); - - struct ImGuiSettings - { - std::string iniFileLocation; - bool windowed = false; - bool showDemo = false; - bool showSettings = false; - int speakerVolume = 50; - int mockingboardVolume = 50; - }; size_t myPitch; size_t myOffset; @@ -49,6 +39,7 @@ namespace sa2 SDL_GLContext myGLContext; ImTextureID myTexture; + std::string myIniFileLocation; ImGuiSettings mySettings; }; diff --git a/source/frontends/sdl/imgui/sdlsettings.cpp b/source/frontends/sdl/imgui/sdlsettings.cpp new file mode 100644 index 00000000..3cd335a9 --- /dev/null +++ b/source/frontends/sdl/imgui/sdlsettings.cpp @@ -0,0 +1,162 @@ +#include "StdAfx.h" +#include "frontends/sdl/imgui/sdlsettings.h" +#include "frontends/sdl/imgui/settingshelper.h" + +#include "Interface.h" +#include "CardManager.h" +#include "Core.h" +#include "CPU.h" +#include "CardManager.h" +#include "Speaker.h" +#include "Mockingboard.h" +#include "Registry.h" + +namespace +{ + + void HelpMarker(const char* desc) + { + ImGui::TextDisabled("(?)"); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); + ImGui::TextUnformatted(desc); + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } + } + +} + +namespace sa2 +{ + + void ImGuiSettings::showSettings() + { + if (ImGui::Begin("Settings", &myShowSettings)) + { + ImGuiIO& io = ImGui::GetIO(); + + if (ImGui::BeginTabBar("Settings")) + { + if (ImGui::BeginTabItem("General")) + { + ImGui::Checkbox("Apple Video windowed", &windowed); + ImGui::SameLine(); HelpMarker("Show Apple Video in a separate window."); + + ImGui::Checkbox("Show Demo", &myShowDemo); + ImGui::SameLine(); HelpMarker("Show Dear ImGui DemoWindow."); + + ImGui::Text("FPS: %d", int(io.Framerate)); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Hardware")) + { + if (ImGui::BeginTable("Cards", 2, ImGuiTableFlags_RowBg)) + { + CardManager & manager = GetCardMgr(); + ImGui::TableSetupColumn("Slot"); + ImGui::TableSetupColumn("Card"); + ImGui::TableHeadersRow(); + + for (size_t slot = 0; slot < 8; ++slot) + { + ImGui::TableNextColumn(); + ImGui::Selectable(std::to_string(slot).c_str()); + ImGui::TableNextColumn(); + const SS_CARDTYPE card = manager.QuerySlot(slot);; + ImGui::Selectable(getCardName(card).c_str()); + } + ImGui::TableNextColumn(); + ImGui::Selectable("AUX"); + ImGui::TableNextColumn(); + const SS_CARDTYPE card = manager.QueryAux(); + ImGui::Selectable(getCardName(card).c_str()); + + ImGui::EndTable(); + } + ImGui::Separator(); + + if (ImGui::BeginTable("Type", 2, ImGuiTableFlags_RowBg)) + { + ImGui::TableNextColumn(); + ImGui::Selectable("Apple 2"); + ImGui::TableNextColumn(); + const eApple2Type a2e = GetApple2Type(); + ImGui::Selectable(getApple2Name(a2e).c_str()); + + ImGui::TableNextColumn(); + ImGui::Selectable("CPU"); + ImGui::TableNextColumn(); + const eCpuType cpu = GetMainCpu(); + ImGui::Selectable(getCPUName(cpu).c_str()); + + ImGui::EndTable(); + } + + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("Audio")) + { + const int volumeMax = GetPropertySheet().GetVolumeMax(); + if (ImGui::SliderInt("Speaker volume", &mySpeakerVolume, 0, volumeMax)) + { + SpkrSetVolume(volumeMax - mySpeakerVolume, volumeMax); + REGSAVE(TEXT(REGVALUE_SPKR_VOLUME), SpkrGetVolume()); + } + + if (ImGui::SliderInt("Mockingboard volume", &myMockingboardVolume, 0, volumeMax)) + { + MB_SetVolume(volumeMax - myMockingboardVolume, volumeMax); + REGSAVE(TEXT(REGVALUE_MB_VOLUME), MB_GetVolume()); + } + + ImGui::EndTabItem(); + } + + ImGui::EndTabBar(); + } + + } + ImGui::End(); + } + + void ImGuiSettings::show() + { + if (myShowSettings) + { + showSettings(); + } + + if (myShowDemo) + { + ImGui::ShowDemoWindow(&myShowDemo); + } + } + + float ImGuiSettings::drawMenuBar() + { + float menuBarHeight; + + if (ImGui::BeginMainMenuBar()) + { + menuBarHeight = ImGui::GetWindowHeight(); + if (ImGui::BeginMenu("System")) + { + ImGui::MenuItem("Settings", nullptr, &myShowSettings); + ImGui::MenuItem("Demo", nullptr, &myShowDemo); + ImGui::EndMenu(); + } + ImGui::EndMainMenuBar(); + } + else + { + menuBarHeight = 0.0; + } + + return menuBarHeight; + } + +} diff --git a/source/frontends/sdl/imgui/sdlsettings.h b/source/frontends/sdl/imgui/sdlsettings.h new file mode 100644 index 00000000..ee2ea0df --- /dev/null +++ b/source/frontends/sdl/imgui/sdlsettings.h @@ -0,0 +1,25 @@ +#pragma once + +#include "frontends/sdl/imgui/gles.h" + +namespace sa2 +{ + + class ImGuiSettings + { + public: + void show(); + float drawMenuBar(); + + bool windowed = false; + + private: + bool myShowDemo = false; + bool myShowSettings = false; + int mySpeakerVolume = 50; + int myMockingboardVolume = 50; + + void showSettings(); + }; + +}