nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
duel_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <cfloat>
6#include <cstdint>
7#include <optional>
8#include <string>
9
10#include "absl/strings/substitute.h"
11#include "client/theme.h"
12#include "client/translations.h"
13
14namespace nanommo {
15
16uint32_t CountdownSeconds(uint32_t milliseconds) {
17 return (milliseconds + 999) / 1000;
18}
19
20std::optional<protocol::DuelAction> DuelWindow::Build(
21 const protocol::Duel& duel) {
22 if (duel.state() != state_) {
23 answered_ = -kAnswering;
24 if (duel.state() == protocol::DUEL_FIGHTING) {
25 started_ = ImGui::GetTime();
26 }
27 state_ = duel.state();
28 }
29 // Big, in gold, over the world and under the windows, as "You died" is.
30 const ImVec2 screen = ImGui::GetIO().DisplaySize;
31 const auto shout = [&screen](const std::string& words, float alpha) {
32 ImFont* title = GetFonts().title;
33 const float size = Ui(110.0f);
34 const ImVec2 extent =
35 title->CalcTextSizeA(size, FLT_MAX, 0.0f, words.c_str());
36 DrawGradientText(ImGui::GetBackgroundDrawList(), title, size,
37 {(screen.x - extent.x) / 2.0f, screen.y * 0.22f},
39 Faded(colors::kGoldDark, alpha), words.c_str());
40 };
41 const double fighting = ImGui::GetTime() - started_;
42 if (duel.state() == protocol::DUEL_COUNTDOWN) {
43 shout(std::to_string(CountdownSeconds(duel.countdown())), 1.0f);
44 } else if (duel.state() == protocol::DUEL_FIGHTING && fighting < kFight) {
45 shout(Tr("Fight!"), static_cast<float>(1.0 - fighting / kFight));
46 }
47 if (duel.state() != protocol::DUEL_ASKED ||
48 ImGui::GetTime() - answered_ < kAnswering) {
49 return std::nullopt;
50 }
51 ImGui::SetNextWindowPos({screen.x / 2.0f, screen.y * 0.3f}, ImGuiCond_Always,
52 {0.5f, 0.5f});
53 bool open = true;
54 std::optional<protocol::DuelAction> action;
55 BeginPanel("Duel?", &open,
56 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove |
57 ImGuiWindowFlags_NoSavedSettings);
58 ImGui::TextUnformatted(
59 absl::Substitute(Tr("$0 asks you to duel."), duel.opponent()).c_str());
60 ImGui::Spacing();
61 if (GameButton("Duel", {Ui(150.0f), 0.0f}, true)) {
62 action.emplace().set_accept(true);
63 }
64 ImGui::SameLine();
65 // Closing it says no too, so that someone else can ask either of you.
66 if (GameButton("Not now", {Ui(150.0f), 0.0f}) || !open) {
67 action.emplace().set_decline(true);
68 }
69 EndPanel();
70 if (action) {
71 answered_ = ImGui::GetTime();
72 }
73 return action;
74}
75
76} // namespace nanommo
std::optional< protocol::DuelAction > Build(const protocol::Duel &duel)
Draws what the duel needs now. Returns the player's answer, once.
static constexpr double kAnswering
Definition duel_window.h:26
static constexpr double kFight
How long Fight! stays up, in seconds, as it fades.
Definition duel_window.h:21
constexpr ImU32 kGoldDark
Definition theme.h:23
constexpr ImU32 kGoldLight
Definition theme.h:22
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
uint32_t CountdownSeconds(uint32_t milliseconds)
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
void DrawGradientText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 top, ImU32 bottom, const char *text)
Definition theme.cc:283
const Fonts & GetFonts()
Definition theme.cc:66
ImFont * title
Definition theme.h:44