nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
herald_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4#include <raylib.h>
5
6#include <algorithm>
7#include <cstdint>
8#include <optional>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "absl/strings/substitute.h"
14#include "client/names.h"
15#include "client/theme.h"
16#include "client/translations.h"
17
18namespace nanommo {
19
20void HeraldWindow::Open(uint32_t npc, std::string name, uint32_t faction) {
21 npc_ = npc;
22 name_ = std::move(name);
23 faction_ = faction;
24 asking_ = false;
25}
26
28 npc_.reset();
29 asking_ = false;
30}
31
32std::optional<uint32_t> HeraldWindow::Build(
33 const std::vector<Figure>& figures) {
34 const auto theirs = factions_.find(faction_);
35 if (!npc_ || theirs == factions_.end()) {
36 return std::nullopt;
37 }
38 const auto you = std::ranges::find_if(figures, &Figure::you);
39 const auto yours =
40 factions_.find(you != figures.end() ? you->faction : uint32_t{0});
41 const char* faction = NameOf(theirs->second);
42 const ImVec2 display = ImGui::GetIO().DisplaySize;
43 ImGui::SetNextWindowPos({display.x / 2, display.y * 0.7f},
44 ImGuiCond_Appearing, {0.5f, 0.5f});
45 ImGui::SetNextWindowSize({Ui(380.0f), 0.0f});
46 bool open = true;
47 BeginPanel(name_.c_str(), &open,
48 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
49 // The faction's name, in its colour, as it goes by its players' names.
50 const Color color = GetColor(theirs->second.color());
51 ImGui::PushFont(GetFonts().title, 22.0f);
52 ImGui::TextColored(
53 ImGui::ColorConvertU32ToFloat4(IM_COL32(color.r, color.g, color.b, 255)),
54 "%s", faction);
55 ImGui::PopFont();
56 ImGui::Separator();
57 std::optional<uint32_t> sworn;
58 if (yours != factions_.end()) {
59 // Theirs or another, it's for good.
60 ImGui::TextWrapped("%s", absl::Substitute(Tr("You're sworn to $0 already."),
61 NameOf(yours->second))
62 .c_str());
63 } else if (!asking_) {
64 ImGui::TextWrapped(
65 "%s",
66 absl::Substitute(Tr("Swear to $0, and its colour goes by your name, "
67 "for everyone to see."),
68 faction)
69 .c_str());
70 if (GameButton("Enlist", {-1.0f, 0.0f}, true)) {
71 asking_ = true;
72 }
73 } else {
74 ImGui::TextWrapped(
75 "%s", absl::Substitute(Tr("It's for good: there's no leaving $0, nor "
76 "swearing to another."),
77 faction)
78 .c_str());
79 if (GameButton("Swear", {Ui(170.0f), 0.0f}, true)) {
80 sworn = npc_;
81 open = false;
82 }
83 ImGui::SameLine();
84 if (GameButton("Not now", {Ui(170.0f), 0.0f})) {
85 open = false;
86 }
87 }
88 EndPanel();
89 if (!open) {
90 Close();
91 }
92 return sworn;
93}
94
95} // namespace nanommo
void Open(uint32_t npc, std::string name, uint32_t faction)
The herald, by the NPC's id, and their faction, by its id.
std::optional< uint32_t > Build(const std::vector< Figure > &figures)
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)
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
const Fonts & GetFonts()
Definition theme.cc:66
const char * NameOf(const items::Item &item)
Definition names.cc:14