diff --git a/Robust.Client/UserInterface/Controls/EntityPrototypeView.cs b/Robust.Client/UserInterface/Controls/EntityPrototypeView.cs new file mode 100644 index 00000000000..7ca262a0572 --- /dev/null +++ b/Robust.Client/UserInterface/Controls/EntityPrototypeView.cs @@ -0,0 +1,55 @@ +using Robust.Client.GameObjects; +using Robust.Shared.GameObjects; +using Robust.Shared.Prototypes; + +namespace Robust.Client.UserInterface.Controls; + +[Virtual] +public class EntityPrototypeView : SpriteView +{ + private string? _currentPrototype; + private EntityUid? _ourEntity; + + public EntityPrototypeView() + { + + } + + public EntityPrototypeView(EntProtoId? entProto, IEntityManager entMan) : base(entMan) + { + SetPrototype(entProto); + } + + public void SetPrototype(EntProtoId? entProto) + { + SpriteSystem ??= EntMan.System(); + + if (entProto == _currentPrototype) + return; + + _currentPrototype = entProto; + SetEntity(null); + if (_ourEntity != null) + { + EntMan.DeleteEntity(_ourEntity); + } + + if (_currentPrototype != null) + { + _ourEntity = EntMan.Spawn(_currentPrototype); + SpriteSystem.ForceUpdate(_ourEntity.Value); + SetEntity(_ourEntity); + } + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + + if (!disposing) + return; + + if (!EntMan.Deleted(_ourEntity)) + EntMan.QueueDeleteEntity(_ourEntity); + } +} diff --git a/Robust.Client/UserInterface/Controls/SpriteView.cs b/Robust.Client/UserInterface/Controls/SpriteView.cs index d5927b6645f..0380a37f218 100644 --- a/Robust.Client/UserInterface/Controls/SpriteView.cs +++ b/Robust.Client/UserInterface/Controls/SpriteView.cs @@ -14,9 +14,9 @@ namespace Robust.Client.UserInterface.Controls [Virtual] public class SpriteView : Control { - private SpriteSystem? _sprite; + protected SpriteSystem? SpriteSystem; private SharedTransformSystem? _transform; - private readonly IEntityManager _entMan; + protected readonly IEntityManager EntMan; [ViewVariables] public SpriteComponent? Sprite => Entity?.Comp1; @@ -120,20 +120,26 @@ public Vector2 Scale public SpriteView() { - IoCManager.Resolve(ref _entMan); + IoCManager.Resolve(ref EntMan); + RectClipContent = true; + } + + public SpriteView(IEntityManager entMan) + { + EntMan = entMan; RectClipContent = true; } public SpriteView(EntityUid? uid, IEntityManager entMan) { - _entMan = entMan; + EntMan = entMan; RectClipContent = true; SetEntity(uid); } public SpriteView(NetEntity uid, IEntityManager entMan) { - _entMan = entMan; + EntMan = entMan; RectClipContent = true; SetEntity(uid); } @@ -154,8 +160,8 @@ public void SetEntity(EntityUid? uid) if (Entity?.Owner == uid) return; - if (!_entMan.TryGetComponent(uid, out SpriteComponent? sprite) - || !_entMan.TryGetComponent(uid, out TransformComponent? xform)) + if (!EntMan.TryGetComponent(uid, out SpriteComponent? sprite) + || !EntMan.TryGetComponent(uid, out TransformComponent? xform)) { Entity = null; NetEnt = null; @@ -163,7 +169,7 @@ public void SetEntity(EntityUid? uid) } Entity = new(uid.Value, sprite, xform); - NetEnt = _entMan.GetNetEntity(uid); + NetEnt = EntMan.GetNetEntity(uid); } protected override Vector2 MeasureOverride(Vector2 availableSize) @@ -223,11 +229,11 @@ internal override void DrawInternal(IRenderHandle renderHandle) if (!ResolveEntity(out var uid, out var sprite, out var xform)) return; - _sprite ??= _entMan.System(); - _transform ??= _entMan.System(); + SpriteSystem ??= EntMan.System(); + _transform ??= EntMan.System(); // Ensure the sprite is animated despite possible not being visible in any viewport. - _sprite.ForceUpdate(uid); + SpriteSystem.ForceUpdate(uid); var stretchVec = Stretch switch { @@ -258,13 +264,13 @@ private bool ResolveEntity( [NotNullWhen(true)] out SpriteComponent? sprite, [NotNullWhen(true)] out TransformComponent? xform) { - if (NetEnt != null && Entity == null && _entMan.TryGetEntity(NetEnt, out var ent)) + if (NetEnt != null && Entity == null && EntMan.TryGetEntity(NetEnt, out var ent)) SetEntity(ent); if (Entity != null) { (uid, sprite, xform) = Entity.Value; - return !_entMan.Deleted(uid); + return !EntMan.Deleted(uid); } sprite = null;