From f77c0c278f343422ce5c7a55ec5c126b3e190f77 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 19 Feb 2023 00:35:48 +0100 Subject: Draw turret ranges for selected ships --- kurator/src/Battle.cpp | 1 + kurator/src/inspect.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- kurator/src/inspect.h | 12 ++++++++++ kurator/src/markers.cpp | 7 +++--- kurator/src/markers.h | 1 + 5 files changed, 74 insertions(+), 5 deletions(-) diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 93a1444..b0719ea 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -161,6 +161,7 @@ Battle::draw() const { ClearBackground(BLACK); Grid().draw(ctx.camera); + draw_turret_visuals(ctx); draw_crosses(ctx); draw_lines(ctx); draw_markers(ctx); diff --git a/kurator/src/inspect.cpp b/kurator/src/inspect.cpp index 4dff0a6..4795068 100644 --- a/kurator/src/inspect.cpp +++ b/kurator/src/inspect.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -12,6 +13,8 @@ #include #include +#include "markers.h" + namespace kurator { @@ -40,6 +43,7 @@ InspectionWindow::show() if (ImGui::Begin("Inspect", &open)) { auto it = selected.begin(); while (it != selected.end()) { + ImGui::PushID(it - selected.begin()); if (!it->valid()) { it = selected.erase(it); continue; @@ -49,30 +53,52 @@ InspectionWindow::show() inspect(*it); inspect(*it); inspect(*it); + inspect(*it); it = std::next(it); + ImGui::PopID(); } if (selected.empty()) ImGui::Text("Nothing selected"); } ImGui::End(); + if (!open) + deselect(); } void InspectionWindow::select(entt::handle entity) { - selected.clear(); + deselect(); + if (!entity) + return; + entity.get().selected = true; auto* registry = entity.registry(); auto turrets = registry->view(); selected.push_back(std::move(entity)); for (auto&& [entity_, turret] : turrets.each()) { - if (turret.owner == entity) + if (turret.owner == entity) { + registry->emplace(entity_); selected.push_back(entt::handle{*registry, entity_}); + } } open = true; } +void +InspectionWindow::deselect() +{ + for (auto&& entity_ : selected) { + if (entity_.all_of()) + entity_.get().selected = false; + if (entity_.all_of()) + entity_.remove(); + } + selected.clear(); +} + + template <> void inspect(entt::handle&, universe::UniqueIdentifier& identifier) @@ -117,4 +143,32 @@ inspect(entt::handle&, sim::HitPoints& points) } +template <> +void +inspect(entt::handle&, TurretVisuals& visuals) +{ + ImGui::Checkbox("Show Turret Ranges", &visuals.visible); +} + + +static Color OPTIMAL {0x88, 0x22, 0x22, 0xff}; +static Color EFFECTIVE {0x77, 0x50, 0x22, 0xff}; + +void +draw_turret_visuals(const sim::State& ctx) +{ + const auto helpers = ctx.registry.view(); + for (const auto& [entity, visuals, type, turret] : helpers.each()) { + if (!visuals.visible) + continue; + const auto& transform = ctx.registry.get(turret.owner); + const auto center = ctx.camera.to_screen(transform.position); + const auto optimal = type.optimal_range * ctx.camera.scale; + const auto effective = type.effective_range() * ctx.camera.scale; + DrawCircleLines(center.x, center.y, optimal, OPTIMAL); + DrawCircleLines(center.x, center.y, effective, EFFECTIVE); + } +} + + } // namespace kurator diff --git a/kurator/src/inspect.h b/kurator/src/inspect.h index 77daa76..b123219 100644 --- a/kurator/src/inspect.h +++ b/kurator/src/inspect.h @@ -4,6 +4,8 @@ #include +#include + namespace kurator { @@ -15,7 +17,17 @@ struct InspectionWindow std::vector selected = {}; void show(); void select(entt::handle entity); + void deselect(); }; +struct TurretVisuals +{ + bool visible = true; +}; + + +void draw_turret_visuals(const sim::State& ctx); + + } // namespace kurator diff --git a/kurator/src/markers.cpp b/kurator/src/markers.cpp index 2b04129..f9e19b7 100644 --- a/kurator/src/markers.cpp +++ b/kurator/src/markers.cpp @@ -72,11 +72,12 @@ draw_markers(const sim::State& ctx) } const engine::Point direction {std::cos(transform.angle), std::sin(transform.angle)}; const auto edge = pos + direction.scale(marker.radius); - if (marker.hovered) - DrawCircle(pos.x, pos.y, marker.radius + 2, WHITE); + if (marker.hovered || marker.selected) + DrawCircle(pos.x, pos.y, marker.radius + 2, marker.selected ? YELLOW : WHITE); DrawCircle(pos.x, pos.y, marker.radius, marker.color); DrawLine(pos.x, pos.y, edge.x, edge.y, WHITE); - DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, marker.hovered ? WHITE : GRAY); + const auto colour = marker.selected ? YELLOW : marker.hovered ? WHITE : GRAY; + DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, colour); } } diff --git a/kurator/src/markers.h b/kurator/src/markers.h index a702b2e..c2fdd8b 100644 --- a/kurator/src/markers.h +++ b/kurator/src/markers.h @@ -21,6 +21,7 @@ struct Marker Color color; std::string name; bool hovered = false; + bool selected = false; }; -- cgit v1.1