GameUI refactor (part 4/X): Move Game::guitext_status, Game::m_statustext, GameRunDat...
authorLoic Blot <loic.blot@unix-experience.fr>
Thu, 4 Jan 2018 20:35:26 +0000 (21:35 +0100)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Fri, 5 Jan 2018 19:59:30 +0000 (20:59 +0100)
Other enhancements:
* Simplify setStatusText to showStatusText, as it shows the label too (preventing almost every setStatusText to call setStatusTextTime(0)
* Add unittests

src/client/gameui.cpp
src/client/gameui.h
src/game.cpp
src/unittest/test_gameui.cpp

index d6d52823a5bc52a2697b8c0f838f8574a31b6857..7955dea596e5544054ed55662dd27ccfc749a662 100644 (file)
@@ -54,10 +54,16 @@ void GameUI::init()
        m_guitext_info = gui::StaticText::add(guienv, L"",
                core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5)
                        + v2s32(100, 200), false, true, guiroot);
+
+       // Status text (displays info when showing and hiding GUI stuff, etc.)
+       m_guitext_status = gui::StaticText::add(guienv, L"<Status>",
+               core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
+
+       m_guitext_status->setVisible(false);
 }
 
 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
-       const CameraOrientation &cam, const PointedThing &pointed_old)
+       const CameraOrientation &cam, const PointedThing &pointed_old, float dtime)
 {
        v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
 
@@ -125,6 +131,44 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_
 
        setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
        m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
+
+       static const float statustext_time_max = 1.5f;
+
+       if (!m_statustext.empty()) {
+               m_statustext_time += dtime;
+
+               if (m_statustext_time >= statustext_time_max) {
+                       clearStatusText();
+                       m_statustext_time = 0.0f;
+               }
+       }
+
+       setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
+       m_guitext_status->setVisible(!m_statustext.empty());
+
+       if (!m_statustext.empty()) {
+               s32 status_width  = m_guitext_status->getTextWidth();
+               s32 status_height = m_guitext_status->getTextHeight();
+               s32 status_y = screensize.Y - 150;
+               s32 status_x = (screensize.X - status_width) / 2;
+
+               m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
+                       status_y - status_height, status_x + status_width, status_y));
+
+               // Fade out
+               video::SColor initial_color(255, 0, 0, 0);
+
+               if (guienv->getSkin())
+                       initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
+
+               video::SColor final_color = initial_color;
+               final_color.setAlpha(0);
+               video::SColor fade_color = initial_color.getInterpolated_quadratic(
+                       initial_color, final_color,
+                       pow(m_statustext_time / statustext_time_max, 2.0f));
+               m_guitext_status->setOverrideColor(fade_color);
+               m_guitext_status->enableOverrideColor(true);
+       }
 }
 
 void GameUI::initFlags()
index 0e1085b0154909453081c7a94578cff45c6c8d7a..fc2b8707e1ca9cd2664a58a58c2d6ec2ffe96efb 100644 (file)
@@ -33,6 +33,9 @@ class GameUI
        // Temporary between coding time to move things here
        friend class Game;
 
+       // Permit unittests to access members directly
+       friend class TestGameUI;
+
 public:
        GameUI() = default;
        ~GameUI() = default;
@@ -51,25 +54,37 @@ public:
 
        void init();
        void update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
-               const CameraOrientation &cam, const PointedThing &pointed_old);
+               const CameraOrientation &cam, const PointedThing &pointed_old, float dtime);
 
        void initFlags();
        const Flags &getFlags() const { return m_flags; }
 
        void showMinimap(bool show);
 
-       void setInfoText(const std::wstring &str) { m_infotext = str; }
-       void clearInfoText() { m_infotext.clear(); }
+       inline void setInfoText(const std::wstring &str) { m_infotext = str; }
+       inline void clearInfoText() { m_infotext.clear(); }
+
+       inline void showStatusText(const std::wstring &str)
+       {
+               m_statustext = str;
+               m_statustext_time = 0.0f;
+       }
+       inline void clearStatusText() { m_statustext.clear(); }
 
 private:
        Flags m_flags;
 
-       gui::IGUIStaticText *m_guitext;          // First line of debug text
-       gui::IGUIStaticText *m_guitext2;         // Second line of debug text
-       gui::IGUIStaticText *m_guitext_info;     // At the middle of the screen
+       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
        std::wstring m_infotext;
