Skip to content

Commit

Permalink
GameUI refactor (part 7/7): Finish to include profiler things to GameUI
Browse files Browse the repository at this point in the history
Other changes:
* Add GameUI clarification comment
* Move force_fog_off & disable_camera_update flags from GameUI to Game, it's not UI related
* Properly init GameUI::Flags
* Move toggleChat toggleHud & toggleProfiler to GameUI
* Add gameui.cpp to LINT whitelist
  • Loading branch information
nerzhul committed Jan 5, 2018
1 parent 02f82ec commit f40f414
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 75 deletions.
44 changes: 34 additions & 10 deletions src/client/gameui.cpp
Expand Up @@ -204,8 +204,7 @@ void GameUI::showTranslatedStatusText(const char *str)
delete[] wmsg;
}

void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count,
u32 profiler_current_page)
void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
{
setStaticText(m_guitext_chat, chat_text);

Expand All @@ -228,15 +227,14 @@ void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count,

// Don't show chat if disabled or empty or profiler is enabled
m_guitext_chat->setVisible(m_flags.show_chat &&
recent_chat_count != 0 && profiler_current_page == 0);
recent_chat_count != 0 && m_profiler_current_page == 0);
}

void GameUI::updateProfiler(u32 profiler_current_page, u32 profiler_max_page)
void GameUI::updateProfiler()
{
if (profiler_current_page != 0) {
if (m_profiler_current_page != 0) {
std::ostringstream os(std::ios_base::binary);
g_profiler->printPage(os, profiler_current_page,
profiler_max_page);
g_profiler->printPage(os, m_profiler_current_page, m_profiler_max_page);

std::wstring text = translate_string(utf8_to_wide(os.str()));
setStaticText(m_guitext_profiler, text.c_str());
Expand All @@ -263,13 +261,39 @@ void GameUI::updateProfiler(u32 profiler_current_page, u32 profiler_max_page)
m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
}

m_guitext_profiler->setVisible(profiler_current_page != 0);
m_guitext_profiler->setVisible(m_profiler_current_page != 0);
}

void GameUI::toggleChat()
{
m_flags.show_chat = !m_flags.show_chat;
if (m_flags.show_chat)
showTranslatedStatusText("Chat shown");
else
showTranslatedStatusText("Chat hidden");
}

void GameUI::toggleHud()
{
m_flags.show_hud = !m_flags.show_hud;
if (m_flags.show_hud)
showTranslatedStatusText("HUD shown");
else
showTranslatedStatusText("HUD hidden");
}

void GameUI::toggleProfiler()
{
m_profiler_current_page = (m_profiler_current_page + 1) % (m_profiler_max_page + 1);

// FIXME: This updates the profiler with incomplete values
updateProfiler();

if (profiler_current_page != 0) {
if (m_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);
m_profiler_current_page, m_profiler_max_page);
delete[] str;
showStatusText(buf);
} else {
Expand Down
34 changes: 23 additions & 11 deletions src/client/gameui.h
Expand Up @@ -29,6 +29,14 @@ using namespace irr;
class Client;
struct MapDrawControl;

/*
* This object intend to contain the core UI elements
* It includes:
* - status texts
* - debug texts
* - chat texts
* - hud flags
*/
class GameUI
{
// Temporary between coding time to move things here
Expand All @@ -44,13 +52,11 @@ class GameUI
// Flags that can, or may, change during main game loop
struct Flags
{
bool show_chat;
bool show_hud;
bool show_minimap;
bool force_fog_off;
bool show_debug;
bool show_profiler_graph;
bool disable_camera_update;
bool show_chat = true;
bool show_hud = true;
bool show_minimap = true;
bool show_debug = true;
bool show_profiler_graph = true;
};

void init();
Expand All @@ -74,15 +80,18 @@ class GameUI
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 setChatText(const EnrichedString &chat_text, u32 recent_chat_count);

void updateProfiler();

void updateProfiler(u32 profiler_current_page, u32 profiler_max_page);
void toggleChat();
void toggleHud();
void toggleProfiler();

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
Expand All @@ -93,5 +102,8 @@ class GameUI
float m_statustext_time = 0.0f;

gui::IGUIStaticText *m_guitext_chat; // Chat text

gui::IGUIStaticText *m_guitext_profiler; // Profiler text
u8 m_profiler_current_page = 0;
const u8 m_profiler_max_page = 3;
};
74 changes: 20 additions & 54 deletions src/game.cpp
Expand Up @@ -1065,9 +1065,6 @@ struct GameRunData {

v3f update_draw_list_last_cam_dir;

u32 profiler_current_page;
u32 profiler_max_page; // Number of pages

float time_of_day_smooth;
};

Expand Down Expand Up @@ -1158,13 +1155,10 @@ class Game {
void toggleCinematic();
void toggleAutoforward();

void toggleChat();
void toggleHud();
void toggleMinimap(bool shift_pressed);
void toggleFog();
void toggleDebug();
void toggleUpdateCamera();
void toggleProfiler();

void increaseViewRange();
void decreaseViewRange();
Expand Down Expand Up @@ -1256,6 +1250,11 @@ class Game {
#endif

private:
struct Flags {
bool force_fog_off = false;
bool disable_camera_update = false;
};

void showPauseMenu();

// ClientEvent handlers
Expand Down Expand Up @@ -1315,6 +1314,7 @@ class Game {
Minimap *mapper = nullptr;

GameRunData runData;
Flags m_flags;

/* 'cache'
This class does take ownership/responsibily for cleaning up etc of any of
Expand Down Expand Up @@ -1496,7 +1496,6 @@ bool Game::startup(bool *kill,

memset(&runData, 0, sizeof(runData));
runData.time_from_last_punch = 10.0;
runData.profiler_max_page = 3;
runData.update_wielded_item_trigger = true;

m_game_ui->initFlags();
Expand Down Expand Up @@ -1790,7 +1789,7 @@ bool Game::createClient(const std::string &playername,
}

GameGlobalShaderConstantSetterFactory *scsf = new GameGlobalShaderConstantSetterFactory(
&m_game_ui->m_flags.force_fog_off, &runData.fog_range, client);
&m_flags.force_fog_off, &runData.fog_range, client);
shader_src->addShaderConstantSetterFactory(scsf);

// Update cached textures, meshes and materials
Expand Down Expand Up @@ -2201,9 +2200,7 @@ void Game::updateProfilers(const RunStats &stats, const FpsControl &draw_times,
g_profiler->print(infostream);
}

m_game_ui->updateProfiler(runData.profiler_current_page,
runData.profiler_max_page);

m_game_ui->updateProfiler();
g_profiler->clear();
}

Expand Down Expand Up @@ -2377,19 +2374,19 @@ void Game::processKeyInput()
} else if (wasKeyDown(KeyType::SCREENSHOT)) {
client->makeScreenshot();
} else if (wasKeyDown(KeyType::TOGGLE_HUD)) {
toggleHud();
m_game_ui->toggleHud();
} else if (wasKeyDown(KeyType::MINIMAP)) {
toggleMinimap(isKeyDown(KeyType::SNEAK));
} else if (wasKeyDown(KeyType::TOGGLE_CHAT)) {
toggleChat();
m_game_ui->toggleChat();
} else if (wasKeyDown(KeyType::TOGGLE_FORCE_FOG_OFF)) {
toggleFog();
} else if (wasKeyDown(KeyType::TOGGLE_UPDATE_CAMERA)) {
toggleUpdateCamera();
} else if (wasKeyDown(KeyType::TOGGLE_DEBUG)) {
toggleDebug();
} else if (wasKeyDown(KeyType::TOGGLE_PROFILER)) {
toggleProfiler();
m_game_ui->toggleProfiler();
} else if (wasKeyDown(KeyType::INCREASE_VIEWING_RANGE)) {
increaseViewRange();
} else if (wasKeyDown(KeyType::DECREASE_VIEWING_RANGE)) {
Expand Down Expand Up @@ -2615,25 +2612,6 @@ void Game::toggleAutoforward()
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)
m_game_ui->showTranslatedStatusText("Chat shown");
else
m_game_ui->showTranslatedStatusText("Chat hidden");
}


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)
m_game_ui->showTranslatedStatusText("HUD shown");
else
m_game_ui->showTranslatedStatusText("HUD hidden");
}

void Game::toggleMinimap(bool shift_pressed)
{
if (!mapper || !m_game_ui->m_flags.show_hud || !g_settings->getBool("enable_minimap"))
Expand Down Expand Up @@ -2689,8 +2667,8 @@ void Game::toggleMinimap(bool shift_pressed)

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)
m_flags.force_fog_off = !m_flags.force_fog_off;
if (m_flags.force_fog_off)
m_game_ui->showTranslatedStatusText("Fog disabled");
else
m_game_ui->showTranslatedStatusText("Fog enabled");
Expand Down Expand Up @@ -2730,24 +2708,14 @@ void Game::toggleDebug()

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)
m_flags.disable_camera_update = !m_flags.disable_camera_update;
if (m_flags.disable_camera_update)
m_game_ui->showTranslatedStatusText("Camera update disabled");
else
m_game_ui->showTranslatedStatusText("Camera update enabled");
}


void Game::toggleProfiler()
{
runData.profiler_current_page =
(runData.profiler_current_page + 1) % (runData.profiler_max_page + 1);

// FIXME: This updates the profiler with incomplete values
m_game_ui->updateProfiler(runData.profiler_current_page, runData.profiler_max_page);
}


void Game::increaseViewRange()
{
s16 range = g_settings->getS16("viewing_range");
Expand Down Expand Up @@ -2944,12 +2912,10 @@ inline void Game::step(f32 *dtime)
if (can_be_and_is_paused) { // This is for a singleplayer server
*dtime = 0; // No time passes
} else {
if (server != NULL) {
//TimeTaker timer("server->step(dtime)");
if (server) {
server->step(*dtime);
}

//TimeTaker timer("client.step(dtime)");
client->step(*dtime);
}
}
Expand Down Expand Up @@ -3276,7 +3242,7 @@ void Game::updateChat(f32 dtime, const v2u32 &screensize)

// Display all messages in a static text element
m_game_ui->setChatText(chat_backend->getRecentChat(),
chat_backend->getRecentBuffer().getLineCount(), runData.profiler_current_page);
chat_backend->getRecentBuffer().getLineCount());
}

void Game::updateCamera(u32 busy_time, f32 dtime)
Expand Down Expand Up @@ -3336,7 +3302,7 @@ void Game::updateCamera(u32 busy_time, f32 dtime)

m_camera_offset_changed = (camera_offset != old_camera_offset);

if (!m_game_ui->m_flags.disable_camera_update) {
if (!m_flags.disable_camera_update) {
client->getEnv().getClientMap().updateCamera(camera_position,
camera_direction, camera_fov, camera_offset);

Expand Down Expand Up @@ -4033,7 +3999,7 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
clouds->update(camera_node_position,
sky->getCloudColor());
if (clouds->isCameraInsideCloud() && m_cache_enable_fog &&
!m_game_ui->m_flags.force_fog_off) {
!m_flags.force_fog_off) {
// if inside clouds, and fog enabled, use that as sky
// color(s)
video::SColor clouds_dark = clouds->getColor()
Expand All @@ -4058,7 +4024,7 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
Fog
*/

if (m_cache_enable_fog && !m_game_ui->m_flags.force_fog_off) {
if (m_cache_enable_fog && !m_flags.force_fog_off) {
driver->setFog(
sky->getBgColor(),
video::EFT_FOG_LINEAR,
Expand Down
1 change: 1 addition & 0 deletions util/travis/clang-format-whitelist.txt
Expand Up @@ -21,6 +21,7 @@ src/clientobject.cpp
src/clientobject.h
src/client/clientlauncher.cpp
src/client/clientlauncher.h
src/client/gameui.cpp
src/client/joystick_controller.cpp
src/client/joystick_controller.h
src/client/renderingengine.cpp
Expand Down

0 comments on commit f40f414

Please sign in to comment.