3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
22 #include <irrlicht_changes/static_text.h>
23 #include "gui/mainmenumanager.h"
24 #include "util/pointedthing.h"
26 #include "clientmap.h"
27 #include "fontengine.h"
29 #include "renderingengine.h"
32 inline static const char *yawToDirectionString(int yaw)
34 static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"};
36 yaw = wrapDegrees_0_360(yaw);
37 yaw = (yaw + 45) % 360 / 90;
39 return direction[yaw];
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);
48 // Second line of debug text
49 m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
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);
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);
62 m_guitext_status->setVisible(false);
65 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
66 const CameraOrientation &cam, const PointedThing &pointed_old, float dtime)
68 v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
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;
75 std::ostringstream os(std::ios_base::binary);
77 << PROJECT_NAME_C " " << g_version_hash
79 << std::setprecision(0)
80 << ", drawtime: " << drawtime_avg << "ms"
81 << std::setprecision(1)
83 << (stats.dtime_jitter.max_fraction * 100.0) << "%"
84 << std::setprecision(1)
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());
91 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
92 5 + g_fontengine->getTextHeight()));
95 // Finally set the guitext visible depending on the flag
96 m_guitext->setVisible(m_flags.show_debug);
98 if (m_flags.show_debug) {
99 LocalPlayer *player = client->getEnv().getLocalPlayer();
100 v3f player_position = player->getPosition();
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());
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);
116 if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
117 os << ", pointed: " << nodedef->get(n).name
118 << ", param2: " << (u64) n.getParam2();
122 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
124 m_guitext2->setRelativePosition(core::rect<s32>(5,
125 5 + g_fontengine->getTextHeight(), screensize.X,
126 5 + g_fontengine->getTextHeight() * 2
130 m_guitext2->setVisible(m_flags.show_debug);
132 setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
133 m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
135 static const float statustext_time_max = 1.5f;
137 if (!m_statustext.empty()) {
138 m_statustext_time += dtime;
140 if (m_statustext_time >= statustext_time_max) {
142 m_statustext_time = 0.0f;
146 setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
147 m_guitext_status->setVisible(!m_statustext.empty());
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;
155 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
156 status_y - status_height, status_x + status_width, status_y));
159 video::SColor initial_color(255, 0, 0, 0);
161 if (guienv->getSkin())
162 initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
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);
174 void GameUI::initFlags()
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");
182 void GameUI::showMinimap(bool show)
184 m_flags.show_minimap = show;