From c8ef298c56b0687df9910d8bd47850139ae791ba Mon Sep 17 00:00:00 2001 From: kichikuou Date: Thu, 6 Mar 2025 08:15:46 +0900 Subject: [PATCH] Add quit confirmation dialog On Android, the back button opens the dialog. --- src/sdlmain.cpp | 1 + src/sys/nact.h | 1 + src/sys/nact_input.cpp | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/sdlmain.cpp b/src/sdlmain.cpp index 0da0240..9d30558 100644 --- a/src/sdlmain.cpp +++ b/src/sdlmain.cpp @@ -44,6 +44,7 @@ SDL_Window* create_window(const GameId& game_id) #endif #ifdef __ANDROID__ + SDL_SetHint(SDL_HINT_ANDROID_TRAP_BACK_BUTTON, "1"); SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight"); Uint32 flags = SDL_WINDOW_FULLSCREEN; #else diff --git a/src/sys/nact.h b/src/sys/nact.h index de1df53..cf87133 100644 --- a/src/sys/nact.h +++ b/src/sys/nact.h @@ -196,6 +196,7 @@ private: void pump_events(); void handle_event(SDL_Event e); bool handle_platform_event(const SDL_Event& e); + void show_quit_dialog(); static NACT* create_system1(const Config& config, const GameId& game_id); static NACT* create_system2(const Config& config, const GameId& game_id); diff --git a/src/sys/nact_input.cpp b/src/sys/nact_input.cpp index 01e1db5..06843eb 100644 --- a/src/sys/nact_input.cpp +++ b/src/sys/nact_input.cpp @@ -33,9 +33,17 @@ void NACT::handle_event(SDL_Event e) switch (e.type) { case SDL_QUIT: - quit(0); + show_quit_dialog(); break; +#ifdef __ANDROID__ + case SDL_KEYUP: + if (e.key.keysym.scancode == SDL_SCANCODE_AC_BACK) { + show_quit_dialog(); + } + break; +#endif + case SDL_MOUSEMOTION: mousex = e.motion.x * ags->screen_width / ags->window_width; mousey = e.motion.y * ags->screen_height / ags->window_height; @@ -148,6 +156,30 @@ void NACT::set_cursor(int x, int y) SDL_WarpMouseInWindow(g_window, x, y); } +void NACT::show_quit_dialog() +{ + const SDL_MessageBoxButtonData buttons[] = { + { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "Quit" }, + { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "Cancel" }, + }; + const SDL_MessageBoxData messagebox_data = { + SDL_MESSAGEBOX_INFORMATION, + nullptr, + "System3", + "Quit game?\nAny unsaved progress will be lost.", + SDL_arraysize(buttons), + buttons, + }; + int buttonid = 0; + if (SDL_ShowMessageBox(&messagebox_data, &buttonid) < 0) { + WARNING("error displaying message box"); + return; + } + if (buttonid == 1) { + quit(0); + } +} + #ifdef __EMSCRIPTEN__ extern "C" {