nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
player_frame.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4#include <raylib.h>
5
6#include <algorithm>
7#include <cfloat>
8#include <string>
9#include <vector>
10
11#include "absl/strings/str_cat.h"
12#include "absl/strings/substitute.h"
13#include "client/names.h"
14#include "client/picking.h"
15#include "client/theme.h"
16#include "client/translations.h"
17#include "extras/IconsFontAwesome6.h"
18
19namespace nanommo {
20
21namespace {
22
23// In points, at a scale of one: from the corner of the screen, the portrait's
24// radius, and the plate beside it.
25constexpr float kMargin = 18.0f;
26constexpr float kPortrait = 40.0f;
27constexpr float kPlateWidth = 300.0f;
28constexpr float kPlateHeight = 66.0f;
29// The target's frame, from the top of the screen.
30constexpr float kTargetTop = 70.0f;
31
32// Close up on the face, a little from one side, as portraits are.
33constexpr Camera3D kPortraitCamera{
34 .position = {0.35f, 1.62f, 1.25f},
35 .target = {0.0f, 1.5f, 0.0f},
36 .up = {0.0f, 1.0f, 0.0f},
37 .fovy = 30.0f,
38 .projection = CAMERA_PERSPECTIVE,
39};
40
41constexpr ImGuiWindowFlags kFrame =
42 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
43 ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoNav |
44 ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoSavedSettings |
45 ImGuiWindowFlags_NoBringToFrontOnFocus;
46
47float Share(int value, int most) {
48 return most > 0 ? std::clamp(static_cast<float>(value) / most, 0.0f, 1.0f)
49 : 0.0f;
50}
51
52// A ring of brass round a circle, with a shadow under it.
53void DrawRing(ImDrawList* draw, ImVec2 centre, float radius) {
54 draw->AddCircleFilled({centre.x + Ui(2.0f), centre.y + Ui(3.0f)},
55 radius + Ui(4.0f), Faded(IM_COL32_BLACK, 0.45f));
56 draw->AddCircle(centre, radius + Ui(2.0f), colors::kBrass, 0, Ui(4.5f));
57 draw->AddCircle(centre, radius + Ui(4.3f), colors::kNight, 0, Ui(1.4f));
58 draw->AddCircle(centre, radius, Faded(colors::kGoldLight, 0.35f), 0,
59 Ui(1.0f));
60}
61
62// The plate beside a portrait, from its middle out to the right, with a window
63// over it for the mouse.
64void BeginFrame(const char* name, ImVec2 min, ImVec2 max) {
65 ImGui::SetNextWindowPos(min);
66 ImGui::SetNextWindowSize({max.x - min.x, max.y - min.y});
67 ImGui::Begin(name, nullptr, kFrame);
68}
69
70} // namespace
71
72void PlayerFrame::Render(const Renderer& renderer) {
73 const std::vector<Figure>& figures = renderer.figures();
74 const auto you = std::ranges::find_if(figures, &Figure::you);
75 if (you == figures.end()) {
76 return;
77 }
78 Figure still = *you;
79 still.position = {};
80 still.yaw = 0.0f;
81 still.from = protocol::MOTION_IDLE;
82 still.to = protocol::MOTION_IDLE;
83 still.to_time = static_cast<float>(GetTime());
84 still.blend = 1.0f;
85 portrait_.RenderPreview(renderer, still, kPortraitCamera);
86 rendered_ = true;
87}
88
89void PlayerFrame::Draw(const std::vector<Figure>& figures,
90 const protocol::World& world) const {
91 const auto you = std::ranges::find_if(figures, &Figure::you);
92 // Our level comes with our name, once a second, so the frame waits for it.
93 if (you == figures.end() || you->level == 0) {
94 return;
95 }
96 const float radius = Ui(kPortrait);
97 const ImVec2 centre{Ui(kMargin) + radius + Ui(4.0f),
98 Ui(kMargin) + radius + Ui(4.0f)};
99 const ImVec2 plate_min{centre.x, centre.y - Ui(kPlateHeight) / 2.0f};
100 const ImVec2 plate_max{centre.x + Ui(kPlateWidth),
101 centre.y + Ui(kPlateHeight) / 2.0f};
102 // Your faction goes under the plate, once you've sworn to one.
103 const auto faction = factions_.find(you->faction);
104 const bool sworn = faction != factions_.end();
105 BeginFrame("You", {Ui(kMargin), Ui(kMargin)},
106 {plate_max.x,
107 sworn ? plate_max.y + Ui(30.0f) : centre.y + radius + Ui(8.0f)});
108 ImDrawList* draw = ImGui::GetWindowDrawList();
109 DrawPanel(draw, plate_min, plate_max, 0.92f);
110
111 // The portrait, in its ring.
112 draw->AddCircleFilled(centre, radius, colors::kCoal);
113 if (rendered_) {
114 // OpenGL keeps the rows bottom up, so the UVs turn the picture over.
115 draw->AddImageRounded(static_cast<ImTextureID>(portrait_.texture().id),
116 {centre.x - radius, centre.y - radius},
117 {centre.x + radius, centre.y + radius}, {0.0f, 1.0f},
118 {1.0f, 0.0f}, IM_COL32_WHITE, radius);
119 }
120 DrawRing(draw, centre, radius);
121
122 // The level, on a gem at the portrait's foot.
123 const ImVec2 gem{centre.x + radius * 0.72f, centre.y + radius * 0.72f};
124 DrawDiamond(draw, gem, Ui(14.0f), colors::kWine, colors::kGold);
125 const std::string level = absl::StrCat(you->level);
126 ImFont* bold = GetFonts().bold;
127 const ImVec2 level_extent =
128 bold->CalcTextSizeA(Ui(14.0f), FLT_MAX, 0.0f, level.c_str());
130 draw, bold, Ui(14.0f),
131 {gem.x - level_extent.x / 2.0f, gem.y - level_extent.y / 2.0f},
132 colors::kGoldLight, level.c_str());
133
134 // The name, and the bars beside the portrait.
135 const float left = centre.x + radius + Ui(12.0f);
136 const float right = plate_max.x - Ui(16.0f);
137 DrawGradientText(draw, GetFonts().title, Ui(16.0f),
138 {left, plate_min.y + Ui(7.0f)}, colors::kGoldLight,
139 colors::kGold, you->name.c_str());
140 // Your guild, across from your name, as the target's level is.
141 if (!you->guild.empty()) {
142 const ImVec2 extent =
143 bold->CalcTextSizeA(Ui(13.0f), FLT_MAX, 0.0f, you->guild.c_str());
144 DrawShadowedText(draw, bold, Ui(13.0f),
145 {right - extent.x, plate_min.y + Ui(9.0f)}, colors::kMuted,
146 you->guild.c_str());
147 }
148 // Your faction's name, in its colour, after a diamond of it, as over your
149 // head.
150 if (sworn) {
151 const Color color = GetColor(faction->second.color());
152 const ImU32 shown = IM_COL32(color.r, color.g, color.b, 255);
153 const float top = plate_max.y + Ui(6.0f);
154 DrawDiamond(draw, {left + Ui(5.0f), top + Ui(8.0f)}, Ui(5.0f), shown,
156 DrawShadowedText(draw, bold, Ui(14.0f), {left + Ui(16.0f), top}, shown,
157 NameOf(faction->second));
158 }
159 const std::string health = absl::StrCat(you->health, " / ", you->max_health);
160 const ImVec2 health_min{left, plate_min.y + Ui(30.0f)};
161 const ImVec2 health_max{right, health_min.y + Ui(16.0f)};
162 DrawBar(draw, health_min, health_max, Share(you->health, you->max_health),
163 colors::kHealth, colors::kHealthDark, health.c_str());
164 const ImVec2 experience_min{left, health_max.y + Ui(5.0f)};
165 const ImVec2 experience_max{right, experience_min.y + Ui(7.0f)};
166 DrawBar(draw, experience_min, experience_max,
167 Share(static_cast<int>(world.experience()),
168 static_cast<int>(world.next_level())),
170 ImGui::SetCursorScreenPos(experience_min);
171 ImGui::InvisibleButton("##experience", {experience_max.x - experience_min.x,
172 experience_max.y - experience_min.y});
173 if (ImGui::IsItemHovered()) {
174 ImGui::SetTooltip(
175 "%s", absl::Substitute(Tr("Experience: $0 / $1"), world.experience(),
176 world.next_level())
177 .c_str());
178 }
179 ImGui::End();
180}
181
182void DrawTargetFrame(const std::vector<Figure>& figures, uint32_t target,
183 uint32_t target_player) {
184 const auto picked = std::ranges::find_if(figures, [&](const Figure& f) {
185 return Picked(f, target, target_player);
186 });
187 if (picked == figures.end() || picked->name.empty()) {
188 return;
189 }
190 const float radius = Ui(26.0f);
191 // Under the compass, and the distance under it.
192 const float middle = Ui(kTargetTop) + Ui(kPlateHeight) / 2.0f;
193 const float width = radius + Ui(250.0f);
194 const ImVec2 centre{(ImGui::GetIO().DisplaySize.x - width) / 2.0f + radius,
195 middle};
196 const ImVec2 plate_min{centre.x, middle - Ui(kPlateHeight) / 2.0f + Ui(6.0f)};
197 const ImVec2 plate_max{centre.x + Ui(250.0f),
198 middle + Ui(kPlateHeight) / 2.0f - Ui(6.0f)};
199 BeginFrame("Target", {centre.x - radius - Ui(6.0f), plate_min.y - Ui(4.0f)},
200 {plate_max.x, plate_max.y + Ui(6.0f)});
201 ImDrawList* draw = ImGui::GetWindowDrawList();
202 DrawPanel(draw, plate_min, plate_max, 0.92f);
203 draw->AddCircleFilled(centre, radius, colors::kWine);
204 DrawRing(draw, centre, radius);
205 ImFont* text = GetFonts().text;
206 const float icon = Ui(22.0f);
207 // A skull for a monster, and someone for the player you're duelling.
208 const char* glyph = picked->monster ? ICON_FA_SKULL : ICON_FA_USER;
209 const ImVec2 icon_extent = text->CalcTextSizeA(icon, FLT_MAX, 0.0f, glyph);
211 draw, text, icon,
212 {centre.x - icon_extent.x / 2.0f, centre.y - icon_extent.y / 2.0f},
213 colors::kGoldLight, glyph);
214
215 const float inner = centre.x + radius + Ui(10.0f);
216 const float right = plate_max.x - Ui(16.0f);
218 draw, GetFonts().title, Ui(15.0f), {inner, plate_min.y + Ui(6.0f)},
219 IM_COL32(255, 170, 150, 255), colors::kEmber, picked->name.c_str());
220 const std::string level = absl::Substitute(Tr("Level $0"), picked->level);
221 ImFont* bold = GetFonts().bold;
222 const ImVec2 level_extent =
223 bold->CalcTextSizeA(Ui(13.0f), FLT_MAX, 0.0f, level.c_str());
224 DrawShadowedText(draw, bold, Ui(13.0f),
225 {right - level_extent.x, plate_min.y + Ui(8.0f)},
226 colors::kMuted, level.c_str());
227 const std::string health =
228 absl::StrCat(picked->health, " / ", picked->max_health);
229 DrawBar(draw, {inner, plate_min.y + Ui(28.0f)},
230 {right, plate_min.y + Ui(42.0f)},
231 Share(picked->health, picked->max_health), colors::kHealth,
232 colors::kHealthDark, health.c_str());
233 ImGui::End();
234}
235
236} // namespace nanommo
void Draw(const std::vector< Figure > &figures, const protocol::World &world) const
void Render(const Renderer &renderer)
const std::vector< Figure > & figures() const
Definition renderer.h:96
constexpr ImU32 kEmber
Definition theme.h:19
constexpr ImU32 kGoldDark
Definition theme.h:23
constexpr ImU32 kGoldLight
Definition theme.h:22
constexpr ImU32 kNight
Definition theme.h:14
constexpr ImU32 kMuted
Definition theme.h:27
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 kCoal
Definition theme.h:15
constexpr ImU32 kGold
Definition theme.h:21
constexpr ImU32 kHealth
Definition theme.h:30
constexpr ImU32 kHealthDark
Definition theme.h:31
Definition chat.cc:9
void DrawPanel(ImDrawList *draw, ImVec2 min, ImVec2 max, float alpha)
Definition theme.cc:223
void DrawTargetFrame(const std::vector< Figure > &figures, uint32_t target, uint32_t target_player)
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
const char * Tr(const char *text)
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
Definition picking.cc:86
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
void DrawDiamond(ImDrawList *draw, ImVec2 centre, float radius, ImU32 fill, ImU32 outline)
A diamond, filled, with an outline.
Definition theme.cc:213
void DrawGradientText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 top, ImU32 bottom, const char *text)
Definition theme.cc:283
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
Definition theme.cc:272
const Fonts & GetFonts()
Definition theme.cc:66
const char * NameOf(const items::Item &item)
Definition names.cc:14
protocol::Motion from
Definition animator.h:73
Vector3 position
Definition animator.h:63
protocol::Motion to
Definition animator.h:77
ImFont * text
Definition theme.h:42
ImFont * bold
Definition theme.h:43