2018-01-03 14:28:55 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gameui.h"
|
2018-01-03 17:28:57 +01:00
|
|
|
#include <irrlicht_changes/static_text.h>
|
2018-01-04 23:32:32 +01:00
|
|
|
#include <gettext.h>
|
2018-01-03 17:28:57 +01:00
|
|
|
#include "gui/mainmenumanager.h"
|
2019-08-07 19:15:33 +02:00
|
|
|
#include "gui/guiChatConsole.h"
|
2018-01-04 19:52:40 +01:00
|
|
|
#include "util/pointedthing.h"
|
2018-01-03 17:28:57 +01:00
|
|
|
#include "client.h"
|
|
|
|
#include "clientmap.h"
|
2018-01-04 19:52:40 +01:00
|
|
|
#include "fontengine.h"
|
|
|
|
#include "nodedef.h"
|
2018-01-04 23:32:32 +01:00
|
|
|
#include "profiler.h"
|
2018-01-03 17:28:57 +01:00
|
|
|
#include "renderingengine.h"
|
2018-01-04 19:52:40 +01:00
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
inline static const char *yawToDirectionString(int yaw)
|
|
|
|
{
|
2018-10-06 01:53:50 +02:00
|
|
|
static const char *direction[4] =
|
|
|
|
{"North +Z", "West -X", "South -Z", "East +X"};
|
2018-01-04 19:52:40 +01:00
|
|
|
|
|
|
|
yaw = wrapDegrees_0_360(yaw);
|
|
|
|
yaw = (yaw + 45) % 360 / 90;
|
|
|
|
|
|
|
|
return direction[yaw];
|
|
|
|
}
|
2018-01-03 17:28:57 +01:00
|
|
|
|
2018-01-05 19:39:06 +01:00
|
|
|
GameUI::GameUI()
|
|
|
|
{
|
|
|
|
if (guienv && guienv->getSkin())
|
|
|
|
m_statustext_initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
|
|
|
|
else
|
|
|
|
m_statustext_initial_color = video::SColor(255, 0, 0, 0);
|
|
|
|
|
|
|
|
}
|
2018-01-03 17:28:57 +01:00
|
|
|
void GameUI::init()
|
|
|
|
{
|
|
|
|
// First line of debug text
|
|
|
|
m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
|
|
|
|
core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
|
2018-01-04 19:52:40 +01:00
|
|
|
|
|
|
|
// Second line of debug text
|
|
|
|
m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
|
|
|
|
false, guiroot);
|
|
|
|
|
2018-01-04 23:04:40 +01:00
|
|
|
// Chat text
|
|
|
|
m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0),
|
|
|
|
//false, false); // Disable word wrap as of now
|
|
|
|
false, true, guiroot);
|
2020-05-24 14:24:13 +02:00
|
|
|
u16 chat_font_size = g_settings->getU16("chat_font_size");
|
|
|
|
if (chat_font_size != 0) {
|
|
|
|
m_guitext_chat->setOverrideFont(g_fontengine->getFont(
|
|
|
|
chat_font_size, FM_Unspecified));
|
|
|
|
}
|
2018-01-04 23:32:32 +01:00
|
|
|
|
2021-08-23 22:13:17 +02:00
|
|
|
|
|
|
|
// Infotext of nodes and objects.
|
|
|
|
// If in debug mode, object debug infos shown here, too.
|
|
|
|
// Located on the left on the screen, below chat.
|
2020-09-22 18:38:33 +02:00
|
|
|
u32 chat_font_height = m_guitext_chat->getActiveFont()->getDimension(L"Ay").Height;
|
|
|
|
m_guitext_info = gui::StaticText::add(guienv, L"",
|
2021-08-23 22:13:17 +02:00
|
|
|
// Size is limited; text will be truncated after 6 lines.
|
|
|
|
core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 6) +
|
2020-09-22 18:38:33 +02:00
|
|
|
v2s32(100, chat_font_height *
|
|
|
|
(g_settings->getU16("recent_chat_messages") + 3)),
|
|
|
|
false, true, guiroot);
|
|
|
|
|
|
|
|
// Status text (displays info when showing and hiding GUI stuff, etc.)
|
|
|
|
m_guitext_status = gui::StaticText::add(guienv, L"<Status>",
|
|
|
|
core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
|
|
|
|
m_guitext_status->setVisible(false);
|
|
|
|
|
2018-01-04 23:32:32 +01:00
|
|
|
// Profiler text (size is updated when text is updated)
|
|
|
|
m_guitext_profiler = gui::StaticText::add(guienv, L"<Profiler>",
|
|
|
|
core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
|
2019-08-13 19:56:55 +02:00
|
|
|
m_guitext_profiler->setOverrideFont(g_fontengine->getFont(
|
|
|
|
g_fontengine->getDefaultFontSize() * 0.9f, FM_Mono));
|
2018-01-04 23:32:32 +01:00
|
|
|
m_guitext_profiler->setVisible(false);
|
2018-01-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 19:52:40 +01:00
|
|
|
void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
|
2019-08-07 19:15:33 +02:00
|
|
|
const CameraOrientation &cam, const PointedThing &pointed_old,
|
|
|
|
const GUIChatConsole *chat_console, float dtime)
|
2018-01-03 17:28:57 +01:00
|
|
|
{
|
2021-04-28 12:48:13 +02:00
|
|
|
v2u32 screensize = RenderingEngine::getWindowSize();
|
2018-01-04 19:52:40 +01:00
|
|
|
|
2021-06-24 20:21:19 +02:00
|
|
|
// Minimal debug text must only contain info that can't give a gameplay advantage
|
|
|
|
if (m_flags.show_minimal_debug) {
|
2018-01-03 17:28:57 +01:00
|
|
|
static float drawtime_avg = 0;
|
|
|
|
drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
|
|
|
|
u16 fps = 1.0 / stats.dtime_jitter.avg;
|
|
|
|
|
|
|
|
std::ostringstream os(std::ios_base::binary);
|
|
|
|
os << std::fixed
|
|
|
|
<< PROJECT_NAME_C " " << g_version_hash
|
2019-04-27 12:45:44 +02:00
|
|
|
<< " | FPS: " << fps
|
2018-01-03 17:28:57 +01:00
|
|
|
<< std::setprecision(0)
|
2019-04-27 12:45:44 +02:00
|
|
|
<< " | drawtime: " << drawtime_avg << "ms"
|
2018-01-03 17:28:57 +01:00
|
|
|
<< std::setprecision(1)
|
2019-04-27 12:45:44 +02:00
|
|
|
<< " | dtime jitter: "
|
2018-01-03 17:28:57 +01:00
|
|
|
<< (stats.dtime_jitter.max_fraction * 100.0) << "%"
|
|
|
|
<< std::setprecision(1)
|
2019-04-27 12:45:44 +02:00
|
|
|
<< " | view range: "
|
2018-01-03 17:28:57 +01:00
|
|
|
<< (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
|
2020-10-29 20:09:59 +01:00
|
|
|
<< std::setprecision(2)
|
|
|
|
<< " | RTT: " << (client->getRTT() * 1000.0f) << "ms";
|
2018-01-03 17:28:57 +01:00
|
|
|
setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
|
|
|
|
|
|
|
|
m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
|
|
|
|
5 + g_fontengine->getTextHeight()));
|
|
|
|
}
|
2018-01-04 19:52:40 +01:00
|
|
|
|
|
|
|
// Finally set the guitext visible depending on the flag
|
2021-06-24 20:21:19 +02:00
|
|
|
m_guitext->setVisible(m_flags.show_minimal_debug);
|
2018-01-04 19:52:40 +01:00
|
|
|
|
2021-06-24 20:21:19 +02:00
|
|
|
// Basic debug text also shows info that might give a gameplay advantage
|
|
|
|
if (m_flags.show_basic_debug) {
|
2018-01-04 19:52:40 +01:00
|
|
|
LocalPlayer *player = client->getEnv().getLocalPlayer();
|
|
|
|
v3f player_position = player->getPosition();
|
|
|
|
|
|
|
|
std::ostringstream os(std::ios_base::binary);
|
|
|
|
os << std::setprecision(1) << std::fixed
|
|
|
|
<< "pos: (" << (player_position.X / BS)
|
|
|
|
<< ", " << (player_position.Y / BS)
|
|
|
|
<< ", " << (player_position.Z / BS)
|
2020-07-29 23:17:52 +02:00
|
|
|
<< ") | yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
|
2018-01-04 19:52:40 +01:00
|
|
|
<< yawToDirectionString(cam.camera_yaw)
|
2020-07-29 23:17:52 +02:00
|
|
|
<< " | pitch: " << (-wrapDegrees_180(cam.camera_pitch)) << "°"
|
2019-04-27 12:45:44 +02:00
|
|
|
<< " | seed: " << ((u64)client->getMapSeed());
|
2018-01-04 19:52:40 +01:00
|
|
|
|
|
|
|
if (pointed_old.type == POINTEDTHING_NODE) {
|
|
|
|
ClientMap &map = client->getEnv().getClientMap();
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *nodedef = client->getNodeDefManager();
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = map.getNode(pointed_old.node_undersurface);
|
2018-01-04 19:52:40 +01:00
|
|
|
|
|
|
|
if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
|
|
|
|
os << ", pointed: " << nodedef->get(n).name
|
|
|
|
<< ", param2: " << (u64) n.getParam2();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
|
|
|
|
|
|
|
|
m_guitext2->setRelativePosition(core::rect<s32>(5,
|
|
|
|
5 + g_fontengine->getTextHeight(), screensize.X,
|
|
|
|
5 + g_fontengine->getTextHeight() * 2
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-06-24 20:21:19 +02:00
|
|
|
m_guitext2->setVisible(m_flags.show_basic_debug);
|
2018-01-04 19:52:40 +01:00
|
|
|
|
2020-01-22 19:09:11 +01:00
|
|
|
setStaticText(m_guitext_info, m_infotext.c_str());
|
2018-01-04 19:52:40 +01:00
|
|
|
m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
|
2018-01-04 21:35:26 +01:00
|
|
|
|
|
|
|
static const float statustext_time_max = 1.5f;
|
|
|
|
|
|
|
|
if (!m_statustext.empty()) {
|
|
|
|
m_statustext_time += dtime;
|
|
|
|
|
|
|
|
if (m_statustext_time >= statustext_time_max) {
|
|
|
|
clearStatusText();
|
|
|
|
m_statustext_time = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:09:11 +01:00
|
|
|
setStaticText(m_guitext_status, m_statustext.c_str());
|
2018-01-04 21:35:26 +01:00
|
|
|
m_guitext_status->setVisible(!m_statustext.empty());
|
|
|
|
|
|
|
|
if (!m_statustext.empty()) {
|
|
|
|
s32 status_width = m_guitext_status->getTextWidth();
|
|
|
|
s32 status_height = m_guitext_status->getTextHeight();
|
|
|
|
s32 status_y = screensize.Y - 150;
|
|
|
|
s32 status_x = (screensize.X - status_width) / 2;
|
|
|
|
|
|
|
|
m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
|
|
|
|
status_y - status_height, status_x + status_width, status_y));
|
|
|
|
|
|
|
|
// Fade out
|
2018-01-05 19:39:06 +01:00
|
|
|
video::SColor final_color = m_statustext_initial_color;
|
2018-01-04 21:35:26 +01:00
|
|
|
final_color.setAlpha(0);
|
2018-01-05 19:39:06 +01:00
|
|
|
video::SColor fade_color = m_statustext_initial_color.getInterpolated_quadratic(
|
|
|
|
m_statustext_initial_color, final_color, m_statustext_time / statustext_time_max);
|
2018-01-04 21:35:26 +01:00
|
|
|
m_guitext_status->setOverrideColor(fade_color);
|
|
|
|
m_guitext_status->enableOverrideColor(true);
|
|
|
|
}
|
2019-08-07 19:15:33 +02:00
|
|
|
|
|
|
|
// Hide chat when console is visible
|
|
|
|
m_guitext_chat->setVisible(isChatVisible() && !chat_console->isVisible());
|
2018-01-03 17:28:57 +01:00
|
|
|
}
|
2018-01-03 14:28:55 +01:00
|
|
|
|
|
|
|
void GameUI::initFlags()
|
|
|
|
{
|
2018-05-28 15:39:02 +02:00
|
|
|
m_flags = GameUI::Flags();
|
2021-06-24 20:21:19 +02:00
|
|
|
m_flags.show_minimal_debug = g_settings->getBool("show_debug");
|
|
|
|
m_flags.show_basic_debug = false;
|
2018-01-03 14:28:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 17:28:57 +01:00
|
|
|
void GameUI::showMinimap(bool show)
|
2018-01-03 14:28:55 +01:00
|
|
|
{
|
|
|
|
m_flags.show_minimap = show;
|
|
|
|
}
|
2018-01-04 23:04:40 +01:00
|
|
|
|
2018-01-04 23:32:32 +01:00
|
|
|
void GameUI::showTranslatedStatusText(const char *str)
|
|
|
|
{
|
|
|
|
const wchar_t *wmsg = wgettext(str);
|
|
|
|
showStatusText(wmsg);
|
|
|
|
delete[] wmsg;
|
|
|
|
}
|
|
|
|
|
2018-01-04 23:58:46 +01:00
|
|
|
void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
|
2018-01-04 23:04:40 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
// Update gui element size and position
|
|
|
|
s32 chat_y = 5;
|
|
|
|
|
2021-06-24 20:21:19 +02:00
|
|
|
if (m_flags.show_minimal_debug)
|
|
|
|
chat_y += g_fontengine->getLineHeight();
|
|
|
|
if (m_flags.show_basic_debug)
|
|
|
|
chat_y += g_fontengine->getLineHeight();
|
2018-01-04 23:04:40 +01:00
|
|
|
|
2021-04-28 12:48:13 +02:00
|
|
|
const v2u32 &window_size = RenderingEngine::getWindowSize();
|
2020-05-24 14:24:13 +02:00
|
|
|
|
|
|
|
core::rect<s32> chat_size(10, chat_y,
|
|
|
|
window_size.X - 20, 0);
|
|
|
|
chat_size.LowerRightCorner.Y = std::min((s32)window_size.Y,
|
|
|
|
m_guitext_chat->getTextHeight() + chat_y);
|
|
|
|
|
|
|
|
m_guitext_chat->setRelativePosition(chat_size);
|
|
|
|
setStaticText(m_guitext_chat, chat_text);
|
2018-01-04 23:04:40 +01:00
|
|
|
|
2019-08-07 19:15:33 +02:00
|
|
|
m_recent_chat_count = recent_chat_count;
|
2018-01-04 23:04:40 +01:00
|
|
|
}
|
2018-01-04 23:32:32 +01:00
|
|
|
|
2018-01-04 23:58:46 +01:00
|
|
|
void GameUI::updateProfiler()
|
2018-01-04 23:32:32 +01:00
|
|
|
{
|
2018-01-04 23:58:46 +01:00
|
|
|
if (m_profiler_current_page != 0) {
|
2018-01-04 23:32:32 +01:00
|
|
|
std::ostringstream os(std::ios_base::binary);
|
2019-08-13 19:56:55 +02:00
|
|
|
os << " Profiler page " << (int)m_profiler_current_page <<
|
|
|
|
", elapsed: " << g_profiler->getElapsedMs() << " ms)" << std::endl;
|
2018-01-04 23:32:32 +01:00
|
|
|
|
2019-08-13 19:56:55 +02:00
|
|
|
int lines = g_profiler->print(os, m_profiler_current_page, m_profiler_max_page);
|
|
|
|
++lines;
|
2018-01-04 23:32:32 +01:00
|
|
|
|
2020-02-01 20:41:32 +01:00
|
|
|
EnrichedString str(utf8_to_wide(os.str()));
|
|
|
|
str.setBackground(video::SColor(120, 0, 0, 0));
|
|
|
|
setStaticText(m_guitext_profiler, str);
|
2018-01-04 23:32:32 +01:00
|
|
|
|
2019-08-13 19:56:55 +02:00
|
|
|
core::dimension2d<u32> size = m_guitext_profiler->getOverrideFont()->
|
2020-02-01 20:41:32 +01:00
|
|
|
getDimension(str.c_str());
|
2019-08-13 19:56:55 +02:00
|
|
|
core::position2di upper_left(6, 50);
|
|
|
|
core::position2di lower_right = upper_left;
|
|
|
|
lower_right.X += size.Width + 10;
|
2021-04-28 12:48:13 +02:00
|
|
|
lower_right.Y += size.Height;
|
2018-01-04 23:32:32 +01:00
|
|
|
|
|
|
|
m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
|
|
|
|
}
|
|
|
|
|
2018-01-04 23:58:46 +01:00
|
|
|
m_guitext_profiler->setVisible(m_profiler_current_page != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameUI::toggleChat()
|
|
|
|
{
|
|
|
|
m_flags.show_chat = !m_flags.show_chat;
|
|
|
|
if (m_flags.show_chat)
|
|
|
|
showTranslatedStatusText("Chat shown");
|
|
|
|
else
|
|
|
|
showTranslatedStatusText("Chat hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameUI::toggleHud()
|
|
|
|
{
|
|
|
|
m_flags.show_hud = !m_flags.show_hud;
|
|
|
|
if (m_flags.show_hud)
|
|
|
|
showTranslatedStatusText("HUD shown");
|
|
|
|
else
|
|
|
|
showTranslatedStatusText("HUD hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameUI::toggleProfiler()
|
|
|
|
{
|
|
|
|
m_profiler_current_page = (m_profiler_current_page + 1) % (m_profiler_max_page + 1);
|
|
|
|
|
|
|
|
// FIXME: This updates the profiler with incomplete values
|
|
|
|
updateProfiler();
|
2018-01-04 23:32:32 +01:00
|
|
|
|
2018-01-04 23:58:46 +01:00
|
|
|
if (m_profiler_current_page != 0) {
|
2021-08-19 20:13:25 +02:00
|
|
|
std::wstring msg = fwgettext("Profiler shown (page %d of %d)",
|
|
|
|
m_profiler_current_page, m_profiler_max_page);
|
|
|
|
showStatusText(msg);
|
2018-01-04 23:32:32 +01:00
|
|
|
} else {
|
|
|
|
showTranslatedStatusText("Profiler hidden");
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 13:01:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
void GameUI::deleteFormspec()
|
|
|
|
{
|
2019-06-21 21:00:30 +02:00
|
|
|
if (m_formspec) {
|
|
|
|
m_formspec->drop();
|
|
|
|
m_formspec = nullptr;
|
|
|
|
}
|
2019-06-10 13:01:07 +02:00
|
|
|
|
|
|
|
m_formname.clear();
|
|
|
|
}
|