From ed85506dbe4f1fb2c78fd33adf987e243525fb23 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 19 Dec 2016 22:23:16 +0100 Subject: [PATCH] iOS: pause game/status text from external code (#50) * external game pausing * external status text * fix compilation on other platforms --- src/game.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++ src/touchscreengui.cpp | 11 ++++++++ src/touchscreengui.h | 8 ++++-- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 55cb14980..024d69303 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1495,6 +1495,10 @@ public: void run(); void shutdown(); +#ifdef __IOS__ + void pauseGame(); + void customStatustext(const std::wstring &text, float time); +#endif protected: @@ -1699,6 +1703,9 @@ private: #ifdef __ANDROID__ bool m_android_chat_open; #endif +#ifdef __IOS__ + GameRunData* runData_ptr; +#endif }; Game::Game() : @@ -1722,6 +1729,9 @@ Game::Game() : sky(NULL), local_inventory(NULL), hud(NULL), +#ifdef __IOS__ + runData_ptr(NULL), +#endif mapper(NULL) { g_settings->registerChangedCallback("doubletap_jump", @@ -1841,6 +1851,9 @@ void Game::run() runData.time_from_last_punch = 10.0; runData.profiler_max_page = 3; runData.update_wielded_item_trigger = true; +#ifdef __IOS__ + runData_ptr = &runData; +#endif flags.show_chat = true; flags.show_hud = true; @@ -4526,6 +4539,28 @@ void Game::readSettings() m_cache_mouse_sensitivity = rangelim(m_cache_mouse_sensitivity, 0.001, 100.0); } +#ifdef __IOS__ +void Game::pauseGame() +{ + g_touchscreengui->handleReleaseAll(); + if (g_menumgr.pausesGame()) + return; + show_pause_menu(¤t_formspec, client, gamedef, + texture_src, device, simple_singleplayer_mode); +} + +void Game::customStatustext(const std::wstring &text, float time) +{ + if (!runData_ptr) + return; + statustext = text; + if(statustext == L"") + runData_ptr->statustext_time = 0; + else + runData_ptr->statustext_time = time; +} +#endif + /****************************************************************************/ /**************************************************************************** Shutdown / cleanup @@ -4560,6 +4595,8 @@ void Game::extendedResourceCleanup() ****************************************************************************/ /****************************************************************************/ +static Game *g_game = NULL; + void the_game(bool *kill, bool random_input, InputHandler *input, @@ -4578,6 +4615,7 @@ void the_game(bool *kill, bool simple_singleplayer_mode) { Game game; + g_game = &game; /* Make a copy of the server address because if a local singleplayer server * is created then this is updated and we don't want to change the value @@ -4607,4 +4645,23 @@ void the_game(bool *kill, error_message = e.what() + strgettext("\nCheck debug.txt for details."); errorstream << "ModError: " << error_message << std::endl; } + + g_game = NULL; } + +#ifdef __IOS__ +void external_pause_game() +{ + if (!g_game) + return; + g_game->pauseGame(); +} + +void external_statustext(const char *text, float duration) +{ + if (!g_game) + return; + std::wstring s = narrow_to_wide(std::string(text)); + g_game->customStatustext(s, duration); +} +#endif diff --git a/src/touchscreengui.cpp b/src/touchscreengui.cpp index a154a2c44..970abb2f6 100644 --- a/src/touchscreengui.cpp +++ b/src/touchscreengui.cpp @@ -609,6 +609,17 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id) } } +#ifdef __IOS__ +void TouchScreenGUI::handleReleaseAll() +{ + m_known_ids.clear(); + if(m_move_id != -1) + handleReleaseEvent(m_move_id); + for(int i = 0; i < after_last_element_id; i++) + m_buttons[i].ids.clear(); +} +#endif + void TouchScreenGUI::translateEvent(const SEvent &event) { if (!m_visible) { diff --git a/src/touchscreengui.h b/src/touchscreengui.h index 330eeddbb..e3c94e253 100644 --- a/src/touchscreengui.h +++ b/src/touchscreengui.h @@ -91,8 +91,9 @@ public: void hide(); void show(); - void storePointerPos(size_t ID, v2s32 pos); - v2s32 loadPointerPos(size_t ID); +#ifdef __IOS__ + void handleReleaseAll(); +#endif private: IrrlichtDevice* m_device; @@ -185,6 +186,9 @@ private: /* array for doubletap detection */ key_event m_key_events[2]; + + void storePointerPos(size_t ID, v2s32 pos); + v2s32 loadPointerPos(size_t ID); }; extern TouchScreenGUI *g_touchscreengui; #endif