-
-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
680de25
commit caddd3a
Showing
12 changed files
with
593 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#pragma once | ||
|
||
#define ECS_EVENT_DESC_ID_COUNT_MAX (8) | ||
|
||
namespace flecs { | ||
|
||
// Event builder interface | ||
struct event_builder { | ||
event_builder(flecs::world_t *world, flecs::entity_t event) | ||
: m_world(world) | ||
, m_desc{} | ||
, m_ids{} | ||
, m_ids_array{} | ||
{ | ||
m_desc.event = event; | ||
} | ||
|
||
/** Add component to trigger on */ | ||
template <typename T> | ||
event_builder& id() { | ||
m_ids.array = m_ids_array; | ||
m_ids.array[m_ids.count] = _::cpp_type<T>().id(m_world); | ||
m_ids.count ++; | ||
return *this; | ||
} | ||
|
||
/** Add (component) id to trigger on */ | ||
event_builder& id(flecs::id_t id) { | ||
m_ids.array = m_ids_array; | ||
m_ids.array[m_ids.count] = id; | ||
m_ids.count ++; | ||
return *this; | ||
} | ||
|
||
/** Set entity for which to trigger */ | ||
event_builder& entity(flecs::entity_t e) { | ||
ecs_record_t *r = ecs_record_find(m_world, e); | ||
|
||
/* can't trigger for empty entity */ | ||
ecs_assert(r != nullptr, ECS_INVALID_PARAMETER, nullptr); | ||
ecs_assert(r->table != nullptr, ECS_INVALID_PARAMETER, nullptr); | ||
|
||
m_desc.table = r->table; | ||
m_desc.offset = ECS_RECORD_TO_ROW(r->row); | ||
m_desc.count = 1; | ||
return *this; | ||
} | ||
|
||
/* Set table for which to trigger */ | ||
event_builder& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) { | ||
m_desc.table = t; | ||
m_desc.offset = offset; | ||
m_desc.count = count; | ||
return *this; | ||
} | ||
|
||
void emit() { | ||
ecs_assert(m_ids.count != 0, ECS_INVALID_PARAMETER, NULL); | ||
ecs_assert(m_desc.table != nullptr, ECS_INVALID_PARAMETER, NULL); | ||
m_ids.array = m_ids_array; | ||
m_desc.ids = &m_ids; | ||
m_desc.observable = m_world; | ||
ecs_emit(m_world, &m_desc); | ||
} | ||
|
||
private: | ||
flecs::world_t *m_world; | ||
ecs_event_desc_t m_desc; | ||
flecs::ids_t m_ids; | ||
flecs::id_t m_ids_array[ECS_EVENT_DESC_ID_COUNT_MAX]; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "builder.hpp" | ||
|
||
namespace flecs { | ||
|
||
struct event_builder; | ||
|
||
template<typename T> | ||
struct event_m : mixin<T> { }; | ||
|
||
/** Event mixin for flecs::world. */ | ||
template<> | ||
struct event_m<flecs::world> : mixin<flecs::world> { | ||
void init() { }; | ||
|
||
/** Create a new event. | ||
* | ||
* @return Event builder. | ||
*/ | ||
flecs::event_builder event(flecs::entity_t event) const; | ||
|
||
/** Create a new event. | ||
* | ||
* @return Event builder. | ||
*/ | ||
template <typename E> | ||
flecs::event_builder event() const; | ||
}; | ||
|
||
using event_m_world = event_m<flecs::world>; | ||
|
||
} // namespace flecs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include "builder.hpp" | ||
|
||
namespace flecs | ||
{ | ||
|
||
// Mixin implementation | ||
|
||
inline flecs::event_builder event_m_world::event(flecs::entity_t evt) const { | ||
return flecs::event_builder(this->me(), evt); | ||
} | ||
|
||
template <typename E> | ||
inline flecs::event_builder event_m_world::event() const { | ||
return flecs::event_builder(this->me(), _::cpp_type<E>().id(this->me())); | ||
} | ||
|
||
} // namespace flecs |
Oops, something went wrong.