Files
RobustToolbox/Robust.Client/State/StateManager.cs
T
Acruid 5e448f20c0 Move parts of the simulation update back into engine, from Content's StateBase.
Allow content to push modal windows in the UI framework.
The MenuBar now closes it's MenuButton frame when you select one of the buttons.
Modal controls on the stack now actually block input from the rest of the UI.
Adds the `scene` concommand for instantly switching between game states (scenes).
2020-07-19 12:31:13 -07:00

68 lines
1.7 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
{
[Dependency] private readonly IDynamicTypeFactory _typeFactory = default!;
public event Action<StateChangedEventArgs>? OnStateChanged;
public State CurrentState { get; private set; }
public StateManager()
{
CurrentState = new DefaultState();
}
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));
}
public void RequestStateChange(Type type)
{
if(!typeof(State).IsAssignableFrom(type))
throw new ArgumentException($"Needs to be derived from {typeof(State).FullName}", nameof(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));
}
}
}