Skip to content

Commit fe510d9

Browse files
committedJan 5, 2018
GameUI refactor (part 4/X): Move Game::guitext_status, Game::m_statustext, GameRunData::statustext_time to GameUI class
Other enhancements: * Simplify setStatusText to showStatusText, as it shows the label too (preventing almost every setStatusText to call setStatusTextTime(0) * Add unittests
1 parent aab3b18 commit fe510d9

File tree

4 files changed

+102
-104
lines changed

4 files changed

+102
-104
lines changed
 

Diff for: ‎src/client/gameui.cpp

+45-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,16 @@ void GameUI::init()
5454
m_guitext_info = gui::StaticText::add(guienv, L"",
5555
core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5)
5656
+ v2s32(100, 200), false, true, guiroot);
57+
58+
// Status text (displays info when showing and hiding GUI stuff, etc.)
59+
m_guitext_status = gui::StaticText::add(guienv, L"<Status>",
60+
core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
61+
62+
m_guitext_status->setVisible(false);
5763
}
5864

5965
void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
60-
const CameraOrientation &cam, const PointedThing &pointed_old)
66+
const CameraOrientation &cam, const PointedThing &pointed_old, float dtime)
6167
{
6268
v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
6369

@@ -125,6 +131,44 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_
125131

126132
setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
127133
m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
134+
135+
static const float statustext_time_max = 1.5f;
136+
137+
if (!m_statustext.empty()) {
138+
m_statustext_time += dtime;
139+
140+
if (m_statustext_time >= statustext_time_max) {
141+
clearStatusText();
142+
m_statustext_time = 0.0f;
143+
}
144+
}
145+
146+
setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
147+
m_guitext_status->setVisible(!m_statustext.empty());
148+
149+
if (!m_statustext.empty()) {
150+
s32 status_width = m_guitext_status->getTextWidth();
151+
s32 status_height = m_guitext_status->getTextHeight();
152+
s32 status_y = screensize.Y - 150;
153+
s32 status_x = (screensize.X - status_width) / 2;
154+
155+
m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
156+
status_y - status_height, status_x + status_width, status_y));
157+
158+
// Fade out
159+
video::SColor initial_color(255, 0, 0, 0);
160+
161+
if (guienv->getSkin())
162+
initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
163+
164+
video::SColor final_color = initial_color;
165+
final_color.setAlpha(0);
166+
video::SColor fade_color = initial_color.getInterpolated_quadratic(
167+
initial_color, final_color,
168+
pow(m_statustext_time / statustext_time_max, 2.0f));
169+
m_guitext_status->setOverrideColor(fade_color);
170+
m_guitext_status->enableOverrideColor(true);
171+
}
128172
}
129173

130174
void GameUI::initFlags()

Diff for: ‎src/client/gameui.h

+22-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class GameUI
3333
// Temporary between coding time to move things here
3434
friend class Game;
3535

36+
// Permit unittests to access members directly
37+
friend class TestGameUI;
38+
3639
public:
3740
GameUI() = default;
3841
~GameUI() = default;
@@ -51,25 +54,37 @@ class GameUI
5154

5255
void init();
5356
void update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
54-
const CameraOrientation &cam, const PointedThing &pointed_old);
57+
const CameraOrientation &cam, const PointedThing &pointed_old, float dtime);
5558

5659
void initFlags();
5760
const Flags &getFlags() const { return m_flags; }
5861

5962
void showMinimap(bool show);
6063

61-
void setInfoText(const std::wstring &str) { m_infotext = str; }
62-
void clearInfoText() { m_infotext.clear(); }
64+
inline void setInfoText(const std::wstring &str) { m_infotext = str; }
65+
inline void clearInfoText() { m_infotext.clear(); }
66+
67+
inline void showStatusText(const std::wstring &str)
68+
{
69+
m_statustext = str;
70+
m_statustext_time = 0.0f;
71+
}
72+
inline void clearStatusText() { m_statustext.clear(); }
6373

6474
private:
6575
Flags m_flags;
6676

67-
gui::IGUIStaticText *m_guitext; // First line of debug text
68-
gui::IGUIStaticText *m_guitext2; // Second line of debug text
69-
gui::IGUIStaticText *m_guitext_info; // At the middle of the screen
77+
gui::IGUIStaticText *m_guitext = nullptr; // First line of debug text
78+
gui::IGUIStaticText *m_guitext2 = nullptr; // Second line of debug text
79+
80+
gui::IGUIStaticText *m_guitext_info = nullptr; // At the middle of the screen
7081
std::wstring m_infotext;
82+
83+
gui::IGUIStaticText *m_guitext_status = nullptr;
84+
std::wstring m_statustext;
85+
float m_statustext_time = 0.0f;
86+
7187
// @TODO future move
72-
// gui::IGUIStaticText *m_guitext_status;
7388
// gui::IGUIStaticText *m_guitext_chat; // Chat text
7489
// gui::IGUIStaticText *m_guitext_profiler; // Profiler text
7590
};

0 commit comments

Comments
 (0)
Please sign in to comment.