Fix cursor popups (#8401)

* Fix cursor popups

* RemoveSwap
This commit is contained in:
Leon Friedrich
2022-05-25 14:33:48 +12:00
committed by GitHub
parent 4350f3b61b
commit 1954e0c3de

View File

@@ -10,6 +10,7 @@ using Robust.Client.UserInterface.Controls;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.Popups namespace Content.Client.Popups
{ {
@@ -22,7 +23,8 @@ namespace Content.Client.Popups
[Dependency] private readonly IPlayerManager _playerManager = default!; [Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly ExamineSystemShared _examineSystem = default!; [Dependency] private readonly ExamineSystemShared _examineSystem = default!;
private readonly List<PopupLabel> _aliveLabels = new(); private readonly List<WorldPopupLabel> _aliveWorldLabels = new();
private readonly List<CursorPopupLabel> _aliveCursorLabels = new();
public const float PopupLifetime = 3f; public const float PopupLifetime = 3f;
@@ -38,15 +40,20 @@ namespace Content.Client.Popups
public void PopupCursor(string message) public void PopupCursor(string message)
{ {
PopupMessage(message, _inputManager.MouseScreenPosition); var label = new CursorPopupLabel(_inputManager.MouseScreenPosition)
{
Text = message,
StyleClasses = { StyleNano.StyleClassPopupMessage },
};
_userInterfaceManager.PopupRoot.AddChild(label);
_aliveCursorLabels.Add(label);
} }
public void PopupCoordinates(string message, EntityCoordinates coordinates) public void PopupCoordinates(string message, EntityCoordinates coordinates)
{ {
var mapCoords = coordinates.ToMap(EntityManager); if (_eyeManager.CurrentMap != Transform(coordinates.EntityId).MapID)
if (_eyeManager.CurrentMap != mapCoords.MapId)
return; return;
PopupMessage(message, _eyeManager.MapToScreen(mapCoords)); PopupMessage(message, coordinates, null);
} }
public void PopupEntity(string message, EntityUid uid) public void PopupEntity(string message, EntityUid uid)
@@ -58,18 +65,12 @@ namespace Content.Client.Popups
if (_eyeManager.CurrentMap != transform.MapID) if (_eyeManager.CurrentMap != transform.MapID)
return; // TODO: entity may be outside of PVS, but enter PVS at a later time. So the pop-up should still get tracked? return; // TODO: entity may be outside of PVS, but enter PVS at a later time. So the pop-up should still get tracked?
PopupMessage(message, _eyeManager.CoordinatesToScreen(transform.Coordinates), uid); PopupMessage(message, transform.Coordinates, uid);
}
private void PopupMessage(string message, ScreenCoordinates coordinates, EntityUid? entity = null)
{
var mapCoords = _eyeManager.ScreenToMap(coordinates);
PopupMessage(message, EntityCoordinates.FromMap(_map, mapCoords), entity);
} }
private void PopupMessage(string message, EntityCoordinates coordinates, EntityUid? entity = null) private void PopupMessage(string message, EntityCoordinates coordinates, EntityUid? entity = null)
{ {
var label = new PopupLabel(_eyeManager, EntityManager) var label = new WorldPopupLabel(_eyeManager, EntityManager)
{ {
Entity = entity, Entity = entity,
Text = message, Text = message,
@@ -80,8 +81,7 @@ namespace Content.Client.Popups
label.Measure(Vector2.Infinity); label.Measure(Vector2.Infinity);
label.InitialPos = coordinates; label.InitialPos = coordinates;
LayoutContainer.SetPosition(label, label.InitialPos.Position); _aliveWorldLabels.Add(label);
_aliveLabels.Add(label);
} }
#endregion #endregion
@@ -133,19 +133,19 @@ namespace Content.Client.Popups
private void OnRoundRestart(RoundRestartCleanupEvent ev) private void OnRoundRestart(RoundRestartCleanupEvent ev)
{ {
foreach (var label in _aliveLabels) foreach (var label in _aliveWorldLabels)
{ {
label.Dispose(); label.Dispose();
} }
_aliveLabels.Clear(); _aliveWorldLabels.Clear();
} }
#endregion #endregion
public override void FrameUpdate(float frameTime) public override void FrameUpdate(float frameTime)
{ {
if (_aliveLabels.Count == 0) return; if (_aliveWorldLabels.Count == 0) return;
var player = _playerManager.LocalPlayer?.ControlledEntity; var player = _playerManager.LocalPlayer?.ControlledEntity;
var playerPos = player != null ? Transform(player.Value).MapPosition : MapCoordinates.Nullspace; var playerPos = player != null ? Transform(player.Value).MapPosition : MapCoordinates.Nullspace;
@@ -155,14 +155,14 @@ namespace Content.Client.Popups
=> uid == data.compOwner || uid == data.attachedEntity; => uid == data.compOwner || uid == data.attachedEntity;
var occluded = player != null && _examineSystem.IsOccluded(player.Value); var occluded = player != null && _examineSystem.IsOccluded(player.Value);
for (var i = _aliveLabels.Count - 1; i >= 0; i--) for (var i = _aliveWorldLabels.Count - 1; i >= 0; i--)
{ {
var label = _aliveLabels[i]; var label = _aliveWorldLabels[i];
if (label.TotalTime > PopupLifetime || if (label.TotalTime > PopupLifetime ||
label.Entity != null && Deleted(label.Entity)) label.Entity != null && Deleted(label.Entity))
{ {
label.Dispose(); label.Dispose();
_aliveLabels.RemoveAt(i); _aliveWorldLabels.RemoveSwap(i);
continue; continue;
} }
@@ -185,14 +185,58 @@ namespace Content.Client.Popups
label.Visible = true; label.Visible = true;
} }
for (var i = _aliveCursorLabels.Count - 1; i >= 0; i--)
{
var label = _aliveCursorLabels[i];
if (label.TotalTime > PopupLifetime)
{
label.Dispose();
_aliveCursorLabels.RemoveSwap(i);
}
}
} }
private sealed class PopupLabel : Label private abstract class PopupLabel : Label
{
public float TotalTime { get; protected set; }
public PopupLabel()
{
ShadowOffsetXOverride = ShadowOffsetYOverride = 1;
FontColorShadowOverride = Color.Black;
Measure(Vector2.Infinity);
}
protected override void FrameUpdate(FrameEventArgs eventArgs)
{
TotalTime += eventArgs.DeltaSeconds;
if (TotalTime > 0.5f)
Modulate = Color.White.WithAlpha(1f - 0.2f * MathF.Pow(TotalTime - 0.5f, 3f));
}
}
private sealed class CursorPopupLabel : PopupLabel
{
public Vector2 InitialPos { get; set; }
public CursorPopupLabel(ScreenCoordinates screenCoords) : base()
{
InitialPos = screenCoords.Position / UIScale - DesiredSize / 2;
}
protected override void FrameUpdate(FrameEventArgs eventArgs)
{
base.FrameUpdate(eventArgs);
LayoutContainer.SetPosition(this, InitialPos - (0, 20 * (TotalTime * TotalTime + TotalTime)));
}
}
private sealed class WorldPopupLabel : PopupLabel
{ {
private readonly IEyeManager _eyeManager; private readonly IEyeManager _eyeManager;
private readonly IEntityManager _entityManager; private readonly IEntityManager _entityManager;
public float TotalTime { get; private set; }
/// <summary> /// <summary>
/// The original Mapid and ScreenPosition of the label. /// The original Mapid and ScreenPosition of the label.
/// </summary> /// </summary>
@@ -202,19 +246,15 @@ namespace Content.Client.Popups
public EntityCoordinates InitialPos { get; set; } public EntityCoordinates InitialPos { get; set; }
public EntityUid? Entity { get; set; } public EntityUid? Entity { get; set; }
public PopupLabel(IEyeManager eyeManager, IEntityManager entityManager) public WorldPopupLabel(IEyeManager eyeManager, IEntityManager entityManager) : base()
{ {
_eyeManager = eyeManager; _eyeManager = eyeManager;
_entityManager = entityManager; _entityManager = entityManager;
ShadowOffsetXOverride = 1;
ShadowOffsetYOverride = 1;
FontColorShadowOverride = Color.Black;
} }
protected override void FrameUpdate(FrameEventArgs eventArgs) protected override void FrameUpdate(FrameEventArgs eventArgs)
{ {
TotalTime += eventArgs.DeltaSeconds; base.FrameUpdate(eventArgs);
ScreenCoordinates screenCoords; ScreenCoordinates screenCoords;
if (Entity == null) if (Entity == null)
@@ -232,11 +272,6 @@ namespace Content.Client.Popups
Visible = true; Visible = true;
var position = screenCoords.Position / UIScale - DesiredSize / 2; var position = screenCoords.Position / UIScale - DesiredSize / 2;
LayoutContainer.SetPosition(this, position - (0, 20 * (TotalTime * TotalTime + TotalTime))); LayoutContainer.SetPosition(this, position - (0, 20 * (TotalTime * TotalTime + TotalTime)));
if (TotalTime > 0.5f)
{
Modulate = Color.White.WithAlpha(1f - 0.2f * (float)Math.Pow(TotalTime - 0.5f, 3f));
}
} }
} }
} }