namespace OM
{
///
/// Represents the current play state.
///
public enum OM_PlayState
{
Playing = 0,
Paused = 1,
Stopped = 2,
}
///
/// A controller for managing and switching between play states.
///
public class OM_PlayStateController
{
///
/// Invoked whenever the play state changes.
/// First param is the new state, second is the old state.
///
public event System.Action OnPlayStateChanged;
///
/// Gets the current play state.
///
public OM_PlayState State { get; private set; } = OM_PlayState.Stopped;
///
/// Returns true if the current state is Playing.
///
public bool IsPlaying()
{
return State == OM_PlayState.Playing;
}
///
/// Returns true if the current state is Paused.
///
public bool IsPaused()
{
return State == OM_PlayState.Paused;
}
///
/// Sets the state to Playing if it's not already playing.
///
public void Play()
{
if (IsPlaying()) return;
SetState(OM_PlayState.Playing);
}
///
/// Sets the state to Paused.
///
public void Pause()
{
SetState(OM_PlayState.Paused);
}
///
/// Sets the state to Stopped.
///
public void Stop()
{
SetState(OM_PlayState.Stopped);
}
///
/// Changes the state and invokes the state changed event if the state has changed.
///
/// The new state to apply.
private void SetState(OM_PlayState newState)
{
if (State == newState) return;
var oldState = State;
State = newState;
OnPlayStateChanged?.Invoke(newState, oldState);
}
}
}