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