Add the ability to list, enable, and disable events based on a numerical identifier (closes #1992)

This commit is contained in:
Alexander Bock
2022-04-12 22:52:24 +02:00
parent 02b6334399
commit 04df80a4a2
4 changed files with 155 additions and 7 deletions

View File

@@ -56,7 +56,7 @@ struct Event {
// return a dictionary with these parameters. This dictionary is passed to actions
// if they are triggered by events
// 6. Add the new enum entry into the `toString` and `fromString` methods
enum class Type {
enum class Type : uint8_t {
SceneGraphNodeAdded,
SceneGraphNodeRemoved,
ParallelConnection,

View File

@@ -36,6 +36,14 @@ namespace events { struct Event; }
class EventEngine {
public:
struct ActionInfo {
events::Event::Type type;
uint32_t id = std::numeric_limits<uint32_t>::max();
bool isEnabled = true;
std::string action;
std::optional<ghoul::Dictionary> filter;
};
/**
* This function returns the first event stored in the EventEngine, or \c nullptr if
* no event exists. To navigate the full list of events, you can access the returned
@@ -90,6 +98,36 @@ public:
const std::string& identifier,
std::optional<ghoul::Dictionary> filter = std::nullopt);
/**
* Removing registration for a specific event identified by the \p identifier.
*
* \param identifier The unique identifier of the event that should be removed.
*/
void unregisterEventAction(uint32_t identifier);
/**
* Returns the list of all registered actions, sorted by their identifiers.
*
* \return The list of all registered actions
*/
std::vector<ActionInfo> registeredActions() const;
/**
* Enables the event identified by the \p identifier. If the event is already enabled,
* this function does nothing.
*
* \param identifier The identifier of the event that should be enabled
*/
void enableEvent(uint32_t identifier);
/**
* Disables the event identified by the \p identifier. If the event is already
* disabled, this function does nothing.
*
* \param identifier The identifier of the event that should be disabled
*/
void disableEvent(uint32_t identifier);
/**
* Triggers all actions that are registered for events that are in the current event
* queue
@@ -106,12 +144,14 @@ private:
/// The last event in the chain of events stored in the memory pool
events::Event* _lastEvent = nullptr;
struct ActionInfo {
std::string action;
std::optional<ghoul::Dictionary> filter;
};
// The type is duplicated in the ActionInfo as well, but we want it in the ActionInfo
// to be able to return them to a caller and we want it in this unordered_map to make
// the lookup really fast. So having this extra wasted memory is probably worth it
std::unordered_map<events::Event::Type, std::vector<ActionInfo>> _eventActions;
static uint32_t nextRegisteredEventId;
#ifdef _DEBUG
/// Stores the total number of events during this frame for debugging purposes
static uint64_t nEvents;