GameUI refactor (part 6/X): Move Game::guitext_profiler & showStatusTextSimple to GameUI class

Other enhancements:
* Move showStatusTextSimple to GameUI class & rename to showTranslatedStatusText
This commit is contained in:
Loic Blot 2018-01-04 23:32:32 +01:00 committed by Loïc Blot
parent 326b0faa5e
commit 02f82eca0b
3 changed files with 109 additions and 116 deletions

@ -20,12 +20,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gameui.h" #include "gameui.h"
#include <irrlicht_changes/static_text.h> #include <irrlicht_changes/static_text.h>
#include <gettext.h>
#include "gui/mainmenumanager.h" #include "gui/mainmenumanager.h"
#include "util/pointedthing.h" #include "util/pointedthing.h"
#include "client.h" #include "client.h"
#include "clientmap.h" #include "clientmap.h"
#include "fontengine.h" #include "fontengine.h"
#include "nodedef.h" #include "nodedef.h"
#include "profiler.h"
#include "renderingengine.h" #include "renderingengine.h"
#include "version.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), m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0),
//false, false); // Disable word wrap as of now //false, false); // Disable word wrap as of now
false, true, guiroot); 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, 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; 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, void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count,
u32 profiler_current_page) 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 && m_guitext_chat->setVisible(m_flags.show_chat &&
recent_chat_count != 0 && profiler_current_page == 0); 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");
}
}

@ -71,11 +71,14 @@ public:
m_statustext = str; m_statustext = str;
m_statustext_time = 0.0f; m_statustext_time = 0.0f;
} }
void showTranslatedStatusText(const char *str);
inline void clearStatusText() { m_statustext.clear(); } inline void clearStatusText() { m_statustext.clear(); }
void setChatText(const EnrichedString &chat_text, u32 recent_chat_count, void setChatText(const EnrichedString &chat_text, u32 recent_chat_count,
u32 profiler_current_page); u32 profiler_current_page);
void updateProfiler(u32 profiler_current_page, u32 profiler_max_page);
private: private:
Flags m_flags; Flags m_flags;
@ -90,7 +93,5 @@ private:
float m_statustext_time = 0.0f; float m_statustext_time = 0.0f;
gui::IGUIStaticText *m_guitext_chat; // Chat text gui::IGUIStaticText *m_guitext_chat; // Chat text
gui::IGUIStaticText *m_guitext_profiler; // Profiler text
// @TODO future move
// gui::IGUIStaticText *m_guitext_profiler; // Profiler text
}; };

