GameUI refactor (part 5/X): Move Game::guitext_chat to GameUI class
[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         m_guitext_status->setVisible(false);
62
63         // Chat text
64         m_guitext_chat = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0),
65                 //false, false); // Disable word wrap as of now
66                 false, true, guiroot);
67 }
68
69 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
70         const CameraOrientation &cam, const PointedThing &pointed_old, float dtime)
71 {
72         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
73
74         if (m_flags.show_debug) {
75                 static float drawtime_avg = 0;
76                 drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
77                 u16 fps = 1.0 / stats.dtime_jitter.avg;
78
79                 std::ostringstream os(std::ios_base::binary);
80                 os << std::fixed
81                         << PROJECT_NAME_C " " << g_version_hash
82                         << ", FPS: " << fps
83                         << std::setprecision(0)
84                         << ", drawtime: " << drawtime_avg << "ms"
85                         << std::setprecision(1)
86                         << ", dtime jitter: "
87                         << (stats.dtime_jitter.max_fraction * 100.0) << "%"
88                         << std::setprecision(1)
89                         << ", view range: "
90                         << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
91                         << std::setprecision(3)
92                         << ", RTT: " << client->getRTT() << "s";
93                 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
94
95                 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
96                         5 + g_fontengine->getTextHeight()));
97         }
98
99         // Finally set the guitext visible depending on the flag
100         m_guitext->setVisible(m_flags.show_debug);
101
102         if (m_flags.show_debug) {
103                 LocalPlayer *player = client->getEnv().getLocalPlayer();
104                 v3f player_position = player->getPosition();
105
106                 std::ostringstream os(std::ios_base::binary);
107                 os << std::setprecision(1) << std::fixed
108                         << "pos: (" << (player_position.X / BS)
109                         << ", " << (player_position.Y / BS)
110                         << ", " << (player_position.Z / BS)
111                         << "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
112                         << yawToDirectionString(cam.camera_yaw)
113                         << ", seed: " << ((u64)client->getMapSeed());
114
115                 if (pointed_old.type == POINTEDTHING_NODE) {
116                         ClientMap &map = client->getEnv().getClientMap();
117                         const INodeDefManager *nodedef = client->getNodeDefManager();
118                         MapNode n = map.getNodeNoEx(pointed_old.node_undersurface);
119
120                         if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
121                                 os << ", pointed: " << nodedef->get(n).name
122                                         << ", param2: " << (u64) n.getParam2();
123                         }
124                 }
125
126                 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
127
128                 m_guitext2->setRelativePosition(core::rect<s32>(5,
129                         5 + g_fontengine->getTextHeight(), screensize.X,
130                         5 + g_fontengine->getTextHeight() * 2
131                 ));
132         }
133
134         m_guitext2->setVisible(m_flags.show_debug);
135
136         setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
137         m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
138
139         static const float statustext_time_max = 1.5f;
140
141         if (!m_statustext.empty()) {
142                 m_statustext_time += dtime;
143
144                 if (m_statustext_time >= statustext_time_max) {
145                         clearStatusText();
146                         m_statustext_time = 0.0f;
147                 }
148         }
149
150         setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
151         m_guitext_status->setVisible(!m_statustext.empty());
152
153         if (!m_statustext.empty()) {
154                 s32 status_width  = m_guitext_status->getTextWidth();
155                 s32 status_height = m_guitext_status->getTextHeight();
156                 s32 status_y = screensize.Y - 150;
157                 s32 status_x = (screensize.X - status_width) / 2;
158
159                 m_guitext_status->setRelativePosition(core::rect<s32>(status_x ,
160                         status_y - status_height, status_x + status_width, status_y));
161
162                 // Fade out
163                 video::SColor initial_color(255, 0, 0, 0);
164
165                 if (guienv->getSkin())
166                         initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
167
168                 video::SColor final_color = initial_color;
169                 final_color.setAlpha(0);
170                 video::SColor fade_color = initial_color.getInterpolated_quadratic(
171                         initial_color, final_color,
172                         pow(m_statustext_time / statustext_time_max, 2.0f));
173                 m_guitext_status->setOverrideColor(fade_color);
174                 m_guitext_status->enableOverrideColor(true);
175         }
176 }
177
178 void GameUI::initFlags()
179 {
180         memset(&m_flags, 0, sizeof(GameUI::Flags));
181         m_flags.show_chat = true;
182         m_flags.show_hud = true;
183         m_flags.show_debug = g_settings->getBool("show_debug");
184 }
185
186 void GameUI::showMinimap(bool show)
187 {
188         m_flags.show_minimap = show;
189 }
190
191 void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count,
192         u32 profiler_current_page)
193 {
194         setStaticText(m_guitext_chat, chat_text);
195
196         // Update gui element size and position
197         s32 chat_y = 5;
198
199         if (m_flags.show_debug)
200                 chat_y += 2 * g_fontengine->getLineHeight();
201
202         // first pass to calculate height of text to be set
203         const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
204         s32 width = std::min(g_fontengine->getTextWidth(chat_text.c_str()) + 10,
205                 window_size.X - 20);
206         m_guitext_chat->setRelativePosition(core::rect<s32>(10, chat_y, width,
207                 chat_y + window_size.Y));
208
209         // now use real height of text and adjust rect according to this size
210         m_guitext_chat->setRelativePosition(core::rect<s32>(10, chat_y, width,
211                 chat_y + m_guitext_chat->getTextHeight()));
212
213         // Don't show chat if disabled or empty or profiler is enabled
214         m_guitext_chat->setVisible(m_flags.show_chat &&
215                 recent_chat_count != 0 && profiler_current_page == 0);
216 }