diff options
author | Loic Blot <loic.blot@unix-experience.fr> | 2018-01-04 23:32:32 +0100 |
---|---|---|
committer | Loïc Blot <nerzhul@users.noreply.github.com> | 2018-01-05 20:59:30 +0100 |
commit | 02f82eca0b20a4e998e1541413e64766db5d12b0 (patch) | |
tree | d08793f430fefa69faa5ec6b665d11386d5d7980 /src | |
parent | 326b0faa5e0bee62ee3100f345c021cee020f2dd (diff) | |
download | minetest-02f82eca0b20a4e998e1541413e64766db5d12b0.tar.gz minetest-02f82eca0b20a4e998e1541413e64766db5d12b0.tar.bz2 minetest-02f82eca0b20a4e998e1541413e64766db5d12b0.zip |
GameUI refactor (part 6/X): Move Game::guitext_profiler & showStatusTextSimple to GameUI class
Other enhancements:
* Move showStatusTextSimple to GameUI class & rename to showTranslatedStatusText
Diffstat (limited to 'src')
-rw-r--r-- | src/client/gameui.cpp | 62 | ||||
-rw-r--r-- | src/client/gameui.h | 9 | ||||
-rw-r--r-- | src/game.cpp | 154 |
3 files changed, 109 insertions, 116 deletions
diff --git a/src/client/gameui.cpp b/src/client/gameui.cpp index 8a8a091c6..924336165 100644 --- a/src/client/gameui.cpp +++ b/src/client/gameui.cpp @@ -20,12 +20,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "gameui.h" #include <irrlicht_changes/static_text.h> +#include <gettext.h> #include "gui/mainmenumanager.h" #include "util/pointedthing.h" #include "client.h" #include "clientmap.h" #include "fontengine.h" #include "nodedef.h" +#include "profiler.h" #include "renderingengine.h" #include "version.h" @@ -64,6 +66,13 @@ void GameUI::init() m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), //false, false); // Disable word wrap as of now false, true, guiroot); + + // Profiler text (size is updated when text is updated) + m_guitext_profiler = gui::StaticText::add(guienv, L"<Profiler>", + core::rect<s32>(0, 0, 0, 0), false, false, guiroot); + m_guitext_profiler->setBackgroundColor(video::SColor(120, 0, 0, 0)); + m_guitext_profiler->setVisible(false); + m_guitext_profiler->setWordWrap(true); } void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control, @@ -188,6 +197,13 @@ void GameUI::showMinimap(bool show) m_flags.show_minimap = show; } +void GameUI::showTranslatedStatusText(const char *str) +{ + const wchar_t *wmsg = wgettext(str); + showStatusText(wmsg); + delete[] wmsg; +} + void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count, u32 profiler_current_page) { @@ -214,3 +230,49 @@ void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count, m_guitext_chat->setVisible(m_flags.show_chat && recent_chat_count != 0 && profiler_current_page == 0); } + +void GameUI::updateProfiler(u32 profiler_current_page, u32 profiler_max_page) +{ + if (profiler_current_page != 0) { + std::ostringstream os(std::ios_base::binary); + g_profiler->printPage(os, profiler_current_page, + profiler_max_page); + + std::wstring text = translate_string(utf8_to_wide(os.str())); + setStaticText(m_guitext_profiler, text.c_str()); + + s32 w = g_fontengine->getTextWidth(text); + + if (w < 400) + w = 400; + + unsigned text_height = g_fontengine->getTextHeight(); + + core::position2di upper_left, lower_right; + + upper_left.X = 6; + upper_left.Y = (text_height + 5) * 2; + lower_right.X = 12 + w; + lower_right.Y = upper_left.Y + (text_height + 1) * MAX_PROFILER_TEXT_ROWS; + + s32 screen_height = RenderingEngine::get_video_driver()->getScreenSize().Height; + + if (lower_right.Y > screen_height * 2 / 3) + lower_right.Y = screen_height * 2 / 3; + + m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right)); + } + + m_guitext_profiler->setVisible(profiler_current_page != 0); + + if (profiler_current_page != 0) { + wchar_t buf[255]; + const wchar_t* str = wgettext("Profiler shown (page %d of %d)"); + swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, + profiler_current_page, profiler_max_page); + delete[] str; + showStatusText(buf); + } else { + showTranslatedStatusText("Profiler hidden"); + } +} diff --git a/src/client/gameui.h b/src/client/gameui.h index d1838f628..b090f5cb0 100644 --- a/src/client/gameui.h +++ b/src/client/gameui.h @@ -71,15 +71,18 @@ public: m_statustext = str; m_statustext_time = 0.0f; } + void showTranslatedStatusText(const char *str); inline void clearStatusText() { m_statustext.clear(); } void setChatText(const EnrichedString &chat_text, u32 recent_chat_count, u32 profiler_current_page); + void updateProfiler(u32 profiler_current_page, u32 profiler_max_page); + private: Flags m_flags; - gui::IGUIStaticText *m_guitext = nullptr; // First line of debug text + gui::IGUIStaticText *m_guitext = nullptr; // First line of debug text gui::IGUIStaticText *m_guitext2 = nullptr; // Second line of debug text gui::IGUIStaticText *m_guitext_info = nullptr; // At the middle of the screen @@ -90,7 +93,5 @@ private: float m_statustext_time = 0.0f; gui::IGUIStaticText *m_guitext_chat; // Chat text - - // @TODO future move - // gui::IGUIStaticText *m_guitext_profiler; // Profiler text + gui::IGUIStaticText *m_guitext_profiler; // Profiler text }; diff --git a/src/game.cpp b/src/game.cpp index f2d85efa9..593fe8439 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1212,7 +1212,6 @@ protected: void showOverlayMessage(const char *msg, float dtime, int percent, bool draw_clouds = true); - void showStatusTextSimple(const char *msg); static void settingChangedCallback(const std::string &setting_name, void *data); void readSettings(); @@ -1277,7 +1276,6 @@ private: void handleClientEvent_CloudParams(ClientEvent *event, CameraOrientation *cam); void updateChat(f32 dtime, const v2u32 &screensize); - void updateProfilerGUI(); static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX]; InputHandler *input; @@ -1338,10 +1336,6 @@ private: */ int crack_animation_length; - /* GUI stuff - */ - gui::IGUIStaticText *guitext_profiler; // Profiler text - KeyCache keycache; IntervalLimiter profiler_interval; @@ -1895,15 +1889,6 @@ bool Game::initGui() return false; } - // Profiler text (size is updated when text is updated) - guitext_profiler = gui::StaticText::add(guienv, - L"<Profiler>", - core::rect<s32>(0, 0, 0, 0), - false, false, guiroot); - guitext_profiler->setBackgroundColor(video::SColor(120, 0, 0, 0)); - guitext_profiler->setVisible(false); - guitext_profiler->setWordWrap(true); - #ifdef HAVE_TOUCHSCREENGUI if (g_touchscreengui) @@ -2216,7 +2201,8 @@ void Game::updateProfilers(const RunStats &stats, const FpsControl &draw_times, g_profiler->print(infostream); } - updateProfilerGUI(); + m_game_ui->updateProfiler(runData.profiler_current_page, + runData.profiler_max_page); g_profiler->clear(); } @@ -2367,9 +2353,9 @@ void Game::processKeyInput() bool new_mute_sound = !g_settings->getBool("mute_sound"); g_settings->setBool("mute_sound", new_mute_sound); if (new_mute_sound) - showStatusTextSimple("Sound muted"); + m_game_ui->showTranslatedStatusText("Sound muted"); else - showStatusTextSimple("Sound unmuted"); + m_game_ui->showTranslatedStatusText("Sound unmuted"); } else if (wasKeyDown(KeyType::INC_VOLUME)) { float new_volume = rangelim(g_settings->getFloat("sound_volume") + 0.1f, 0.0f, 1.0f); wchar_t buf[100]; @@ -2551,16 +2537,15 @@ void Game::toggleFreeMove() if (free_move) { if (client->checkPrivilege("fly")) { - showStatusTextSimple("Fly mode enabled"); + m_game_ui->showTranslatedStatusText("Fly mode enabled"); } else { - showStatusTextSimple("Fly mode enabled (note: no 'fly' privilege)"); + m_game_ui->showTranslatedStatusText("Fly mode enabled (note: no 'fly' privilege)"); } } else { - showStatusTextSimple("Fly mode disabled"); + m_game_ui->showTranslatedStatusText("Fly mode disabled"); } } - void Game::toggleFreeMoveAlt() { if (m_cache_doubletap_jump && runData.jump_timer < 0.2f) @@ -2577,12 +2562,12 @@ void Game::toggleFast() if (fast_move) { if (client->checkPrivilege("fast")) { - showStatusTextSimple("Fast mode enabled"); + m_game_ui->showTranslatedStatusText("Fast mode enabled"); } else { - showStatusTextSimple("Fast mode enabled (note: no 'fast' privilege)"); + m_game_ui->showTranslatedStatusText("Fast mode enabled (note: no 'fast' privilege)"); } } else { - showStatusTextSimple("Fast mode disabled"); + m_game_ui->showTranslatedStatusText("Fast mode disabled"); } #ifdef __ANDROID__ @@ -2598,12 +2583,12 @@ void Game::toggleNoClip() if (noclip) { if (client->checkPrivilege("noclip")) { - showStatusTextSimple("Noclip mode enabled"); + m_game_ui->showTranslatedStatusText("Noclip mode enabled"); } else { - showStatusTextSimple("Noclip mode enabled (note: no 'noclip' privilege)"); + m_game_ui->showTranslatedStatusText("Noclip mode enabled (note: no 'noclip' privilege)"); } } else { - showStatusTextSimple("Noclip mode disabled"); + m_game_ui->showTranslatedStatusText("Noclip mode disabled"); } } @@ -2613,9 +2598,9 @@ void Game::toggleCinematic() g_settings->set("cinematic", bool_to_cstr(cinematic)); if (cinematic) - showStatusTextSimple("Cinematic mode enabled"); + m_game_ui->showTranslatedStatusText("Cinematic mode enabled"); else - showStatusTextSimple("Cinematic mode disabled"); + m_game_ui->showTranslatedStatusText("Cinematic mode disabled"); } // Autoforward by toggling continuous forward. @@ -2625,18 +2610,18 @@ void Game::toggleAutoforward() g_settings->set("continuous_forward", bool_to_cstr(autorun_enabled)); if (autorun_enabled) - showStatusTextSimple("Automatic forwards enabled"); + m_game_ui->showTranslatedStatusText("Automatic forwards enabled"); else - showStatusTextSimple("Automatic forwards disabled"); + m_game_ui->showTranslatedStatusText("Automatic forwards disabled"); } void Game::toggleChat() { m_game_ui->m_flags.show_chat = !m_game_ui->m_flags.show_chat; if (m_game_ui->m_flags.show_chat) - showStatusTextSimple("Chat shown"); + m_game_ui->showTranslatedStatusText("Chat shown"); else - showStatusTextSimple("Chat hidden"); + m_game_ui->showTranslatedStatusText("Chat hidden"); } @@ -2644,9 +2629,9 @@ void Game::toggleHud() { m_game_ui->m_flags.show_hud = !m_game_ui->m_flags.show_hud; if (m_game_ui->m_flags.show_hud) - showStatusTextSimple("HUD shown"); + m_game_ui->showTranslatedStatusText("HUD shown"); else - showStatusTextSimple("HUD hidden"); + m_game_ui->showTranslatedStatusText("HUD hidden"); } void Game::toggleMinimap(bool shift_pressed) @@ -2673,30 +2658,30 @@ void Game::toggleMinimap(bool shift_pressed) m_game_ui->m_flags.show_minimap = true; switch (mode) { case MINIMAP_MODE_SURFACEx1: - showStatusTextSimple("Minimap in surface mode, Zoom x1"); + m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x1"); break; case MINIMAP_MODE_SURFACEx2: - showStatusTextSimple("Minimap in surface mode, Zoom x2"); + m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x2"); break; case MINIMAP_MODE_SURFACEx4: - showStatusTextSimple("Minimap in surface mode, Zoom x4"); + m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x4"); break; case MINIMAP_MODE_RADARx1: - showStatusTextSimple("Minimap in radar mode, Zoom x1"); + m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x1"); break; case MINIMAP_MODE_RADARx2: - showStatusTextSimple("Minimap in radar mode, Zoom x2"); + m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x2"); break; case MINIMAP_MODE_RADARx4: - showStatusTextSimple("Minimap in radar mode, Zoom x4"); + m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x4"); break; default: mode = MINIMAP_MODE_OFF; m_game_ui->m_flags.show_minimap = false; if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) - showStatusTextSimple("Minimap hidden"); + m_game_ui->showTranslatedStatusText("Minimap hidden"); else - showStatusTextSimple("Minimap currently disabled by game or mod"); + m_game_ui->showTranslatedStatusText("Minimap currently disabled by game or mod"); } mapper->setMinimapMode(mode); @@ -2706,9 +2691,9 @@ void Game::toggleFog() { m_game_ui->m_flags.force_fog_off = !m_game_ui->m_flags.force_fog_off; if (m_game_ui->m_flags.force_fog_off) - showStatusTextSimple("Fog disabled"); + m_game_ui->showTranslatedStatusText("Fog disabled"); else - showStatusTextSimple("Fog enabled"); + m_game_ui->showTranslatedStatusText("Fog enabled"); } @@ -2722,22 +2707,22 @@ void Game::toggleDebug() m_game_ui->m_flags.show_debug = true; m_game_ui->m_flags.show_profiler_graph = false; draw_control->show_wireframe = false; - showStatusTextSimple("Debug info shown"); + m_game_ui->showTranslatedStatusText("Debug info shown"); } else if (!m_game_ui->m_flags.show_profiler_graph && !draw_control->show_wireframe) { m_game_ui->m_flags.show_profiler_graph = true; - showStatusTextSimple("Profiler graph shown"); + m_game_ui->showTranslatedStatusText("Profiler graph shown"); } else if (!draw_control->show_wireframe && client->checkPrivilege("debug")) { m_game_ui->m_flags.show_profiler_graph = false; draw_control->show_wireframe = true; - showStatusTextSimple("Wireframe shown"); + m_game_ui->showTranslatedStatusText("Wireframe shown"); } else { m_game_ui->m_flags.show_debug = false; m_game_ui->m_flags.show_profiler_graph = false; draw_control->show_wireframe = false; if (client->checkPrivilege("debug")) { - showStatusTextSimple("Debug info, profiler graph, and wireframe hidden"); + m_game_ui->showTranslatedStatusText("Debug info, profiler graph, and wireframe hidden"); } else { - showStatusTextSimple("Debug info and profiler graph hidden"); + m_game_ui->showTranslatedStatusText("Debug info and profiler graph hidden"); } } } @@ -2747,9 +2732,9 @@ void Game::toggleUpdateCamera() { m_game_ui->m_flags.disable_camera_update = !m_game_ui->m_flags.disable_camera_update; if (m_game_ui->m_flags.disable_camera_update) - showStatusTextSimple("Camera update disabled"); + m_game_ui->showTranslatedStatusText("Camera update disabled"); else - showStatusTextSimple("Camera update enabled"); + m_game_ui->showTranslatedStatusText("Camera update enabled"); } @@ -2759,19 +2744,7 @@ void Game::toggleProfiler() (runData.profiler_current_page + 1) % (runData.profiler_max_page + 1); // FIXME: This updates the profiler with incomplete values - updateProfilerGUI(); - - if (runData.profiler_current_page != 0) { - wchar_t buf[255]; - const wchar_t* str = wgettext("Profiler shown (page %d of %d)"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, - runData.profiler_current_page, - runData.profiler_max_page); - delete[] str; - m_game_ui->showStatusText(buf); - } else { - showStatusTextSimple("Profiler hidden"); - } + m_game_ui->updateProfiler(runData.profiler_current_page, runData.profiler_max_page); } @@ -2826,9 +2799,9 @@ void Game::toggleFullViewRange() { draw_control->range_all = !draw_control->range_all; if (draw_control->range_all) - showStatusTextSimple("Enabled unlimited viewing range"); + m_game_ui->showTranslatedStatusText("Enabled unlimited viewing range"); else - showStatusTextSimple("Disabled unlimited viewing range"); + m_game_ui->showTranslatedStatusText("Disabled unlimited viewing range"); } @@ -2836,7 +2809,7 @@ void Game::checkZoomEnabled() { LocalPlayer *player = client->getEnv().getLocalPlayer(); if (player->getZoomFOV() < 0.001f) - showStatusTextSimple("Zoom currently disabled by game or mod"); + m_game_ui->showTranslatedStatusText("Zoom currently disabled by game or mod"); } @@ -3306,42 +3279,6 @@ void Game::updateChat(f32 dtime, const v2u32 &screensize) chat_backend->getRecentBuffer().getLineCount(), runData.profiler_current_page); } -void Game::updateProfilerGUI() -{ - if (runData.profiler_current_page != 0) { - std::ostringstream os(std::ios_base::binary); - g_profiler->printPage(os, runData.profiler_current_page, - runData.profiler_max_page); - - std::wstring text = translate_string(utf8_to_wide(os.str())); - setStaticText(guitext_profiler, text.c_str()); - - s32 w = g_fontengine->getTextWidth(text); - - if (w < 400) - w = 400; - - unsigned text_height = g_fontengine->getTextHeight(); - - core::position2di upper_left, lower_right; - - upper_left.X = 6; - upper_left.Y = (text_height + 5) * 2; - lower_right.X = 12 + w; - lower_right.Y = upper_left.Y + (text_height + 1) * MAX_PROFILER_TEXT_ROWS; - - s32 screen_height = driver->getScreenSize().Height; - - if (lower_right.Y > screen_height * 2 / 3) - lower_right.Y = screen_height * 2 / 3; - - guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right)); - } - - guitext_profiler->setVisible(runData.profiler_current_page != 0); -} - - void Game::updateCamera(u32 busy_time, f32 dtime) { LocalPlayer *player = client->getEnv().getLocalPlayer(); @@ -4345,13 +4282,6 @@ void Game::showOverlayMessage(const char *msg, float dtime, int percent, bool dr delete[] wmsg; } -void Game::showStatusTextSimple(const char *msg) -{ - const wchar_t *wmsg = wgettext(msg); - m_game_ui->showStatusText(wmsg); - delete[] wmsg; -} - void Game::settingChangedCallback(const std::string &setting_name, void *data) { ((Game *)data)->readSettings(); |