+
+       gui::IGUIStaticText *m_guitext_status = nullptr;
+       std::wstring m_statustext;
+       float m_statustext_time = 0.0f;
+
        // @TODO future move
-       //      gui::IGUIStaticText *m_guitext_status;
        //      gui::IGUIStaticText *m_guitext_chat;     // Chat text
        //      gui::IGUIStaticText *m_guitext_profiler; // Profiler text
 };
index bbc4294bd3d805f4444251f59e3196cee154294b..3dc92d4f59f1a1636f3c13dce01d0d2cc03a974c 100644 (file)
@@ -1157,7 +1157,6 @@ struct GameRunData {
        float jump_timer;
        float damage_flash;
        float update_draw_list_timer;
-       float statustext_time;
 
        f32 fog_range;
 
@@ -1303,7 +1302,6 @@ protected:
                        const ToolCapabilities &playeritem_toolcap, f32 dtime);
        void updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
                        const CameraOrientation &cam);
-       void updateGui(const RunStats &stats, f32 dtime, const CameraOrientation &cam);
        void updateProfilerGraphs(ProfilerGraph *graph);
 
        // Misc
@@ -1437,12 +1435,9 @@ private:
 
        /* GUI stuff
         */
-       gui::IGUIStaticText *guitext_status;
        gui::IGUIStaticText *guitext_chat;         // Chat text
        gui::IGUIStaticText *guitext_profiler; // Profiler text
 
-       std::wstring m_statustext;
-
        KeyCache keycache;
 
        IntervalLimiter profiler_interval;
@@ -1981,13 +1976,6 @@ bool Game::initGui()
 {
        m_game_ui->init();
 
-       // Status text (displays info when showing and hiding GUI stuff, etc.)
-       guitext_status = gui::StaticText::add(guienv,
-                       L"<Status>",
-                       core::rect<s32>(0, 0, 0, 0),
-                       false, false, guiroot);
-       guitext_status->setVisible(false);
-
        // Chat text
        guitext_chat = gui::StaticText::add(
                        guienv,
@@ -2488,7 +2476,6 @@ void Game::processKeyInput()
                        showStatusTextSimple("Sound muted");
                else
                        showStatusTextSimple("Sound unmuted");
-               runData.statustext_time = 0;
        } 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];
@@ -2496,8 +2483,7 @@ void Game::processKeyInput()
                const wchar_t *str = wgettext("Volume changed to %d%%");
                swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100));
                delete[] str;
-               m_statustext = buf;
-               runData.statustext_time = 0;
+               m_game_ui->showStatusText(buf);
        } else if (wasKeyDown(KeyType::DEC_VOLUME)) {
                float new_volume = rangelim(g_settings->getFloat("sound_volume") - 0.1f, 0.0f, 1.0f);
                wchar_t buf[100];
@@ -2505,8 +2491,7 @@ void Game::processKeyInput()
                const wchar_t *str = wgettext("Volume changed to %d%%");
                swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100));
                delete[] str;
-               m_statustext = buf;
-               runData.statustext_time = 0;
+               m_game_ui->showStatusText(buf);
        } else if (wasKeyDown(KeyType::CINEMATIC)) {
                toggleCinematic();
        } else if (wasKeyDown(KeyType::SCREENSHOT)) {
@@ -2549,8 +2534,7 @@ void Game::processKeyInput()
        }
 
        if (quicktune->hasMessage()) {
-               m_statustext = utf8_to_wide(quicktune->getMessage());
-               runData.statustext_time = 0.0f;
+               m_game_ui->showStatusText(utf8_to_wide(quicktune->getMessage()));
        }
 }
 
@@ -2671,8 +2655,6 @@ void Game::toggleFreeMove()
        bool free_move = !g_settings->getBool("free_move");
        g_settings->set("free_move", bool_to_cstr(free_move));
 
