pkecs 8.0.0

Another ECS implementation.
Documentation
//! Types for triggering and responding to events.

use std::{
    collections::HashMap,
    any::{Any, TypeId},
};
use crate::entity::Entities;

/// An in-memory publisher-subscriber eventing system.
#[derive(Default)]
pub struct Events {
    /// Contains [`EventHandlerChannel`]s keyed by the event they're triggered by.
    channels: HashMap<TypeId, Box<dyn ErasedEventChannel>>,
}

impl Events {
    /// Registers a system which responds to a specific event.
    pub fn register<TEvent, TEventHandler>(&mut self, handler: TEventHandler)
        where
            TEvent: 'static,
            TEventHandler: EventHandler<TEvent> + 'static,
    {
        let id = TypeId::of::<TEvent>();

        self.channels
            .entry(id)
            .or_insert(EventChannel::<TEvent>::new_box())
            .as_any_mut()
            .downcast_mut::<EventChannel<TEvent>>()
            .map(|c| c.register(handler));
    }

    /// Queues an event.
    ///
    /// The event is dropped if there are no registered event handlers for it.
    /// Event handlers will be called at the end of the current execution context.
    pub fn trigger<TEvent>(&self, event: &TEvent, entities: &mut Entities)
        where
            TEvent: 'static,
    {
        let id = TypeId::of::<TEvent>();

        self.channels
            .get(&id)
            .map(|c| c.as_any())
            .and_then(|c| c.downcast_ref::<EventChannel<TEvent>>())
            .map(|c| c.trigger(event, entities));
    }
}

/// A system which responds to an event.
pub trait EventHandler<T> {
    /// Executes the system with an event.
    fn trigger(&self, event: &T, entities: &mut Entities);
}

impl<TFunc, TEvent> EventHandler<TEvent> for TFunc
    where
        TFunc: Fn(&TEvent, &mut Entities),
{
    fn trigger(&self, event: &TEvent, entities: &mut Entities) {
        self(event, entities)
    }
}


/// Contains event handlers of a specific type.
struct EventChannel<T> {
    /// Registered handlers for the event channel.
    handlers: Vec<Box<dyn EventHandler<T>>>,
}

impl<T> EventChannel<T> {
    /// Initializes a new, empty instance of [`self`].
    pub fn new() -> Self {
        let handlers = vec![];

        Self { handlers }
    }

    /// Initializes a new, empty instance of [`self`] in a [`Box`].
    pub fn new_box() -> Box<Self> {
        let this = Self::new();

        Box::new(this)
    }

    /// Adds an [`EventHandler`] to the channel.
    pub fn register<TEventHandler>(&mut self, handler: TEventHandler)
        where
            TEventHandler: EventHandler<T> + 'static,
    {
        let handler = Box::new(handler);

        self.handlers.push(handler);
    }

    /// Queues an event onto the back of [`self`].
    pub fn trigger(&self, event: &T, entities: &mut Entities) {
        self.handlers
            .iter()
            .for_each(|h| h.trigger(event, entities))
    }
}

/// A type-erased wrapper over a channel of typed event handlers.
trait ErasedEventChannel {
    /// Casts [`self`] to [`Any`].
    fn as_any(&self) -> &dyn Any;

    /// Casts [`self`] to [`Any`].
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

impl<T> ErasedEventChannel for EventChannel<T>
    where
        T: 'static,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}