Popup ECS Refactor (#4692)
This commit is contained in:
committed by
GitHub
parent
2051970cc1
commit
0767bd3777
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using Content.Client.Markers;
|
||||
using Content.Client.Notifications.Managers;
|
||||
using Content.Client.Popups;
|
||||
using Content.Shared.SubFloor;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.Console;
|
||||
@@ -73,8 +73,7 @@ namespace Content.Client.Commands
|
||||
{
|
||||
var message = args[0];
|
||||
|
||||
var notifyManager = IoCManager.Resolve<IClientNotifyManager>();
|
||||
notifyManager.PopupMessage(message);
|
||||
EntitySystem.Get<PopupSystem>().PopupCursor(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -176,7 +176,7 @@ namespace Content.Client.ContextMenu.UI
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ContextMenuPopup : Popup
|
||||
public sealed class ContextMenuPopup : Robust.Client.UserInterface.Controls.Popup
|
||||
{
|
||||
private static readonly Color DefaultColor = Color.FromHex("#1116");
|
||||
private static readonly Color MarginColor = Color.FromHex("#222E");
|
||||
|
||||
@@ -4,8 +4,7 @@ using Content.Client.Viewport;
|
||||
using Content.Shared.DragDrop;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Content.Shared.Popups;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
|
||||
@@ -6,14 +6,12 @@ using Content.Client.Chat.Managers;
|
||||
using Content.Client.EscapeMenu;
|
||||
using Content.Client.Eui;
|
||||
using Content.Client.Flash;
|
||||
using Content.Client.GameTicking.Managers;
|
||||
using Content.Client.HUD;
|
||||
using Content.Client.Input;
|
||||
using Content.Client.IoC;
|
||||
using Content.Client.Launcher;
|
||||
using Content.Client.MainMenu;
|
||||
using Content.Client.MobState.Overlays;
|
||||
using Content.Client.Notifications.Managers;
|
||||
using Content.Client.Parallax;
|
||||
using Content.Client.Parallax.Managers;
|
||||
using Content.Client.Preferences;
|
||||
@@ -32,7 +30,6 @@ using Content.Shared.Cargo.Components;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.Dispenser;
|
||||
using Content.Shared.Gravity;
|
||||
using Content.Shared.Kitchen.Components;
|
||||
using Content.Shared.Lathe;
|
||||
using Content.Shared.Markers;
|
||||
using Content.Shared.Research.Components;
|
||||
@@ -166,7 +163,6 @@ namespace Content.Client.Entry
|
||||
ContentContexts.SetupContexts(inputMan.Contexts);
|
||||
|
||||
IoCManager.Resolve<IGameHud>().Initialize();
|
||||
IoCManager.Resolve<IClientNotifyManager>().Initialize();
|
||||
|
||||
var overlayMgr = IoCManager.Resolve<IOverlayManager>();
|
||||
overlayMgr.AddOverlay(new ParallaxOverlay());
|
||||
@@ -228,7 +224,7 @@ namespace Content.Client.Entry
|
||||
switch (level)
|
||||
{
|
||||
case ModUpdateLevel.FramePreEngine:
|
||||
IoCManager.Resolve<IClientNotifyManager>().FrameUpdate(frameEventArgs);
|
||||
// TODO: Turn IChatManager into an EntitySystem and remove the line below.
|
||||
IoCManager.Resolve<IChatManager>().FrameUpdate(frameEventArgs);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -4,11 +4,9 @@ using Content.Client.Chat.Managers;
|
||||
using Content.Client.Clickable;
|
||||
using Content.Client.EscapeMenu;
|
||||
using Content.Client.Eui;
|
||||
using Content.Client.GameTicking.Managers;
|
||||
using Content.Client.HUD;
|
||||
using Content.Client.Items.Managers;
|
||||
using Content.Client.Module;
|
||||
using Content.Client.Notifications.Managers;
|
||||
using Content.Client.Parallax.Managers;
|
||||
using Content.Client.Preferences;
|
||||
using Content.Client.Sandbox;
|
||||
@@ -20,8 +18,6 @@ using Content.Client.Voting;
|
||||
using Content.Shared.Actions;
|
||||
using Content.Shared.Alert;
|
||||
using Content.Shared.Module;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Client.IoC
|
||||
@@ -31,8 +27,6 @@ namespace Content.Client.IoC
|
||||
public static void Register()
|
||||
{
|
||||
IoCManager.Register<IGameHud, GameHud>();
|
||||
IoCManager.Register<IClientNotifyManager, ClientNotifyManager>();
|
||||
IoCManager.Register<ISharedNotifyManager, ClientNotifyManager>();
|
||||
IoCManager.Register<IParallaxManager, ParallaxManager>();
|
||||
IoCManager.Register<IChatManager, ChatManager>();
|
||||
IoCManager.Register<IEscapeMenuOwner, EscapeMenuOwner>();
|
||||
|
||||
@@ -1,178 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Client.Stylesheets;
|
||||
using Content.Shared;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.Notifications.Managers
|
||||
{
|
||||
public class ClientNotifyManager : SharedNotifyManager, IClientNotifyManager
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
[Dependency] private readonly IClientNetManager _netManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
|
||||
private readonly List<PopupLabel> _aliveLabels = new();
|
||||
private bool _initialized;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
DebugTools.Assert(!_initialized);
|
||||
|
||||
_netManager.RegisterNetMessage<MsgDoNotifyCursor>(DoNotifyCursor);
|
||||
_netManager.RegisterNetMessage<MsgDoNotifyCoordinates>(DoNotifyCoordinates);
|
||||
_netManager.RegisterNetMessage<MsgDoNotifyEntity>(DoNotifyEntity);
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
private void DoNotifyCursor(MsgDoNotifyCursor message)
|
||||
{
|
||||
PopupMessage(message.Message);
|
||||
}
|
||||
|
||||
private void DoNotifyCoordinates(MsgDoNotifyCoordinates message)
|
||||
{
|
||||
PopupMessage(_eyeManager.CoordinatesToScreen(message.Coordinates), message.Message);
|
||||
}
|
||||
|
||||
private void DoNotifyEntity(MsgDoNotifyEntity message)
|
||||
{
|
||||
if (_playerManager.LocalPlayer?.ControlledEntity == null ||
|
||||
!_entityManager.TryGetEntity(message.Entity, out var entity))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PopupMessage(entity, _playerManager.LocalPlayer.ControlledEntity, message.Message);
|
||||
}
|
||||
|
||||
public override void PopupMessage(IEntity source, IEntity viewer, string message)
|
||||
{
|
||||
PopupMessage(_eyeManager.CoordinatesToScreen(source.Transform.Coordinates), message, source);
|
||||
}
|
||||
|
||||
public override void PopupMessage(EntityCoordinates coordinates, IEntity viewer, string message)
|
||||
{
|
||||
if (viewer != _playerManager.LocalPlayer?.ControlledEntity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PopupMessage(_eyeManager.CoordinatesToScreen(coordinates), message);
|
||||
}
|
||||
|
||||
public override void PopupMessageCursor(IEntity viewer, string message)
|
||||
{
|
||||
if (viewer != _playerManager.LocalPlayer?.ControlledEntity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PopupMessage(message);
|
||||
}
|
||||
|
||||
public void PopupMessage(ScreenCoordinates coordinates, string message)
|
||||
{
|
||||
PopupMessage(coordinates, message, null);
|
||||
}
|
||||
|
||||
public void PopupMessage(ScreenCoordinates coordinates, string message, IEntity? entity)
|
||||
{
|
||||
var label = new PopupLabel(_eyeManager)
|
||||
{
|
||||
Entity = entity,
|
||||
Text = message,
|
||||
StyleClasses = { StyleNano.StyleClassPopupMessage },
|
||||
};
|
||||
|
||||
_userInterfaceManager.PopupRoot.AddChild(label);
|
||||
label.Measure(Vector2.Infinity);
|
||||
var minimumSize = label.DesiredSize;
|
||||
|
||||
label.InitialPos = (coordinates.Position / label.UIScale) - minimumSize / 2;
|
||||
LayoutContainer.SetPosition(label, label.InitialPos);
|
||||
_aliveLabels.Add(label);
|
||||
}
|
||||
|
||||
public void PopupMessage(string message)
|
||||
{
|
||||
PopupMessage(_userInterfaceManager.MousePositionScaled, message);
|
||||
}
|
||||
|
||||
public void FrameUpdate(FrameEventArgs eventArgs)
|
||||
{
|
||||
_aliveLabels.ForEach(l =>
|
||||
{
|
||||
if (l.TimeLeft > 3f)
|
||||
{
|
||||
l.Dispose();
|
||||
}
|
||||
});
|
||||
|
||||
_aliveLabels.RemoveAll(l => l.Disposed);
|
||||
}
|
||||
|
||||
private class PopupLabel : Label
|
||||
{
|
||||
private readonly IEyeManager _eyeManager;
|
||||
|
||||
public float TimeLeft { get; private set; }
|
||||
public Vector2 InitialPos { get; set; }
|
||||
public IEntity? Entity { get; set; }
|
||||
|
||||
public PopupLabel(IEyeManager eyeManager)
|
||||
{
|
||||
_eyeManager = eyeManager;
|
||||
ShadowOffsetXOverride = 1;
|
||||
ShadowOffsetYOverride = 1;
|
||||
FontColorShadowOverride = Color.Black;
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs eventArgs)
|
||||
{
|
||||
TimeLeft += eventArgs.DeltaSeconds;
|
||||
|
||||
var position = Entity == null
|
||||
? InitialPos
|
||||
: (_eyeManager.CoordinatesToScreen(Entity.Transform.Coordinates).Position / UIScale) - DesiredSize / 2;
|
||||
|
||||
LayoutContainer.SetPosition(this, position - (0, 20 * (TimeLeft * TimeLeft + TimeLeft)));
|
||||
|
||||
if (TimeLeft > 0.5f)
|
||||
{
|
||||
Modulate = Color.White.WithAlpha(1f - 0.2f * (float)Math.Pow(TimeLeft - 0.5f, 3f));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class PopupMessageCommand : IConsoleCommand
|
||||
{
|
||||
public string Command => "popupmsg";
|
||||
public string Description => "";
|
||||
public string Help => "";
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
var arg = args[0];
|
||||
var mgr = IoCManager.Resolve<IClientNotifyManager>();
|
||||
mgr.PopupMessage(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Notifications.Managers
|
||||
{
|
||||
public interface IClientNotifyManager : ISharedNotifyManager
|
||||
{
|
||||
void Initialize();
|
||||
void PopupMessage(ScreenCoordinates coordinates, string message);
|
||||
void PopupMessage(string message);
|
||||
void FrameUpdate(FrameEventArgs eventArgs);
|
||||
}
|
||||
}
|
||||
177
Content.Client/Popups/PopupSystem.cs
Normal file
177
Content.Client/Popups/PopupSystem.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Client.Stylesheets;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Player;
|
||||
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.Player;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Popups
|
||||
{
|
||||
public class PopupSystem : SharedPopupSystem
|
||||
{
|
||||
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
|
||||
private readonly List<PopupLabel> _aliveLabels = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeNetworkEvent<PopupCursorEvent>(OnPopupCursorEvent);
|
||||
SubscribeNetworkEvent<PopupCoordinatesEvent>(OnPopupCoordinatesEvent);
|
||||
SubscribeNetworkEvent<PopupEntityEvent>(OnPopupEntityEvent);
|
||||
SubscribeNetworkEvent<RoundRestartCleanupEvent>(OnRoundRestart);
|
||||
}
|
||||
|
||||
#region Actual Implementation
|
||||
|
||||
public void PopupCursor(string message)
|
||||
{
|
||||
PopupMessage(message, _userInterfaceManager.MousePositionScaled);
|
||||
}
|
||||
|
||||
public void PopupCoordinates(string message, EntityCoordinates coordinates)
|
||||
{
|
||||
PopupMessage(message, _eyeManager.CoordinatesToScreen(coordinates));
|
||||
}
|
||||
|
||||
public void PopupEntity(string message, EntityUid uid)
|
||||
{
|
||||
if (!EntityManager.EntityExists(uid))
|
||||
return;
|
||||
|
||||
var transform = ComponentManager.GetComponent<ITransformComponent>(uid);
|
||||
PopupMessage(message, _eyeManager.CoordinatesToScreen(transform.Coordinates));
|
||||
}
|
||||
|
||||
public void PopupMessage(string message, ScreenCoordinates coordinates, IEntity? entity = null)
|
||||
{
|
||||
var label = new PopupLabel(_eyeManager)
|
||||
{
|
||||
Entity = entity,
|
||||
Text = message,
|
||||
StyleClasses = { StyleNano.StyleClassPopupMessage },
|
||||
};
|
||||
|
||||
_userInterfaceManager.PopupRoot.AddChild(label);
|
||||
label.Measure(Vector2.Infinity);
|
||||
var minimumSize = label.DesiredSize;
|
||||
|
||||
label.InitialPos = coordinates.Position / label.UIScale - minimumSize / 2;
|
||||
LayoutContainer.SetPosition(label, label.InitialPos);
|
||||
_aliveLabels.Add(label);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Abstract Method Implementations
|
||||
|
||||
public override void PopupCursor(string message, Filter filter)
|
||||
{
|
||||
if (!filter.CheckPrediction)
|
||||
return;
|
||||
}
|
||||
|
||||
public override void PopupCoordinates(string message, EntityCoordinates coordinates, Filter filter)
|
||||
{
|
||||
if (!filter.CheckPrediction)
|
||||
return;
|
||||
}
|
||||
|
||||
public override void PopupEntity(string message, EntityUid uid, Filter filter)
|
||||
{
|
||||
if (!filter.CheckPrediction)
|
||||
return;
|
||||
}
|
||||
|
||||
public override Filter GetFilterFromEntity(IEntity entity)
|
||||
{
|
||||
return _playerManager.LocalPlayer?.ControlledEntity == entity
|
||||
? Filter.Local() : Filter.Empty().Unpredicted();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Network Event Handlers
|
||||
|
||||
private void OnPopupCursorEvent(PopupCursorEvent ev)
|
||||
{
|
||||
PopupCursor(ev.Message);
|
||||
}
|
||||
|
||||
private void OnPopupCoordinatesEvent(PopupCoordinatesEvent ev)
|
||||
{
|
||||
PopupCoordinates(ev.Message, ev.Coordinates);
|
||||
}
|
||||
|
||||
private void OnPopupEntityEvent(PopupEntityEvent ev)
|
||||
{
|
||||
PopupEntity(ev.Message, ev.Uid);
|
||||
}
|
||||
|
||||
private void OnRoundRestart(RoundRestartCleanupEvent ev)
|
||||
{
|
||||
foreach (var label in _aliveLabels)
|
||||
{
|
||||
label.Dispose();
|
||||
}
|
||||
|
||||
_aliveLabels.Clear();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override void FrameUpdate(float frameTime)
|
||||
{
|
||||
foreach (var l in _aliveLabels)
|
||||
{
|
||||
if (l.TimeLeft > 3f)
|
||||
l.Dispose();
|
||||
}
|
||||
|
||||
_aliveLabels.RemoveAll(l => l.Disposed);
|
||||
}
|
||||
|
||||
private class PopupLabel : Label
|
||||
{
|
||||
private readonly IEyeManager _eyeManager;
|
||||
|
||||
public float TimeLeft { get; private set; }
|
||||
public Vector2 InitialPos { get; set; }
|
||||
public IEntity? Entity { get; set; }
|
||||
|
||||
public PopupLabel(IEyeManager eyeManager)
|
||||
{
|
||||
_eyeManager = eyeManager;
|
||||
ShadowOffsetXOverride = 1;
|
||||
ShadowOffsetYOverride = 1;
|
||||
FontColorShadowOverride = Color.Black;
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs eventArgs)
|
||||
{
|
||||
TimeLeft += eventArgs.DeltaSeconds;
|
||||
|
||||
var position = Entity == null
|
||||
? InitialPos
|
||||
: (_eyeManager.CoordinatesToScreen(Entity.Transform.Coordinates).Position / UIScale) - DesiredSize / 2;
|
||||
|
||||
LayoutContainer.SetPosition(this, position - (0, 20 * (TimeLeft * TimeLeft + TimeLeft)));
|
||||
|
||||
if (TimeLeft > 0.5f)
|
||||
{
|
||||
Modulate = Color.White.WithAlpha(1f - 0.2f * (float)Math.Pow(TimeLeft - 0.5f, 3f));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,7 @@ using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.UserInterface;
|
||||
|
||||
Reference in New Issue
Block a user