Fix do-after flicker (#10020)

This commit is contained in:
metalgearsloth
2022-07-26 23:57:31 +10:00
committed by GitHub
parent 535f16a199
commit 5eb0e62142
4 changed files with 70 additions and 67 deletions

View File

@@ -6,6 +6,7 @@ using JetBrains.Annotations;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility; using Robust.Shared.Utility;

View File

@@ -72,6 +72,7 @@ namespace Content.Client.DoAfter.UI
{ {
IoCManager.InjectDependencies(this); IoCManager.InjectDependencies(this);
_shader = IoCManager.Resolve<IPrototypeManager>().Index<ShaderPrototype>("unshaded").Instance(); _shader = IoCManager.Resolve<IPrototypeManager>().Index<ShaderPrototype>("unshaded").Instance();
VerticalAlignment = VAlignment.Center;
} }
protected override void FrameUpdate(FrameEventArgs args) protected override void FrameUpdate(FrameEventArgs args)

View File

@@ -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<IResourceCache>();
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);
}
}

View File

@@ -1,17 +1,8 @@
using System;
using System.Collections.Generic;
using Content.Client.IoC;
using Content.Client.Resources;
using Content.Shared.DoAfter; using Content.Shared.DoAfter;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; 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.Timing;
using Robust.Shared.Utility;
namespace Content.Client.DoAfter.UI namespace Content.Client.DoAfter.UI
{ {
@@ -21,14 +12,12 @@ namespace Content.Client.DoAfter.UI
[Dependency] private readonly IEyeManager _eyeManager = default!; [Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!; [Dependency] private readonly IGameTiming _gameTiming = default!;
private readonly Dictionary<byte, PanelContainer> _doAfterControls = new(); private readonly Dictionary<byte, DoAfterControl> _doAfterControls = new();
private readonly Dictionary<byte, DoAfterBar> _doAfterBars = new();
// We'll store cancellations for a little bit just so we can flash the graphic to indicate it's cancelled // We'll store cancellations for a little bit just so we can flash the graphic to indicate it's cancelled
private readonly Dictionary<byte, TimeSpan> _cancelledDoAfters = new(); private readonly Dictionary<byte, TimeSpan> _cancelledDoAfters = new();
public EntityUid AttachedEntity { get; set; } public EntityUid? AttachedEntity { get; set; }
private ScreenCoordinates _playerPosition;
public DoAfterGui() public DoAfterGui()
{ {
@@ -37,8 +26,6 @@ namespace Content.Client.DoAfter.UI
IoCManager.InjectDependencies(this); IoCManager.InjectDependencies(this);
IoCManager.Resolve<IUserInterfaceManager>().StateRoot.AddChild(this); IoCManager.Resolve<IUserInterfaceManager>().StateRoot.AddChild(this);
SeparationOverride = 0; SeparationOverride = 0;
LayoutContainer.SetGrowVertical(this, LayoutContainer.GrowDirection.Begin);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
@@ -53,43 +40,21 @@ namespace Content.Client.DoAfter.UI
} }
_doAfterControls.Clear(); _doAfterControls.Clear();
_doAfterBars.Clear();
_cancelledDoAfters.Clear(); _cancelledDoAfters.Clear();
} }
/// <summary> /// <summary>
/// Add the necessary control for a DoAfter progress bar. /// Add the necessary control for a DoAfter progress bar.
/// </summary> /// </summary>
/// <param name="message"></param>
public void AddDoAfter(ClientDoAfter message) public void AddDoAfter(ClientDoAfter message)
{ {
if (_doAfterControls.ContainsKey(message.ID)) if (_doAfterControls.ContainsKey(message.ID))
return; return;
var doAfterBar = new DoAfterBar var doAfterBar = new DoAfterControl();
{ AddChild(doAfterBar);
VerticalAlignment = VAlignment.Center _doAfterControls.Add(message.ID, doAfterBar);
}; Measure(Vector2.Infinity);
_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);
} }
// NOTE THAT THE BELOW ONLY HANDLES THE UI SIDE // NOTE THAT THE BELOW ONLY HANDLES THE UI SIDE
@@ -107,8 +72,6 @@ namespace Content.Client.DoAfter.UI
RemoveChild(control); RemoveChild(control);
control.DisposeAllChildren(); control.DisposeAllChildren();
_doAfterControls.Remove(id); _doAfterControls.Remove(id);
_doAfterBars.Remove(id);
_cancelledDoAfters.Remove(id); _cancelledDoAfters.Remove(id);
} }
@@ -122,23 +85,13 @@ namespace Content.Client.DoAfter.UI
if (_cancelledDoAfters.ContainsKey(id)) if (_cancelledDoAfters.ContainsKey(id))
return; return;
DoAfterBar doAfterBar;
if (!_doAfterControls.TryGetValue(id, out var doAfterControl)) if (!_doAfterControls.TryGetValue(id, out var doAfterControl))
{ {
doAfterControl = new PanelContainer(); doAfterControl = new DoAfterControl();
AddChild(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); _cancelledDoAfters.Add(id, _gameTiming.CurTime);
} }
@@ -146,32 +99,29 @@ namespace Content.Client.DoAfter.UI
{ {
base.FrameUpdate(args); base.FrameUpdate(args);
if (!AttachedEntity.IsValid() || if (!_entityManager.TryGetComponent(AttachedEntity, out DoAfterComponent? doAfterComponent))
!_entityManager.TryGetComponent(AttachedEntity, out DoAfterComponent? doAfterComponent))
{ {
Visible = false; Visible = false;
return; return;
} }
var doAfters = doAfterComponent.DoAfters; var doAfters = doAfterComponent.DoAfters;
if (doAfters.Count == 0) if (doAfters.Count == 0)
{ {
Visible = false; Visible = false;
return; return;
} }
var transform = _entityManager.GetComponent<TransformComponent>(AttachedEntity); var transform = _entityManager.GetComponent<TransformComponent>(AttachedEntity.Value);
if (_eyeManager.CurrentMap != transform.MapID || !transform.Coordinates.IsValid(_entityManager)) if (_eyeManager.CurrentMap != transform.MapID || !transform.Coordinates.IsValid(_entityManager))
{ {
Visible = false; Visible = false;
return; return;
} }
else
{
Visible = true;
}
Visible = true;
var currentTime = _gameTiming.CurTime; var currentTime = _gameTiming.CurTime;
var toRemove = new List<byte>(); var toRemove = new List<byte>();
@@ -195,15 +145,16 @@ namespace Content.Client.DoAfter.UI
if (_cancelledDoAfters.ContainsKey(id) || !_doAfterControls.ContainsKey(id)) if (_cancelledDoAfters.ContainsKey(id) || !_doAfterControls.ContainsKey(id))
continue; continue;
var doAfterBar = _doAfterBars[id]; var control = _doAfterControls[id];
var ratio = (currentTime - message.StartTime).TotalSeconds; var ratio = (currentTime - message.StartTime).TotalSeconds;
doAfterBar.Ratio = MathF.Min(1.0f, control.Ratio = MathF.Min(1.0f,
(float) ratio / message.Delay); (float) ratio / message.Delay);
// Just in case it doesn't get cleaned up by the system for whatever reason. // Just in case it doesn't get cleaned up by the system for whatever reason.
if (ratio > message.Delay + DoAfterSystem.ExcessTime) if (ratio > message.Delay + DoAfterSystem.ExcessTime)
{ {
toRemove.Add(id); toRemove.Add(id);
continue;
} }
} }
@@ -212,9 +163,14 @@ namespace Content.Client.DoAfter.UI
RemoveDoAfter(id); RemoveDoAfter(id);
} }
var screenCoordinates = _eyeManager.CoordinatesToScreen(transform.Coordinates); UpdatePosition(transform);
_playerPosition = new ScreenCoordinates(screenCoordinates.Position / UIScale, screenCoordinates.Window); }
LayoutContainer.SetPosition(this, new Vector2(_playerPosition.X - Width / 2, _playerPosition.Y - Height - 30.0f));
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));
} }
} }
} }