From 573bf36b3852e934c8d1b23d26e7828dd3e2cafc Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 19 Apr 2023 23:56:15 +0200 Subject: Extracted chunk of user interaction to own Controller This also introduced Mouse and a simple error of dragging box always appearing during pause (or in scenario editor). This can be fixed via pause update in Battle class. --- engine/include/kurator/engine/Point.h | 2 ++ engine/src/Point.cpp | 14 +++++++++++ kurator/CMakeLists.txt | 2 ++ kurator/src/Battle.cpp | 15 ++++------- kurator/src/Battle.h | 4 +-- kurator/src/Controller.cpp | 42 +++++++++++++++++++++++++++++++ kurator/src/Controller.h | 23 +++++++++++++++++ kurator/src/Mouse.cpp | 47 +++++++++++++++++++++++++++++++++++ kurator/src/Mouse.h | 23 +++++++++++++++++ kurator/src/markers.cpp | 13 +++++----- kurator/src/markers.h | 5 ++-- 11 files changed, 169 insertions(+), 21 deletions(-) create mode 100644 kurator/src/Controller.cpp create mode 100644 kurator/src/Controller.h create mode 100644 kurator/src/Mouse.cpp create mode 100644 kurator/src/Mouse.h diff --git a/engine/include/kurator/engine/Point.h b/engine/include/kurator/engine/Point.h index 3dc0c8b..4b352d6 100644 --- a/engine/include/kurator/engine/Point.h +++ b/engine/include/kurator/engine/Point.h @@ -21,6 +21,8 @@ struct Point Point operator+(const Point& other) const; Point subtract(const Point& other) const; Point subtract(double _x, double _y) const; + bool operator==(const Point& other) const; + bool operator!=(const Point& other) const; }; diff --git a/engine/src/Point.cpp b/engine/src/Point.cpp index f02991c..e01de57 100644 --- a/engine/src/Point.cpp +++ b/engine/src/Point.cpp @@ -82,5 +82,19 @@ Point::subtract(const double _x, const double _y) const } +bool +Point::operator==(const Point& other) const +{ + return x == other.x && y == other.y; +} + + +bool +Point::operator!=(const Point& other) const +{ + return x != other.x || y != other.y; +} + + } // namespace engine } // namespace kurator diff --git a/kurator/CMakeLists.txt b/kurator/CMakeLists.txt index 1465ba3..947aa52 100644 --- a/kurator/CMakeLists.txt +++ b/kurator/CMakeLists.txt @@ -5,12 +5,14 @@ add_executable( src/Battle.cpp src/Campaign.cpp src/colors.cpp + src/Controller.cpp src/ForceBalance.cpp src/Grid.cpp src/inspect.cpp src/lines.cpp src/main.cpp src/markers.cpp + src/Mouse.cpp src/Pause.cpp src/PopupEmitter.cpp src/ScenarioEditor.cpp diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index d214b89..0eaeac1 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -1,7 +1,6 @@ #include "Battle.h" #include -#include #include #include #include @@ -86,27 +85,22 @@ static void blink_crosses(sim::State& ctx); void Battle::update() { - if (IsKeyPressed(KEY_ESCAPE)) + if (IsKeyPressed(KEY_ESCAPE)) // Move to Controller? return session->set(std::make_shared(session, session->current())); - if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { - const auto delta = GetMouseDelta(); - ctx.camera.offset.x -= delta.x / ctx.camera.scale; - ctx.camera.offset.y -= delta.y / ctx.camera.scale; - } if (IsWindowResized()) ctx.camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); ctx.clock.update(); + controller.update(ctx); simulation_base(ctx); - update_markers(ctx, std::bind(&InspectionWindow::select, &inspect, std::placeholders::_1)); + update_markers(ctx, controller); progress_timers(ctx); move_ui_pops(ctx); blink_crosses(ctx); animate_lines(ctx); balance.update(ctx.registry); - inspect.show(); ImGui::SetNextWindowPos({GetScreenWidth()/2.0f, GetScreenHeight()-100.0f}, ImGuiCond_Once, {0.5f, 0.5f}); ImGui::SetNextWindowSize({240.0f, 0.0f}, ImGuiCond_Once); - if (ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoFocusOnAppearing)) + if (ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoFocusOnAppearing)) // Move to Controller? time_controls("TimeControls", ctx.clock.time_factor); ImGui::End(); } @@ -167,6 +161,7 @@ Battle::draw() const draw_lines(ctx); draw_markers(ctx); draw_pops(ctx); + controller.draw(ctx); balance.draw(); } diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index 0de492a..67d1d6b 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -10,8 +10,8 @@ #include #include +#include "Controller.h" #include "ForceBalance.h" -#include "inspect.h" #include "Scene.h" #include "Session.h" @@ -40,7 +40,7 @@ private: ForceBalance balance; stats::EventLog log; Callback report; - InspectionWindow inspect; + Controller controller; }; diff --git a/kurator/src/Controller.cpp b/kurator/src/Controller.cpp new file mode 100644 index 0000000..d7efcdb --- /dev/null +++ b/kurator/src/Controller.cpp @@ -0,0 +1,42 @@ +#include "Controller.h" + +#include + +#include + +#include "Mouse.h" + + +namespace kurator +{ + + +void Controller::update(sim::State& ctx) +{ + if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { + const auto delta = GetMouseDelta(); + ctx.camera.offset.x -= delta.x / ctx.camera.scale; + ctx.camera.offset.y -= delta.y / ctx.camera.scale; + } + mouse.update(); + inspect.show(); +} + + +void Controller::draw(const sim::State&) const +{ + if (mouse.is_dragging()) { + const auto pos = mouse.position(); + const auto start = mouse.start(); + const auto diff = start - pos; + DrawRectangleLines( + std::min(pos.x, start.x), + std::min(pos.y, start.y), + std::abs(diff.x), + std::abs(diff.y), + GRAY); + } +} + + +} // namespace kurator diff --git a/kurator/src/Controller.h b/kurator/src/Controller.h new file mode 100644 index 0000000..a5deff9 --- /dev/null +++ b/kurator/src/Controller.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include "inspect.h" +#include "Mouse.h" + + +namespace kurator +{ + + +class Controller +{ +public: + void update(sim::State& ctx); + void draw(const sim::State& ctx) const; + InspectionWindow inspect; + Mouse mouse; +}; + + +} // namespace kurator diff --git a/kurator/src/Mouse.cpp b/kurator/src/Mouse.cpp new file mode 100644 index 0000000..e66d869 --- /dev/null +++ b/kurator/src/Mouse.cpp @@ -0,0 +1,47 @@ +#include "Mouse.h" + +#include + +#include + + +namespace kurator +{ + + +Mouse::Mouse() : + start_ {position()} +{ +} + + +void +Mouse::update() +{ + if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) + start_ = position(); +} + + +engine::Point +Mouse::position() const +{ + return {static_cast(GetMouseX()), static_cast(GetMouseY())}; +} + + +engine::Point +Mouse::start() const +{ + return start_; +} + + +bool +Mouse::is_dragging() const +{ + return start_ != position(); +} + + +} // namespace kurator diff --git a/kurator/src/Mouse.h b/kurator/src/Mouse.h new file mode 100644 index 0000000..c5c1f0e --- /dev/null +++ b/kurator/src/Mouse.h @@ -0,0 +1,23 @@ +#pragma once + +#include + + +namespace kurator +{ + + +class Mouse +{ +public: + Mouse(); + void update(); + engine::Point position() const; + engine::Point start() const; + bool is_dragging() const; +private: + engine::Point start_; +}; + + +} // namespace kurator diff --git a/kurator/src/markers.cpp b/kurator/src/markers.cpp index f9e19b7..e46a751 100644 --- a/kurator/src/markers.cpp +++ b/kurator/src/markers.cpp @@ -1,7 +1,6 @@ #include "markers.h" #include -#include #include #include @@ -16,6 +15,7 @@ #include #include "colors.h" +#include "Controller.h" #include "PopupEmitter.h" @@ -37,22 +37,21 @@ attach_markers(sim::State& ctx) void -update_markers(sim::State& ctx, std::function select) +update_markers(sim::State& ctx, Controller& controller) { - 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) + if (controller.mouse.position().distance(marker.screen) <= marker.radius) hover = entity; } - if (ctx.registry.valid(hover)) { + 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) && select) - select(entt::handle{ctx.registry, hover}); + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) // Most likely it should + controller.inspect.select(entt::handle{ctx.registry, hover}); } } diff --git a/kurator/src/markers.h b/kurator/src/markers.h index c2fdd8b..122a6b1 100644 --- a/kurator/src/markers.h +++ b/kurator/src/markers.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include @@ -9,6 +8,8 @@ #include #include +#include "Controller.h" + namespace kurator { @@ -26,7 +27,7 @@ struct Marker void attach_markers(sim::State& ctx); -void update_markers(sim::State& ctx, std::function select); +void update_markers(sim::State& ctx, Controller& controller); void draw_markers(const sim::State& ctx); -- cgit v1.1