summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-04-19 23:56:15 +0200
committerAki <please@ignore.pl>2024-04-05 19:41:19 +0200
commit573bf36b3852e934c8d1b23d26e7828dd3e2cafc (patch)
tree7b8b71ce9963030087820cd75b80ad42e45f4560
parentd26f892bbe906d2a44b39049fca013c1503c1655 (diff)
downloadkurator-573bf36b3852e934c8d1b23d26e7828dd3e2cafc.zip
kurator-573bf36b3852e934c8d1b23d26e7828dd3e2cafc.tar.gz
kurator-573bf36b3852e934c8d1b23d26e7828dd3e2cafc.tar.bz2
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.
-rw-r--r--engine/include/kurator/engine/Point.h2
-rw-r--r--engine/src/Point.cpp14
-rw-r--r--kurator/CMakeLists.txt2
-rw-r--r--kurator/src/Battle.cpp15
-rw-r--r--kurator/src/Battle.h4
-rw-r--r--kurator/src/Controller.cpp42
-rw-r--r--kurator/src/Controller.h23
-rw-r--r--kurator/src/Mouse.cpp47
-rw-r--r--kurator/src/Mouse.h23
-rw-r--r--kurator/src/markers.cpp13
-rw-r--r--kurator/src/markers.h5
11 files changed, 169 insertions, 21 deletions
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 <algorithm>
-#include <functional>
#include <cmath>
#include <limits>
#include <memory>
@@ -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<Pause>(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 <kurator/stats/EventLog.h>
#include <kurator/stats/events.h>
+#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 <raylib.h>
+
+#include <kurator/sim/State.h>
+
+#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 <kurator/sim/State.h>
+
+#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 <raylib.h>
+
+#include <kurator/engine/Point.h>
+
+
+namespace kurator
+{
+
+
+Mouse::Mouse() :
+ start_ {position()}
+{
+}
+
+
+void
+Mouse::update()
+{
+ if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT))
+ start_ = position();
+}
+
+
+engine::Point
+Mouse::position() const
+{
+ return {static_cast<double>(GetMouseX()), static_cast<double>(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 <kurator/engine/Point.h>
+
+
+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 <cmath>
-#include <functional>
#include <string>
#include <utility>
@@ -16,6 +15,7 @@
#include <kurator/universe/UniqueIdentifier.h>
#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<void(entt::handle)> select)
+update_markers(sim::State& ctx, Controller& controller)
{
- engine::Point mouse {static_cast<double>(GetMouseX()), static_cast<double>(GetMouseY())};
entt::entity hover {entt::null};
auto markers = ctx.registry.view<Marker, sim::Transform>();
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<Marker>(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 <functional>
#include <string>
#include <entt/entt.hpp>
@@ -9,6 +8,8 @@
#include <kurator/engine/Point.h>
#include <kurator/sim/State.h>
+#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<void(entt::handle)> select);
+void update_markers(sim::State& ctx, Controller& controller);
void draw_markers(const sim::State& ctx);