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