fix(client): stabilize HUD and anchor draggable panels
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 20s
Foundation / server-image (push) Canceled after 1m43s
Foundation / image (push) Canceled after 1m43s

This commit is contained in:
dingo
2026-10-26 15:13:15 -04:00
parent 840816c541
commit d057374148
9 changed files with 334 additions and 31 deletions
+94 -26
View File
@@ -1145,12 +1145,68 @@ bool action_button(const char* label, ImVec2 size, ImVec4 accent) {
ImGui::PopStyleColor(4);
return pressed;
}
void panel_heading(const char* title, bool& minimized) {
ImVec2 hud_position(ImVec2 position, ImVec2 size) {
const auto display = ImGui::GetIO().DisplaySize;
const float margin = px(8);
return {std::clamp(position.x, margin, std::max(margin, display.x - size.x - margin)),
std::clamp(position.y, margin, std::max(margin, display.y - size.y - margin))};
}
void place_hud_panel(HudPanelPlacement& placement, ImVec2 fallback, ImVec2 size) {
const auto display = ImGui::GetIO().DisplaySize;
size.x = std::min(size.x, std::max(1.0F, display.x - 2 * px(8)));
size.y = std::min(size.y, std::max(1.0F, display.y - 2 * px(8)));
auto position = fallback;
if (placement.anchor)
position = {placement.anchor->first * display.x, placement.anchor->second * display.y};
position = hud_position(position, size);
if (placement.anchor)
placement.anchor = {position.x / display.x, position.y / display.y};
ImGui::SetNextWindowPos(position, ImGuiCond_Always);
ImGui::SetNextWindowSize(size, ImGuiCond_Always);
}
float collapsed_height(bool energy = false) {
return 2 * ImGui::GetStyle().WindowPadding.y + ImGui::GetFrameHeight() +
(energy ? ImGui::GetTextLineHeightWithSpacing() : 0);
}
void begin_hud_body(const char* id) {
ImGui::BeginChild(id, {0, 0}, ImGuiChildFlags_None, ImGuiWindowFlags_AlwaysVerticalScrollbar);
ImGui::PushTextWrapPos(0);
}
void end_hud_body() {
ImGui::PopTextWrapPos();
ImGui::EndChild();
}
// The heading stays outside the scrollable body. Only this handle moves a panel.
void panel_heading(const char* title, bool& minimized, HudPanelPlacement& placement) {
ImGui::PushID(title);
const float right = ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x;
ImGui::AlignTextToFramePadding();
ImGui::TextUnformatted(title);
ImGui::SameLine();
const float small_width = ImGui::CalcTextSize(" + ").x + 2 * ImGui::GetStyle().FramePadding.x;
ImGui::SetCursorPosX(right - 2 * small_width - ImGui::GetStyle().ItemSpacing.x);
ImGui::SmallButton("::");
if (ImGui::IsItemActivated()) {
const auto position = ImGui::GetWindowPos();
placement.drag_origin = {position.x, position.y};
}
if (ImGui::IsItemActive() && ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
const auto delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left);
const auto position = hud_position(
{placement.drag_origin.first + delta.x, placement.drag_origin.second + delta.y},
ImGui::GetWindowSize());
const auto display = ImGui::GetIO().DisplaySize;
placement.anchor = {position.x / display.x, position.y / display.y};
ImGui::SetWindowPos(position);
}
if (ImGui::IsItemHovered()) {
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
ImGui::SetTooltip("Drag to place %s. Double-click to reset its position.", title);
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
placement.anchor.reset();
}
ImGui::SameLine();
ImGui::SetCursorPosX(right - ImGui::CalcTextSize(" + ").x -
2 * ImGui::GetStyle().FramePadding.x);
if (ImGui::SmallButton(minimized ? " + " : " - "))
@@ -1201,35 +1257,32 @@ void hud_vitals(const PanelView& v, FormBuffers& form, bool compact) {
const auto& io = ImGui::GetIO();
const float clearance = eyepiece_clearance();
const float width = std::min(px(300), io.DisplaySize.x - 2 * px(12));
if (compact)
ImGui::SetNextWindowPos({px(12), px(64)}, ImGuiCond_Always);
else
ImGui::SetNextWindowPos({clearance, io.DisplaySize.y - px(16)}, ImGuiCond_Always, {0, 1});
const auto* play = v.cellular_play;
const auto* d = diagnostics(v);
if (!play || play->life() == 0)
return; // The life window speaks for a cell that is not alive.
const float max_height =
compact ? std::max(px(120), io.DisplaySize.y - px(220)) : io.DisplaySize.y * 0.43F;
ImGui::SetNextWindowSizeConstraints({width, 0}, {width, max_height});
// Keep table width stable when the constrained card gains its vertical scrollbar.
ImGui::SetNextWindowContentSize(
{width - 2 * ImGui::GetStyle().WindowPadding.x - ImGui::GetStyle().ScrollbarSize, 0});
const float height = form.vitals_minimized ? collapsed_height(true) : max_height;
place_hud_panel(form.vitals_placement,
compact ? ImVec2{px(12), px(64)}
: ImVec2{clearance, io.DisplaySize.y - px(16) - height},
{width, height});
ImGui::Begin("Vitals", nullptr,
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize |
(form.vitals_minimized ? ImGuiWindowFlags_None
: ImGuiWindowFlags_AlwaysVerticalScrollbar));
panel_heading("Your cell", form.vitals_minimized);
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
panel_heading("Your cell", form.vitals_minimized, form.vitals_placement);
if (form.vitals_minimized) {
if (d)
ImGui::Text("Energy %s", energy_text(d->reserve_energy_pj).c_str());
ImGui::End();
return;
}
begin_hud_body("##body");
if (!play->fresh())
ImGui::TextColored(amber, "Waiting for a fresh server view");
if (!d) {
disabled_wrapped("Your cell's reserves appear with the next server view.");
end_hud_body();
ImGui::End();
return;
}
@@ -1321,6 +1374,7 @@ void hud_vitals(const PanelView& v, FormBuffers& form, bool compact) {
if (d->light_pj != 0)
ImGui::TextDisabled("Sunlight +%s", energy_text(d->light_pj).c_str());
}
end_hud_body();
ImGui::End();
}
// Bottom centre: the three things a cell does, with what a bud still needs and the last
@@ -1339,9 +1393,11 @@ void hud_dock(const PanelView& v, FormBuffers& form, PanelActions& a, bool compa
left = std::max(left, eyepiece_clearance() + std::min(px(300), io.DisplaySize.x) + px(12));
left = std::max(px(12), std::min(left, io.DisplaySize.x - width - px(12)));
ImGui::SetNextWindowPos({left, io.DisplaySize.y - px(16)}, ImGuiCond_Always, {0, 1});
ImGui::SetNextWindowSizeConstraints({width, 0}, {width, io.DisplaySize.y});
ImGui::SetNextWindowSize(
{width, std::min(px(compact ? 140.0F : 180.0F), io.DisplaySize.y - px(32))},
ImGuiCond_Always);
ImGui::Begin("Actions", nullptr,
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize);
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
const float each =
(ImGui::GetContentRegionAvail().x - 2 * ImGui::GetStyle().ItemSpacing.x) / 3.0F;
const bool active = play && play->fresh() && play->life() != 0;
@@ -1383,6 +1439,7 @@ void hud_dock(const PanelView& v, FormBuffers& form, PanelActions& a, bool compa
}
ImGui::EndTooltip();
}
begin_hud_body("##details");
// Rules 13 (v14): continue as the bud, or grow the organism toward Emerge.
if (d && d->members != 0 && play && play->life() != 0) {
if (d->members == 1) {
@@ -1436,6 +1493,7 @@ void hud_dock(const PanelView& v, FormBuffers& form, PanelActions& a, bool compa
}
}
}
end_hud_body();
ImGui::End();
}
// Upper left: what the colours mean, steering, food in range, recent actions.
@@ -1444,16 +1502,20 @@ void hud_notes(const PanelView& v, FormBuffers& form, PanelActions& a, bool comp
return;
const auto& io = ImGui::GetIO();
const float clearance = eyepiece_clearance();
ImGui::SetNextWindowPos({clearance, px(64)}, ImGuiCond_Always);
ImGui::SetNextWindowSizeConstraints({px(260), 0},
{px(260), std::max(px(120), io.DisplaySize.y * 0.41F)});
place_hud_panel(
form.notes_placement, {clearance, px(64)},
{px(260), form.notes_minimized
? collapsed_height()
: std::min(px(260), std::max(px(120), io.DisplaySize.y * 0.41F))});
ImGui::Begin("Field notes", nullptr,
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize);
panel_heading("Field notes", form.notes_minimized);
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
panel_heading("Field notes", form.notes_minimized, form.notes_placement);
if (form.notes_minimized) {
ImGui::End();
return;
}
begin_hud_body("##body");
ImGui::PushTextWrapPos(-1);
for (std::size_t i = 0; i < nutrient_names.size(); ++i) {
const float glyph_size = ImGui::GetTextLineHeight();
const auto at = ImGui::GetCursorScreenPos();
@@ -1463,8 +1525,9 @@ void hud_notes(const PanelView& v, FormBuffers& form, PanelActions& a, bool comp
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
ImGui::TextUnformatted(nutrient_names.at(i));
if (i % 2 == 0 && i + 1 < nutrient_names.size())
ImGui::SameLine(px(130));
ImGui::SameLine(px(112));
}
ImGui::PopTextWrapPos();
if (ImGui::CollapsingHeader("Food in range")) {
const bool active =
v.cellular_play && v.cellular_play->fresh() && v.cellular_play->life() != 0;
@@ -1564,6 +1627,7 @@ void hud_notes(const PanelView& v, FormBuffers& form, PanelActions& a, bool comp
"vulnerable for 10 seconds before it is suspended.");
ImGui::Checkbox("Engineering panels", &form.engineering);
}
end_hud_body();
ImGui::End();
}
// Upper right: a quiet callout while an offer waits; click it or press E.
@@ -1888,16 +1952,19 @@ void hud_map(const PanelView& v, FormBuffers& form, bool compact) {
const auto& scene = *v.cellular_scene;
const auto& io = ImGui::GetIO();
const float width = px(compact ? 184.0F : 210.0F);
ImGui::SetNextWindowPos({io.DisplaySize.x - (compact ? px(12) : eyepiece_clearance()), px(126)},
ImGuiCond_Always, {1, 0});
ImGui::SetNextWindowSizeConstraints({width, 0}, {width, io.DisplaySize.y - px(200)});
place_hud_panel(form.map_placement,
{io.DisplaySize.x - (compact ? px(12) : eyepiece_clearance()) - width, px(126)},
{width, form.map_minimized ? collapsed_height()
: std::min(px(compact ? 192.0F : 300.0F),
io.DisplaySize.y - px(200))});
ImGui::Begin("Nearby", nullptr,
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize);
panel_heading("Nearby", form.map_minimized);
hud_flags | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
panel_heading("Nearby", form.map_minimized, form.map_placement);
if (form.map_minimized) {
ImGui::End();
return;
}
begin_hud_body("##body");
if (const auto* s = v.cellular_scene; s && s->region_cells != 0)
ImGui::TextDisabled("%zu cells in this region", s->region_cells);
auto at = ImGui::GetCursorScreenPos();
@@ -1972,6 +2039,7 @@ void hud_map(const PanelView& v, FormBuffers& form, bool compact) {
ImGui::SetTooltip(
"Camera-centred local map. Unseen space is unknown.\nRings: pools. Dots: nutrients. "
"Triangles: cells.\nLocal +Y is up; planetary north is not defined.");
end_hud_body();
ImGui::End();
}
void cellular(const PanelView& v, FormBuffers& form, PanelActions& a) {