11#include "absl/strings/str_cat.h"
12#include "absl/strings/substitute.h"
17#include "extras/IconsFontAwesome6.h"
25constexpr float kMargin = 18.0f;
26constexpr float kPortrait = 40.0f;
27constexpr float kPlateWidth = 300.0f;
28constexpr float kPlateHeight = 66.0f;
30constexpr float kTargetTop = 70.0f;
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},
38 .projection = CAMERA_PERSPECTIVE,
41constexpr ImGuiWindowFlags kFrame =
42 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
43 ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoNav |
44 ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoSavedSettings |
45 ImGuiWindowFlags_NoBringToFrontOnFocus;
47float Share(
int value,
int most) {
48 return most > 0 ? std::clamp(
static_cast<float>(value) / most, 0.0f, 1.0f)
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));
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);
73 const std::vector<Figure>& figures = renderer.
figures();
74 const auto you = std::ranges::find_if(figures, &
Figure::you);
75 if (you == figures.end()) {
81 still.
from = protocol::MOTION_IDLE;
82 still.
to = protocol::MOTION_IDLE;
83 still.
to_time =
static_cast<float>(GetTime());
85 portrait_.RenderPreview(renderer, still, kPortraitCamera);
90 const protocol::World& world)
const {
91 const auto you = std::ranges::find_if(figures, &
Figure::you);
93 if (you == figures.end() || you->level == 0) {
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};
103 const auto faction = factions_.find(you->faction);
104 const bool sworn = faction != factions_.end();
105 BeginFrame(
"You", {
Ui(kMargin),
Ui(kMargin)},
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);
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);
120 DrawRing(draw, centre, radius);
123 const ImVec2 gem{centre.x + radius * 0.72f, centre.y + radius * 0.72f};
125 const std::string level = absl::StrCat(you->level);
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},
135 const float left = centre.x + radius +
Ui(12.0f);
136 const float right = plate_max.x -
Ui(16.0f);
141 if (!you->guild.empty()) {
142 const ImVec2 extent =
143 bold->CalcTextSizeA(
Ui(13.0f), FLT_MAX, 0.0f, you->guild.c_str());
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);
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),
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()) {
175 "%s", absl::Substitute(
Tr(
"Experience: $0 / $1"), world.experience(),
183 uint32_t target_player) {
184 const auto picked = std::ranges::find_if(figures, [&](
const Figure& f) {
185 return Picked(f, target, target_player);
187 if (picked == figures.end() || picked->name.empty()) {
190 const float radius =
Ui(26.0f);
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,
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);
204 DrawRing(draw, centre, radius);
206 const float icon =
Ui(22.0f);
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);
212 {centre.x - icon_extent.x / 2.0f, centre.y - icon_extent.y / 2.0f},
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);
222 const ImVec2 level_extent =
223 bold->CalcTextSizeA(
Ui(13.0f), FLT_MAX, 0.0f, level.c_str());
225 {right - level_extent.x, plate_min.y +
Ui(8.0f)},
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)},
void Draw(const std::vector< Figure > &figures, const protocol::World &world) const
void Render(const Renderer &renderer)
const std::vector< Figure > & figures() const
constexpr ImU32 kGoldDark
constexpr ImU32 kGoldLight
constexpr ImU32 kBrass
The trim round every panel: gold gone dark with age.
constexpr ImU32 kHealthDark
void DrawPanel(ImDrawList *draw, ImVec2 min, ImVec2 max, float alpha)
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)
const char * Tr(const char *text)
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
void DrawDiamond(ImDrawList *draw, ImVec2 centre, float radius, ImU32 fill, ImU32 outline)
A diamond, filled, with an outline.
void DrawGradientText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 top, ImU32 bottom, const char *text)
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
const char * NameOf(const items::Item &item)