From bdfb27ba04528f213dcadc00afdcf1f35a61f6fc Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 22 Apr 2023 00:37:06 +0200 Subject: Multiple ships may now get selected via dragging --- kurator/src/Battle.cpp | 1 + kurator/src/Controller.cpp | 38 ++++++++++++++++++++++++++++++++++++++ kurator/src/Controller.h | 8 ++++++++ kurator/src/Mouse.cpp | 38 ++++++++++++++++++++++++++++++++------ kurator/src/Mouse.h | 5 ++++- kurator/src/inspect.cpp | 10 +++++----- kurator/src/markers.cpp | 25 +++++++++---------------- kurator/src/markers.h | 2 -- 8 files changed, 97 insertions(+), 30 deletions(-) diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 5434eb2..49774ce 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -50,6 +50,7 @@ Battle::Battle(std::shared_ptr _session, campaign::Scenario scenario, B ctx.dispatcher.sink().connect<&Battle::on_ship_left>(*this); ctx.camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); attach_markers(ctx); + controller.attach(ctx); balance.update(ctx.registry); } diff --git a/kurator/src/Controller.cpp b/kurator/src/Controller.cpp index 3512f3e..bb20413 100644 --- a/kurator/src/Controller.cpp +++ b/kurator/src/Controller.cpp @@ -1,9 +1,14 @@ #include "Controller.h" +#include +#include + +#include #include #include +#include "markers.h" #include "Mouse.h" @@ -11,6 +16,14 @@ namespace kurator { +void Controller::attach(sim::State& ctx) +{ + auto markers = ctx.registry.view(); + for (auto&& [entity, _] : markers.each()) + ctx.registry.emplace(entity); +} + + void Controller::update(sim::State& ctx) { if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { @@ -19,6 +32,31 @@ void Controller::update(sim::State& ctx) ctx.camera.offset.y -= delta.y / ctx.camera.scale; } mouse.update(); + auto markers = ctx.registry.view(); + std::vector selection; + for (auto&& [entity, select, marker] : markers.each()) { + select.hover = false; + if (mouse.is_dragging() || mouse.finished_dragging()) { + if (mouse.selection().contains(marker.screen, marker.radius)) + selection.push_back(std::move(entity)); + } + else { + if (mouse.position().distance(marker.screen) <= marker.radius) { + selection.clear(); + selection.push_back(std::move(entity)); + } + } + } + const bool change = + mouse.finished_dragging() || (!mouse.is_dragging() && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)); + if (!selection.empty() && change) + inspect.deselect(); + for (auto&& entity : selection) { + auto& select = ctx.registry.get(entity); + select.hover = true; + if (change) + inspect.select(entt::handle{ctx.registry, entity}); + } inspect.show(); } diff --git a/kurator/src/Controller.h b/kurator/src/Controller.h index a5deff9..5feb63c 100644 --- a/kurator/src/Controller.h +++ b/kurator/src/Controller.h @@ -13,6 +13,7 @@ namespace kurator class Controller { public: + void attach(sim::State& ctx); void update(sim::State& ctx); void draw(const sim::State& ctx) const; InspectionWindow inspect; @@ -20,4 +21,11 @@ public: }; +struct Selectable +{ + bool active = false; + bool hover = false; +}; + + } // namespace kurator diff --git a/kurator/src/Mouse.cpp b/kurator/src/Mouse.cpp index aeaa8e5..a4eff9d 100644 --- a/kurator/src/Mouse.cpp +++ b/kurator/src/Mouse.cpp @@ -10,8 +10,13 @@ namespace kurator { +auto from_raylib() -> engine::Point; + + Mouse::Mouse() : - start_ {position()} + position_ {from_raylib()}, + start {position_}, + dragging {false} { } @@ -19,29 +24,50 @@ Mouse::Mouse() : void Mouse::update() { - if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) - start_ = position(); + position_ = from_raylib(); + if (!dragging) + start = position_; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + start = position_; + dragging = true; + } + if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) + dragging = false; } engine::Point Mouse::position() const { - return {static_cast(GetMouseX()), static_cast(GetMouseY())}; + return position_; } engine::Rect Mouse::selection() const { - return {start_, position()}; + return {start, position_}; } bool Mouse::is_dragging() const { - return start_ != position(); + return dragging && start != position_; +} + + +bool +Mouse::finished_dragging() const +{ + return !dragging && start != position_; +} + + +engine::Point +from_raylib() +{ + return {static_cast(GetMouseX()), static_cast(GetMouseY())}; } diff --git a/kurator/src/Mouse.h b/kurator/src/Mouse.h index 81afe2c..1172f74 100644 --- a/kurator/src/Mouse.h +++ b/kurator/src/Mouse.h @@ -16,8 +16,11 @@ public: engine::Point position() const; engine::Rect selection() const; bool is_dragging() const; + bool finished_dragging() const; private: - engine::Point start_; + engine::Point position_; + engine::Point start; + bool dragging; }; diff --git a/kurator/src/inspect.cpp b/kurator/src/inspect.cpp index 454700e..ea70baf 100644 --- a/kurator/src/inspect.cpp +++ b/kurator/src/inspect.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -13,7 +14,7 @@ #include #include -#include "markers.h" +#include "Controller.h" namespace kurator @@ -71,10 +72,9 @@ InspectionWindow::show() void InspectionWindow::select(entt::handle entity) { - deselect(); if (!entity) return; - entity.get().selected = true; + entity.get().active = true; auto* registry = entity.registry(); auto turrets = registry->view(); entity.emplace(); @@ -95,8 +95,8 @@ InspectionWindow::deselect() for (auto&& entity_ : selected) { if (!entity_.valid()) continue; - if (entity_.all_of()) - entity_.get().selected = false; + if (entity_.all_of()) + entity_.get().active = false; if (entity_.all_of()) entity_.remove(); if (entity_.all_of()) diff --git a/kurator/src/markers.cpp b/kurator/src/markers.cpp index d3fe01f..33ff907 100644 --- a/kurator/src/markers.cpp +++ b/kurator/src/markers.cpp @@ -39,20 +39,9 @@ attach_markers(sim::State& ctx) void update_markers(sim::State& ctx, Controller& controller) { - entt::entity hover {entt::null}; auto markers = ctx.registry.view(); - for (auto&& [entity, marker, transform] : markers.each()) { + for (auto&& [entity, marker, transform] : markers.each()) marker.screen = ctx.camera.to_screen(transform.position); - marker.hovered = false; - if (controller.mouse.position().distance(marker.screen) <= marker.radius) - hover = entity; - } - if (ctx.registry.valid(hover)) { // Marker selection should be in Controller? - auto& marker = ctx.registry.get(hover); - marker.hovered = true; - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) // Most likely it should - controller.inspect.select(entt::handle{ctx.registry, hover}); - } } @@ -71,12 +60,16 @@ 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 || marker.selected) - DrawCircle(pos.x, pos.y, marker.radius + 2, marker.selected ? YELLOW : WHITE); + auto text_colour = GRAY; + if (ctx.registry.all_of(entity)) { + const auto& select = ctx.registry.get(entity); + text_colour = select.active ? YELLOW : select.hover ? WHITE : text_colour; + if (select.hover || select.active) + DrawCircle(pos.x, pos.y, marker.radius + 2, select.active ? YELLOW : WHITE); + } DrawCircle(pos.x, pos.y, marker.radius, marker.color); DrawLine(pos.x, pos.y, edge.x, edge.y, WHITE); - const auto colour = marker.selected ? YELLOW : marker.hovered ? WHITE : GRAY; - DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, colour); + DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, text_colour); } } diff --git a/kurator/src/markers.h b/kurator/src/markers.h index 122a6b1..f5ba3f0 100644 --- a/kurator/src/markers.h +++ b/kurator/src/markers.h @@ -21,8 +21,6 @@ struct Marker double radius; Color color; std::string name; - bool hovered = false; - bool selected = false; }; -- cgit v1.1