]> git.lizzy.rs Git - minetest.git/blob - src/client/gameui.cpp
6f6d2c2a88dbae01ff4bd5b780f8e428aa67c1bd
[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 "util/pointedthing.h"
26 #include "client.h"
27 #include "clientmap.h"
28 #include "fontengine.h"
29 #include "nodedef.h"
30 #include "profiler.h"
31 #include "renderingengine.h"
32 #include "version.h"
33
34 inline static const char *yawToDirectionString(int yaw)
35 {
36         static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"};
37
38         yaw = wrapDegrees_0_360(yaw);
39         yaw = (yaw + 45) % 360 / 90;
40
41         return direction[yaw];
42 }
43
44 void GameUI::init()
45 {
46         // First line of debug text
47         m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
48                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
49
50         // Second line of debug text
51         m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
52                 false, guiroot);
53
54         // At the middle of the screen
55         // Object infos are shown in this
56         m_guitext_info = gui::StaticText::add(guienv, L"",
57                 core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5)
58                         + v2s32(100, 200), false, true, guiroot);
59
60         // Status text (displays info when showing and hiding GUI stuff, etc.)
61         m_guitext_status = gui::StaticText::add(guienv, L"<Status>",
62                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
63         m_guitext_status->setVisible(false);
64
65         // Chat text
66         m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0),
67                 //false, false); // Disable word wrap as of now
68                 false, true, guiroot);
69
70         // Profiler text (size is updated when text is updated)
71         m_guitext_profiler = gui::StaticText::add(guienv, L"<Profiler>",
72                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
73         m_guitext_profiler->setBackgroundColor(video::SColor(120, 0, 0, 0));
74         m_guitext_profiler->setVisible(false);
75         m_guitext_profiler->setWordWrap(true);
76 }
77
78 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
79         const CameraOrientation &cam, const PointedThing &pointed_old, float dtime)
80 {
81         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
82
83         if (m_flags.show_debug) {
84                 static float drawtime_avg = 0;
85                 drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
86                 u16 fps = 1.0 / stats.dtime_jitter.avg;
87
88                 std::ostringstream os(std::ios_base::binary);
89                 os << std::fixed
90                         << PROJECT_NAME_C " " << g_version_hash
91                         << ", FPS: " << fps
92                         << std::setprecision(0)
93                         << ", drawtime: " << drawtime_avg << "ms"
94                         << std::setprecision(1)
95                         << ", dtime jitter: "
96                         << (stats.dtime_jitter.max_fraction * 100.0) << "%"
97                         << std::setprecision(1)
98                         << ", view range: "
99                         << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
100                         << std::setprecision(3)
101                         << ", RTT: " << client->getRTT() << "s";
102                 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
103
104                 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
105                         5 + g_fontengine->getTextHeight()));
106         }
107
108         // Finally set the guitext visible depending on the flag
109         m_guitext->setVisible(m_flags.show_debug);
110
111         if (m_flags.show_debug) {
112                 LocalPlayer *player = client->getEnv().getLocalPlayer();
113                 v3f player_position = player->getPosition();
114
115                 std::ostringstream os(std::ios_base::binary);
116                 os << std::setprecision(1) << std::fixed
117                         << "pos: (" << (player_position.X / BS)
118                         << ", " << (player_position.Y / BS)
119                         << ", " << (player_position.Z / BS)
120                         << "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
121                         << yawToDirectionString(cam.camera_yaw)
122                         << ", seed: " << ((u64)client->getMapSeed());
123
124                 if (pointed_old.type == POINTEDTHING_NODE) {
125                         ClientMap &map = client->getEnv().getClientMap();
126                         const INodeDefManager *nodedef = client->getNodeDefManager();
127                         MapNode n = map.getNodeNoEx(pointed_old.node_undersurface);
128
129                         if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
130                                 os << ", pointed: " << nodedef->get(n).name
131                                         << ", param2: " << (u64) n.getParam2();
132                         }
133                 }
134
135                 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
136
137                 m_guitext2->setRelativePosition(core::rect<s32>(5,
138                         5 + g_fontengine->getTextHeight(), screensize.X,
139                         5 + g_fontengine->getTextHeight() * 2
140                 ));
141         }
142
143         m_guitext2->setVisible(m_flags.show_debug);
144
145         setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
146         m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
147
148         static const float statustext_time_max = 1.5f;
149
150         if (!m_statustext.empty()) {
151                 m_statustext_time += dtime;
152
153                 if (m_statustext_time >= statustext_time_max) {
154                         clearStatusText();
155                         m_statustext_time = 0.0f;
156                 }
157         }
158
159         setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
160         m_guitext_status->setVisible(!m_statustext.empty());
161
162         if (!m_statustext.empty()) {
163                 s32 status_width  = m_guitext_status->getTextWidth();
164                 s32 status_height = m_guitext_status->getTextHeight();
165                 s32 status_y = screensize.Y - 150;
166                 s32 status_x = (screensize.X - status_width) / 2;
167
168                 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
169                         status_y - status_height, status_x + status_width, status_y));
170
171                 // Fade out
172                 video::SColor initial_color(255, 0, 0, 0);
173
174                 if (guienv->getSkin())
175                         initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
176
177                 video::SColor final_color = initial_color;
178                 final_color.setAlpha(0);
179                 video::SColor fade_color = initial_color.getInterpolated_quadratic(
180                         initial_color, final_color,
181                         pow(m_statustext_time / statustext_time_max, 2.0f));
182                 m_guitext_status->setOverrideColor(fade_color);
183                 m_guitext_status->enableOverrideColor(true);
184         }
185 }
186
187 void GameUI::initFlags()
188 {
189         memset(&m_flags, 0, sizeof(GameUI::Flags));
190         m_flags.show_chat = true;
191         m_flags.show_hud = true;
192         m_flags.show_debug = g_settings->getBool("show_debug");
193 }
194
195 void GameUI::showMinimap(bool show)
196 {
197         m_flags.show_minimap = show;
198 }
199
200 void GameUI::showTranslatedStatusText(const char *str)
201 {
202         const wchar_t *wmsg = wgettext(str);
203         showStatusText(wmsg);
204         delete[] wmsg;
205 }
206
207 void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
208 {
209         setStaticText(m_guitext_chat, chat_text);
210
211         // Update gui element size and position
212         s32 chat_y = 5;
213
214         if (m_flags.show_debug)
215                 chat_y += 2 * g_fontengine->getLineHeight();
216
217         // first pass to calculate height of text to be set
218         const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
219         s32 width = std::min(g_fontengine->getTextWidth(chat_text.c_str()) + 10,
220                 window_size.X - 20);
221         m_guitext_chat->setRelativePosition(core::rect<s32>(10, chat_y, width,
222                 chat_y + window_size.Y));
223
224         // now use real height of text and adjust rect according to this size
225         m_guitext_chat->setRelativePosition(core::rect<s32>(10, chat_y, width,
226                 chat_y + m_guitext_chat->getTextHeight()));
227
228         // Don't show chat if disabled or empty or profiler is enabled
229         m_guitext_chat->setVisible(m_flags.show_chat &&
230                 recent_chat_count != 0 && m_profiler_current_page == 0);
231 }
232
233 void GameUI::updateProfiler()
234 {
235         if (m_profiler_current_page != 0) {
236                 std::ostringstream os(std::ios_base::binary);
237                 g_profiler->printPage(os, m_profiler_current_page, m_profiler_max_page);
238
239                 std::wstring text = translate_string(utf8_to_wide(os.str()));
240                 setStaticText(m_guitext_profiler, text.c_str());
241
242                 s32 w = g_fontengine->getTextWidth(text);
243
244                 if (w < 400)
245                         w = 400;
246
247                 unsigned text_height = g_fontengine->getTextHeight();
248
249                 core::position2di upper_left, lower_right;
250
251                 upper_left.X  = 6;
252                 upper_left.Y  = (text_height + 5) * 2;
253                 lower_right.X = 12 + w;
254                 lower_right.Y = upper_left.Y + (text_height + 1) * MAX_PROFILER_TEXT_ROWS;
255
256                 s32 screen_height = RenderingEngine::get_video_driver()->getScreenSize().Height;
257
258                 if (lower_right.Y > screen_height * 2 / 3)
259                         lower_right.Y = screen_height * 2 / 3;
260
261                 m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
262         }
263
264         m_guitext_profiler->setVisible(m_profiler_current_page != 0);
265 }
266
267 void GameUI::toggleChat()
268 {
269         m_flags.show_chat = !m_flags.show_chat;
270         if (m_flags.show_chat)
271                 showTranslatedStatusText("Chat shown");
272         else
273                 showTranslatedStatusText("Chat hidden");
274 }
275
276 void GameUI::toggleHud()
277 {
278         m_flags.show_hud = !m_flags.show_hud;
279         if (m_flags.show_hud)
280                 showTranslatedStatusText("HUD shown");
281         else
282                 showTranslatedStatusText("HUD hidden");
283 }
284
285 void GameUI::toggleProfiler()
286 {
287         m_profiler_current_page = (m_profiler_current_page + 1) % (m_profiler_max_page + 1);
288
289         // FIXME: This updates the profiler with incomplete values
290         updateProfiler();
291
292         if (m_profiler_current_page != 0) {
293                 wchar_t buf[255];
294                 const wchar_t* str = wgettext("Profiler shown (page %d of %d)");
295                 swprintf(buf, sizeof(buf) / sizeof(wchar_t), str,
296                         m_profiler_current_page, m_profiler_max_page);
297                 delete[] str;
298                 showStatusText(buf);
299         } else {
300                 showTranslatedStatusText("Profiler hidden");
301         }
302 }