<commit_before>using System;
using System.Collections.Generic;
using Content.Client.Interfaces;
using Content.Client.UserInterface.Stylesheets;
using Content.Shared;
using Robust.Client.Interfaces.Console;
using Robust.Client.Interfaces.Graphics.ClientEye;
using Robust.Client.Interfaces.Input;
using Robust.Client.Interfaces.UserInterface;
using Robust.Client.Player;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
using Robust.Shared.Utility;

namespace Content.Client
{
    public class ClientNotifyManager : SharedNotifyManager, IClientNotifyManager
    {
        [Dependency] private readonly IPlayerManager _playerManager = default!;
        [Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
        [Dependency] private readonly IInputManager _inputManager = 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 List<PopupLabel>();
        private bool _initialized;

        public void Initialize()
        {
            DebugTools.Assert(!_initialized);

            _netManager.RegisterNetMessage<MsgDoNotifyCursor>(nameof(MsgDoNotifyCursor), DoNotifyCursor);
            _netManager.RegisterNetMessage<MsgDoNotifyCoordinates>(nameof(MsgDoNotifyCoordinates), DoNotifyCoordinates);
            _netManager.RegisterNetMessage<MsgDoNotifyEntity>(nameof(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 (!_entityManager.TryGetEntity(message.Entity, out var entity))
            {
                return;
            }

            PopupMessage(_eyeManager.CoordinatesToScreen(entity.Transform.Coordinates), message.Message);
        }

        public override void PopupMessage(IEntity source, IEntity viewer, string message)
        {
            if (viewer != _playerManager.LocalPlayer?.ControlledEntity)
            {
                return;
            }

            PopupMessage(_eyeManager.CoordinatesToScreen(source.Transform.Coordinates), message);
        }

        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)
        {
            var label = new PopupLabel
            {
                Text = message,
                StyleClasses = { StyleNano.StyleClassPopupMessage },
            };
            _userInterfaceManager.PopupRoot.AddChild(label);
            var minimumSize = label.CombinedMinimumSize;
            LayoutContainer.SetPosition(label, label.InitialPos = coordinates.Position - minimumSize / 2);
            _aliveLabels.Add(label);
        }

        public void PopupMessage(string message)
        {
            PopupMessage(new ScreenCoordinates(_inputManager.MouseScreenPosition), message);
        }

        public void FrameUpdate(FrameEventArgs eventArgs)
        {
            _aliveLabels.ForEach(l =>
            {
                if (l.TimeLeft > 3f)
                {
                    l.Dispose();
                }
            });

            _aliveLabels.RemoveAll(l => l.Disposed);
        }

        private class PopupLabel : Label
        {
            public float TimeLeft { get; private set; }
            public Vector2 InitialPos { get; set; }

            public PopupLabel()
            {
                ShadowOffsetXOverride = 1;
                ShadowOffsetYOverride = 1;
                FontColorShadowOverride = Color.Black;
            }

            protected override void Update(FrameEventArgs eventArgs)
            {
                TimeLeft += eventArgs.DeltaSeconds;
                LayoutContainer.SetPosition(this, InitialPos - (0, 20 * (TimeLeft * TimeLeft + TimeLeft)));
                if (TimeLeft > 0.5f)
                {
                    Modulate = Color.White.WithAlpha(1f - 0.2f * (float)Math.Pow(TimeLeft - 0.5f, 3f));
                }
            }
        }
    }<commit_msg>Make popup messages track the source entity (#2414)<commit_after>#nullable enable
using System;
using System.Collections.Generic;
using Content.Client.Interfaces;
using Content.Client.UserInterface.Stylesheets;
using Content.Shared;
using Robust.Client.Interfaces.Console;
using Robust.Client.Interfaces.Graphics.ClientEye;
using Robust.Client.Interfaces.Input;
using Robust.Client.Interfaces.UserInterface;
using Robust.Client.Player;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
using Robust.Shared.Utility;

namespace Content.Client
{
    public class ClientNotifyManager : SharedNotifyManager, IClientNotifyManager
    {
        [Dependency] private readonly IPlayerManager _playerManager = default!;
        [Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
        [Dependency] private readonly IInputManager _inputManager = 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 List<PopupLabel>();
        private bool _initialized;

        public void Initialize()
        {
            DebugTools.Assert(!_initialized);

            _netManager.RegisterNetMessage<MsgDoNotifyCursor>(nameof(MsgDoNotifyCursor), DoNotifyCursor);
            _netManager.RegisterNetMessage<MsgDoNotifyCoordinates>(nameof(MsgDoNotifyCoordinates), DoNotifyCoordinates);
            _netManager.RegisterNetMessage<MsgDoNotifyEntity>(nameof(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);
            var minimumSize = label.CombinedMinimumSize;

            LayoutContainer.SetPosition(label, label.InitialPos = coordinates.Position - minimumSize / 2);
            _aliveLabels.Add(label);
        }

        public void PopupMessage(string message)
        {
            PopupMessage(new ScreenCoordinates(_inputManager.MouseScreenPosition), 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 - CombinedMinimumSize / 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));
                }
            }
        }
    }
{'fraction_non_alphanumeric': 0.07648287764556272, 'fraction_numerical': 0.0033608865473703335, 'mean_word_length': 2.2145985401459853, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3295300', 'n_tokens_mistral': 2796, 'n_tokens_neox': 2664, 'n_words': 548}