using System;
namespace Chernobyl.StateMachine
{
///
/// Extension methods for implementations of the
/// interface.
///
public static class StateExtensions
{
///
/// Determines whether the specified has been
/// entered and has been left (i.e. it is an active state).
///
/// The to check to see if it
/// is active.
/// True if has been entered and has
/// not been left.
public static bool IsActive(this IState state)
{
return state.ParentState != null;
}
///
/// Adds the specified to the
/// event of the passed in
/// and also invokes the passed in method if
/// the is currently active.
///
/// The whose
/// will have the
/// added to it.
/// The to add
/// to the evernt of .
public static void CheckedEnteredAdd(this IState state, EventHandler eventHandler)
{
state.Entered += eventHandler;
if (state.IsActive())
eventHandler(state, EventArgs.Empty);
}
///
/// Adds the specified to the
/// event of the passed in
/// and also invokes the passed in method if
/// the is currently inactive.
///
/// The whose
/// will have the
/// added to it.
/// The to add
/// to the evernt of .
public static void CheckedLeftAdd(this IState state, EventHandler eventHandler)
{
state.Left += eventHandler;
if (state.IsActive() == false)
eventHandler(state, EventArgs.Empty);
}
///
/// This method will cause the
/// to immediately become the
/// instance's child (and therefore be
/// considered an active or entered state) when the
/// has become active (i.e.
/// is raised). This method can be used to
/// give an (the ) a
/// default sub-state (the ) when it is made
/// active.
///
/// The that is to have its
/// set to the
/// when it is activated (i.e.
/// is raised).
/// The that is to become the
/// child of when
/// has been activated (i.e.
/// is raised).
public static void EnterWith(this IState parent, IState child)
{
parent.CheckedEnteredAdd((sender, e) => parent.ChildState = child);
}
}
}