Skip to content

Commit

Permalink
feat(Action): add option to toggle emit events
Browse files Browse the repository at this point in the history
The EmitEvents property can be unchecked to prevent any action events
from being emitted when action values are changed.

There are also two new methods `ResetToInitialValue` and
`ResetToDefaultValue` that will reset an Action value to its default
or inital value without calling any events either.
  • Loading branch information
thestonefox committed Jul 10, 2023
1 parent 53952e2 commit d5dd0bc
Show file tree
Hide file tree
Showing 3 changed files with 426 additions and 44 deletions.
74 changes: 66 additions & 8 deletions Runtime/Action/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ public abstract class Action : MonoBehaviour
[Serializable]
public class BooleanUnityEvent : UnityEvent<bool> { }

[Tooltip("Whether to emit the action events.")]
[SerializeField]
private bool emitEvents = true;
/// <summary>
/// Whether to emit the action events.
/// </summary>
public bool EmitEvents
{
get
{
return emitEvents;
}
set
{
emitEvents = value;
}
}
/// <summary>
/// Emitted when <see cref="IsActivated"/> changes.
/// </summary>
Expand All @@ -41,7 +58,10 @@ protected set
}

isActivated = value;
ActivationStateChanged?.Invoke(value);
if (emitEvents)
{
ActivationStateChanged?.Invoke(value);
}
}
}

Expand Down Expand Up @@ -178,7 +198,7 @@ protected List<TSelf> Sources
/// </summary>
public TEvent Deactivated = new TEvent();

[Tooltip("Actions to subscribe to when this action is Behaviour.enabled. Allows chaining the source actions to this action.")]
[Tooltip("The value of the action.")]
[SerializeField]
private TValue value;
/// <summary>
Expand Down Expand Up @@ -257,13 +277,19 @@ public override void EmitActivationState()

if (IsActivated)
{
Activated?.Invoke(Value);
ValueChanged?.Invoke(Value);
if (EmitEvents)
{
Activated?.Invoke(Value);
ValueChanged?.Invoke(Value);
}
}
else
{
ValueChanged?.Invoke(Value);
Deactivated?.Invoke(Value);
if (EmitEvents)
{
ValueChanged?.Invoke(Value);
Deactivated?.Invoke(Value);
}
}
}

Expand Down Expand Up @@ -302,13 +328,32 @@ public virtual void Receive(TValue value)

if (IsValueEqual(value))
{
ValueUnchanged?.Invoke(Value);
if (EmitEvents)
{
ValueUnchanged?.Invoke(Value);
}
return;
}

ProcessValue(value);
}

/// <summary>
/// Resets the action to the initial value.
/// </summary>
public virtual void ResetToInitialValue()
{
ResetToValue(InitialValue);
}

/// <summary>
/// Resets the action to the default value.
/// </summary>
public virtual void ResetToDefaultValue()
{
ResetToValue(DefaultValue);
}

protected virtual void Awake()
{
Value = DefaultValue;
Expand All @@ -333,6 +378,16 @@ protected virtual void OnDisable()
UnsubscribeFromSources();
}

/// <summary>
/// Resets the action to the given value.
/// </summary>
/// <param name="value">The value to reset to.</param>
protected virtual void ResetToValue(TValue value)
{
Value = value;
IsActivated = ShouldActivate(value);
}

/// <summary>
/// Subscribes the current action as a listener to the given action.
/// </summary>
Expand Down Expand Up @@ -409,7 +464,10 @@ protected virtual void ProcessValue(TValue value)
}
else
{
ValueChanged?.Invoke(Value);
if (EmitEvents)
{
ValueChanged?.Invoke(Value);
}
}
}

Expand Down
Loading

0 comments on commit d5dd0bc

Please sign in to comment.