-       runData.statustext_time = 0;
-
        if (free_move) {
                if (client->checkPrivilege("fly")) {
                        showStatusTextSimple("Fly mode enabled");
@@ -2699,8 +2681,6 @@ void Game::toggleFast()
        bool fast_move = !g_settings->getBool("fast_move");
        g_settings->set("fast_move", bool_to_cstr(fast_move));
 
-       runData.statustext_time = 0;
-
        if (fast_move) {
                if (client->checkPrivilege("fast")) {
                        showStatusTextSimple("Fast mode enabled");
@@ -2722,7 +2702,6 @@ void Game::toggleNoClip()
        bool noclip = !g_settings->getBool("noclip");
        g_settings->set("noclip", bool_to_cstr(noclip));
 
-       runData.statustext_time = 0;
        if (noclip) {
                if (client->checkPrivilege("noclip")) {
                        showStatusTextSimple("Noclip mode enabled");
@@ -2739,7 +2718,6 @@ void Game::toggleCinematic()
        bool cinematic = !g_settings->getBool("cinematic");
        g_settings->set("cinematic", bool_to_cstr(cinematic));
 
-       runData.statustext_time = 0;
        if (cinematic)
                showStatusTextSimple("Cinematic mode enabled");
        else
@@ -2752,7 +2730,6 @@ void Game::toggleAutoforward()
        bool autorun_enabled = !g_settings->getBool("continuous_forward");
        g_settings->set("continuous_forward", bool_to_cstr(autorun_enabled));
 
-       runData.statustext_time = 0;
        if (autorun_enabled)
                showStatusTextSimple("Automatic forwards enabled");
        else
@@ -2762,7 +2739,6 @@ void Game::toggleAutoforward()
 void Game::toggleChat()
 {
        m_game_ui->m_flags.show_chat = !m_game_ui->m_flags.show_chat;
-       runData.statustext_time = 0;
        if (m_game_ui->m_flags.show_chat)
                showStatusTextSimple("Chat shown");
        else
@@ -2773,7 +2749,6 @@ void Game::toggleChat()
 void Game::toggleHud()
 {
        m_game_ui->m_flags.show_hud = !m_game_ui->m_flags.show_hud;
-       runData.statustext_time = 0;
        if (m_game_ui->m_flags.show_hud)
                showStatusTextSimple("HUD shown");
        else
@@ -2830,18 +2805,16 @@ void Game::toggleMinimap(bool shift_pressed)
                                showStatusTextSimple("Minimap currently disabled by game or mod");
        }
 
-       runData.statustext_time = 0;
        mapper->setMinimapMode(mode);
 }
 
 void Game::toggleFog()
 {
        m_game_ui->m_flags.force_fog_off = !m_game_ui->m_flags.force_fog_off;
-       runData.statustext_time = 0;
-        if (m_game_ui->m_flags.force_fog_off)
-                showStatusTextSimple("Fog disabled");
-        else
-                showStatusTextSimple("Fog enabled");
+       if (m_game_ui->m_flags.force_fog_off)
+                       showStatusTextSimple("Fog disabled");
+       else
+                       showStatusTextSimple("Fog enabled");
 }
 
 
@@ -2873,14 +2846,12 @@ void Game::toggleDebug()
                        showStatusTextSimple("Debug info and profiler graph hidden");
                }
        }
-       runData.statustext_time = 0;
 }
 
 
 void Game::toggleUpdateCamera()
 {
        m_game_ui->m_flags.disable_camera_update = !m_game_ui->m_flags.disable_camera_update;
-       runData.statustext_time = 0;
        if (m_game_ui->m_flags.disable_camera_update)
                showStatusTextSimple("Camera update disabled");
        else
@@ -2904,11 +2875,10 @@ void Game::toggleProfiler()
                                runData.profiler_current_page,
                                runData.profiler_max_page);
                delete[] str;
-               m_statustext = buf;
+               m_game_ui->showStatusText(buf);
        } else {
                showStatusTextSimple("Profiler hidden");
        }
-       runData.statustext_time = 0;
 }
 
 
@@ -2924,16 +2894,15 @@ void Game::increaseViewRange()
                str = wgettext("Viewing range is at maximum: %d");
                swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
                delete[] str;
-               m_statustext = buf;
+               m_game_ui->showStatusText(buf);
 
        } else {
                str = wgettext("Viewing range changed to %d");
                swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
                delete[] str;
-               m_statustext = buf;
+               m_game_ui->showStatusText(buf);
        }
        g_settings->set("viewing_range", itos(range_new));
-       runData.statustext_time = 0;
 }
 
 
@@ -2949,22 +2918,20 @@ void Game::decreaseViewRange()
                str = wgettext("Viewing range is at minimum: %d");
                swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
                delete[] str;
