Skip to content

Commit

Permalink
Emit OnSet events after setting Poly components
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jun 30, 2022
1 parent 916b00c commit a3736c3
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 39 deletions.
24 changes: 24 additions & 0 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,14 @@ EcsPoly* _ecs_poly_bind(
#define ecs_poly_bind(world, entity, T) \
_ecs_poly_bind(world, entity, T##_tag)

void _ecs_poly_modified(
ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag);

#define ecs_poly_modified(world, entity, T) \
_ecs_poly_modified(world, entity, T##_tag)

/* Get poly component for an entity */
const EcsPoly* _ecs_poly_bind_get(
const ecs_world_t *world,
Expand Down Expand Up @@ -4935,6 +4943,14 @@ EcsPoly* _ecs_poly_bind(
return result;
}

void _ecs_poly_modified(
ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag)
{
ecs_modified_pair(world, entity, ecs_id(EcsPoly), tag);
}

const EcsPoly* _ecs_poly_bind_get(
const ecs_world_t *world,
ecs_entity_t entity,
Expand Down Expand Up @@ -29198,6 +29214,8 @@ ecs_entity_t ecs_system_init(
}
}

ecs_poly_modified(world, entity, ecs_system_t);

return entity;
error:
return 0;
Expand Down Expand Up @@ -40683,6 +40701,8 @@ ecs_entity_t ecs_observer_init(
}
}

ecs_poly_modified(world, entity, ecs_observer_t);

return entity;
error:
if (entity) {
Expand Down Expand Up @@ -43327,6 +43347,8 @@ ecs_query_t* ecs_query_init(
ecs_add_id(world, entity, EcsEmpty);
}

ecs_poly_modified(world, entity, ecs_query_t);

ecs_log_pop_1();

return result;
Expand Down Expand Up @@ -47395,6 +47417,8 @@ ecs_entity_t ecs_trigger_init(
}
}

ecs_poly_modified(world, entity, ecs_trigger_t);

return entity;
error:
ecs_delete(world, entity);
Expand Down
2 changes: 2 additions & 0 deletions src/addons/system/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ ecs_entity_t ecs_system_init(
}
}

ecs_poly_modified(world, entity, ecs_system_t);

return entity;
error:
return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ ecs_entity_t ecs_observer_init(
}
}

ecs_poly_modified(world, entity, ecs_observer_t);

return entity;
error:
if (entity) {
Expand Down
8 changes: 8 additions & 0 deletions src/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ EcsPoly* _ecs_poly_bind(
return result;
}

void _ecs_poly_modified(
ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag)
{
ecs_modified_pair(world, entity, ecs_id(EcsPoly), tag);
}

const EcsPoly* _ecs_poly_bind_get(
const ecs_world_t *world,
ecs_entity_t entity,
Expand Down
8 changes: 8 additions & 0 deletions src/poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ EcsPoly* _ecs_poly_bind(
#define ecs_poly_bind(world, entity, T) \
_ecs_poly_bind(world, entity, T##_tag)

void _ecs_poly_modified(
ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t tag);

#define ecs_poly_modified(world, entity, T) \
_ecs_poly_modified(world, entity, T##_tag)

/* Get poly component for an entity */
const EcsPoly* _ecs_poly_bind_get(
const ecs_world_t *world,
Expand Down
2 changes: 2 additions & 0 deletions src/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,8 @@ ecs_query_t* ecs_query_init(
ecs_add_id(world, entity, EcsEmpty);
}

ecs_poly_modified(world, entity, ecs_query_t);

ecs_log_pop_1();

return result;
Expand Down
2 changes: 2 additions & 0 deletions src/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,8 @@ ecs_entity_t ecs_trigger_init(
}
}

ecs_poly_modified(world, entity, ecs_trigger_t);

return entity;
error:
ecs_delete(world, entity);
Expand Down
8 changes: 6 additions & 2 deletions test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@
"create_1m_set_two_components"
]
}, {
"id": "MixinIterable",
"id": "Poly",
"testcases": [
"iter_query",
"iter_query_w_filter",
Expand All @@ -2076,7 +2076,11 @@
"iter_rule",
"iter_rule_w_filter",
"iter_filter",
"iter_filter_w_filter"
"iter_filter_w_filter",
"on_set_poly_trigger",
"on_set_poly_observer",
"on_set_poly_query",
"on_set_poly_system"
]
}, {
"id": "Internals",
Expand Down
156 changes: 140 additions & 16 deletions test/api/src/MixinIterable.c → test/api/src/Poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void test_w_chain(
test_assert(!ecs_iter_next(it));
}

void MixinIterable_iter_query() {
ecs_world_t *world = ecs_init();
void Poly_iter_query() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand All @@ -110,8 +110,8 @@ void MixinIterable_iter_query() {
ecs_fini(world);
}

void MixinIterable_iter_query_w_filter() {
ecs_world_t *world = ecs_init();
void Poly_iter_query_w_filter() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand All @@ -124,8 +124,8 @@ void MixinIterable_iter_query_w_filter() {
ecs_fini(world);
}

void MixinIterable_iter_world() {
ecs_world_t *world = ecs_init();
void Poly_iter_world() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand Down Expand Up @@ -168,8 +168,8 @@ void MixinIterable_iter_world() {
ecs_fini(world);
}

void MixinIterable_iter_world_w_filter() {
ecs_world_t *world = ecs_init();
void Poly_iter_world_w_filter() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand All @@ -179,8 +179,8 @@ void MixinIterable_iter_world_w_filter() {
ecs_fini(world);
}

void MixinIterable_iter_rule() {
ecs_world_t *world = ecs_init();
void Poly_iter_rule() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand All @@ -195,8 +195,8 @@ void MixinIterable_iter_rule() {
ecs_fini(world);
}

void MixinIterable_iter_rule_w_filter() {
ecs_world_t *world = ecs_init();
void Poly_iter_rule_w_filter() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand All @@ -211,8 +211,8 @@ void MixinIterable_iter_rule_w_filter() {
ecs_fini(world);
}

void MixinIterable_iter_filter() {
ecs_world_t *world = ecs_init();
void Poly_iter_filter() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand All @@ -229,8 +229,8 @@ void MixinIterable_iter_filter() {
ecs_fini(world);
}

void MixinIterable_iter_filter_w_filter() {
ecs_world_t *world = ecs_init();
void Poly_iter_filter_w_filter() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_TAG_DEFINE(world, Tag);
Expand All @@ -246,3 +246,127 @@ void MixinIterable_iter_filter_w_filter() {

ecs_fini(world);
}

static
void FooTrigger(ecs_iter_t *it) {}

static
void PolyTrigger(ecs_iter_t *it) {
probe_system_w_ctx(it, it->ctx);

EcsPoly *poly = ecs_term(it, EcsPoly, 1);

test_int(1, it->count);
test_assert(poly->poly != NULL);
}

void Poly_on_set_poly_trigger() {
ecs_world_t *world = ecs_mini();

ecs_entity_t tag = ecs_new_id(world);

Probe ctx = {0};
ecs_entity_t t = ecs_trigger_init(world, &(ecs_trigger_desc_t) {
.term = { ecs_pair(ecs_id(EcsPoly), EcsTrigger) },
.events = { EcsOnSet },
.callback = PolyTrigger,
.ctx = &ctx
});

test_int(1, ctx.invoked); /* Triggered on self */
test_int(1, ctx.count);
test_uint(t, ctx.e[0]);
ecs_os_zeromem(&ctx);

t = ecs_trigger_init(world, &(ecs_trigger_desc_t) {
.term = { tag },
.events = { EcsOnAdd },
.callback = FooTrigger
});

test_int(1, ctx.invoked);
test_int(1, ctx.count);
test_uint(t, ctx.e[0]);

ecs_fini(world);
}

void Poly_on_set_poly_observer() {
ecs_world_t *world = ecs_mini();

ecs_entity_t tag = ecs_new_id(world);

Probe ctx = {0};
ecs_trigger_init(world, &(ecs_trigger_desc_t) {
.term = { ecs_pair(ecs_id(EcsPoly), EcsObserver) },
.events = { EcsOnSet },
.callback = PolyTrigger,
.ctx = &ctx
});

test_int(0, ctx.invoked);

ecs_entity_t t = ecs_observer_init(world, &(ecs_observer_desc_t) {
.filter.terms = {{ tag }},
.events = { EcsOnAdd },
.callback = FooTrigger
});

test_int(1, ctx.invoked);
test_int(1, ctx.count);
test_uint(t, ctx.e[0]);

ecs_fini(world);
}

void Poly_on_set_poly_query() {
ecs_world_t *world = ecs_mini();

ecs_entity_t tag = ecs_new_id(world);

Probe ctx = {0};
ecs_trigger_init(world, &(ecs_trigger_desc_t) {
.term = { ecs_pair(ecs_id(EcsPoly), EcsObserver) },
.events = { EcsOnSet },
.callback = PolyTrigger,
.ctx = &ctx
});

test_int(0, ctx.invoked);

ecs_query_init(world, &(ecs_query_desc_t) {
.filter.terms = {{ tag }},
});

test_int(1, ctx.invoked);
test_int(1, ctx.count);

ecs_fini(world);
}

void Poly_on_set_poly_system() {
ecs_world_t *world = ecs_mini();

ecs_entity_t tag = ecs_new_id(world);

Probe ctx = {0};
ecs_trigger_init(world, &(ecs_trigger_desc_t) {
.term = { ecs_pair(ecs_id(EcsPoly), EcsObserver) },
.events = { EcsOnSet },
.callback = PolyTrigger,
.ctx = &ctx
});

test_int(0, ctx.invoked);

ecs_entity_t s = ecs_system_init(world, &(ecs_system_desc_t) {
.query.filter.terms = {{ tag }},
.callback = FooTrigger
});

test_int(1, ctx.invoked);
test_int(1, ctx.count);
test_uint(s, ctx.e[0]);

ecs_fini(world);
}
3 changes: 3 additions & 0 deletions test/api/src/Trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,9 @@ void Trigger_on_set_pair_wildcard() {
.ctx = &ctx
});

test_int(ctx.invoked, 1); /* Triggers for self */
ecs_os_zeromem(&ctx);

ecs_entity_t e = ecs_entity_init(world, &(ecs_entity_desc_t){
.add = {ecs_pair(ecs_id(Position), Obj)}
});
Expand Down
Loading

0 comments on commit a3736c3

Please sign in to comment.