]> git.lizzy.rs Git - minetest.git/blob - src/client/gameui.cpp
Add chat HUD flag (#13189)
[minetest.git] / src / client / gameui.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "gameui.h"
22 #include <irrlicht_changes/static_text.h>
23 #include <gettext.h>
24 #include "gui/mainmenumanager.h"
25 #include "gui/guiChatConsole.h"
26 #include "util/pointedthing.h"
27 #include "client.h"
28 #include "clientmap.h"
29 #include "fontengine.h"
30 #include "nodedef.h"
31 #include "profiler.h"
32 #include "renderingengine.h"
33 #include "version.h"
34
35 inline static const char *yawToDirectionString(int yaw)
36 {
37         static const char *direction[4] =
38                 {"North +Z", "West -X", "South -Z", "East +X"};
39
40         yaw = wrapDegrees_0_360(yaw);
41         yaw = (yaw + 45) % 360 / 90;
42
43         return direction[yaw];
44 }
45
46 GameUI::GameUI()
47 {
48         if (guienv && guienv->getSkin())
49                 m_statustext_initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
50         else
51                 m_statustext_initial_color = video::SColor(255, 0, 0, 0);
52
53 }
54 void GameUI::init()
55 {
56         // First line of debug text
57         m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
58                 core::rect<s32>(0, 0, 0, 0), false, true, guiroot);
59
60         // Second line of debug text
61         m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
62                 true, guiroot);
63
64         // Chat text
65         m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0),
66                 //false, false); // Disable word wrap as of now
67                 false, true, guiroot);
68         u16 chat_font_size = g_settings->getU16("chat_font_size");
69         if (chat_font_size != 0) {
70                 m_guitext_chat->setOverrideFont(g_fontengine->getFont(
71                         rangelim(chat_font_size, 5, 72), FM_Unspecified));
72         }
73
74
75         // Infotext of nodes and objects.
76         // If in debug mode, object debug infos shown here, too.
77         // Located on the left on the screen, below chat.
78         u32 chat_font_height = m_guitext_chat->getActiveFont()->getDimension(L"Ay").Height;
79         m_guitext_info = gui::StaticText::add(guienv, L"",
80                 // Size is limited; text will be truncated after 6 lines.
81                 core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 6) +
82                         v2s32(100, chat_font_height *
83                         (g_settings->getU16("recent_chat_messages") + 3)),
84                         false, true, guiroot);
85
86         // Status text (displays info when showing and hiding GUI stuff, etc.)
87         m_guitext_status = gui::StaticText::add(guienv, L"<Status>",
88                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
89         m_guitext_status->setVisible(false);
90
91         // Profiler text (size is updated when text is updated)
92         m_guitext_profiler = gui::StaticText::add(guienv, L"<Profiler>",
93                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
94         m_guitext_profiler->setOverrideFont(g_fontengine->getFont(
95                 g_fontengine->getDefaultFontSize() * 0.9f, FM_Mono));
96         m_guitext_profiler->setVisible(false);
97 }
98
99 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
100         const CameraOrientation &cam, const PointedThing &pointed_old,
101         const GUIChatConsole *chat_console, float dtime)
102 {
103         v2u32 screensize = RenderingEngine::getWindowSize();
104
105         LocalPlayer *player = client->getEnv().getLocalPlayer();
106
107         s32 minimal_debug_height = 0;
108
109         // Minimal debug text must only contain info that can't give a gameplay advantage
110         if (m_flags.show_minimal_debug) {
111                 const u16 fps = 1.0 / stats.dtime_jitter.avg;
112                 m_drawtime_avg *= 0.95f;
113                 m_drawtime_avg += 0.05f * (stats.drawtime / 1000);
114
115                 std::ostringstream os(std::ios_base::binary);
116                 os << std::fixed
117                         << PROJECT_NAME_C " " << g_version_hash
118                         << " | FPS: " << fps
119                         << std::setprecision(0)
120                         << " | drawtime: " << m_drawtime_avg << "ms"
121                         << std::setprecision(1)
122                         << " | dtime jitter: "
123                         << (stats.dtime_jitter.max_fraction * 100.0) << "%"
124                         << std::setprecision(1)
125                         << " | view range: "
126                         << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
127                         << std::setprecision(2)
128                         << " | RTT: " << (client->getRTT() * 1000.0f) << "ms";
129
130                 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X, screensize.Y));
131
132                 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
133
134                 minimal_debug_height = m_guitext->getTextHeight();
135         }
136
137         // Finally set the guitext visible depending on the flag
138         m_guitext->setVisible(m_flags.show_minimal_debug);
139
140         // Basic debug text also shows info that might give a gameplay advantage
141         if (m_flags.show_basic_debug) {
142                 v3f player_position = player->getPosition();
143
144                 std::ostringstream os(std::ios_base::binary);
145                 os << std::setprecision(1) << std::fixed
146                         << "pos: (" << (player_position.X / BS)
147                         << ", " << (player_position.Y / BS)
148                         << ", " << (player_position.Z / BS)
149                         << ") | yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
150                         << yawToDirectionString(cam.camera_yaw)
151                         << " | pitch: " << (-wrapDegrees_180(cam.camera_pitch)) << "°"
152                         << " | seed: " << ((u64)client->getMapSeed());
153
154                 if (pointed_old.type == POINTEDTHING_NODE) {
155                         ClientMap &map = client->getEnv().getClientMap();
156                         const NodeDefManager *nodedef = client->getNodeDefManager();
157                         MapNode n = map.getNode(pointed_old.node_undersurface);
158
159                         if (n.getContent() != CONTENT_IGNORE) {
160                                 if (nodedef->get(n).name == "unknown") {
161                                         os << ", pointed: <unknown node>";
162                                 } else {
163                                         os << ", pointed: " << nodedef->get(n).name;
164                                 }
165                                 os << ", param2: " << (u64) n.getParam2();
166                         }
167                 }
168
169                 m_guitext2->setRelativePosition(core::rect<s32>(5, 5 + minimal_debug_height,
170                                 screensize.X, screensize.Y));
171
172                 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
173         }
174
175         m_guitext2->setVisible(m_flags.show_basic_debug);
176
177         setStaticText(m_guitext_info, m_infotext.c_str());
178         m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
179
180         static const float statustext_time_max = 1.5f;
181
182         if (!m_statustext.empty()) {
183                 m_statustext_time += dtime;
184
185                 if (m_statustext_time >= statustext_time_max) {
186                         clearStatusText();
187                         m_statustext_time = 0.0f;
188                 }
189         }
190
191         setStaticText(m_guitext_status, m_statustext.c_str());
192         m_guitext_status->setVisible(!m_statustext.empty());
193
194         if (!m_statustext.empty()) {
195                 s32 status_width  = m_guitext_status->getTextWidth();
196                 s32 status_height = m_guitext_status->getTextHeight();
197                 s32 status_y = screensize.Y - 150;
198                 s32 status_x = (screensize.X - status_width) / 2;
199
200                 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
201                         status_y - status_height, status_x + status_width, status_y));
202
203                 // Fade out
204                 video::SColor final_color = m_statustext_initial_color;
205                 final_color.setAlpha(0);
206                 video::SColor fade_color = m_statustext_initial_color.getInterpolated_quadratic(
207                         m_statustext_initial_color, final_color, m_statustext_time / statustext_time_max);
208                 m_guitext_status->setOverrideColor(fade_color);
209                 m_guitext_status->enableOverrideColor(true);
210         }
211
212         // Hide chat when disabled by server or when console is visible
213         m_guitext_chat->setVisible(isChatVisible() && !chat_console->isVisible() && (player->hud_flags & HUD_FLAG_CHAT_VISIBLE));
214 }
215
216 void GameUI::initFlags()
217 {
218         m_flags = GameUI::Flags();
219         m_flags.show_minimal_debug = g_settings->getBool("show_debug");
220 }
221
222 void GameUI::showMinimap(bool show)
223 {
224         m_flags.show_minimap = show;
225 }
226
227 void GameUI::showTranslatedStatusText(const char *str)
228 {
229         const wchar_t *wmsg = wgettext(str);
230         showStatusText(wmsg);
231         delete[] wmsg;
232 }
233
234 void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
235 {
236         setStaticText(m_guitext_chat, chat_text);
237
238         m_recent_chat_count = recent_chat_count;
239 }
240
241 void GameUI::updateChatSize()
242 {
243         // Update gui element size and position
244         s32 chat_y = 5;
245
246         if (m_flags.show_minimal_debug)
247                 chat_y += m_guitext->getTextHeight();
248         if (m_flags.show_basic_debug)
249                 chat_y += m_guitext2->getTextHeight();
250
251         const v2u32 &window_size = RenderingEngine::getWindowSize();
252
253         core::rect<s32> chat_size(10, chat_y, window_size.X - 20, 0);
254         chat_size.LowerRightCorner.Y = std::min((s32)window_size.Y,
255                         m_guitext_chat->getTextHeight() + chat_y);
256
257         if (chat_size == m_current_chat_size)
258                 return;
259         m_current_chat_size = chat_size;
260
261         m_guitext_chat->setRelativePosition(chat_size);
262 }
263
264 void GameUI::updateProfiler()
265 {
266         if (m_profiler_current_page != 0) {
267                 std::ostringstream os(std::ios_base::binary);
268                 os << "   Profiler page " << (int)m_profiler_current_page <<
269                                 ", elapsed: " << g_profiler->getElapsedMs() << " ms)" << std::endl;
270
271                 int lines = g_profiler->print(os, m_profiler_current_page, m_profiler_max_page);
272                 ++lines;
273
274                 EnrichedString str(utf8_to_wide(os.str()));
275                 str.setBackground(video::SColor(120, 0, 0, 0));
276                 setStaticText(m_guitext_profiler, str);
277
278                 core::dimension2d<u32> size = m_guitext_profiler->getOverrideFont()->
279                                 getDimension(str.c_str());
280                 core::position2di upper_left(6, m_guitext->getTextHeight() * 2.5f);
281                 core::position2di lower_right = upper_left;
282                 lower_right.X += size.Width + 10;
283                 lower_right.Y += size.Height;
284
285                 m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
286         }
287
288         m_guitext_profiler->setVisible(m_profiler_current_page != 0);
289 }
290
291 void GameUI::toggleChat(Client *client)
292 {
293         if (client->getEnv().getLocalPlayer()->hud_flags & HUD_FLAG_CHAT_VISIBLE) {
294                 m_flags.show_chat = !m_flags.show_chat;
295                 if (m_flags.show_chat)
296                         showTranslatedStatusText("Chat shown");
297                 else
298                         showTranslatedStatusText("Chat hidden");
299         } else {
300                 showTranslatedStatusText("Chat currently disabled by game or mod");
301         }
302
303 }
304
305 void GameUI::toggleHud()
306 {
307         m_flags.show_hud = !m_flags.show_hud;
308         if (m_flags.show_hud)
309                 showTranslatedStatusText("HUD shown");
310         else
311                 showTranslatedStatusText("HUD hidden");
312 }
313
314 void GameUI::toggleProfiler()
315 {
316         m_profiler_current_page = (m_profiler_current_page + 1) % (m_profiler_max_page + 1);
317
318         // FIXME: This updates the profiler with incomplete values
319         updateProfiler();
320
321         if (m_profiler_current_page != 0) {
322                 std::wstring msg = fwgettext("Profiler shown (page %d of %d)",
323                                 m_profiler_current_page, m_profiler_max_page);
324                 showStatusText(msg);
325         } else {
326                 showTranslatedStatusText("Profiler hidden");
327         }
328 }
329
330
331 void GameUI::deleteFormspec()
332 {
333         if (m_formspec) {
334                 m_formspec->drop();
335                 m_formspec = nullptr;
336         }
337
338         m_formname.clear();
339 }