]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/gameui.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.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         m_guitext_coords = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
57                 false, guiroot);
58
59         // First line of debug text
60         m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
61                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
62
63         // Second line of debug text
64         m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
65                 false, guiroot);
66
67         // Chat text
68         m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0),
69                 //false, false); // Disable word wrap as of now
70                 false, true, guiroot);
71         u16 chat_font_size = g_settings->getU16("chat_font_size");
72         if (chat_font_size != 0) {
73                 m_guitext_chat->setOverrideFont(g_fontengine->getFont(
74                         chat_font_size, FM_Unspecified));
75         }
76
77
78         // Infotext of nodes and objects.
79         // If in debug mode, object debug infos shown here, too.
80         // Located on the left on the screen, below chat.
81         u32 chat_font_height = m_guitext_chat->getActiveFont()->getDimension(L"Ay").Height;
82         m_guitext_info = gui::StaticText::add(guienv, L"",
83                 // Size is limited; text will be truncated after 6 lines.
84                 core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 6) +
85                         v2s32(100, chat_font_height *
86                         (g_settings->getU16("recent_chat_messages") + 3)),
87                         false, true, guiroot);
88
89         // Status text (displays info when showing and hiding GUI stuff, etc.)
90         m_guitext_status = gui::StaticText::add(guienv, L"<Status>",
91                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
92         m_guitext_status->setVisible(false);
93
94         // Profiler text (size is updated when text is updated)
95         m_guitext_profiler = gui::StaticText::add(guienv, L"<Profiler>",
96                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
97         m_guitext_profiler->setOverrideFont(g_fontengine->getFont(
98                 g_fontengine->getDefaultFontSize() * 0.9f, FM_Mono));
99         m_guitext_profiler->setVisible(false);
100 }
101
102 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
103         const CameraOrientation &cam, const PointedThing &pointed_old,
104         const GUIChatConsole *chat_console, float dtime)
105 {
106         LocalPlayer *player = client->getEnv().getLocalPlayer();
107         v3f player_position = player->getPosition();
108
109         v2u32 screensize = RenderingEngine::getWindowSize();
110
111         bool show_coords = g_settings->getBool("coords");
112
113         if (show_coords) {
114                 std::ostringstream os(std::ios_base::binary);
115                 os << std::setprecision(1) << std::fixed
116                         << (player_position.X / BS)
117                         << ", " << (player_position.Y / BS)
118                         << ", " << (player_position.Z / BS);
119                 setStaticText(m_guitext_coords, utf8_to_wide(os.str()).c_str());
120                 m_guitext_coords->setRelativePosition(core::rect<s32>(5, screensize.Y - 5 - g_fontengine->getTextHeight(), screensize.X, screensize.Y));
121         }
122
123         m_guitext_coords->setVisible(show_coords);
124
125         // Minimal debug text must only contain info that can't give a gameplay advantage
126         if (m_flags.show_minimal_debug) {
127                 const u16 fps = 1.0 / stats.dtime_jitter.avg;
128                 m_drawtime_avg *= 0.95f;
129                 m_drawtime_avg += 0.05f * (stats.drawtime / 1000);
130
131                 std::ostringstream os(std::ios_base::binary);
132                 os << std::fixed
133                         << PROJECT_NAME_C " " << g_version_hash
134                         << " | FPS: " << fps
135                         << std::setprecision(0)
136                         << " | drawtime: " << m_drawtime_avg << "ms"
137                         << std::setprecision(1)
138                         << " | dtime jitter: "
139                         << (stats.dtime_jitter.max_fraction * 100.0) << "%"
140                         << std::setprecision(1)
141                         << " | view range: "
142                         << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
143                         << std::setprecision(2)
144                         << " | RTT: " << (client->getRTT() * 1000.0f) << "ms";
145                 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
146
147                 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
148                         5 + g_fontengine->getTextHeight()));
149         }
150
151         // Finally set the guitext visible depending on the flag
152         m_guitext->setVisible(m_flags.show_minimal_debug);
153
154         // Basic debug text also shows info that might give a gameplay advantage
155         if (m_flags.show_basic_debug) {
156                 LocalPlayer *player = client->getEnv().getLocalPlayer();
157                 v3f player_position = player->getPosition();
158
159                 std::ostringstream os(std::ios_base::binary);
160                 os << std::setprecision(1) << std::fixed
161                         << "pos: (" << (player_position.X / BS)
162                         << ", " << (player_position.Y / BS)
163                         << ", " << (player_position.Z / BS)
164                         << ") | yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
165                         << yawToDirectionString(cam.camera_yaw)
166                         << " | pitch: " << (-wrapDegrees_180(cam.camera_pitch)) << "°"
167                         << " | seed: " << ((u64)client->getMapSeed());
168
169                 if (pointed_old.type == POINTEDTHING_NODE) {
170                         ClientMap &map = client->getEnv().getClientMap();
171                         const NodeDefManager *nodedef = client->getNodeDefManager();
172                         MapNode n = map.getNode(pointed_old.node_undersurface);
173
174                         if (n.getContent() != CONTENT_IGNORE) {
175                                 if (nodedef->get(n).name == "unknown") {
176                                         os << ", pointed: <unknown node>";
177                                 } else {
178                                         os << ", pointed: " << nodedef->get(n).name;
179                                 }
180                                 os << ", param2: " << (u64) n.getParam2();
181                         }
182                 }
183
184                 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
185
186                 m_guitext2->setRelativePosition(core::rect<s32>(5,
187                         5 + g_fontengine->getTextHeight(), screensize.X,
188                         5 + g_fontengine->getTextHeight() * 2
189                 ));
190         }
191
192         m_guitext2->setVisible(m_flags.show_basic_debug);
193
194         setStaticText(m_guitext_info, m_infotext.c_str());
195         m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
196
197         static const float statustext_time_max = 1.5f;
198
199         if (!m_statustext.empty()) {
200                 m_statustext_time += dtime;
201
202                 if (m_statustext_time >= statustext_time_max) {
203                         clearStatusText();
204                         m_statustext_time = 0.0f;
205                 }
206         }
207
208         setStaticText(m_guitext_status, m_statustext.c_str());
209         m_guitext_status->setVisible(!m_statustext.empty());
210
211         if (!m_statustext.empty()) {
212                 s32 status_width  = m_guitext_status->getTextWidth();
213                 s32 status_height = m_guitext_status->getTextHeight();
214                 s32 status_y = screensize.Y - 150;
215                 s32 status_x = (screensize.X - status_width) / 2;
216
217                 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
218                         status_y - status_height, status_x + status_width, status_y));
219
220                 // Fade out
221                 video::SColor final_color = m_statustext_initial_color;
222                 final_color.setAlpha(0);
223                 video::SColor fade_color = m_statustext_initial_color.getInterpolated_quadratic(
224                         m_statustext_initial_color, final_color, m_statustext_time / statustext_time_max);
225                 m_guitext_status->setOverrideColor(fade_color);
226                 m_guitext_status->enableOverrideColor(true);
227         }
228
229         m_guitext_chat->setVisible(isChatVisible());
230 }
231
232 void GameUI::initFlags()
233 {
234         m_flags = GameUI::Flags();
235         m_flags.show_minimal_debug = g_settings->getBool("show_debug");
236 }
237
238 void GameUI::showMinimap(bool show)
239 {
240         m_flags.show_minimap = show;
241 }
242
243 void GameUI::showTranslatedStatusText(const char *str)
244 {
245         const wchar_t *wmsg = wgettext(str);
246         showStatusText(wmsg);
247         delete[] wmsg;
248 }
249
250 void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
251 {
252         setStaticText(m_guitext_chat, chat_text);
253
254         m_recent_chat_count = recent_chat_count;
255 }
256
257 void GameUI::updateChatSize()
258 {
259         // Update gui element size and position
260
261         const v2u32 &window_size = RenderingEngine::getWindowSize();
262
263         s32 chat_y = window_size.Y - 150 - m_guitext_chat->getTextHeight();
264
265         if (m_flags.show_minimal_debug)
266                 chat_y += g_fontengine->getLineHeight();
267         if (m_flags.show_basic_debug)
268                 chat_y += g_fontengine->getLineHeight();
269
270         core::rect<s32> chat_size(10, chat_y, window_size.X - 20, 0);
271         chat_size.LowerRightCorner.Y = std::min((s32)window_size.Y,
272                         m_guitext_chat->getTextHeight() + chat_y);
273
274         if (chat_size == m_current_chat_size)
275                 return;
276         m_current_chat_size = chat_size;
277
278         m_guitext_chat->setRelativePosition(chat_size);
279 }
280
281 void GameUI::updateProfiler()
282 {
283         if (m_profiler_current_page != 0) {
284                 std::ostringstream os(std::ios_base::binary);
285                 os << "   Profiler page " << (int)m_profiler_current_page <<
286                                 ", elapsed: " << g_profiler->getElapsedMs() << " ms)" << std::endl;
287
288                 int lines = g_profiler->print(os, m_profiler_current_page, m_profiler_max_page);
289                 ++lines;
290
291                 EnrichedString str(utf8_to_wide(os.str()));
292                 str.setBackground(video::SColor(120, 0, 0, 0));
293                 setStaticText(m_guitext_profiler, str);
294
295                 core::dimension2d<u32> size = m_guitext_profiler->getOverrideFont()->
296                                 getDimension(str.c_str());
297                 core::position2di upper_left(6, 50);
298                 core::position2di lower_right = upper_left;
299                 lower_right.X += size.Width + 10;
300                 lower_right.Y += size.Height;
301
302                 m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
303         }
304
305         m_guitext_profiler->setVisible(m_profiler_current_page != 0);
306 }
307
308 void GameUI::toggleChat()
309 {
310         m_flags.show_chat = !m_flags.show_chat;
311         if (m_flags.show_chat)
312                 showTranslatedStatusText("Chat shown");
313         else
314                 showTranslatedStatusText("Chat hidden");
315 }
316
317 void GameUI::toggleCheatMenu()
318 {
319         m_flags.show_cheat_menu = !m_flags.show_cheat_menu;
320         if (m_flags.show_cheat_menu)
321                 showTranslatedStatusText("Cheat Menu shown");
322         else
323                 showTranslatedStatusText("Cheat Menu hidden");
324 }
325
326 void GameUI::toggleHud()
327 {
328         m_flags.show_hud = !m_flags.show_hud;
329         if (m_flags.show_hud)
330                 showTranslatedStatusText("HUD shown");
331         else
332                 showTranslatedStatusText("HUD hidden");
333 }
334
335 void GameUI::toggleProfiler()
336 {
337         m_profiler_current_page = (m_profiler_current_page + 1) % (m_profiler_max_page + 1);
338
339         // FIXME: This updates the profiler with incomplete values
340         updateProfiler();
341
342         if (m_profiler_current_page != 0) {
343                 std::wstring msg = fwgettext("Profiler shown (page %d of %d)",
344                                 m_profiler_current_page, m_profiler_max_page);
345                 showStatusText(msg);
346         } else {
347                 showTranslatedStatusText("Profiler hidden");
348         }
349 }
350
351
352 void GameUI::deleteFormspec()
353 {
354         if (m_formspec) {
355                 m_formspec->drop();
356                 m_formspec = nullptr;
357         }
358
359         m_formname.clear();
360 }