summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-04-22 00:37:06 +0200
committerAki <please@ignore.pl>2024-04-05 19:41:19 +0200
commitbdfb27ba04528f213dcadc00afdcf1f35a61f6fc (patch)
tree251e260ccf89fe44268f16cd8598e652f0668cbd
parent654c94e1d312970413e376214b11bd554040d30f (diff)
downloadkurator-bdfb27ba04528f213dcadc00afdcf1f35a61f6fc.zip
kurator-bdfb27ba04528f213dcadc00afdcf1f35a61f6fc.tar.gz
kurator-bdfb27ba04528f213dcadc00afdcf1f35a61f6fc.tar.bz2
Multiple ships may now get selected via dragging
-rw-r--r--kurator/src/Battle.cpp1
-rw-r--r--kurator/src/Controller.cpp38
-rw-r--r--kurator/src/Controller.h8
-rw-r--r--kurator/src/Mouse.cpp38
-rw-r--r--kurator/src/Mouse.h5
-rw-r--r--kurator/src/inspect.cpp10
-rw-r--r--kurator/src/markers.cpp25
-rw-r--r--kurator/src/markers.h2
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> _session, campaign::Scenario scenario, B
ctx.dispatcher.sink<stats::ShipLeft>().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 <vector>
+#include <utility>
+
+#include <entt/entt.hpp>
#include <raylib.h>
#include <kurator/sim/State.h>
+#include "markers.h"
#include "Mouse.h"
@@ -11,6 +16,14 @@ namespace kurator
{
+void Controller::attach(sim::State& ctx)
+{
+ auto markers = ctx.registry.view<Marker>();
+ for (auto&& [entity, _] : markers.each())
+ ctx.registry.emplace<Selectable>(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<Selectable, Marker>();
+ std::vector<entt::entity> 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<Selectable>(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<double>(GetMouseX()), static_cast<double>(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<double>(GetMouseX()), static_cast<double>(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 <entt/entt.hpp>
#include <imgui.h>
+#include <raylib.h>
#include <kurator/sim/components.h>
#include <kurator/sim/FloatingMovement.h>
@@ -13,7 +14,7 @@
#include <kurator/universe/TurretType.h>
#include <kurator/universe/UniqueIdentifier.h>
-#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<Marker>().selected = true;
+ entity.get<Selectable>().active = true;
auto* registry = entity.registry();
auto turrets = registry->view<sim::TurretControl>();
entity.emplace<AIVisuals>();
@@ -95,8 +95,8 @@ InspectionWindow::deselect()
for (auto&& entity_ : selected) {
if (!entity_.valid())
continue;
- if (entity_.all_of<Marker>())
- entity_.get<Marker>().selected = false;
+ if (entity_.all_of<Selectable>())
+ entity_.get<Selectable>().active = false;
if (entity_.all_of<TurretVisuals>())
entity_.remove<TurretVisuals>();
if (entity_.all_of<AIVisuals>())
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<Marker, sim::Transform>();
- 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<Marker>(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<Selectable>(entity)) {
+ const auto& select = ctx.registry.get<Selectable>(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;
};