nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
trainer_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cstdint>
7#include <optional>
8#include <string>
9#include <utility>
10#include <vector>
11
12#include "absl/strings/substitute.h"
13#include "client/icon.h"
14#include "client/names.h"
15#include "client/theme.h"
16#include "client/translations.h"
17
18namespace nanommo {
19
20namespace {
21
22// In points from the window's left, so that the buttons line up whatever the
23// skills' names.
24constexpr float kButtons = 200.0f;
25
26} // namespace
27
28void TrainerWindow::Open(uint32_t npc, std::string name,
29 std::vector<uint32_t> teaches) {
30 npc_ = npc;
31 name_ = std::move(name);
32 teaches_ = std::move(teaches);
33}
34
35std::optional<protocol::Learn> TrainerWindow::Build(
36 const std::vector<Figure>& figures, const protocol::World& world) {
37 if (!npc_) {
38 return std::nullopt;
39 }
40 const auto you = std::ranges::find_if(figures, &Figure::you);
41 const int level = you != figures.end() ? you->level : 0;
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(360.0f), 0.0f});
46 bool open = true;
47 BeginPanel(name_.c_str(), &open,
48 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
49 ImGui::TextWrapped("%s",
50 Tr("I can teach you these, once you're ready for them."));
51 ImGui::Separator();
52 std::optional<protocol::Learn> learn;
53 for (const uint32_t id : teaches_) {
54 const skills::Skill& skill = skills_.by_id.at(id);
55 ImGui::PushID(static_cast<int>(id));
56 ImGui::AlignTextToFramePadding();
57 ImGui::Text("%s %s", Icon(skill.look().icon()).c_str(), NameOf(skill));
58 ImGui::SameLine(Ui(kButtons));
59 if (std::ranges::find(world.skills(), id) != world.skills().end()) {
60 ImGui::TextDisabled("%s", Tr("Known"));
61 } else if (level < static_cast<int>(skill.level())) {
62 ImGui::TextDisabled(
63 "%s", absl::Substitute(Tr("From level $0"), skill.level()).c_str());
64 } else if (GameButton("Learn", {Ui(90.0f), Ui(28.0f)}, true)) {
65 learn.emplace();
66 learn->set_npc(*npc_);
67 learn->set_skill(id);
68 }
69 ImGui::PopID();
70 }
71 EndPanel();
72 if (!open) {
73 npc_.reset();
74 }
75 return learn;
76}
77
78} // namespace nanommo
std::optional< protocol::Learn > Build(const std::vector< Figure > &figures, const protocol::World &world)
void Open(uint32_t npc, std::string name, std::vector< uint32_t > teaches)
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)
std::string Icon(uint32_t code)
Definition icon.h:12
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
const char * NameOf(const items::Item &item)
Definition names.cc:14