|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
-
- using Svelto.ECS;
-
- namespace GamecraftModdingAPI.Events
- {
- [Obsolete]
- public class HandlerBuilder
- {
- private string name;
-
- private int? type;
-
- private Action<EntitiesDB> activated;
-
- private Action<EntitiesDB> destroyed;
-
- /// <summary>
- /// Create a new event handler builder.
- /// </summary>
- public HandlerBuilder()
- {
- }
-
- /// <summary>
- /// Create a new event handler builder.
- /// This is equivalent to new <code>HandlerBuilder().Name(name)</code>
- /// </summary>
- /// <param name="name">The handler name.</param>
- public HandlerBuilder(string name)
- {
- this.name = name;
- }
-
- /// <summary>
- /// Create and return an event handler builder.
- /// </summary>
- /// <returns>The builder.</returns>
- public static HandlerBuilder Builder()
- {
- return new HandlerBuilder();
- }
-
- /// <summary>
- /// Create and return an event handler builder.
- /// This is equivalent to <code>Builder().Name(name)</code>
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="name">The handler name.</param>
- public static HandlerBuilder Builder(string name)
- {
- return new HandlerBuilder(name);
- }
-
- /// <summary>
- /// Name the event handler.
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="name">The event handler name.</param>
- public HandlerBuilder Name(string name)
- {
- this.name = name;
- return this;
- }
-
- /// <summary>
- /// Set the action to perform on when the activated event occurs.
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="action">The activated event action.</param>
- public HandlerBuilder OnActivation(Action action)
- {
- return OnActivation((_) => { action(); });
- }
-
- /// <summary>
- /// Set the action to perform on when the activated event occurs.
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="action">The activated event action.</param>
- public HandlerBuilder OnActivation(Action<EntitiesDB> action)
- {
- this.activated = action;
- return this;
- }
-
- /// <summary>
- /// Set the action to perform when the destroyed event occurs.
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="action">The destroyed event action.</param>
- public HandlerBuilder OnDestruction(Action action)
- {
- return OnDestruction((_) => { action(); });
- }
-
- /// <summary>
- /// Set the action to perform when the destroyed event occurs.
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="action">The destroyed event action.</param>
- public HandlerBuilder OnDestruction(Action<EntitiesDB> action)
- {
- this.destroyed = action;
- return this;
- }
-
- /// <summary>
- /// Set the type of event to handle.
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="eventType">The event type.</param>
- public HandlerBuilder Handle(EventType eventType)
- {
- return Handle((int)eventType);
- }
-
- /// <summary>
- /// Set the type of event to handle.
- /// </summary>
- /// <returns>The builder.</returns>
- /// <param name="eventType">The event type.</param>
- public HandlerBuilder Handle(int eventType)
- {
- this.type = eventType;
- return this;
- }
-
- /// <summary>
- /// Build the event handler.
- /// </summary>
- /// <returns>The event handler.</returns>
- /// <param name="register">Automatically register the event handler with EventManager.AddEventHandler().</param>
- 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<EntitiesDB> validActivated = activated;
- if (validActivated == null)
- {
- validActivated = (_) => { };
- }
- Action<EntitiesDB> validDestroyed = destroyed;
- if (validDestroyed == null)
- {
- validDestroyed = (_) => { };
- }
- SimpleEventHandlerEngine result = new SimpleEventHandlerEngine(validActivated, validDestroyed, type.Value, name);
- if (register)
- {
- EventManager.AddEventHandler(result);
- }
- return result;
- }
- }
- }
|