using System;
using Chernobyl.StateMachine;
namespace Chernobyl
{
///
/// Used to represent a type whose functionality can be enabled/disabled.
///
public interface IEnableable
{
///
/// Active if the functionality of the object is enabled, inactive if
/// otherwise.
///
IState Enabled { get; }
}
///
/// A class that holds extension methods for types.
///
public static class EnableableExtensions
{
///
/// When invoked this method will enable the
/// instance.
///
/// The instance that is to be enabled.
public static void Enable(this IEnableable enableable)
{
enableable.Enabled.ParentState = new State();
}
///
/// An event handler that can be added to an event. This method performs
/// the same operation as when invoked.
///
/// The instance that is to be enabled.
/// The instance that generated the event.
/// The instance containing the
/// event data.
public static void Enable(this IEnableable enableable, object sender, EventArgs e)
{
enableable.Enable();
}
///
/// When invoked this method will disable the
/// instance.
///
/// The instance that is to be disabled.
public static void Disable(this IEnableable enableable)
{
enableable.Enabled.ParentState = null;
}
///
/// An event handler that can be added to an event. This method performs
/// the same operation as when invoked.
///
/// The instance that is to be disabled.
/// The instance that generated the event.
/// The instance containing the
/// event data.
public static void Disable(this IEnableable enableable, object sender, EventArgs e)
{
enableable.Disable();
}
///
/// When invoked this method will disable the
/// instance if it is enabled and enable it if it is disabled.
///
/// The instance that is to be disabled or
/// enabled.
public static void Toggle(this IEnableable enableable)
{
if (enableable.Enabled.IsActive())
enableable.Disable();
else
enableable.Enable();
}
///
/// An event handler that can be added to an event. This method performs
/// the same operation as when invoked.
///
/// The instance that is to be disabled or
/// enabled.
/// The instance that generated the event.
/// The instance containing the
/// event data.
public static void Toggle(this IEnableable enableable, object sender, EventArgs e)
{
enableable.Toggle();
}
}
}