-               m_statustext = buf;
+               m_game_ui->showStatusText(buf);
        } else {
                str = wgettext("Viewing range changed to %d");
                swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
                delete[] str;
-               m_statustext = buf;
+               m_game_ui->showStatusText(buf);
        }
        g_settings->set("viewing_range", itos(range_new));
-       runData.statustext_time = 0;
 }
 
 
 void Game::toggleFullViewRange()
 {
        draw_control->range_all = !draw_control->range_all;
-       runData.statustext_time = 0;
        if (draw_control->range_all)
                showStatusTextSimple("Enabled unlimited viewing range");
        else
@@ -4274,7 +4241,7 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
                runData.update_draw_list_last_cam_dir = camera_direction;
        }
 
-       updateGui(*stats, dtime, cam);
+       m_game_ui->update(*stats, client, draw_control, cam, runData.pointed_old, dtime);
 
        /*
           make sure menu is on top
@@ -4358,55 +4325,6 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
        g_profiler->graphAdd("mainloop_draw", stats->drawtime / 1000.0f);
 }
 
-
-void Game::updateGui(const RunStats &stats, f32 dtime, const CameraOrientation &cam)
-{
-       v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
-
-       m_game_ui->update(stats, client, draw_control, cam, runData.pointed_old);
-
-       float statustext_time_max = 1.5;
-
-       if (!m_statustext.empty()) {
-               runData.statustext_time += dtime;
-
-               if (runData.statustext_time >= statustext_time_max) {
-                       m_statustext = L"";
-                       runData.statustext_time = 0;
-               }
-       }
-
-       setStaticText(guitext_status, translate_string(m_statustext).c_str());
-       guitext_status->setVisible(!m_statustext.empty());
-
-       if (!m_statustext.empty()) {
-               s32 status_width  = guitext_status->getTextWidth();
-               s32 status_height = guitext_status->getTextHeight();
-               s32 status_y = screensize.Y - 150;
-               s32 status_x = (screensize.X - status_width) / 2;
-               core::rect<s32> rect(
-                               status_x , status_y - status_height,
-                               status_x + status_width, status_y
-               );
-               guitext_status->setRelativePosition(rect);
-
-               // Fade out
-               video::SColor initial_color(255, 0, 0, 0);
-
-               if (guienv->getSkin())
-                       initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
-
-               video::SColor final_color = initial_color;
-               final_color.setAlpha(0);
-               video::SColor fade_color = initial_color.getInterpolated_quadratic(
-                               initial_color, final_color,
-                               pow(runData.statustext_time / statustext_time_max, 2.0f));
-               guitext_status->setOverrideColor(fade_color);
-               guitext_status->enableOverrideColor(true);
-       }
-}
-
-
 /* Log times and stuff for visualization */
 inline void Game::updateProfilerGraphs(ProfilerGraph *graph)
 {
@@ -4474,7 +4392,7 @@ void Game::showOverlayMessage(const char *msg, float dtime, int percent, bool dr
 void Game::showStatusTextSimple(const char *msg)
 {
        const wchar_t *wmsg = wgettext(msg);
-       m_statustext = wmsg;
+       m_game_ui->showStatusText(wmsg);
        delete[] wmsg;
 }
 
index d529774697e0b4f78ba020f654bcec04790d0df6..9bc9d8cf5aa22af93483f4c5e3a9b3f4b7f86cbf 100644 (file)
@@ -31,6 +31,8 @@ public:
 
        void testInit();
        void testFlagSetters();
+       void testInfoText();
+       void testStatusText();
 };
 
 static TestGameUI g_test_instance;
@@ -39,6 +41,8 @@ void TestGameUI::runTests(IGameDef *gamedef)
 {
        TEST(testInit);
        TEST(testFlagSetters);
+       TEST(testInfoText);
+       TEST(testStatusText);
 }
 
 void TestGameUI::testInit()
@@ -61,3 +65,20 @@ void TestGameUI::testFlagSetters()
        gui.showMinimap(false);
        UASSERT(!gui.getFlags().show_minimap);
 }
+
+void TestGameUI::testStatusText()
+{
+       GameUI gui{};
+       gui.showStatusText(L"test status");
+
+       UASSERT(gui.m_statustext_time == 0.0f);
+       UASSERT(gui.m_statustext == L"test status");
+}
+
+void TestGameUI::testInfoText()
+{
+       GameUI gui{};
+       gui.setInfoText(L"test info");
+
+       UASSERT(gui.m_infotext == L"test info");
+}