From aab3b18e4b9f847ba1e521d5a73624b0bb6a467a Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 4 Jan 2018 19:52:40 +0100 Subject: [PATCH] GameUI refactor (part 3/X): Move Game::guitext2, Game::guitext_info, Game::infotext to GameUI class Other enhancements: * Drop unused GameRunData::time_of_day * Little GameUI::update code path optimizations --- src/client/gameui.cpp | 78 ++++++++++++++++++++++++++++---- src/client/gameui.h | 12 +++-- src/game.cpp | 88 ++++-------------------------------- src/game.h | 5 ++ src/unittest/test_gameui.cpp | 2 +- 5 files changed, 92 insertions(+), 93 deletions(-) diff --git a/src/client/gameui.cpp b/src/client/gameui.cpp index 53d9f4a9c..d6d52823a 100644 --- a/src/client/gameui.cpp +++ b/src/client/gameui.cpp @@ -21,22 +21,46 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "gameui.h" #include #include "gui/mainmenumanager.h" +#include "util/pointedthing.h" #include "client.h" -#include "fontengine.h" #include "clientmap.h" -#include "version.h" +#include "fontengine.h" +#include "nodedef.h" #include "renderingengine.h" +#include "version.h" + +inline static const char *yawToDirectionString(int yaw) +{ + static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"}; + + yaw = wrapDegrees_0_360(yaw); + yaw = (yaw + 45) % 360 / 90; + + return direction[yaw]; +} void GameUI::init() { // First line of debug text m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(), core::rect(0, 0, 0, 0), false, false, guiroot); + + // Second line of debug text + m_guitext2 = gui::StaticText::add(guienv, L"", core::rect(0, 0, 0, 0), false, + false, guiroot); + + // At the middle of the screen + // Object infos are shown in this + m_guitext_info = gui::StaticText::add(guienv, L"", + core::rect(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5) + + v2s32(100, 200), false, true, guiroot); } -void GameUI::update(const RunStats &stats, Client *client, - const MapDrawControl *draw_control) +void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control, + const CameraOrientation &cam, const PointedThing &pointed_old) { + v2u32 screensize = RenderingEngine::get_instance()->getWindowSize(); + if (m_flags.show_debug) { static float drawtime_avg = 0; drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05; @@ -57,16 +81,50 @@ void GameUI::update(const RunStats &stats, Client *client, << std::setprecision(3) << ", RTT: " << client->getRTT() << "s"; setStaticText(m_guitext, utf8_to_wide(os.str()).c_str()); - m_guitext->setVisible(true); - } else { - m_guitext->setVisible(false); - } - if (m_guitext->isVisible()) { - v2u32 screensize = RenderingEngine::get_instance()->getWindowSize(); m_guitext->setRelativePosition(core::rect(5, 5, screensize.X, 5 + g_fontengine->getTextHeight())); } + + // Finally set the guitext visible depending on the flag + m_guitext->setVisible(m_flags.show_debug); + + if (m_flags.show_debug) { + LocalPlayer *player = client->getEnv().getLocalPlayer(); + v3f player_position = player->getPosition(); + + std::ostringstream os(std::ios_base::binary); + os << std::setprecision(1) << std::fixed + << "pos: (" << (player_position.X / BS) + << ", " << (player_position.Y / BS) + << ", " << (player_position.Z / BS) + << "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° " + << yawToDirectionString(cam.camera_yaw) + << ", seed: " << ((u64)client->getMapSeed()); + + if (pointed_old.type == POINTEDTHING_NODE) { + ClientMap &map = client->getEnv().getClientMap(); + const INodeDefManager *nodedef = client->getNodeDefManager(); + MapNode n = map.getNodeNoEx(pointed_old.node_undersurface); + + if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") { + os << ", pointed: " << nodedef->get(n).name + << ", param2: " << (u64) n.getParam2(); + } + } + + setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str()); + + m_guitext2->setRelativePosition(core::rect(5, + 5 + g_fontengine->getTextHeight(), screensize.X, + 5 + g_fontengine->getTextHeight() * 2 + )); + } + + m_guitext2->setVisible(m_flags.show_debug); + + setStaticText(m_guitext_info, translate_string(m_infotext).c_str()); + m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0); } void GameUI::initFlags() diff --git a/src/client/gameui.h b/src/client/gameui.h index 593fd4ff3..0e1085b01 100644 --- a/src/client/gameui.h +++ b/src/client/gameui.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once #include +#include #include "game.h" using namespace irr; @@ -49,20 +50,25 @@ public: }; void init(); - void update(const RunStats &stats, Client *client, const MapDrawControl *draw_control); + void update(const RunStats &stats, Client *client, MapDrawControl *draw_control, + const CameraOrientation &cam, const PointedThing &pointed_old); 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(); } + 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 + std::wstring m_infotext; // @TODO future move - // gui::IGUIStaticText *m_guitext2; // Second line of debug text - // gui::IGUIStaticText *m_guitext_info; // At the middle of the screen // gui::IGUIStaticText *m_guitext_status; // gui::IGUIStaticText *m_guitext_chat; // Chat text // gui::IGUIStaticText *m_guitext_profiler; // Profiler text diff --git a/src/game.cpp b/src/game.cpp index d31daf32b..bbc4294bd 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1134,10 +1134,6 @@ struct FpsControl { * many functions that do require objects of thse types do not modify them * (so they can be passed as a const qualified parameter) */ -struct CameraOrientation { - f32 camera_yaw; // "right/left" - f32 camera_pitch; // "up/down" -}; struct GameRunData { u16 dig_index; @@ -1170,7 +1166,6 @@ struct GameRunData { u32 profiler_current_page; u32 profiler_max_page; // Number of pages - float time_of_day; float time_of_day_smooth; }; @@ -1442,13 +1437,10 @@ private: /* GUI stuff */ - gui::IGUIStaticText *guitext2; // Second line of debug text - gui::IGUIStaticText *guitext_info; // At the middle of the screen gui::IGUIStaticText *guitext_status; gui::IGUIStaticText *guitext_chat; // Chat text gui::IGUIStaticText *guitext_profiler; // Profiler text - std::wstring infotext; std::wstring m_statustext; KeyCache keycache; @@ -1690,7 +1682,7 @@ void Game::run() processQueues(); - infotext = L""; + m_game_ui->clearInfoText(); hud->resizeHotbar(); updateProfilers(stats, draw_times, dtime); @@ -1989,19 +1981,6 @@ bool Game::initGui() { m_game_ui->init(); - // Second line of debug text - guitext2 = gui::StaticText::add(guienv, - L"", - core::rect(0, 0, 0, 0), - false, false, guiroot); - - // At the middle of the screen - // Object infos are shown in this - guitext_info = gui::StaticText::add(guienv, - L"", - core::rect(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.) guitext_status = gui::StaticText::add(guienv, L"", @@ -3840,13 +3819,14 @@ void Game::handlePointingAtNode(const PointedThing &pointed, NodeMetadata *meta = map.getNodeMetadata(nodepos); if (meta) { - infotext = unescape_translate(utf8_to_wide(meta->getString("infotext"))); + m_game_ui->setInfoText(unescape_translate(utf8_to_wide( + meta->getString("infotext")))); } else { MapNode n = map.getNodeNoEx(nodepos); if (nodedef_manager->get(n).tiledef[0].name == "unknown_node.png") { - infotext = L"Unknown node: "; - infotext += utf8_to_wide(nodedef_manager->get(n).name); + m_game_ui->setInfoText(L"Unknown node: " + + utf8_to_wide(nodedef_manager->get(n).name)); } } @@ -3917,7 +3897,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed, void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &playeritem, const v3f &player_position, bool show_debug) { - infotext = unescape_translate( + std::wstring infotext = unescape_translate( utf8_to_wide(runData.selected_object->infoText())); if (show_debug) { @@ -3927,6 +3907,8 @@ void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack & infotext += utf8_to_wide(runData.selected_object->debugInfoText()); } + m_game_ui->setInfoText(infotext); + if (isLeftPressed()) { bool do_punch = false; bool do_punch_damage = false; @@ -4167,7 +4149,6 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime, time_of_day_smooth = time_of_day_smooth * (1.0 - todsm) + time_of_day * todsm; - runData.time_of_day = time_of_day; runData.time_of_day_smooth = time_of_day_smooth; sky->update(time_of_day_smooth, time_brightness, direct_brightness, @@ -4378,62 +4359,11 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime, } -inline static const char *yawToDirectionString(int yaw) -{ - static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"}; - - yaw = wrapDegrees_0_360(yaw); - yaw = (yaw + 45) % 360 / 90; - - return direction[yaw]; -} - - void Game::updateGui(const RunStats &stats, f32 dtime, const CameraOrientation &cam) { v2u32 screensize = RenderingEngine::get_instance()->getWindowSize(); - LocalPlayer *player = client->getEnv().getLocalPlayer(); - v3f player_position = player->getPosition(); - - m_game_ui->update(stats, client, draw_control); - - if (m_game_ui->m_flags.show_debug) { - std::ostringstream os(std::ios_base::binary); - os << std::setprecision(1) << std::fixed - << "pos: (" << (player_position.X / BS) - << ", " << (player_position.Y / BS) - << ", " << (player_position.Z / BS) - << "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° " - << yawToDirectionString(cam.camera_yaw) - << ", seed: " << ((u64)client->getMapSeed()); - - if (runData.pointed_old.type == POINTEDTHING_NODE) { - ClientMap &map = client->getEnv().getClientMap(); - const INodeDefManager *nodedef = client->getNodeDefManager(); - MapNode n = map.getNodeNoEx(runData.pointed_old.node_undersurface); - - if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") { - os << ", pointed: " << nodedef->get(n).name - << ", param2: " << (u64) n.getParam2(); - } - } - - setStaticText(guitext2, utf8_to_wide(os.str()).c_str()); - guitext2->setVisible(true); - } else { - guitext2->setVisible(false); - } - - if (guitext2->isVisible()) { - core::rect rect( - 5, 5 + g_fontengine->getTextHeight(), - screensize.X, 5 + g_fontengine->getTextHeight() * 2 - ); - guitext2->setRelativePosition(rect); - } - setStaticText(guitext_info, translate_string(infotext).c_str()); - guitext_info->setVisible(m_game_ui->m_flags.show_hud && g_menumgr.menuCount() == 0); + m_game_ui->update(stats, client, draw_control, cam, runData.pointed_old); float statustext_time_max = 1.5; diff --git a/src/game.h b/src/game.h index 6101c4f7a..69e6eed0b 100644 --- a/src/game.h +++ b/src/game.h @@ -36,6 +36,11 @@ struct RunStats { Jitter dtime_jitter, busy_time_jitter; }; +struct CameraOrientation { + f32 camera_yaw; // "right/left" + f32 camera_pitch; // "up/down" +}; + void the_game(bool *kill, bool random_input, InputHandler *input, diff --git a/src/unittest/test_gameui.cpp b/src/unittest/test_gameui.cpp index 3c6f2a5a4..d52977469 100644 --- a/src/unittest/test_gameui.cpp +++ b/src/unittest/test_gameui.cpp @@ -49,7 +49,7 @@ void TestGameUI::testInit() UASSERT(gui.getFlags().show_hud) // @TODO verify if we can create non UI nulldevice to test this function - gui.init(); + // gui.init(); } void TestGameUI::testFlagSetters() -- 2.25.1