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>
24 #include "gui/mainmenumanager.h"
25 #include "gui/guiChatConsole.h"
26 #include "util/pointedthing.h"
28 #include "clientmap.h"
29 #include "fontengine.h"
32 #include "renderingengine.h"
35 inline static const char *yawToDirectionString(int yaw)
37 static const char *direction[4] =
38 {"North +Z", "West -X", "South -Z", "East +X"};
40 yaw = wrapDegrees_0_360(yaw);
41 yaw = (yaw + 45) % 360 / 90;
43 return direction[yaw];
48 if (guienv && guienv->getSkin())
49 m_statustext_initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
51 m_statustext_initial_color = video::SColor(255, 0, 0, 0);
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);
60 // Second line of debug text
61 m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
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);
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);
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);
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->setOverrideFont(g_fontengine->getFont(
84 g_fontengine->getDefaultFontSize() * 0.9f, FM_Mono));
85 m_guitext_profiler->setBackgroundColor(video::SColor(120, 0, 0, 0));
86 m_guitext_profiler->setVisible(false);
89 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
90 const CameraOrientation &cam, const PointedThing &pointed_old,
91 const GUIChatConsole *chat_console, float dtime)
93 v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
95 if (m_flags.show_debug) {
96 static float drawtime_avg = 0;
97 drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
98 u16 fps = 1.0 / stats.dtime_jitter.avg;
100 std::ostringstream os(std::ios_base::binary);
102 << PROJECT_NAME_C " " << g_version_hash
104 << std::setprecision(0)
105 << " | drawtime: " << drawtime_avg << "ms"
106 << std::setprecision(1)
107 << " | dtime jitter: "
108 << (stats.dtime_jitter.max_fraction * 100.0) << "%"
109 << std::setprecision(1)
111 << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
112 << std::setprecision(3)
113 << " | RTT: " << client->getRTT() << "s";
114 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
116 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
117 5 + g_fontengine->getTextHeight()));
120 // Finally set the guitext visible depending on the flag
121 m_guitext->setVisible(m_flags.show_debug);
123 if (m_flags.show_debug) {
124 LocalPlayer *player = client->getEnv().getLocalPlayer();
125 v3f player_position = player->getPosition();
127 std::ostringstream os(std::ios_base::binary);
128 os << std::setprecision(1) << std::fixed
129 << "pos: (" << (player_position.X / BS)
130 << ", " << (player_position.Y / BS)
131 << ", " << (player_position.Z / BS)
132 << ") | yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
133 << yawToDirectionString(cam.camera_yaw)
134 << " | pitch: " << (-wrapDegrees_180(cam.camera_pitch)) << "°"
135 << " | seed: " << ((u64)client->getMapSeed());
137 if (pointed_old.type == POINTEDTHING_NODE) {
138 ClientMap &map = client->getEnv().getClientMap();
139 const NodeDefManager *nodedef = client->getNodeDefManager();
140 MapNode n = map.getNode(pointed_old.node_undersurface);
142 if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
143 os << ", pointed: " << nodedef->get(n).name
144 << ", param2: " << (u64) n.getParam2();
148 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
150 m_guitext2->setRelativePosition(core::rect<s32>(5,
151 5 + g_fontengine->getTextHeight(), screensize.X,
152 5 + g_fontengine->getTextHeight() * 2
156 m_guitext2->setVisible(m_flags.show_debug);
158 setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
159 m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
161 static const float statustext_time_max = 1.5f;
163 if (!m_statustext.empty()) {
164 m_statustext_time += dtime;
166 if (m_statustext_time >= statustext_time_max) {
168 m_statustext_time = 0.0f;
172 setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
173 m_guitext_status->setVisible(!m_statustext.empty());
175 if (!m_statustext.empty()) {
176 s32 status_width = m_guitext_status->getTextWidth();
177 s32 status_height = m_guitext_status->getTextHeight();
178 s32 status_y = screensize.Y - 150;
179 s32 status_x = (screensize.X - status_width) / 2;
181 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
182 status_y - status_height, status_x + status_width, status_y));
185 video::SColor final_color = m_statustext_initial_color;
186 final_color.setAlpha(0);
187 video::SColor fade_color = m_statustext_initial_color.getInterpolated_quadratic(
188 m_statustext_initial_color, final_color, m_statustext_time / statustext_time_max);
189 m_guitext_status->setOverrideColor(fade_color);
190 m_guitext_status->enableOverrideColor(true);
193 // Hide chat when console is visible
194 m_guitext_chat->setVisible(isChatVisible() && !chat_console->isVisible());
197 void GameUI::initFlags()
199 m_flags = GameUI::Flags();
200 m_flags.show_debug = g_settings->getBool("show_debug");
203 void GameUI::showMinimap(bool show)
205 m_flags.show_minimap = show;
208 void GameUI::showTranslatedStatusText(const char *str)
210 const wchar_t *wmsg = wgettext(str);
211 showStatusText(wmsg);
215 void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
217 setStaticText(m_guitext_chat, chat_text);
219 // Update gui element size and position
222 if (m_flags.show_debug)
223 chat_y += 2 * g_fontengine->getLineHeight();
225 // first pass to calculate height of text to be set
226 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
227 s32 width = std::min(g_fontengine->getTextWidth(chat_text.c_str()) + 10,
229 m_guitext_chat->setRelativePosition(core::rect<s32>(10, chat_y, width,
230 chat_y + window_size.Y));
232 // now use real height of text and adjust rect according to this size
233 m_guitext_chat->setRelativePosition(core::rect<s32>(10, chat_y, width,
234 chat_y + m_guitext_chat->getTextHeight()));
236 m_recent_chat_count = recent_chat_count;
239 void GameUI::updateProfiler()
241 if (m_profiler_current_page != 0) {
242 std::ostringstream os(std::ios_base::binary);
243 os << " Profiler page " << (int)m_profiler_current_page <<
244 ", elapsed: " << g_profiler->getElapsedMs() << " ms)" << std::endl;
246 int lines = g_profiler->print(os, m_profiler_current_page, m_profiler_max_page);
249 std::wstring text = utf8_to_wide(os.str());
250 setStaticText(m_guitext_profiler, text.c_str());
252 core::dimension2d<u32> size = m_guitext_profiler->getOverrideFont()->
253 getDimension(text.c_str());
254 core::position2di upper_left(6, 50);
255 core::position2di lower_right = upper_left;
256 lower_right.X += size.Width + 10;
257 lower_right.Y += size.Height;
259 m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
262 m_guitext_profiler->setVisible(m_profiler_current_page != 0);
265 void GameUI::toggleChat()
267 m_flags.show_chat = !m_flags.show_chat;
268 if (m_flags.show_chat)
269 showTranslatedStatusText("Chat shown");
271 showTranslatedStatusText("Chat hidden");
274 void GameUI::toggleHud()
276 m_flags.show_hud = !m_flags.show_hud;
277 if (m_flags.show_hud)
278 showTranslatedStatusText("HUD shown");
280 showTranslatedStatusText("HUD hidden");
283 void GameUI::toggleProfiler()
285 m_profiler_current_page = (m_profiler_current_page + 1) % (m_profiler_max_page + 1);
287 // FIXME: This updates the profiler with incomplete values
290 if (m_profiler_current_page != 0) {
292 const wchar_t* str = wgettext("Profiler shown (page %d of %d)");
293 swprintf(buf, sizeof(buf) / sizeof(wchar_t), str,
294 m_profiler_current_page, m_profiler_max_page);
298 showTranslatedStatusText("Profiler hidden");
303 void GameUI::deleteFormspec()
307 m_formspec = nullptr;