refactor(input): adjust to new ecs
This commit is contained in:
parent
21e7291189
commit
91d86545dc
3 changed files with 46 additions and 27 deletions
|
@ -16,15 +16,16 @@ System::System(Ref<ecs::Registry> registry): m_registry(std::move(registry))
|
|||
|
||||
auto System::tick() -> bool
|
||||
{
|
||||
m_registry->view<surface::SurfaceComponent>().each([&](const entt::entity,
|
||||
surface::SurfaceComponent &surface) {
|
||||
for (auto &[entity, surface] : m_registry->view<surface::SurfaceComponent>())
|
||||
{
|
||||
for (const auto &event : surface.peek_events())
|
||||
{
|
||||
handle_event(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
m_registry->view<InputComponent>().each([&](const entt::entity, InputComponent &input) {
|
||||
for (auto &[entity, input] : m_registry->view<InputComponent>())
|
||||
{
|
||||
// TODO(Light): instead of iterating over all actions each frame,
|
||||
// make a list of "dirty" actions to reset
|
||||
// and a surface_input->input_action mapping to get to action through input
|
||||
|
@ -48,7 +49,7 @@ auto System::tick() -> bool
|
|||
action.state = InputAction::State::inactive;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <ecs/entity.hpp>
|
||||
#include <input/components.hpp>
|
||||
#include <input/system.hpp>
|
||||
#include <ranges>
|
||||
#include <test/test.hpp>
|
||||
|
||||
// NOLINTBEGIN
|
||||
|
@ -27,16 +28,19 @@ public:
|
|||
|
||||
auto add_input_component() -> ecs::Entity
|
||||
{
|
||||
auto entity = m_registry->create_entity("");
|
||||
entity.add_component<InputComponent>();
|
||||
auto entity = m_registry->create_entity();
|
||||
m_registry->add<InputComponent>(entity, {});
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
auto add_surface_component() -> ecs::Entity
|
||||
{
|
||||
auto entity = m_registry->create_entity("");
|
||||
entity.add_component<surface::SurfaceComponent>(surface::SurfaceComponent::CreateInfo {});
|
||||
auto entity = m_registry->create_entity();
|
||||
m_registry->add<surface::SurfaceComponent>(
|
||||
entity,
|
||||
surface::SurfaceComponent::CreateInfo {}
|
||||
);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
@ -66,53 +70,58 @@ Suite raii = [] {
|
|||
Suite system_events = [] {
|
||||
Case { "on_register won't throw" } = [] {
|
||||
auto fixture = Fixture {};
|
||||
auto system = System { fixture.registry() };
|
||||
auto registry = fixture.registry();
|
||||
auto system = System { registry };
|
||||
|
||||
system.on_register();
|
||||
expect_eq(fixture.registry()->view<InputComponent>().size(), 0);
|
||||
expect_eq(registry->view<InputComponent>().get_size(), 0);
|
||||
};
|
||||
|
||||
Case { "on_unregister won't throw" } = [] {
|
||||
auto fixture = Fixture {};
|
||||
auto system = System { fixture.registry() };
|
||||
auto registry = fixture.registry();
|
||||
auto system = System { registry };
|
||||
|
||||
system.on_register();
|
||||
system.on_unregister();
|
||||
expect_eq(fixture.registry()->view<InputComponent>().size(), 0);
|
||||
expect_eq(registry->view<InputComponent>().get_size(), 0);
|
||||
};
|
||||
};
|
||||
|
||||
Suite registry_events = [] {
|
||||
Case { "on_construct<InputComnent>" } = [] {
|
||||
auto fixture = Fixture {};
|
||||
auto system = System { fixture.registry() };
|
||||
auto registry = fixture.registry();
|
||||
auto system = System { registry };
|
||||
|
||||
const auto &entity = fixture.add_input_component();
|
||||
expect_eq(fixture.registry()->view<InputComponent>().size(), 1);
|
||||
expect_eq(registry->view<InputComponent>().get_size(), 1);
|
||||
};
|
||||
|
||||
Case { "on_destrroy<InputComponent>" } = [] {
|
||||
auto fixture = Fixture {};
|
||||
auto system = create_scope<System>(fixture.registry());
|
||||
auto registry = fixture.registry();
|
||||
auto system = create_scope<System>(registry);
|
||||
|
||||
auto entity_a = fixture.add_input_component();
|
||||
auto entity_b = fixture.add_input_component();
|
||||
expect_eq(fixture.registry()->view<InputComponent>().size(), 2);
|
||||
expect_eq(registry->view<InputComponent>().get_size(), 2);
|
||||
|
||||
entity_a.remove_component<InputComponent>();
|
||||
expect_eq(fixture.registry()->view<InputComponent>().size(), 1);
|
||||
registry->remove<InputComponent>(entity_a);
|
||||
expect_eq(registry->view<InputComponent>().get_size(), 1);
|
||||
|
||||
system.reset();
|
||||
expect_eq(fixture.registry()->view<InputComponent>().size(), 1);
|
||||
expect_eq(registry->view<InputComponent>().get_size(), 1);
|
||||
|
||||
entity_b.remove_component<InputComponent>();
|
||||
expect_eq(fixture.registry()->view<InputComponent>().size(), 0);
|
||||
registry->remove<InputComponent>(entity_b);
|
||||
expect_eq(registry->view<InputComponent>().get_size(), 0);
|
||||
};
|
||||
};
|
||||
|
||||
Suite tick = [] {
|
||||
Case { "Empty tick won't throw" } = [] {
|
||||
auto fixture = Fixture {};
|
||||
auto registry = fixture.registry();
|
||||
auto system = System { fixture.registry() };
|
||||
|
||||
expect_false(system.tick());
|
||||
|
@ -120,10 +129,14 @@ Suite tick = [] {
|
|||
|
||||
Case { "Tick triggers input action" } = [] {
|
||||
auto fixture = Fixture {};
|
||||
auto registry = fixture.registry();
|
||||
auto system = System { fixture.registry() };
|
||||
|
||||
auto &surface = fixture.add_surface_component().get_component<surface::SurfaceComponent>();
|
||||
auto &input = fixture.add_input_component().get_component<InputComponent>();
|
||||
auto surface_entity = fixture.add_surface_component();
|
||||
auto &surface = registry->get<surface::SurfaceComponent>(surface_entity);
|
||||
|
||||
auto input_entity = fixture.add_input_component();
|
||||
auto &input = registry->get<InputComponent>(input_entity);
|
||||
|
||||
auto action_key = input.add_action(
|
||||
{
|
||||
|
@ -155,10 +168,15 @@ Suite tick = [] {
|
|||
|
||||
Case { "Tick triggers" } = [] {
|
||||
auto fixture = Fixture {};
|
||||
auto registry = fixture.registry();
|
||||
auto system = System { fixture.registry() };
|
||||
|
||||
auto &surface = fixture.add_surface_component().get_component<surface::SurfaceComponent>();
|
||||
auto &input = fixture.add_input_component().get_component<InputComponent>();
|
||||
auto surface_entity = fixture.add_surface_component();
|
||||
auto &surface = registry->get<surface::SurfaceComponent>(surface_entity);
|
||||
|
||||
auto input_entity = fixture.add_input_component();
|
||||
auto &input = registry->get<InputComponent>(input_entity);
|
||||
|
||||
|
||||
auto action_key = input.add_action(
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <app/system.hpp>
|
||||
#include <ecs/scene.hpp>
|
||||
#include <ecs/registry.hpp>
|
||||
#include <surface/components.hpp>
|
||||
#include <surface/events/keyboard.hpp>
|
||||
#include <surface/events/mouse.hpp>
|
||||
|
|
Loading…
Add table
Reference in a new issue