mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-02 18:17:09 +02:00
62 lines
1.5 KiB
C#
62 lines
1.5 KiB
C#
using Robust.Client.Interfaces.State;
|
|
using Robust.Shared.Log;
|
|
using System;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Robust.Client.State
|
|
{
|
|
internal sealed class StateManager : IStateManager
|
|
{
|
|
#pragma warning disable 649
|
|
[Dependency] private readonly IDynamicTypeFactory _typeFactory;
|
|
#pragma warning restore 649
|
|
|
|
public event Action<StateChangedEventArgs> OnStateChanged;
|
|
public State CurrentState { get; private set; }
|
|
|
|
public void Update(FrameEventArgs e)
|
|
{
|
|
CurrentState?.Update(e);
|
|
}
|
|
|
|
public void FrameUpdate(FrameEventArgs e)
|
|
{
|
|
CurrentState?.FrameUpdate(e);
|
|
}
|
|
|
|
public void FormResize()
|
|
{
|
|
CurrentState?.FormResize();
|
|
}
|
|
|
|
public void RequestStateChange<T>() where T : State, new()
|
|
{
|
|
RequestStateChange(typeof(T));
|
|
}
|
|
|
|
private void RequestStateChange(Type type)
|
|
{
|
|
if (CurrentState?.GetType() != type)
|
|
{
|
|
SwitchToState(type);
|
|
}
|
|
}
|
|
|
|
private void SwitchToState(Type type)
|
|
{
|
|
Logger.Debug($"Switching to state {type}");
|
|
|
|
var newState = _typeFactory.CreateInstance<State>(type);
|
|
|
|
var old = CurrentState;
|
|
CurrentState?.Shutdown();
|
|
|
|
CurrentState = newState;
|
|
CurrentState.Startup();
|
|
|
|
OnStateChanged?.Invoke(new StateChangedEventArgs(old, CurrentState));
|
|
}
|
|
}
|
|
}
|