@ -1212,7 +1212,6 @@ protected:
void showOverlayMessage(const char *msg, float dtime, int percent, void showOverlayMessage(const char *msg, float dtime, int percent,
bool draw_clouds = true); bool draw_clouds = true);
void showStatusTextSimple(const char *msg);
static void settingChangedCallback(const std::string &setting_name, void *data); static void settingChangedCallback(const std::string &setting_name, void *data);
void readSettings(); void readSettings();
@ -1277,7 +1276,6 @@ private:
void handleClientEvent_CloudParams(ClientEvent *event, CameraOrientation *cam); void handleClientEvent_CloudParams(ClientEvent *event, CameraOrientation *cam);
void updateChat(f32 dtime, const v2u32 &screensize); void updateChat(f32 dtime, const v2u32 &screensize);
void updateProfilerGUI();
static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX]; static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX];
InputHandler *input; InputHandler *input;
@ -1338,10 +1336,6 @@ private:
*/ */
int crack_animation_length; int crack_animation_length;
/* GUI stuff
*/
gui::IGUIStaticText *guitext_profiler; // Profiler text
KeyCache keycache; KeyCache keycache;
IntervalLimiter profiler_interval; IntervalLimiter profiler_interval;
@ -1895,15 +1889,6 @@ bool Game::initGui()
return false; 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 #ifdef HAVE_TOUCHSCREENGUI
if (g_touchscreengui) if (g_touchscreengui)
@ -2216,7 +2201,8 @@ void Game::updateProfilers(const RunStats &stats, const FpsControl &draw_times,
g_profiler->print(infostream); g_profiler->print(infostream);
} }
updateProfilerGUI(); m_game_ui->updateProfiler(runData.profiler_current_page,
runData.profiler_max_page);
g_profiler->clear(); g_profiler->clear();
} }
@ -2367,9 +2353,9 @@ void Game::processKeyInput()
bool new_mute_sound = !g_settings->getBool("mute_sound"); bool new_mute_sound = !g_settings->getBool("mute_sound");
g_settings->setBool("mute_sound", new_mute_sound); g_settings->setBool("mute_sound", new_mute_sound);
if (new_mute_sound) if (new_mute_sound)
showStatusTextSimple("Sound muted"); m_game_ui->showTranslatedStatusText("Sound muted");
else else
showStatusTextSimple("Sound unmuted"); m_game_ui->showTranslatedStatusText("Sound unmuted");
} else if (wasKeyDown(KeyType::INC_VOLUME)) { } else if (wasKeyDown(KeyType::INC_VOLUME)) {
float new_volume = rangelim(g_settings->getFloat("sound_volume") + 0.1f, 0.0f, 1.0f); float new_volume = rangelim(g_settings->getFloat("sound_volume") + 0.1f, 0.0f, 1.0f);
wchar_t buf[100]; wchar_t buf[100];
@ -2551,16 +2537,15 @@ void Game::toggleFreeMove()
if (free_move) { if (free_move) {
if (client->checkPrivilege("fly")) { if (client->checkPrivilege("fly")) {
showStatusTextSimple("Fly mode enabled"); m_game_ui->showTranslatedStatusText("Fly mode enabled");
} else { } else {
showStatusTextSimple("Fly mode enabled (note: no 'fly' privilege)"); m_game_ui->showTranslatedStatusText("Fly mode enabled (note: no 'fly' privilege)");
} }
} else { } else {
showStatusTextSimple("Fly mode disabled"); m_game_ui->showTranslatedStatusText("Fly mode disabled");
} }
} }
void Game::toggleFreeMoveAlt() void Game::toggleFreeMoveAlt()
{ {
if (m_cache_doubletap_jump && runData.jump_timer < 0.2f) if (m_cache_doubletap_jump && runData.jump_timer < 0.2f)
@ -2577,12 +2562,12 @@ void Game::toggleFast()
if (fast_move) { if (fast_move) {
if (client->checkPrivilege("fast")) { if (client->checkPrivilege("fast")) {
showStatusTextSimple("Fast mode enabled"); m_game_ui->showTranslatedStatusText("Fast mode enabled");
} else { } else {
showStatusTextSimple("Fast mode enabled (note: no 'fast' privilege)"); m_game_ui->showTranslatedStatusText("Fast mode enabled (note: no 'fast' privilege)");
} }
} else { } else {
showStatusTextSimple("Fast mode disabled"); m_game_ui->showTranslatedStatusText("Fast mode disabled");
} }
#ifdef __ANDROID__ #ifdef __ANDROID__
@ -2598,12 +2583,12 @@ void Game::toggleNoClip()
if (noclip) { if (noclip) {
if (client->checkPrivilege("noclip")) { if (client->checkPrivilege("noclip")) {
showStatusTextSimple("Noclip mode enabled"); m_game_ui->showTranslatedStatusText("Noclip mode enabled");
} else { } else {
showStatusTextSimple("Noclip mode enabled (note: no 'noclip' privilege)"); m_game_ui->showTranslatedStatusText("Noclip mode enabled (note: no 'noclip' privilege)");
} }
} else { } 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)); g_settings->set("cinematic", bool_to_cstr(cinematic));
if (cinematic) if (cinematic)
showStatusTextSimple("Cinematic mode enabled"); m_game_ui->showTranslatedStatusText("Cinematic mode enabled");
else else
showStatusTextSimple("Cinematic mode disabled"); m_game_ui->showTranslatedStatusText("Cinematic mode disabled");
} }
// Autoforward by toggling continuous forward. // Autoforward by toggling continuous forward.
@ -2625,18 +2610,18 @@ void Game::toggleAutoforward()
g_settings->set("continuous_forward", bool_to_cstr(autorun_enabled)); g_settings->set("continuous_forward", bool_to_cstr(autorun_enabled));
if (autorun_enabled) if (autorun_enabled)
showStatusTextSimple("Automatic forwards enabled"); m_game_ui->showTranslatedStatusText("Automatic forwards enabled");
else else
showStatusTextSimple("Automatic forwards disabled"); m_game_ui->showTranslatedStatusText("Automatic forwards disabled");
} }
void Game::toggleChat() void Game::toggleChat()
{ {
m_game_ui->m_flags.show_chat = !m_game_ui->m_flags.show_chat; m_game_ui->m_flags.show_chat = !m_game_ui->m_flags.show_chat;
if (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 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; m_game_ui->m_flags.show_hud = !m_game_ui->m_flags.show_hud;
if (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 else
showStatusTextSimple("HUD hidden"); m_game_ui->showTranslatedStatusText("HUD hidden");
} }
void Game::toggleMinimap(bool shift_pressed) void Game::toggleMinimap(bool shift_pressed)
@ -2673,30 +2658,30 @@ void Game::toggleMinimap(bool shift_pressed)
m_game_ui->m_flags.show_minimap = true; m_game_ui->m_flags.show_minimap = true;
switch (mode) { switch (mode) {
case MINIMAP_MODE_SURFACEx1: case MINIMAP_MODE_SURFACEx1:
showStatusTextSimple("Minimap in surface mode, Zoom x1"); m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x1");
break; break;
case MINIMAP_MODE_SURFACEx2: case MINIMAP_MODE_SURFACEx2:
showStatusTextSimple("Minimap in surface mode, Zoom x2"); m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x2");
break; break;
case MINIMAP_MODE_SURFACEx4: case MINIMAP_MODE_SURFACEx4:
showStatusTextSimple("Minimap in surface mode, Zoom x4"); m_game_ui->showTranslatedStatusText("Minimap in surface mode, Zoom x4");
break; break;
case MINIMAP_MODE_RADARx1: case MINIMAP_MODE_RADARx1:
showStatusTextSimple("Minimap in radar mode, Zoom x1"); m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x1");
break; break;
case MINIMAP_MODE_RADARx2: case MINIMAP_MODE_RADARx2:
showStatusTextSimple("Minimap in radar mode, Zoom x2"); m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x2");
break; break;
case MINIMAP_MODE_RADARx4: case MINIMAP_MODE_RADARx4:
showStatusTextSimple("Minimap in radar mode, Zoom x4"); m_game_ui->showTranslatedStatusText("Minimap in radar mode, Zoom x4");
break; break;
default: default:
mode = MINIMAP_MODE_OFF; mode = MINIMAP_MODE_OFF;
m_game_ui->m_flags.show_minimap = false; m_game_ui->m_flags.show_minimap = false;
if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE) if (hud_flags & HUD_FLAG_MINIMAP_VISIBLE)
showStatusTextSimple("Minimap hidden"); m_game_ui->showTranslatedStatusText("Minimap hidden");
else else
showStatusTextSimple("Minimap currently disabled by game or mod"); m_game_ui->showTranslatedStatusText("Minimap currently disabled by game or mod");
} }
mapper->setMinimapMode(mode); 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; 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) if (m_game_ui->m_flags.force_fog_off)
showStatusTextSimple("Fog disabled"); m_game_ui->showTranslatedStatusText("Fog disabled");
else 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_debug = true;
m_game_ui->m_flags.show_profiler_graph = false; m_game_ui->m_flags.show_profiler_graph = false;
draw_control->show_wireframe = 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) { } else if (!m_game_ui->m_flags.show_profiler_graph && !draw_control->show_wireframe) {
m_game_ui->m_flags.show_profiler_graph = true; 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")) { } else if (!draw_control->show_wireframe && client->checkPrivilege("debug")) {
m_game_ui->m_flags.show_profiler_graph = false; m_game_ui->m_flags.show_profiler_graph = false;
draw_control->show_wireframe = true; draw_control->show_wireframe = true;
showStatusTextSimple("Wireframe shown"); m_game_ui->showTranslatedStatusText("Wireframe shown");
} else { } else {
m_game_ui->m_flags.show_debug = false; m_game_ui->m_flags.show_debug = false;
m_game_ui->m_flags.show_profiler_graph = false; m_game_ui->m_flags.show_profiler_graph = false;
draw_control->show_wireframe = false; draw_control->show_wireframe = false;
if (client->checkPrivilege("debug")) { if (client->checkPrivilege("debug")) {
showStatusTextSimple("Debug info, profiler graph, and wireframe hidden"); m_game_ui->showTranslatedStatusText("Debug info, profiler graph, and wireframe hidden");
} else { } 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; 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) if (m_game_ui->m_flags.disable_camera_update)
showStatusTextSimple("Camera update disabled"); m_game_ui->showTranslatedStatusText("Camera update disabled");
else 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); (runData.profiler_current_page + 1) % (runData.profiler_max_page + 1);
// FIXME: This updates the profiler with incomplete values // FIXME: This updates the profiler with incomplete values
updateProfilerGUI(); m_game_ui->updateProfiler(runData.profiler_current_page, runData.profiler_max_page);
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");
}
} }
@ -2826,9 +2799,9 @@ void Game::toggleFullViewRange()
{ {
draw_control->range_all = !draw_control->range_all; draw_control->range_all = !draw_control->range_all;
if (draw_control->range_all) if (draw_control->range_all)
showStatusTextSimple("Enabled unlimited viewing range"); m_game_ui->showTranslatedStatusText("Enabled unlimited viewing range");
else 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(); LocalPlayer *player = client->getEnv().getLocalPlayer();
if (player->getZoomFOV() < 0.001f) 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); 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) void Game::updateCamera(u32 busy_time, f32 dtime)
{ {
LocalPlayer *player = client->getEnv().getLocalPlayer(); LocalPlayer *player = client->getEnv().getLocalPlayer();
@ -4345,13 +4282,6 @@ void Game::showOverlayMessage(const char *msg, float dtime, int percent, bool dr
delete[] wmsg; 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) void Game::settingChangedCallback(const std::string &setting_name, void *data)
{ {
((Game *)data)->readSettings(); ((Game *)data)->readSettings();