refactor(surface): adjusted to new changes

This commit is contained in:
light7734 2025-09-22 18:52:22 +03:30
parent e77a42cf7f
commit d66ef55cc8
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
3 changed files with 45 additions and 19 deletions

View file

@ -44,13 +44,13 @@ System::System(Ref<ecs::Registry> registry): m_registry(std::move(registry))
);
m_registry->connect_on_construct<SurfaceComponent>(
[this](ecs::Registry &registry, ecs::Entity entity) {
[this](ecs::Registry &registry, ecs::EntityId entity) {
on_surface_construct(registry, entity);
}
);
m_registry->connect_on_destruct<SurfaceComponent>(
[this](ecs::Registry &registry, ecs::Entity entity) {
[this](ecs::Registry &registry, ecs::EntityId entity) {
on_surface_destruct(registry, entity);
}
);
@ -58,10 +58,15 @@ System::System(Ref<ecs::Registry> registry): m_registry(std::move(registry))
System::~System()
{
if (!m_registry)
{
return;
}
try
{
// TODO(Light): make registry.remove not validate iterators
auto entities_to_remove = std::vector<ecs::Entity> {};
auto entities_to_remove = std::vector<ecs::EntityId> {};
for (auto &[entity, surface] : m_registry->view<SurfaceComponent>())
{
entities_to_remove.emplace_back(entity);
@ -90,7 +95,7 @@ void System::on_unregister()
{
}
void System::on_surface_construct(ecs::Registry &registry, ecs::Entity entity)
void System::on_surface_construct(ecs::Registry &registry, ecs::EntityId entity)
{
try
{
@ -182,7 +187,7 @@ void System::on_surface_construct(ecs::Registry &registry, ecs::Entity entity)
}
}
void System::on_surface_destruct(ecs::Registry &registry, ecs::Entity entity)
void System::on_surface_destruct(ecs::Registry &registry, ecs::EntityId entity)
{
const auto &[display, window, _] = registry.get<SurfaceComponent>(entity).get_native_data();
if (!display)
@ -355,7 +360,7 @@ void System::modify_visiblity(SurfaceComponent &surface, const ModifyVisibilityR
}
}
auto System::tick() -> bool
void System::tick(app::TickInfo tick)
{
for (auto &dense : m_registry->view<SurfaceComponent>())
{
@ -364,7 +369,12 @@ auto System::tick() -> bool
handle_events(surface);
}
return false;
const auto now = std::chrono::steady_clock::now();
m_last_tick_result = app::TickResult {
.info = tick,
.duration = now - tick.start_time,
.end_time = now,
};
}
void ensure_component_sanity(const SurfaceComponent &component)

View file

@ -16,6 +16,15 @@ using test::expect_not_nullptr;
using test::expect_throw;
using test::Suite;
[[nodiscard]] auto tick_info() -> app::TickInfo
{
return {
.delta_time = std::chrono::milliseconds { 16 },
.budget = std::chrono::milliseconds { 10 },
.start_time = std::chrono::steady_clock::now(),
};
}
constexpr auto title = "TestWindow";
constexpr auto width = 800u;
constexpr auto height = 600u;
@ -188,7 +197,7 @@ Suite registry_events = [] {
Suite tick = [] {
Case { "ticking on empty registry won't throw" } = [] {
auto fixture = Fixture {};
System { fixture.registry() }.tick();
System { fixture.registry() }.tick(tick_info());
};
Case { "ticking on non-empty registry won't throw" } = [] {
@ -196,7 +205,7 @@ Suite tick = [] {
auto system = System { fixture.registry() };
fixture.add_surface_component();
system.tick();
system.tick(tick_info());
};
};
@ -207,7 +216,7 @@ Suite tick_handles_events = [] {
auto &surface = fixture.add_surface_component();
// flush window-creation events
system.tick();
system.tick(tick_info());
expect_eq(surface.peek_events().size(), 0);
surface.push_event(surface::MovedEvent({}, {}));
@ -216,7 +225,7 @@ Suite tick_handles_events = [] {
surface.push_event(surface::ButtonPressedEvent({}));
expect_eq(surface.peek_events().size(), 2);
system.tick();
system.tick(tick_info());
expect_eq(surface.peek_events().size(), 0);
};
};
@ -235,7 +244,7 @@ Suite tick_handles_requests = [] {
surface.push_request(surface::ModifyVisibilityRequest(true));
expect_eq(surface.peek_requests().size(), 1);
system.tick();
system.tick(tick_info());
expect_eq(surface.peek_requests().size(), 0);
surface.push_request(surface::ModifyTitleRequest(title));
@ -250,7 +259,7 @@ Suite tick_handles_requests = [] {
surface.push_request(surface::ModifyVisibilityRequest(false));
expect_eq(surface.peek_requests().size(), 1 + 2 + 3);
system.tick();
system.tick(tick_info());
expect_eq(surface.peek_requests().size(), 0);
expect_eq(surface.get_title(), title);

View file

@ -25,12 +25,17 @@ public:
void on_unregister() override;
auto tick() -> bool override;
void tick(app::TickInfo tick) override;
[[nodiscard]] auto get_last_tick_result() const -> const app::TickResult & override
{
return m_last_tick_result;
}
private:
void on_surface_construct(ecs::Registry &registry, ecs::Entity entity);
void on_surface_construct(ecs::Registry &registry, ecs::EntityId entity);
void on_surface_destruct(ecs::Registry &registry, ecs::Entity entity);
void on_surface_destruct(ecs::Registry &registry, ecs::EntityId entity);
void handle_requests(struct SurfaceComponent &surface);
@ -53,13 +58,15 @@ private:
const struct ModifyVisibilityRequest &request
);
void modify_position(ecs::Entity surface_entity, const math::ivec2 &new_size);
void modify_position(ecs::EntityId surface_entity, const math::ivec2 &new_size);
void modify_position(ecs::Entity surface_entity, const math::uvec2 &new_size);
void modify_position(ecs::EntityId surface_entity, const math::uvec2 &new_size);
void set_visibility(ecs::Entity surface_entity, bool visible);
void set_visibility(ecs::EntityId surface_entity, bool visible);
Ref<ecs::Registry> m_registry;
app::TickResult m_last_tick_result;
};
} // namespace lt::surface