Skip to content

Commit

Permalink
ent proto view
Browse files Browse the repository at this point in the history
  • Loading branch information
EmoGarbage404 committed May 30, 2024
1 parent c89c529 commit 224d691
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 13 deletions.
55 changes: 55 additions & 0 deletions Robust.Client/UserInterface/Controls/EntityPrototypeView.cs
Original file line number Diff line number Diff line change
@@ -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<SpriteSystem>();

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);
}
}
32 changes: 19 additions & 13 deletions Robust.Client/UserInterface/Controls/SpriteView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -154,16 +160,16 @@ 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;
return;
}

Entity = new(uid.Value, sprite, xform);
NetEnt = _entMan.GetNetEntity(uid);
NetEnt = EntMan.GetNetEntity(uid);
}

protected override Vector2 MeasureOverride(Vector2 availableSize)
Expand Down Expand Up @@ -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<SpriteSystem>();
_transform ??= _entMan.System<TransformSystem>();
SpriteSystem ??= EntMan.System<SpriteSystem>();
_transform ??= EntMan.System<TransformSystem>();

// Ensure the sprite is animated despite possible not being visible in any viewport.
_sprite.ForceUpdate(uid);
SpriteSystem.ForceUpdate(uid);

var stretchVec = Stretch switch
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 224d691

Please sign in to comment.