1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
#include "client/pch.hh"
#include "client/chat.hh"
#include "core/config.hh"
#include "core/resource.hh"
#include "core/strtools.hh"
#include "shared/protocol.hh"
#include "client/game.hh"
#include "client/glfw.hh"
#include "client/globals.hh"
#include "client/gui_screen.hh"
#include "client/keybind.hh"
#include "client/language.hh"
#include "client/session.hh"
#include "client/settings.hh"
#include "client/sound_effect.hh"
#include "client/sound.hh"
constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration;
constexpr static unsigned int MAX_HISTORY_SIZE = 128U;
struct GuiChatMessage final {
std::uint64_t spawn;
std::string text;
ImVec4 color;
};
static ConfigKeyBind key_chat(GLFW_KEY_ENTER);
static ConfigUnsigned history_size(32U, 0U, MAX_HISTORY_SIZE);
static std::deque<GuiChatMessage> history;
static std::string chat_input;
static bool needs_focus;
static resource_ptr<SoundEffect> sfx_chat_message;
static void append_text_message(const std::string &sender, const std::string &text)
{
GuiChatMessage message;
message.spawn = globals::curtime;
message.text = fmt::format("<{}> {}", sender, text);
message.color = ImGui::GetStyleColorVec4(ImGuiCol_Text);
history.push_back(message);
if(sfx_chat_message) {
sound::play_ui(sfx_chat_message, false, 1.0f);
}
}
static void append_player_join(const std::string &sender)
{
GuiChatMessage message;
message.spawn = globals::curtime;
message.text = fmt::format("{} {}", sender, language::resolve("chat.client_join"));
message.color = ImGui::GetStyleColorVec4(ImGuiCol_DragDropTarget);
history.push_back(message);
if(sfx_chat_message) {
sound::play_ui(sfx_chat_message, false, 1.0f);
}
}
static void append_player_leave(const std::string &sender, const std::string &reason)
{
GuiChatMessage message;
message.spawn = globals::curtime;
message.text = fmt::format("{} {} ({})", sender, language::resolve("chat.client_left"), language::resolve(reason.c_str()));
message.color = ImGui::GetStyleColorVec4(ImGuiCol_DragDropTarget);
history.push_back(message);
if(sfx_chat_message) {
sound::play_ui(sfx_chat_message, false, 1.0f);
}
}
static void on_chat_message_packet(const protocol::ChatMessage &packet)
{
if(packet.type == protocol::ChatMessage::TEXT_MESSAGE) {
append_text_message(packet.sender, packet.message);
return;
}
if(packet.type == protocol::ChatMessage::PLAYER_JOIN) {
append_player_join(packet.sender);
return;
}
if(packet.type == protocol::ChatMessage::PLAYER_LEAVE) {
append_player_leave(packet.sender, packet.message);
return;
}
}
static void on_glfw_key(const GlfwKeyEvent &event)
{
if(event.action == GLFW_PRESS) {
if((event.key == GLFW_KEY_ENTER) && (globals::gui_screen == GUI_CHAT)) {
if(!strtools::is_whitespace(chat_input)) {
protocol::ChatMessage packet;
packet.type = protocol::ChatMessage::TEXT_MESSAGE;
packet.sender = client_game::username.get();
packet.message = chat_input;
protocol::send(session::peer, protocol::encode(packet));
}
globals::gui_screen = GUI_SCREEN_NONE;
chat_input.clear();
return;
}
if((event.key == GLFW_KEY_ESCAPE) && (globals::gui_screen == GUI_CHAT)) {
globals::gui_screen = GUI_SCREEN_NONE;
return;
}
if(key_chat.equals(event.key) && !globals::gui_screen) {
globals::gui_screen = GUI_CHAT;
needs_focus = true;
return;
}
}
}
void client_chat::init(void)
{
globals::client_config.add_value("chat.key", key_chat);
globals::client_config.add_value("chat.history_size", history_size);
settings::add_keybind(2, key_chat, settings_location::KEYBOARD_MISC, "key.chat");
settings::add_slider(1, history_size, settings_location::VIDEO_GUI, "chat.history_size", false);
globals::dispatcher.sink<protocol::ChatMessage>().connect<&on_chat_message_packet>();
globals::dispatcher.sink<GlfwKeyEvent>().connect<&on_glfw_key>();
sfx_chat_message = resource::load<SoundEffect>("sounds/ui/chat_message.wav");
}
void client_chat::init_late(void)
{
}
void client_chat::deinit(void)
{
sfx_chat_message = nullptr;
}
void client_chat::update(void)
{
while(history.size() > history_size.get_value()) {
history.pop_front();
}
}
void client_chat::layout(void)
{
auto viewport = ImGui::GetMainViewport();
auto window_start = ImVec2(0.0f, 0.0f);
auto window_size = ImVec2(0.75f * viewport->Size.x, viewport->Size.y);
ImGui::SetNextWindowPos(window_start);
ImGui::SetNextWindowSize(window_size);
ImGui::PushFont(globals::font_chat);
if(!ImGui::Begin("###chat", nullptr, WINDOW_FLAGS)) {
ImGui::End();
return;
}
auto &padding = ImGui::GetStyle().FramePadding;
auto &spacing = ImGui::GetStyle().ItemSpacing;
auto font = ImGui::GetFont();
auto draw_list = ImGui::GetWindowDrawList();
// The text input widget occupies the bottom part
// of the chat window, we need to reserve some space for it
auto ypos = window_size.y - 2.5f * font->FontSize - 2.0f * padding.y - 2.0f * spacing.y;
if(globals::gui_screen == GUI_CHAT) {
if(needs_focus) {
ImGui::SetKeyboardFocusHere();
needs_focus = false;
}
ImGui::SetNextItemWidth(window_size.x + 32.0f * padding.x);
ImGui::SetCursorScreenPos(ImVec2(padding.x, ypos));
ImGui::InputText("###chat.input", &chat_input);
}
if((globals::gui_screen == GUI_SCREEN_NONE) || (globals::gui_screen == GUI_CHAT) || (globals::gui_screen == GUI_DEBUG_WINDOW)) {
for(auto it = history.crbegin(); it < history.crend(); ++it) {
auto text_size = ImGui::CalcTextSize(it->text.c_str(), it->text.c_str() + it->text.size(), false, window_size.x);
auto rect_size = ImVec2(window_size.x, text_size.y + 2.0f * padding.y);
auto rect_pos = ImVec2(padding.x, ypos - text_size.y - 2.0f * padding.y);
auto rect_end = ImVec2(rect_pos.x + rect_size.x, rect_pos.y + rect_size.y);
auto text_pos = ImVec2(rect_pos.x + padding.x, rect_pos.y + padding.y);
auto fadeout_seconds = 10.0f;
auto fadeout = std::exp(-1.0f * std::pow(1.0e-6 * static_cast<float>(globals::curtime - it->spawn) / fadeout_seconds, 10.0f));
auto rect_alpha = ((globals::gui_screen == GUI_CHAT) ? (0.75f) : (0.50f * fadeout));
auto text_alpha = ((globals::gui_screen == GUI_CHAT) ? (1.00f) : (1.00f * fadeout));
auto rect_col = ImGui::GetColorU32(ImGuiCol_FrameBg, rect_alpha);
auto text_col = ImGui::GetColorU32(ImVec4(it->color.x, it->color.y, it->color.z, it->color.w * text_alpha));
draw_list->AddRectFilled(rect_pos, rect_end, rect_col);
draw_list->AddText(font, font->FontSize, text_pos, text_col, it->text.c_str(), it->text.c_str() + it->text.size(), window_size.x);
ypos -= rect_size.y;
}
}
ImGui::End();
ImGui::PopFont();
}
void client_chat::clear(void)
{
history.clear();
}
void client_chat::refresh_timings(void)
{
for(auto it = history.begin(); it < history.end(); ++it) {
// Reset the spawn time so the fadeout timer
// is reset; SpawnPlayer handler might call this
it->spawn = globals::curtime;
}
}
void client_chat::print(const std::string &text)
{
GuiChatMessage message = {};
message.spawn = globals::curtime;
message.text = text;
message.color = ImGui::GetStyleColorVec4(ImGuiCol_Text);
history.push_back(message);
if(sfx_chat_message) {
sound::play_ui(sfx_chat_message, false, 1.0f);
}
}
|