mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-01 09:37:03 +02:00
* [Dependency] source generator No more reflection, no more codegen at runtime Also various changes to Roslyn helpers to make this easier to write. Requires all types with dependencies to be partial and not have readonly dependency fields. An analyzer enforces this at warning level, the previous injection strategies have remained in the code *for now* as a fallback. No fallback is available for [field: Dependency] properties, due to a Roslyn bug. Code Fixes exist. We love Roslyn * Apply dependencies generator changes to all code * Release notes * Preprocessor got hands * Handle nullable dependencies These are bad but gotta deal with it. * Apply suggestions from code review Co-authored-by: Moony <moony@hellomouse.net> * Fine, let's not use collection expressions --------- Co-authored-by: Moony <moony@hellomouse.net>
152 lines
4.2 KiB
C#
152 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
namespace Robust.Shared.GameObjects
|
|
{
|
|
/// <summary>
|
|
/// An abstract class to override to implement bound user interfaces.
|
|
/// </summary>
|
|
public abstract partial class BoundUserInterface : IDisposable
|
|
{
|
|
[Dependency] protected internal IEntityManager EntMan = default!;
|
|
[Dependency] protected ISharedPlayerManager PlayerManager = default!;
|
|
protected readonly SharedUserInterfaceSystem UiSystem;
|
|
|
|
public bool IsOpened { get; protected set; }
|
|
|
|
public readonly Enum UiKey;
|
|
public EntityUid Owner { get; }
|
|
|
|
/// <summary>
|
|
/// Additional controls to be disposed when this BUI is disposed.
|
|
/// </summary>
|
|
internal List<IDisposable>? Disposals;
|
|
|
|
/// <summary>
|
|
/// The last received state object sent from the server.
|
|
/// </summary>
|
|
protected internal BoundUserInterfaceState? State { get; internal set; }
|
|
|
|
protected BoundUserInterface(EntityUid owner, Enum uiKey)
|
|
{
|
|
IoCManager.Resolve(ref EntMan);
|
|
EntMan.EntitySysManager.DependencyCollection.InjectDependencies(this);
|
|
UiSystem = EntMan.System<SharedUserInterfaceSystem>();
|
|
|
|
Owner = owner;
|
|
UiKey = uiKey;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked when the UI is opened.
|
|
/// Do all creation and opening of things like windows in here.
|
|
/// </summary>
|
|
[MustCallBase]
|
|
protected internal virtual void Open()
|
|
{
|
|
if (IsOpened)
|
|
return;
|
|
|
|
IsOpened = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked when the server uses <c>SetState</c>.
|
|
/// </summary>
|
|
protected internal virtual void UpdateState(BoundUserInterfaceState state)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calls <see cref="UpdateState"/> if the supplied state exists and calls <see cref="Update"/>
|
|
/// </summary>
|
|
public void Update<T>() where T : BoundUserInterfaceState
|
|
{
|
|
if (UiSystem.TryGetUiState<T>(Owner, UiKey, out var state))
|
|
{
|
|
UpdateState(state);
|
|
}
|
|
|
|
Update();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generic update method called whenever the BUI should update.
|
|
/// </summary>
|
|
public virtual void Update()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper method that gets called upon prototype reload.
|
|
/// </summary>
|
|
public virtual void OnProtoReload(PrototypesReloadedEventArgs args)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked when the server sends an arbitrary message.
|
|
/// </summary>
|
|
protected internal virtual void ReceiveMessage(BoundUserInterfaceMessage message)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked to close the UI.
|
|
/// </summary>
|
|
public void Close()
|
|
{
|
|
if (!IsOpened)
|
|
return;
|
|
|
|
IsOpened = false;
|
|
UiSystem.CloseUi(Owner, UiKey, PlayerManager.LocalEntity, predicted: true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends a message to the server-side UI.
|
|
/// </summary>
|
|
public void SendMessage(BoundUserInterfaceMessage message)
|
|
{
|
|
UiSystem.ClientSendUiMessage(Owner, UiKey, message);
|
|
}
|
|
|
|
public void SendPredictedMessage(BoundUserInterfaceMessage message)
|
|
{
|
|
UiSystem.SendPredictedUiMessage(this, message);
|
|
}
|
|
|
|
~BoundUserInterface()
|
|
{
|
|
Dispose(false);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
if (Disposals != null)
|
|
{
|
|
foreach (var control in Disposals)
|
|
{
|
|
control.Dispose();
|
|
}
|
|
|
|
Disposals = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|