nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
character_screen.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <array>
7#include <optional>
8#include <string>
9#include <utility>
10#include <vector>
11
12#include "absl/strings/substitute.h"
13#include "client/bag_window.h"
14#include "client/theme.h"
15#include "client/translations.h"
16#include "protocol/places.h"
17
18namespace nanommo {
19
20namespace {
21
22// In points, at a scale of one.
23constexpr float kSlot = 52.0f;
24constexpr float kGap = 10.0f;
25constexpr ImVec2 kStage{250.0f, 328.0f};
26
27// Four metres in front of the character, level with its middle, which shows
28// all of it in the preview's shape.
29constexpr Camera3D kPreviewCamera{
30 .position = {0.0f, 0.95f, 4.2f},
31 .target = {0.0f, 0.9f, 0.0f},
32 .up = {0.0f, 1.0f, 0.0f},
33 .fovy = 35.0f,
34 .projection = CAMERA_PERSPECTIVE,
35};
36
37// What's worn on the body down the left, from the head to the feet, and
38// what's held on the right.
39constexpr std::array<items::Slot, 4> kLeft = {
40 items::SLOT_HEAD, items::SLOT_BODY, items::SLOT_LEGS, items::SLOT_FEET};
41constexpr std::array<items::Slot, 2> kRight = {items::SLOT_MAIN_HAND,
42 items::SLOT_OFF_HAND};
43
44// A glow behind the character, from the floor up, and nothing on the floor.
45void DrawStage(ImDrawList* draw, ImVec2 min, ImVec2 max) {
46 draw->AddRectFilledMultiColor(
47 min, max, Faded(colors::kNight, 0.9f), Faded(colors::kNight, 0.9f),
48 Faded(colors::kWine, 0.9f), Faded(colors::kWine, 0.9f));
49 draw->AddRect(min, max, colors::kBrass, 0.0f, 0, Ui(1.2f));
50}
51
52} // namespace
53
54void CharacterScreen::Render(const Renderer& renderer) {
55 if (!visible_) {
56 return;
57 }
58 const std::vector<Figure>& figures = renderer.figures();
59 const auto you = std::ranges::find_if(figures, &Figure::you);
60 if (you == figures.end()) {
61 return;
62 }
63 Figure idle = *you;
64 idle.position = {};
65 idle.yaw = 0.3f;
66 idle.from = protocol::MOTION_IDLE;
67 idle.to = protocol::MOTION_IDLE;
68 idle.to_time = static_cast<float>(GetTime());
69 idle.blend = 1.0f;
70 preview_.RenderPreview(renderer, idle, kPreviewCamera);
71}
72
73std::optional<protocol::ItemAction> CharacterScreen::Build(
74 const std::vector<Figure>& figures, const protocol::World& world,
75 const protocol::Inventory& bag) {
76 if (!visible_) {
77 return std::nullopt;
78 }
79 const auto you = std::ranges::find_if(figures, &Figure::you);
80 if (you == figures.end() || you->level == 0) {
81 return std::nullopt;
82 }
83 std::optional<protocol::ItemAction> action;
84 const ImVec2 display = ImGui::GetIO().DisplaySize;
85 ImGui::SetNextWindowPos({display.x * 0.3f, display.y * 0.5f},
86 ImGuiCond_FirstUseEver, {0.5f, 0.5f});
87 if (BeginPanel("Character", &visible_,
88 ImGuiWindowFlags_AlwaysAutoResize |
89 ImGuiWindowFlags_NoSavedSettings)) {
90 const float slot = Ui(kSlot);
91 const float gap = Ui(kGap);
92 const ImVec2 stage = Ui(kStage);
93 const ImVec2 origin = ImGui::GetCursorScreenPos();
94 ImDrawList* draw = ImGui::GetWindowDrawList();
95
96 // The stage in the middle, with the character on it.
97 const ImVec2 stage_min{origin.x + slot + gap, origin.y};
98 const ImVec2 stage_max{stage_min.x + stage.x, stage_min.y + stage.y};
99 DrawStage(draw, stage_min, stage_max);
100 // OpenGL keeps the rows bottom up, so the UVs turn the picture over.
101 draw->AddImage(static_cast<ImTextureID>(preview_.texture().id), stage_min,
102 stage_max, {0.0f, 1.0f}, {1.0f, 0.0f});
103
104 const auto slots = [&](auto column, float x) {
105 for (std::size_t i = 0; i < column.size(); ++i) {
106 ImGui::SetCursorScreenPos(
107 {x, origin.y + static_cast<float>(i) * (slot + gap)});
108 if (std::optional<protocol::ItemAction> done =
109 ItemSlot(items_, bag, Worn(column[i]), slot)) {
110 action = std::move(done);
111 }
112 }
113 };
114 slots(kLeft, origin.x);
115 slots(kRight, stage_max.x + gap);
116
117 // Under it, how you are.
118 const float width = stage_max.x + gap + slot - origin.x;
119 const float bars = stage_max.y + gap;
120 const std::string health =
121 absl::Substitute(Tr("Health $0 / $1"), you->health, you->max_health);
122 DrawBar(draw, {origin.x, bars}, {origin.x + width, bars + Ui(18.0f)},
123 you->max_health > 0
124 ? static_cast<float>(you->health) / you->max_health
125 : 0.0f,
126 colors::kHealth, colors::kHealthDark, health.c_str());
127 const std::string experience = absl::Substitute(
128 Tr("Experience $0 / $1"), world.experience(), world.next_level());
129 DrawBar(draw, {origin.x, bars + Ui(24.0f)},
130 {origin.x + width, bars + Ui(42.0f)},
131 world.next_level() > 0 ? static_cast<float>(world.experience()) /
132 static_cast<float>(world.next_level())
133 : 0.0f,
134 colors::kGoldDark, IM_COL32(90, 58, 10, 255), experience.c_str());
135 ImGui::SetCursorScreenPos({origin.x, bars + Ui(42.0f)});
136 ImGui::Dummy({width, Ui(4.0f)});
137 }
138 EndPanel();
139 return action;
140}
141
142} // namespace nanommo
std::optional< protocol::ItemAction > Build(const std::vector< Figure > &figures, const protocol::World &world, const protocol::Inventory &bag)
void Render(const Renderer &renderer)
const std::vector< Figure > & figures() const
Definition renderer.h:96
constexpr ImU32 kGoldDark
Definition theme.h:23
constexpr ImU32 kNight
Definition theme.h:14
constexpr ImU32 kWine
Definition theme.h:16
constexpr ImU32 kBrass
The trim round every panel: gold gone dark with age.
Definition theme.h:25
constexpr ImU32 kHealth
Definition theme.h:30
constexpr ImU32 kHealthDark
Definition theme.h:31
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
void DrawBar(ImDrawList *draw, ImVec2 min, ImVec2 max, float share, ImU32 color, ImU32 dark, const char *text)
Definition theme.cc:452
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
protocol::Place Worn(items::Slot slot)
A slot that's worn.
Definition places.cc:13
std::optional< protocol::ItemAction > ItemSlot(const ItemTable &items, const protocol::Inventory &bag, const protocol::Place &place, float size, bool locked)
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
protocol::Motion from
Definition animator.h:73
Vector3 position
Definition animator.h:63
protocol::Motion to
Definition animator.h:77