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