Skip to content

Add IActionable.HasAction() method #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Base/IActionable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IActionable<TEvent>
{
void OnAction(TEvent trigger);
void OnAction<TData>(TEvent trigger, TData data);
bool HasAction(TEvent trigger);
}

/// <inheritdoc />
Expand Down
6 changes: 6 additions & 0 deletions src/StateMachine/HybridStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public override void OnAction<TData>(TEvent trigger, TData data)
base.OnAction<TData>(trigger, data);
}

public override bool HasAction(TEvent trigger)
{
return (actionStorage?.HasAction(trigger) ?? false)
|| base.HasAction(trigger);
}

/// <summary>
/// Adds an action that can be called with <c>OnAction()</c>. Actions are like the builtin events
/// <c>OnEnter</c> / <c>OnLogic</c> / ... but are defined by the user.
Expand Down
11 changes: 11 additions & 0 deletions src/StateMachine/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,17 @@ public virtual void OnAction<TData>(TEvent trigger, TData data)
(activeState as IActionable<TEvent>)?.OnAction<TData>(trigger, data);
}

/// <summary>
/// Checks if currently active state has this action.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <returns></returns>
public virtual bool HasAction(TEvent trigger)
{
EnsureIsInitializedFor("Running HasAction of the active state");
return (activeState as IActionable<TEvent>)?.HasAction(trigger) ?? false;
}

public StateBase<TStateId> GetState(TStateId name)
{
StateBundle bundle;
Expand Down
8 changes: 8 additions & 0 deletions src/States/ActionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public void OnAction(TEvent trigger)
/// <typeparam name="TData">Type of the data parameter.</typeparam>
public void OnAction<TData>(TEvent trigger, TData data)
=> actionStorage?.RunAction<TData>(trigger, data);

/// <summary>
/// Checks if this action is defined / has been added.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <returns></returns>
public bool HasAction(TEvent trigger)
=> actionStorage?.HasAction(trigger) ?? false;
}

/// <inheritdoc />
Expand Down
8 changes: 8 additions & 0 deletions src/States/ActionStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,13 @@ public void RunAction(TEvent trigger)
/// <typeparam name="TData">Type of the data parameter.</typeparam>
public void RunAction<TData>(TEvent trigger, TData data)
=> TryGetAndCastAction<Action<TData>>(trigger)?.Invoke(data);

/// <summary>
/// Checks if this action is defined / has been added.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <returns></returns>
public bool HasAction(TEvent trigger)
=> actionsByEvent.ContainsKey(trigger);
}
}
5 changes: 5 additions & 0 deletions src/States/DecoratedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public void OnAction<TData>(TEvent trigger, TData data)
(state as IActionable<TEvent>)?.OnAction<TData>(trigger, data);
}

public bool HasAction(TEvent trigger)
{
return (state as IActionable<TEvent>)?.HasAction(trigger) ?? false;
}

public override string GetActiveHierarchyPath()
{
return state.GetActiveHierarchyPath();
Expand Down
10 changes: 10 additions & 0 deletions src/States/ParallelStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ public void OnAction<TData>(TEvent trigger, TData data)
}
}

public bool HasAction(TEvent trigger)
{
foreach (var state in states)
{
if ((state as IActionable<TEvent>)?.HasAction(trigger) ?? false)
return true;
}
return false;
}

public void StateCanExit()
{
// Try to exit as soon as any one of the child states can exit, unless the exit behaviour
Expand Down