From 9454e11fd9a06b6cde30249b53859393cb56bed6 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Sun, 12 Feb 2023 08:43:08 +0300 Subject: [PATCH] Adjusted exit key: added shortcut binding, confirmation dialog. --- SpaceCadetPinball/font_selection.cpp | 4 +- SpaceCadetPinball/high_score.cpp | 13 ++--- SpaceCadetPinball/options.cpp | 25 +++++--- SpaceCadetPinball/options.h | 1 + SpaceCadetPinball/translations.cpp | 62 +------------------- SpaceCadetPinball/translations.h | 6 +- SpaceCadetPinball/winmain.cpp | 86 ++++++++++++++++------------ SpaceCadetPinball/winmain.h | 6 +- 8 files changed, 81 insertions(+), 122 deletions(-) diff --git a/SpaceCadetPinball/font_selection.cpp b/SpaceCadetPinball/font_selection.cpp index 146a8d1..496004d 100644 --- a/SpaceCadetPinball/font_selection.cpp +++ b/SpaceCadetPinball/font_selection.cpp @@ -35,7 +35,7 @@ void font_selection::RenderDialog() ImGui::SameLine(); ImGui::InputText("##Font", DialogInputBuffer, IM_ARRAYSIZE(DialogInputBuffer)); - if (ImGui::Button(pb::get_rc_string(Msg::HIGHSCORES_Ok))) + if (ImGui::Button(pb::get_rc_string(Msg::GenericOk))) { options::Options.FontFileName.V = DialogInputBuffer; ImGui::CloseCurrentPopup(); @@ -43,7 +43,7 @@ void font_selection::RenderDialog() } ImGui::SameLine(); - if (ImGui::Button(pb::get_rc_string(Msg::HIGHSCORES_Cancel))) + if (ImGui::Button(pb::get_rc_string(Msg::GenericCancel))) ImGui::CloseCurrentPopup(); ImGui::EndPopup(); diff --git a/SpaceCadetPinball/high_score.cpp b/SpaceCadetPinball/high_score.cpp index 93dee92..8afb519 100644 --- a/SpaceCadetPinball/high_score.cpp +++ b/SpaceCadetPinball/high_score.cpp @@ -172,12 +172,9 @@ void high_score::RenderHighScoreDialog() score = DlgData.Entry.Score; ImGui::PushItemWidth(200); - static bool doubleFocus = false; - if (ImGui::IsWindowAppearing() || doubleFocus) + if (ImGui::IsWindowAppearing()) { - // Hack - set focus only works on the second time. ImGui::SetKeyboardFocusHere(0); - doubleFocus ^= true; } if (ImGui::InputText("", DlgData.Entry.Name, IM_ARRAYSIZE(DlgData.Entry.Name), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll)) @@ -197,7 +194,7 @@ void high_score::RenderHighScoreDialog() ImGui::EndTable(); } - if (ImGui::Button(pb::get_rc_string(Msg::HIGHSCORES_Ok)) || textBoxSubmit) + if (ImGui::Button(pb::get_rc_string(Msg::GenericOk)) || textBoxSubmit) { if (dlg_enter_name) { @@ -207,7 +204,7 @@ void high_score::RenderHighScoreDialog() } ImGui::SameLine(); - if (ImGui::Button(pb::get_rc_string(Msg::HIGHSCORES_Cancel))) + if (ImGui::Button(pb::get_rc_string(Msg::GenericCancel))) ImGui::CloseCurrentPopup(); ImGui::SameLine(); @@ -216,14 +213,14 @@ void high_score::RenderHighScoreDialog() if (ImGui::BeginPopupModal("Confirm", nullptr, ImGuiWindowFlags_MenuBar)) { ImGui::TextUnformatted(pb::get_rc_string(Msg::STRING141)); - if (ImGui::Button(pb::get_rc_string(Msg::HIGHSCORES_Ok), ImVec2(120, 0))) + if (ImGui::Button(pb::get_rc_string(Msg::GenericOk), ImVec2(120, 0))) { clear_table(); ImGui::CloseCurrentPopup(); } ImGui::SetItemDefaultFocus(); ImGui::SameLine(); - if (ImGui::Button(pb::get_rc_string(Msg::HIGHSCORES_Cancel), ImVec2(120, 0))) + if (ImGui::Button(pb::get_rc_string(Msg::GenericCancel), ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } diff --git a/SpaceCadetPinball/options.cpp b/SpaceCadetPinball/options.cpp index 51f3bdb..fae5a65 100644 --- a/SpaceCadetPinball/options.cpp +++ b/SpaceCadetPinball/options.cpp @@ -67,51 +67,58 @@ optionsStruct options::Options "New Game", Msg::Menu1_New_Game, {InputTypes::Keyboard, SDLK_F2}, - {InputTypes::None,-1}, + {InputTypes::None, -1}, {InputTypes::None, -1} }, { "Toggle Pause", Msg::Menu1_Pause_Resume_Game, {InputTypes::Keyboard, SDLK_F3}, - {InputTypes::None,-1}, + {InputTypes::None, -1}, {InputTypes::GameController, SDL_CONTROLLER_BUTTON_START} }, { "Toggle FullScreen", Msg::Menu1_Full_Screen, {InputTypes::Keyboard, SDLK_F4}, - {InputTypes::None,-1}, + {InputTypes::None, -1}, {InputTypes::None, -1} }, { "Toggle Sounds", Msg::Menu1_Sounds, {InputTypes::Keyboard, SDLK_F5}, - {InputTypes::None,-1}, + {InputTypes::None, -1}, {InputTypes::None, -1} }, { "Toggle Music", Msg::Menu1_Music, {InputTypes::Keyboard, SDLK_F6}, - {InputTypes::None,-1}, + {InputTypes::None, -1}, {InputTypes::None, -1} }, { "Show Control Dialog", Msg::Menu1_Player_Controls, {InputTypes::Keyboard, SDLK_F8}, - {InputTypes::None,-1}, + {InputTypes::None, -1}, {InputTypes::None, -1} }, { "Toggle Menu Display", Msg::Menu1_ToggleShowMenu, {InputTypes::Keyboard, SDLK_F9}, - {InputTypes::None,-1}, + {InputTypes::None, -1}, {InputTypes::None, -1} }, + { + "Exit", + Msg::Menu1_Exit, + {InputTypes::Keyboard, SDLK_ESCAPE}, + {InputTypes::None, -1}, + {InputTypes::GameController, SDL_CONTROLLER_BUTTON_BACK} + }, }, {"Sounds", true}, {"Music", false}, @@ -411,13 +418,13 @@ void options::RenderControlDialog() ImGui::PopStyleVar(); ImGui::Spacing(); - if (ImGui::Button(pb::get_rc_string(Msg::KEYMAPPER_Ok))) + if (ImGui::Button(pb::get_rc_string(Msg::GenericOk))) { ShowDialog = false; } ImGui::SameLine(); - if (ImGui::Button(pb::get_rc_string(Msg::KEYMAPPER_Cancel))) + if (ImGui::Button(pb::get_rc_string(Msg::GenericCancel))) { for (auto& control : Options.Key) { diff --git a/SpaceCadetPinball/options.h b/SpaceCadetPinball/options.h index e64e36a..3d53c60 100644 --- a/SpaceCadetPinball/options.h +++ b/SpaceCadetPinball/options.h @@ -72,6 +72,7 @@ enum class GameBindings ToggleMusic, ShowControlDialog, ToggleMenuDisplay, + Exit, Max }; diff --git a/SpaceCadetPinball/translations.cpp b/SpaceCadetPinball/translations.cpp index c80ca82..eebe99a 100644 --- a/SpaceCadetPinball/translations.cpp +++ b/SpaceCadetPinball/translations.cpp @@ -5056,7 +5056,7 @@ const TextArray translations::Translations = }, }, { - Msg::HIGHSCORES_Ok, + Msg::GenericOk, { { Lang::Arabic, "موافق" }, { Lang::Czech, "OK" }, @@ -5085,7 +5085,7 @@ const TextArray translations::Translations = }, }, { - Msg::HIGHSCORES_Cancel, + Msg::GenericCancel, { { Lang::Arabic, "إلغاء الأمر" }, { Lang::Czech, "Storno" }, @@ -5258,64 +5258,6 @@ const TextArray translations::Translations = { Lang::TraditionalChinese, "立體彈珠台:玩家控制" }, }, }, - { - Msg::KEYMAPPER_Ok, - { - { Lang::Arabic, "موافق" }, - { Lang::Czech, "OK" }, - { Lang::Danish, "OK" }, - { Lang::German, "OK" }, - { Lang::Greek, "OK" }, - { Lang::English, "OK" }, - { Lang::Spanish, "Aceptar" }, - { Lang::Finnish, "OK" }, - { Lang::French, "OK" }, - { Lang::Hebrew, "אישור" }, - { Lang::Hungarian, "OK" }, - { Lang::Italian, "OK" }, - { Lang::Japanese, "OK" }, - { Lang::Korean, "확인" }, - { Lang::Norwegian, "OK" }, - { Lang::Dutch, "OK" }, - { Lang::Polish, "OK" }, - { Lang::BrazilianPortuguese, "OK" }, - { Lang::Portuguese, "OK" }, - { Lang::Russian, "ОК" }, - { Lang::Swedish, "OK" }, - { Lang::Turkish, "Tamam" }, - { Lang::SimplifiedChinese, "确定" }, - { Lang::TraditionalChinese, "確定" }, - }, - }, - { - Msg::KEYMAPPER_Cancel, - { - { Lang::Arabic, "إلغاء الأمر" }, - { Lang::Czech, "Storno" }, - { Lang::Danish, "Annuller" }, - { Lang::German, "Abbrechen" }, - { Lang::Greek, "Άκυρο" }, - { Lang::English, "Cancel" }, - { Lang::Spanish, "Cancelar" }, - { Lang::Finnish, "Peruuta" }, - { Lang::French, "Annuler" }, - { Lang::Hebrew, "ביטול" }, - { Lang::Hungarian, "Mégse" }, - { Lang::Italian, "Annulla" }, - { Lang::Japanese, "キャンセル" }, - { Lang::Korean, "취소" }, - { Lang::Norwegian, "Avbryt" }, - { Lang::Dutch, "Annuleren" }, - { Lang::Polish, "Anuluj" }, - { Lang::BrazilianPortuguese, "Cancelar" }, - { Lang::Portuguese, "Cancelar" }, - { Lang::Russian, "Отмена" }, - { Lang::Swedish, "Avbryt" }, - { Lang::Turkish, "İptal" }, - { Lang::SimplifiedChinese, "取消" }, - { Lang::TraditionalChinese, "取消" }, - }, - }, { Msg::KEYMAPPER_FlipperL, { diff --git a/SpaceCadetPinball/translations.h b/SpaceCadetPinball/translations.h index 173a181..fa33489 100644 --- a/SpaceCadetPinball/translations.h +++ b/SpaceCadetPinball/translations.h @@ -175,15 +175,13 @@ enum class Msg : int TextBoxColor, HIGHSCORES_Caption, - HIGHSCORES_Ok, - HIGHSCORES_Cancel, + GenericOk, + GenericCancel, HIGHSCORES_Clear, HIGHSCORES_Name, HIGHSCORES_Score, HIGHSCORES_Rank, KEYMAPPER_Caption, - KEYMAPPER_Ok, - KEYMAPPER_Cancel, KEYMAPPER_FlipperL, KEYMAPPER_FlipperR, KEYMAPPER_Plunger, diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index ccc216f..d7e995b 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -20,7 +20,7 @@ ImGuiIO* winmain::ImIO = nullptr; int winmain::return_value = 0; bool winmain::bQuit = false; bool winmain::activated = false; -int winmain::DispFrameRate = 0; +bool winmain::DispFrameRate = false; bool winmain::DispGRhistory = false; bool winmain::single_step = false; bool winmain::has_focus = true; @@ -31,12 +31,13 @@ bool winmain::no_time_loss = false; bool winmain::restart = false; -std::vector winmain::gfrDisplay {}; +std::vector winmain::gfrDisplay{}; unsigned winmain::gfrOffset = 0; float winmain::gfrWindow = 5.0f; bool winmain::ShowAboutDialog = false; bool winmain::ShowImGuiDemo = false; bool winmain::ShowSpriteViewer = false; +bool winmain::ShowExitPopup = false; bool winmain::LaunchBallEnabled = true; bool winmain::HighScoresEnabled = true; bool winmain::DemoActive = false; @@ -173,7 +174,7 @@ int winmain::WinMain(LPCSTR lpCmdLine) } ImGui_Render_Init(renderer); ImGui::StyleColorsDark(); - + ImGui_ImplSDL2_InitForSDLRenderer(window, Renderer); io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad; @@ -488,11 +489,7 @@ void winmain::RenderUi() end_pause(); pb::toggle_demo(); } - if (ImGui::MenuItem(pb::get_rc_string(Msg::Menu1_Exit))) - { - SDL_Event event{SDL_QUIT}; - SDL_PushEvent(&event); - } + ImGuiMenuItemWShortcut(GameBindings::Exit); ImGui::EndMenu(); } @@ -594,8 +591,8 @@ void winmain::RenderUi() { options::toggle(Menu1::WindowIntegerScale); } - if(ImGui::DragFloat("UI Scale", &Options.UIScale.V, 0.005f, 0.8f, 5, - "%.2f", ImGuiSliderFlags_AlwaysClamp)) + if (ImGui::DragFloat("UI Scale", &Options.UIScale.V, 0.005f, 0.8f, 5, + "%.2f", ImGuiSliderFlags_AlwaysClamp)) { ImIO->FontGlobalScale = Options.UIScale; } @@ -771,6 +768,38 @@ void winmain::RenderUi() if (DispGRhistory) RenderFrameTimeDialog(); + const auto exitText = translations::GetTranslation(Msg::Menu1_Exit); + if (ShowExitPopup) + { + ShowExitPopup = false; + pause(false); + ImGui::OpenPopup(exitText); + } + if (ImGui::BeginPopupModal(exitText, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) + { + ImGui::Text("Exit the game?"); + ImGui::Separator(); + + if (ImGui::Button(pb::get_rc_string(Msg::GenericOk), ImVec2(120, 0))) + { + SDL_Event event{SDL_QUIT}; + SDL_PushEvent(&event); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::IsWindowAppearing()) + { + ImGui::SetKeyboardFocusHere(0); + } + if (ImGui::Button(pb::get_rc_string(Msg::GenericCancel), ImVec2(120, 0))) + { + end_pause(); + ImGui::CloseCurrentPopup(); + } + ImGui::SetItemDefaultFocus(); + ImGui::EndPopup(); + } + // Print game texts on the sidebar gdrv::grtext_draw_ttext_in_box(); } @@ -821,19 +850,10 @@ int winmain::event_handler(const SDL_Event* event) pb::InputUp({InputTypes::Keyboard, event->key.keysym.sym}); break; case SDL_KEYDOWN: - if (!event->key.repeat) - pb::InputDown({InputTypes::Keyboard, event->key.keysym.sym}); - switch (event->key.keysym.sym) - { - case SDLK_ESCAPE: - if (Options.FullScreen) - options::toggle(Menu1::Full_Screen); - SDL_MinimizeWindow(MainWindow); + if (event->key.repeat) break; - default: - break; - } + pb::InputDown({InputTypes::Keyboard, event->key.keysym.sym}); if (!pb::cheat_mode) break; @@ -863,7 +883,7 @@ int winmain::event_handler(const SDL_Event* event) break; case SDLK_y: SDL_SetWindowTitle(MainWindow, "Pinball"); - DispFrameRate = DispFrameRate == 0; + DispFrameRate ^= true; break; case SDLK_F1: pb::frame(10); @@ -968,17 +988,6 @@ int winmain::event_handler(const SDL_Event* event) break; case SDL_CONTROLLERBUTTONDOWN: pb::InputDown({InputTypes::GameController, event->cbutton.button}); - switch (event->cbutton.button) - { - case SDL_CONTROLLER_BUTTON_BACK: - if (single_step) - { - SDL_Event event{SDL_QUIT}; - SDL_PushEvent(&event); - } - break; - default: ; - } break; case SDL_CONTROLLERBUTTONUP: pb::InputUp({InputTypes::GameController, event->cbutton.button}); @@ -1124,6 +1133,11 @@ void winmain::HandleGameBinding(GameBindings binding) case GameBindings::ToggleMenuDisplay: options::toggle(Menu1::Show_Menu); break; + case GameBindings::Exit: + { + ShowExitPopup = true; + break; + } default: break; } @@ -1143,13 +1157,13 @@ void winmain::RenderFrameTimeDialog() static bool scrollPlot = true; ImGui::Checkbox("Scroll Plot", &scrollPlot); - + ImGui::SameLine(); ImGui::SliderFloat("Window Size", &gfrWindow, 0.1f, 15, "%.3fsec", ImGuiSliderFlags_AlwaysClamp); { float average = 0.0f, dev = 0.0f; - for (auto n : gfrDisplay) + for (auto n : gfrDisplay) { average += n; dev += std::abs(target - n); @@ -1161,7 +1175,7 @@ void winmain::RenderFrameTimeDialog() auto region = ImGui::GetContentRegionAvail(); ImGui::PlotLines("Lines", gfrDisplay.data(), gfrDisplay.size(), - scrollPlot ? gfrOffset : 0, overlay, 0, yMax, region); + scrollPlot ? gfrOffset : 0, overlay, 0, yMax, region); } } ImGui::End(); diff --git a/SpaceCadetPinball/winmain.h b/SpaceCadetPinball/winmain.h index 4a59626..a78c222 100644 --- a/SpaceCadetPinball/winmain.h +++ b/SpaceCadetPinball/winmain.h @@ -43,7 +43,6 @@ struct WelfordState WelfordState() : mean(0.005), M2(0), count(1) { - } void Advance(double newValue) @@ -89,15 +88,16 @@ public: static void UpdateFrameRate(); static void HandleGameBinding(GameBindings binding); private: - static int return_value, DispFrameRate; + static int return_value; static int mouse_down, last_mouse_x, last_mouse_y; - static bool no_time_loss, activated, bQuit, has_focus, DispGRhistory; + static bool no_time_loss, activated, bQuit, has_focus, DispGRhistory, DispFrameRate; static std::vector gfrDisplay; static std::string FpsDetails, PrevSdlError; static bool restart; static bool ShowAboutDialog; static bool ShowImGuiDemo; static bool ShowSpriteViewer; + static bool ShowExitPopup; static double UpdateToFrameRatio; static DurationMs TargetFrameTime; static struct optionsStruct& Options;