GameUI refactor (part 4/X): Move Game::guitext_status, Game::m_statustext, GameRunDat...
[oweals/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 "gui/mainmenumanager.h"
24 #include "util/pointedthing.h"
25 #include "client.h"
26 #include "clientmap.h"
27 #include "fontengine.h"
28 #include "nodedef.h"
29 #include "renderingengine.h"
30 #include "version.h"
31
32 inline static const char *yawToDirectionString(int yaw)
33 {
34         static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"};
35
36         yaw = wrapDegrees_0_360(yaw);
37         yaw = (yaw + 45) % 360 / 90;
38
39         return direction[yaw];
40 }
41
42 void GameUI::init()
43 {
44         // First line of debug text
45         m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
46                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
47
48         // Second line of debug text
49         m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
50                 false, guiroot);
51
52         // At the middle of the screen
53         // Object infos are shown in this
54         m_guitext_info = gui::StaticText::add(guienv, L"",
55                 core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5)
56                         + v2s32(100, 200), false, true, guiroot);
57
58         // Status text (displays info when showing and hiding GUI stuff, etc.)
59         m_guitext_status = gui::StaticText::add(guienv, L"<Status>",
60                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
61
62         m_guitext_status->setVisible(false);
63 }
64
65 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
66         const CameraOrientation &cam, const PointedThing &pointed_old, float dtime)
67 {
68         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
69
70         if (m_flags.show_debug) {
71                 static float drawtime_avg = 0;
72                 drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
73                 u16 fps = 1.0 / stats.dtime_jitter.avg;
74
75                 std::ostringstream os(std::ios_base::binary);
76                 os << std::fixed
77                         << PROJECT_NAME_C " " << g_version_hash
78                         << ", FPS: " << fps
79                         << std::setprecision(0)
80                         << ", drawtime: " << drawtime_avg << "ms"
81                         << std::setprecision(1)
82                         << ", dtime jitter: "
83                         << (stats.dtime_jitter.max_fraction * 100.0) << "%"
84                         << std::setprecision(1)
85                         << ", view range: "
86                         << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
87                         << std::setprecision(3)
88                         << ", RTT: " << client->getRTT() << "s";
89                 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
90
91                 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
92                         5 + g_fontengine->getTextHeight()));
93         }
94
95         // Finally set the guitext visible depending on the flag
96         m_guitext->setVisible(m_flags.show_debug);
97
98         if (m_flags.show_debug) {
99                 LocalPlayer *player = client->getEnv().getLocalPlayer();
100                 v3f player_position = player->getPosition();
101
102                 std::ostringstream os(std::ios_base::binary);
103                 os << std::setprecision(1) << std::fixed
104                         << "pos: (" << (player_position.X / BS)
105                         << ", " << (player_position.Y / BS)
106                         << ", " << (player_position.Z / BS)
107                         << "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
108                         << yawToDirectionString(cam.camera_yaw)
109                         << ", seed: " << ((u64)client->getMapSeed());
110
111                 if (pointed_old.type == POINTEDTHING_NODE) {
112                         ClientMap &map = client->getEnv().getClientMap();
113                         const INodeDefManager *nodedef = client->getNodeDefManager();
114                         MapNode n = map.getNodeNoEx(pointed_old.node_undersurface);
115
116                         if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
117                                 os << ", pointed: " << nodedef->get(n).name
118                                         << ", param2: " << (u64) n.getParam2();
119                         }
120                 }
121
122                 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
123
124                 m_guitext2->setRelativePosition(core::rect<s32>(5,
125                         5 + g_fontengine->getTextHeight(), screensize.X,
126                         5 + g_fontengine->getTextHeight() * 2
127                 ));
128         }
129
130         m_guitext2->setVisible(m_flags.show_debug);
131
132         setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
133         m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
134
135         static const float statustext_time_max = 1.5f;
136
137         if (!m_statustext.empty()) {
138                 m_statustext_time += dtime;
139
140                 if (m_statustext_time >= statustext_time_max) {
141                         clearStatusText();
142                         m_statustext_time = 0.0f;
143                 }
144         }
145
146         setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
147         m_guitext_status->setVisible(!m_statustext.empty());
148
149         if (!m_statustext.empty()) {
150                 s32 status_width  = m_guitext_status->getTextWidth();
151                 s32 status_height = m_guitext_status->getTextHeight();
152                 s32 status_y = screensize.Y - 150;
153                 s32 status_x = (screensize.X - status_width) / 2;
154
155                 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
156                         status_y - status_height, status_x + status_width, status_y));
157
158                 // Fade out
159                 video::SColor initial_color(255, 0, 0, 0);
160
161                 if (guienv->getSkin())
162                         initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
163
164                 video::SColor final_color = initial_color;
165                 final_color.setAlpha(0);
166                 video::SColor fade_color = initial_color.getInterpolated_quadratic(
167                         initial_color, final_color,
168                         pow(m_statustext_time / statustext_time_max, 2.0f));
169                 m_guitext_status->setOverrideColor(fade_color);
170                 m_guitext_status->enableOverrideColor(true);
171         }
172 }
173
174 void GameUI::initFlags()
175 {
176         memset(&m_flags, 0, sizeof(GameUI::Flags));
177         m_flags.show_chat = true;
178         m_flags.show_hud = true;
179         m_flags.show_debug = g_settings->getBool("show_debug");
180 }
181
182 void GameUI::showMinimap(bool show)
183 {
184         m_flags.show_minimap = show;
185 }