using System; using Svelto.ECS; namespace GamecraftModdingAPI.Events { [Obsolete] public class HandlerBuilder { private string name; private int? type; private Action activated; private Action destroyed; /// /// Create a new event handler builder. /// public HandlerBuilder() { } /// /// Create a new event handler builder. /// This is equivalent to new HandlerBuilder().Name(name) /// /// The handler name. public HandlerBuilder(string name) { this.name = name; } /// /// Create and return an event handler builder. /// /// The builder. public static HandlerBuilder Builder() { return new HandlerBuilder(); } /// /// Create and return an event handler builder. /// This is equivalent to Builder().Name(name) /// /// The builder. /// The handler name. public static HandlerBuilder Builder(string name) { return new HandlerBuilder(name); } /// /// Name the event handler. /// /// The builder. /// The event handler name. public HandlerBuilder Name(string name) { this.name = name; return this; } /// /// Set the action to perform on when the activated event occurs. /// /// The builder. /// The activated event action. public HandlerBuilder OnActivation(Action action) { return OnActivation((_) => { action(); }); } /// /// Set the action to perform on when the activated event occurs. /// /// The builder. /// The activated event action. public HandlerBuilder OnActivation(Action action) { this.activated = action; return this; } /// /// Set the action to perform when the destroyed event occurs. /// /// The builder. /// The destroyed event action. public HandlerBuilder OnDestruction(Action action) { return OnDestruction((_) => { action(); }); } /// /// Set the action to perform when the destroyed event occurs. /// /// The builder. /// The destroyed event action. public HandlerBuilder OnDestruction(Action action) { this.destroyed = action; return this; } /// /// Set the type of event to handle. /// /// The builder. /// The event type. public HandlerBuilder Handle(EventType eventType) { return Handle((int)eventType); } /// /// Set the type of event to handle. /// /// The builder. /// The event type. public HandlerBuilder Handle(int eventType) { this.type = eventType; return this; } /// /// Build the event handler. /// /// The event handler. /// Automatically register the event handler with EventManager.AddEventHandler(). public IEventHandlerEngine Build(bool register = true) { if (string.IsNullOrWhiteSpace(name)) { throw new EventParameterMissingException("Event handler name must be defined before Build() is called"); } if (activated == null && destroyed == null) { throw new EventParameterMissingException("Event handler destruction or activated event action must be defined before Build() is called"); } if (!type.HasValue) { throw new EventParameterMissingException("Event handler event type must be defined before Build() is called"); } Action validActivated = activated; if (validActivated == null) { validActivated = (_) => { }; } Action validDestroyed = destroyed; if (validDestroyed == null) { validDestroyed = (_) => { }; } SimpleEventHandlerEngine result = new SimpleEventHandlerEngine(validActivated, validDestroyed, type.Value, name); if (register) { EventManager.AddEventHandler(result); } return result; } } }