From c4fce4331d5c80c3e97221eb30070f760838544d Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 16 Feb 2023 01:08:05 +0100 Subject: Hovered ship markers are now highlighted --- kurator/src/Battle.cpp | 1 + kurator/src/markers.cpp | 33 ++++++++++++++++++++++++++++----- kurator/src/markers.h | 4 ++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 68879c6..0918e30 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -95,6 +95,7 @@ Battle::update() ctx.camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); ctx.clock.update(); simulation_base(ctx); + update_markers(ctx); progress_timers(ctx); move_ui_pops(ctx); blink_crosses(ctx); diff --git a/kurator/src/markers.cpp b/kurator/src/markers.cpp index a4ed417..436bd33 100644 --- a/kurator/src/markers.cpp +++ b/kurator/src/markers.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -24,21 +25,41 @@ namespace kurator void attach_markers(sim::State& ctx) { - auto ships = ctx.registry.view(); - for (const auto& [entity, team, type, identifier] : ships.each()) { + auto ships = ctx.registry.view(); + for (const auto& [entity, team, type, identifier, transform] : ships.each()) { + auto pos = ctx.camera.to_screen(transform.position); std::string label = TextFormat("%s (%d)", type.name.c_str(), identifier.id); - ctx.registry.emplace(entity, 5.0, team_color(team.id), std::move(label)); + ctx.registry.emplace(entity, std::move(pos), 5.0, team_color(team.id), std::move(label)); ctx.registry.emplace(entity); } } void +update_markers(sim::State& ctx) +{ + engine::Point mouse {static_cast(GetMouseX()), static_cast(GetMouseY())}; + entt::entity hover {entt::null}; + auto markers = ctx.registry.view(); + for (auto&& [entity, marker, transform] : markers.each()) { + marker.screen = ctx.camera.to_screen(transform.position); + marker.hovered = false; + if (mouse.distance(marker.screen) <= marker.radius) + hover = entity; + } + if (ctx.registry.valid(hover)) { + auto& marker = ctx.registry.get(hover); + marker.hovered = true; + } +} + + +void draw_markers(const sim::State& ctx) { auto view = ctx.registry.view(); for (const auto& [entity, marker, transform] : view.each()) { - const auto pos = ctx.camera.to_screen(transform.position); + const auto& pos = marker.screen; if (ctx.registry.all_of(entity)) { const auto& movement = ctx.registry.get(entity); const auto& velocity = movement.speed; @@ -48,9 +69,11 @@ 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); 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, GRAY); + DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, marker.hovered ? WHITE : GRAY); } } diff --git a/kurator/src/markers.h b/kurator/src/markers.h index c6522cf..f390ff6 100644 --- a/kurator/src/markers.h +++ b/kurator/src/markers.h @@ -4,6 +4,7 @@ #include +#include #include @@ -13,13 +14,16 @@ namespace kurator struct Marker { + engine::Point screen; double radius; Color color; std::string name; + bool hovered = false; }; void attach_markers(sim::State& ctx); +void update_markers(sim::State& ctx); void draw_markers(const sim::State& ctx); -- cgit v1.1