nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
chat_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cfloat>
7#include <cstddef>
8#include <deque>
9#include <optional>
10#include <string>
11#include <string_view>
12
13#include "absl/strings/substitute.h"
14#include "client/messages.h"
15#include "client/names.h"
16#include "client/theme.h"
17#include "client/translations.h"
18#include "extras/IconsFontAwesome6.h"
19
20namespace nanommo {
21
22namespace {
23
24// In points, at a scale of one.
25constexpr float kMargin = 18.0f;
26constexpr ImVec2 kSize{440.0f, 240.0f};
27constexpr float kButton = 23.0f;
28constexpr float kRim = 4.0f;
29
30constexpr ImVec4 kName{1.0f, 0.63f, 0.29f, 1.0f};
31constexpr ImVec4 kServer{1.0f, 0.83f, 0.3f, 1.0f};
32constexpr ImVec4 kGuild{0.45f, 0.9f, 0.45f, 1.0f};
33constexpr ImVec4 kLand{1.0f, 0.95f, 0.77f, 1.0f};
34
35bool Same(const protocol::Chat& a, const protocol::Chat& b) {
36 return a.SerializeAsString() == b.SerializeAsString();
37}
38
39// Who said the line: a player, by the name they gave, or an NPC, by its kind,
40// in the language in use.
41std::string SpeakerOf(const protocol::Chat& line, const Content& content) {
42 if (line.npc() == 0) {
43 return line.name();
44 }
45 const auto kind = content.npcs.kinds.find(line.npc());
46 return kind != content.npcs.kinds.end() ? NameOf(kind->second)
47 : Tr("something");
48}
49
50} // namespace
51
52std::optional<std::string> GuildLine(std::string_view typed) {
53 constexpr std::string_view kCommand = "/g ";
54 if (!typed.starts_with(kCommand)) {
55 return std::nullopt;
56 }
57 return std::string(typed.substr(kCommand.size()));
58}
59
60std::optional<std::string> TradeName(std::string_view typed) {
61 constexpr std::string_view kCommand = "/trade ";
62 if (!typed.starts_with(kCommand)) {
63 return std::nullopt;
64 }
65 return std::string(typed.substr(kCommand.size()));
66}
67
68std::optional<std::string> WarName(std::string_view typed) {
69 constexpr std::string_view kCommand = "/war ";
70 if (!typed.starts_with(kCommand)) {
71 return std::nullopt;
72 }
73 return std::string(typed.substr(kCommand.size()));
74}
75
76std::optional<protocol::DuelAction> DuelLine(std::string_view typed) {
77 constexpr std::string_view kCommand = "/duel ";
78 if (!typed.starts_with(kCommand)) {
79 return std::nullopt;
80 }
81 const std::string_view what = typed.substr(kCommand.size());
82 protocol::DuelAction action;
83 if (what == "yes") {
84 action.set_accept(true);
85 } else if (what == "no") {
86 action.set_decline(true);
87 } else if (what == "yield") {
88 action.set_yield(true);
89 } else {
90 action.set_ask(std::string(what));
91 }
92 return action;
93}
94
95std::size_t ChatWindow::Unread(const std::deque<protocol::Chat>& lines,
96 const std::optional<protocol::Chat>& seen) {
97 if (!seen) {
98 return lines.size();
99 }
100 // From the newest back, to the one seen; one that's gone off the top means
101 // they're all new.
102 for (std::size_t after = 0; after < lines.size(); ++after) {
103 if (Same(lines[lines.size() - 1 - after], *seen)) {
104 return after;
105 }
106 }
107 return lines.size();
108}
109
110std::optional<std::string> ChatWindow::Build(
111 const std::deque<protocol::Chat>& lines, const Content& content) {
112 const ImVec2 display = ImGui::GetIO().DisplaySize;
113 const bool enter = !ImGui::GetIO().WantTextInput &&
114 ImGui::IsKeyPressed(ImGuiKey_Enter, false);
115 if (!open_) {
116 // A round button in the corner, with how many lines are new on it.
117 const float radius = Ui(kButton);
118 // Room round the disc for its rim, which the window would cut, as far out
119 // as the disc stays in the corner, and above and to the right of it for
120 // the count of new lines.
121 const float room = Ui(kRim);
122 ImGui::SetNextWindowPos(
123 {Ui(kMargin) - room, display.y - Ui(kMargin) + room}, ImGuiCond_Always,
124 {0.0f, 1.0f});
125 ImGui::SetNextWindowSize(
126 {room + radius * 2.0f + Ui(14.0f), Ui(6.0f) + radius * 2.0f + room});
127 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0.0f, 0.0f});
128 ImGui::Begin("Chat closed", nullptr,
129 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
130 ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoNav |
131 ImGuiWindowFlags_NoFocusOnAppearing |
132 ImGuiWindowFlags_NoSavedSettings);
133 ImGui::PopStyleVar();
134 const ImVec2 corner = ImGui::GetWindowPos();
135 const ImVec2 centre{corner.x + room + radius, corner.y + Ui(6.0f) + radius};
136 ImGui::SetCursorScreenPos({centre.x - radius, centre.y - radius});
137 const bool clicked =
138 ImGui::InvisibleButton("##open", {radius * 2.0f, radius * 2.0f});
139 const bool hovered = ImGui::IsItemHovered();
140 if (hovered) {
141 ImGui::SetTooltip("%s", Tr("Chat (Enter)"));
142 }
143 ImDrawList* draw = ImGui::GetWindowDrawList();
144 DrawRoundButton(draw, centre, radius, ICON_FA_COMMENTS, hovered);
145 if (const std::size_t unread = Unread(lines, seen_); unread > 0) {
146 const std::string count = unread > 99 ? "99+" : std::to_string(unread);
147 ImFont* bold = GetFonts().bold;
148 const float size = Ui(13.0f);
149 const ImVec2 count_extent =
150 bold->CalcTextSizeA(size, FLT_MAX, 0.0f, count.c_str());
151 const ImVec2 badge{centre.x + radius * 0.75f, centre.y - radius * 0.75f};
152 const float badge_radius =
153 std::max(Ui(9.0f), count_extent.x / 2.0f + Ui(4.0f));
154 draw->AddCircleFilled(badge, badge_radius, colors::kEmber);
155 draw->AddCircle(badge, badge_radius, colors::kNight, 0, Ui(1.5f));
156 draw->AddText(
157 bold, size,
158 {badge.x - count_extent.x / 2.0f, badge.y - count_extent.y / 2.0f},
159 colors::kNight, count.c_str());
160 }
161 ImGui::End();
162 if (clicked || enter) {
163 open_ = true;
164 focus_ = enter;
165 }
166 return std::nullopt;
167 }
168
169 ImGui::SetNextWindowPos({Ui(kMargin), display.y - Ui(kMargin)},
170 ImGuiCond_Always, {0.0f, 1.0f});
171 ImGui::SetNextWindowSize(Ui(kSize));
172 // Quieter while nobody's using it, so that it doesn't hide the world.
173 const bool using_it =
174 ImGui::GetIO().WantTextInput ||
175 ImGui::IsMouseHoveringRect(
176 {Ui(kMargin), display.y - Ui(kMargin) - Ui(kSize.y)},
177 {Ui(kMargin) + Ui(kSize.x), display.y - Ui(kMargin)}, false);
178 // Space presses the widget that ImGui's keyboard navigation is on, which
179 // would open the field again after each line.
180 bool open = true;
181 BeginPanel("Chat", &open,
182 ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNavInputs |
183 ImGuiWindowFlags_NoFocusOnAppearing |
184 ImGuiWindowFlags_NoSavedSettings,
185 using_it ? 0.94f : 0.6f);
186
187 ImGui::BeginChild("Lines", {0.0f, -ImGui::GetFrameHeightWithSpacing()});
188 for (const protocol::Chat& line : lines) {
189 // A server's sentence is translated as it's shown, as the rest is, and
190 // what a player said is as they said it.
191 const std::string text =
192 line.has_message() ? Translated(line.message(), content) : line.text();
193 // What everyone in the land hears, such as a boss's fall, stands out, in
194 // bold, in the light gold of a title.
195 if (line.channel() == protocol::CHANNEL_LAND) {
196 ImGui::PushFont(GetFonts().bold, 0.0f);
197 ImGui::PushStyleColor(ImGuiCol_Text, kLand);
198 ImGui::TextWrapped("%s", text.c_str());
199 ImGui::PopStyleColor();
200 ImGui::PopFont();
201 continue;
202 }
203 // A line from nobody is from the server, and one of those for the guild
204 // alone, such as a kill in its war, is green, as its players' lines are.
205 const bool from_server = line.name().empty() && line.npc() == 0;
206 if (from_server && line.channel() == protocol::CHANNEL_GUILD) {
207 ImGui::PushStyleColor(ImGuiCol_Text, kGuild);
208 ImGui::TextWrapped("%s",
209 absl::Substitute(Tr("[Guild] $0"), text).c_str());
210 ImGui::PopStyleColor();
211 continue;
212 }
213 if (from_server) {
214 ImGui::PushStyleColor(ImGuiCol_Text, kServer);
215 ImGui::TextWrapped("%s", text.c_str());
216 ImGui::PopStyleColor();
217 continue;
218 }
219 // The guild's lines are green, all of them, so they stand apart.
220 const bool guild = line.channel() == protocol::CHANNEL_GUILD;
221 if (guild) {
222 ImGui::PushStyleColor(ImGuiCol_Text, kGuild);
223 }
224 ImGui::PushFont(GetFonts().bold, 0.0f);
225 const std::string speaker = SpeakerOf(line, content);
226 if (guild) {
227 ImGui::TextUnformatted(
228 absl::Substitute(Tr("[Guild] $0:"), speaker).c_str());
229 } else {
230 ImGui::TextColored(kName, "%s:", speaker.c_str());
231 }
232 ImGui::PopFont();
233 ImGui::SameLine();
234 ImGui::TextWrapped("%s", text.c_str());
235 if (guild) {
236 ImGui::PopStyleColor();
237 }
238 }
239 // Follow the newest line, unless the player has scrolled up to read.
240 if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
241 ImGui::SetScrollHereY(1.0f);
242 }
243 ImGui::EndChild();
244
245 if (enter || focus_) {
246 ImGui::SetKeyboardFocusHere();
247 focus_ = false;
248 }
249 ImGui::SetNextItemWidth(-FLT_MIN);
250 std::optional<std::string> said;
251 if (ImGui::InputTextWithHint(
252 "##say", Tr("Press Enter to chat, /g for your guild"), typing_.data(),
253 typing_.size(), ImGuiInputTextFlags_EnterReturnsTrue) &&
254 typing_[0] != '\0') {
255 said = typing_.data();
256 typing_.fill('\0');
257 }
258 EndPanel();
259 open_ = open;
260 if (!lines.empty()) {
261 seen_ = lines.back();
262 }
263 return said;
264}
265
266} // namespace nanommo
std::optional< std::string > Build(const std::deque< protocol::Chat > &lines, const Content &content)
static std::size_t Unread(const std::deque< protocol::Chat > &lines, const std::optional< protocol::Chat > &seen)
How many of the lines came after the last one seen while it was open.
constexpr ImU32 kEmber
Definition theme.h:19
constexpr ImU32 kNight
Definition theme.h:14
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
std::string Translated(const protocol::Message &message, const Content &content)
Definition messages.cc:108
std::optional< std::string > WarName(std::string_view typed)
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
void DrawRoundButton(ImDrawList *draw, ImVec2 centre, float radius, const char *icon, bool hovered, bool on)
Definition theme.cc:503
std::optional< std::string > TradeName(std::string_view typed)
std::optional< protocol::DuelAction > DuelLine(std::string_view typed)
std::optional< std::string > GuildLine(std::string_view typed)
const Fonts & GetFonts()
Definition theme.cc:66
const char * NameOf(const items::Item &item)
Definition names.cc:14
ImFont * bold
Definition theme.h:43