diff --git a/Content.Client/DoAfter/DoAfterSystem.cs b/Content.Client/DoAfter/DoAfterSystem.cs index bfdd6f7dea..af457e3da9 100644 --- a/Content.Client/DoAfter/DoAfterSystem.cs +++ b/Content.Client/DoAfter/DoAfterSystem.cs @@ -6,6 +6,7 @@ using JetBrains.Annotations; using Robust.Client.GameObjects; using Robust.Client.Graphics; using Robust.Client.Player; +using Robust.Client.UserInterface.Controls; using Robust.Shared.GameStates; using Robust.Shared.Timing; using Robust.Shared.Utility; diff --git a/Content.Client/DoAfter/UI/DoAfterBar.cs b/Content.Client/DoAfter/UI/DoAfterBar.cs index fb47a17048..06a92daf14 100644 --- a/Content.Client/DoAfter/UI/DoAfterBar.cs +++ b/Content.Client/DoAfter/UI/DoAfterBar.cs @@ -72,6 +72,7 @@ namespace Content.Client.DoAfter.UI { IoCManager.InjectDependencies(this); _shader = IoCManager.Resolve().Index("unshaded").Instance(); + VerticalAlignment = VAlignment.Center; } protected override void FrameUpdate(FrameEventArgs args) diff --git a/Content.Client/DoAfter/UI/DoAfterControl.cs b/Content.Client/DoAfter/UI/DoAfterControl.cs new file mode 100644 index 0000000000..1f7d4dbcdf --- /dev/null +++ b/Content.Client/DoAfter/UI/DoAfterControl.cs @@ -0,0 +1,45 @@ +using Content.Client.Resources; +using Robust.Client.Graphics; +using Robust.Client.ResourceManagement; +using Robust.Client.UserInterface.Controls; +using Robust.Shared.Map; +using Robust.Shared.Timing; + +namespace Content.Client.DoAfter.UI; + +public sealed class DoAfterControl : PanelContainer +{ + public float Ratio + { + get => _bar.Ratio; + set => _bar.Ratio = value; + } + + public bool Cancelled + { + get => _bar.Cancelled; + set => _bar.Cancelled = value; + } + + private DoAfterBar _bar; + + public DoAfterControl() + { + IoCManager.InjectDependencies(this); + + var cache = IoCManager.Resolve(); + + AddChild(new TextureRect + { + Texture = cache.GetTexture("/Textures/Interface/Misc/progress_bar.rsi/icon.png"), + TextureScale = Vector2.One * DoAfterBar.DoAfterBarScale, + VerticalAlignment = VAlignment.Center, + }); + + _bar = new DoAfterBar(); + AddChild(_bar); + VerticalAlignment = VAlignment.Bottom; + _bar.Measure(Vector2.Infinity); + Measure(Vector2.Infinity); + } +} diff --git a/Content.Client/DoAfter/UI/DoAfterGui.cs b/Content.Client/DoAfter/UI/DoAfterGui.cs index a7973adc61..7b40b20bf2 100644 --- a/Content.Client/DoAfter/UI/DoAfterGui.cs +++ b/Content.Client/DoAfter/UI/DoAfterGui.cs @@ -1,17 +1,8 @@ -using System; -using System.Collections.Generic; -using Content.Client.IoC; -using Content.Client.Resources; using Content.Shared.DoAfter; using Robust.Client.Graphics; using Robust.Client.UserInterface; using Robust.Client.UserInterface.Controls; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Map; -using Robust.Shared.Maths; using Robust.Shared.Timing; -using Robust.Shared.Utility; namespace Content.Client.DoAfter.UI { @@ -21,14 +12,12 @@ namespace Content.Client.DoAfter.UI [Dependency] private readonly IEyeManager _eyeManager = default!; [Dependency] private readonly IGameTiming _gameTiming = default!; - private readonly Dictionary _doAfterControls = new(); - private readonly Dictionary _doAfterBars = new(); + private readonly Dictionary _doAfterControls = new(); // We'll store cancellations for a little bit just so we can flash the graphic to indicate it's cancelled private readonly Dictionary _cancelledDoAfters = new(); - public EntityUid AttachedEntity { get; set; } - private ScreenCoordinates _playerPosition; + public EntityUid? AttachedEntity { get; set; } public DoAfterGui() { @@ -37,8 +26,6 @@ namespace Content.Client.DoAfter.UI IoCManager.InjectDependencies(this); IoCManager.Resolve().StateRoot.AddChild(this); SeparationOverride = 0; - - LayoutContainer.SetGrowVertical(this, LayoutContainer.GrowDirection.Begin); } protected override void Dispose(bool disposing) @@ -53,43 +40,21 @@ namespace Content.Client.DoAfter.UI } _doAfterControls.Clear(); - _doAfterBars.Clear(); _cancelledDoAfters.Clear(); } /// /// Add the necessary control for a DoAfter progress bar. /// - /// public void AddDoAfter(ClientDoAfter message) { if (_doAfterControls.ContainsKey(message.ID)) return; - var doAfterBar = new DoAfterBar - { - VerticalAlignment = VAlignment.Center - }; - - _doAfterBars[message.ID] = doAfterBar; - - var control = new PanelContainer - { - Children = - { - new TextureRect - { - Texture = StaticIoC.ResC.GetTexture("/Textures/Interface/Misc/progress_bar.rsi/icon.png"), - TextureScale = Vector2.One * DoAfterBar.DoAfterBarScale, - VerticalAlignment = VAlignment.Center, - }, - - doAfterBar - } - }; - - AddChild(control); - _doAfterControls.Add(message.ID, control); + var doAfterBar = new DoAfterControl(); + AddChild(doAfterBar); + _doAfterControls.Add(message.ID, doAfterBar); + Measure(Vector2.Infinity); } // NOTE THAT THE BELOW ONLY HANDLES THE UI SIDE @@ -107,8 +72,6 @@ namespace Content.Client.DoAfter.UI RemoveChild(control); control.DisposeAllChildren(); _doAfterControls.Remove(id); - _doAfterBars.Remove(id); - _cancelledDoAfters.Remove(id); } @@ -122,23 +85,13 @@ namespace Content.Client.DoAfter.UI if (_cancelledDoAfters.ContainsKey(id)) return; - DoAfterBar doAfterBar; - if (!_doAfterControls.TryGetValue(id, out var doAfterControl)) { - doAfterControl = new PanelContainer(); + doAfterControl = new DoAfterControl(); AddChild(doAfterControl); - DebugTools.Assert(!_doAfterBars.ContainsKey(id)); - doAfterBar = new DoAfterBar(); - doAfterControl.AddChild(doAfterBar); - _doAfterBars[id] = doAfterBar; - } - else - { - doAfterBar = _doAfterBars[id]; } - doAfterBar.Cancelled = true; + doAfterControl.Cancelled = true; _cancelledDoAfters.Add(id, _gameTiming.CurTime); } @@ -146,32 +99,29 @@ namespace Content.Client.DoAfter.UI { base.FrameUpdate(args); - if (!AttachedEntity.IsValid() || - !_entityManager.TryGetComponent(AttachedEntity, out DoAfterComponent? doAfterComponent)) + if (!_entityManager.TryGetComponent(AttachedEntity, out DoAfterComponent? doAfterComponent)) { Visible = false; return; } var doAfters = doAfterComponent.DoAfters; + if (doAfters.Count == 0) { Visible = false; return; } - var transform = _entityManager.GetComponent(AttachedEntity); + var transform = _entityManager.GetComponent(AttachedEntity.Value); if (_eyeManager.CurrentMap != transform.MapID || !transform.Coordinates.IsValid(_entityManager)) { Visible = false; return; } - else - { - Visible = true; - } + Visible = true; var currentTime = _gameTiming.CurTime; var toRemove = new List(); @@ -195,15 +145,16 @@ namespace Content.Client.DoAfter.UI if (_cancelledDoAfters.ContainsKey(id) || !_doAfterControls.ContainsKey(id)) continue; - var doAfterBar = _doAfterBars[id]; + var control = _doAfterControls[id]; var ratio = (currentTime - message.StartTime).TotalSeconds; - doAfterBar.Ratio = MathF.Min(1.0f, + control.Ratio = MathF.Min(1.0f, (float) ratio / message.Delay); // Just in case it doesn't get cleaned up by the system for whatever reason. if (ratio > message.Delay + DoAfterSystem.ExcessTime) { toRemove.Add(id); + continue; } } @@ -212,9 +163,14 @@ namespace Content.Client.DoAfter.UI RemoveDoAfter(id); } - var screenCoordinates = _eyeManager.CoordinatesToScreen(transform.Coordinates); - _playerPosition = new ScreenCoordinates(screenCoordinates.Position / UIScale, screenCoordinates.Window); - LayoutContainer.SetPosition(this, new Vector2(_playerPosition.X - Width / 2, _playerPosition.Y - Height - 30.0f)); + UpdatePosition(transform); + } + + public void UpdatePosition(TransformComponent xform) + { + var screenCoordinates = _eyeManager.CoordinatesToScreen(xform.Coordinates); + var position = screenCoordinates.Position / UIScale - DesiredSize / 2f; + LayoutContainer.SetPosition(this, position - new Vector2(0, 40f)); } } }