]> 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         // At the middle of the screen
78         // Object infos are shown in this
79         u32 chat_font_height = m_guitext_chat->getActiveFont()->getDimension(L"Ay").Height;
80         m_guitext_info = gui::StaticText::add(guienv, L"",
81                 core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5) +
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         LocalPlayer *player = client->getEnv().getLocalPlayer();
104         v3f player_position = player->getPosition();
105         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
106
107         bool show_coords = g_settings->getBool("coords");
108
109         if (show_coords) {
110                 std::ostringstream os(std::ios_base::binary);
111                 os << std::setprecision(1) << std::fixed
112                         << (player_position.X / BS)
113                         << ", " << (player_position.Y / BS)
114                         << ", " << (player_position.Z / BS);
115                 setStaticText(m_guitext_coords, utf8_to_wide(os.str()).c_str());
116                 m_guitext_coords->setRelativePosition(core::rect<s32>(5, screensize.Y - 5 - g_fontengine->getTextHeight(), screensize.X, screensize.Y));
117         }
118         
119         m_guitext_coords->setVisible(show_coords);
120
121         if (m_flags.show_debug) {
122                 static float drawtime_avg = 0;
123                 drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
124                 u16 fps = 1.0 / stats.dtime_jitter.avg;
125
126                 std::ostringstream os(std::ios_base::binary);
127                 os << std::fixed
128                         << PROJECT_NAME_C " " << g_version_hash
129                         << " | FPS: " << fps
130                         << std::setprecision(0)
131                         << " | drawtime: " << drawtime_avg << "ms"
132                         << std::setprecision(1)
133                         << " | dtime jitter: "
134                         << (stats.dtime_jitter.max_fraction * 100.0) << "%"
135                         << std::setprecision(1)
136                         << " | view range: "
137                         << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
138                         << std::setprecision(2)
139                         << " | RTT: " << (client->getRTT() * 1000.0f) << "ms";
140                 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
141
142                 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
143                         5 + g_fontengine->getTextHeight()));
144         }
145
146         // Finally set the guitext visible depending on the flag
147         m_guitext->setVisible(m_flags.show_debug);
148
149         if (m_flags.show_debug) {
150                 std::ostringstream os(std::ios_base::binary);
151                 os << std::setprecision(1) << std::fixed
152                         << "pos: (" << (player_position.X / BS)
153                         << ", " << (player_position.Y / BS)
154                         << ", " << (player_position.Z / BS)
155                         << ") | yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
156                         << yawToDirectionString(cam.camera_yaw)
157                         << " | pitch: " << (-wrapDegrees_180(cam.camera_pitch)) << "°"
158                         << " | seed: " << ((u64)client->getMapSeed());
159
160                 if (pointed_old.type == POINTEDTHING_NODE) {
161                         ClientMap &map = client->getEnv().getClientMap();
162                         const NodeDefManager *nodedef = client->getNodeDefManager();
163                         MapNode n = map.getNode(pointed_old.node_undersurface);
164
165                         if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
166                                 os << ", pointed: " << nodedef->get(n).name
167                                         << ", param2: " << (u64) n.getParam2();
168                         }
169                 }
170
171                 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
172
173                 m_guitext2->setRelativePosition(core::rect<s32>(5,
174                         5 + g_fontengine->getTextHeight(), screensize.X,
175                         5 + g_fontengine->getTextHeight() * 2
176                 ));
177         }
178
179         m_guitext2->setVisible(m_flags.show_debug);
180
181         setStaticText(m_guitext_info, m_infotext.c_str());
182         m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
183
184         static const float statustext_time_max = 1.5f;
185
186         if (!m_statustext.empty()) {
187                 m_statustext_time += dtime;
188
189                 if (m_statustext_time >= statustext_time_max) {
190                         clearStatusText();
191                         m_statustext_time = 0.0f;
192                 }
193         }
194
195         setStaticText(m_guitext_status, m_statustext.c_str());
196         m_guitext_status->setVisible(!m_statustext.empty());
197
198         if (!m_statustext.empty()) {
199                 s32 status_width  = m_guitext_status->getTextWidth();
200                 s32 status_height = m_guitext_status->getTextHeight();
201                 s32 status_y = screensize.Y - 150;
202                 s32 status_x = (screensize.X - status_width) / 2;
203
204                 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
205                         status_y - status_height, status_x + status_width, status_y));
206
207                 // Fade out
208                 video::SColor final_color = m_statustext_initial_color;
209                 final_color.setAlpha(0);
210                 video::SColor fade_color = m_statustext_initial_color.getInterpolated_quadratic(
211                         m_statustext_initial_color, final_color, m_statustext_time / statustext_time_max);
212                 m_guitext_status->setOverrideColor(fade_color);
213                 m_guitext_status->enableOverrideColor(true);
214         }
215
216         m_guitext_chat->setVisible(isChatVisible());
217 }
218
219 void GameUI::initFlags()
220 {
221         m_flags = GameUI::Flags();
222         m_flags.show_debug = g_settings->getBool("show_debug");
223 }
224
225 void GameUI::showMinimap(bool show)
226 {
227         m_flags.show_minimap = show;
228 }
229
230 void GameUI::showTranslatedStatusText(const char *str)
231 {
232         const wchar_t *wmsg = wgettext(str);
233         showStatusText(wmsg);
234         delete[] wmsg;
235 }
236
237 void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
238 {
239
240         // Update gui element size and position
241         
242         const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
243         
244         s32 chat_y = window_size.Y - 150 - m_guitext_chat->getTextHeight();
245
246         if (m_flags.show_debug)
247                 chat_y += 2 * g_fontengine->getLineHeight();
248
249         core::rect<s32> chat_size(10, chat_y,
250                 window_size.X - 20, 0);
251         chat_size.LowerRightCorner.Y = std::min((s32)window_size.Y,
252                 m_guitext_chat->getTextHeight() + chat_y);
253
254         m_guitext_chat->setRelativePosition(chat_size);
255         setStaticText(m_guitext_chat, chat_text);
256
257         m_recent_chat_count = recent_chat_count;
258 }
259
260 void GameUI::updateProfiler()
261 {
262         if (m_profiler_current_page != 0) {
263                 std::ostringstream os(std::ios_base::binary);
264                 os << "   Profiler page " << (int)m_profiler_current_page <<
265                                 ", elapsed: " << g_profiler->getElapsedMs() << " ms)" << std::endl;
266
267                 int lines = g_profiler->print(os, m_profiler_current_page, m_profiler_max_page);
268                 ++lines;
269
270                 EnrichedString str(utf8_to_wide(os.str()));
271                 str.setBackground(video::SColor(120, 0, 0, 0));
272                 setStaticText(m_guitext_profiler, str);
273
274                 core::dimension2d<u32> size = m_guitext_profiler->getOverrideFont()->
275                                 getDimension(str.c_str());
276                 core::position2di upper_left(6, 50);
277                 core::position2di lower_right = upper_left;
278                 lower_right.X += size.Width + 10;
279                 lower_right.Y += size.Height; 
280
281                 m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
282         }
283
284         m_guitext_profiler->setVisible(m_profiler_current_page != 0);
285 }
286
287 void GameUI::toggleChat()
288 {
289         m_flags.show_chat = !m_flags.show_chat;
290         if (m_flags.show_chat)
291                 showTranslatedStatusText("Chat shown");
292         else
293                 showTranslatedStatusText("Chat hidden");
294 }
295
296 void GameUI::toggleCheatMenu()
297 {
298         m_flags.show_cheat_menu = !m_flags.show_cheat_menu;
299         if (m_flags.show_cheat_menu)
300                 showTranslatedStatusText("Cheat Menu shown");
301         else
302                 showTranslatedStatusText("Cheat Menu hidden");
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                 wchar_t buf[255];
323                 const wchar_t* str = wgettext("Profiler shown (page %d of %d)");
324                 swprintf(buf, sizeof(buf) / sizeof(wchar_t), str,
325                         m_profiler_current_page, m_profiler_max_page);
326                 delete[] str;
327                 showStatusText(buf);
328         } else {
329                 showTranslatedStatusText("Profiler hidden");
330         }
331 }
332
333
334 void GameUI::deleteFormspec()
335 {
336         if (m_formspec) {
337                 m_formspec->drop();
338                 m_formspec = nullptr;
339         }
340
341         m_formname.clear();
342 }