fish-station/Content.Client/Popups/PopupUIController.cs
ThereDrD d07cbcda6e
Красивые циферки урона (#993)
* damage overlay refactor: new visuals and ignored damage types feature

* remove unused popup types

* add horizontal direction movement

* fix default popup type

* fix fix

* fix fix fix

* add damage overlay to structures

* comments + better implementation

* add ui dropdown option

* better values

* fix missing disabled sessions check

* asdasdadasd new commit on my pr

* ЧЕ БЛЯДЬ СЛОЖНО ДА

* ы

---------

Co-authored-by: Vigers Ray <vigersray@gmail.com>
2025-01-05 14:51:20 +03:00

149 lines
5 KiB
C#

using System.Numerics;
using Content.Client.Gameplay;
using Content.Shared.Popups;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controllers;
namespace Content.Client.Popups;
/// <summary>
/// Handles screens-space popups. World popups are handled via PopupOverlay.
/// </summary>
public sealed class PopupUIController : UIController, IOnStateEntered<GameplayState>, IOnStateExited<GameplayState>
{
[UISystemDependency] private readonly PopupSystem? _popup = default!;
private Font _smallFont = default!;
private Font _mediumFont = default!;
private Font _largeFont = default!;
private PopupRootControl? _popupControl;
public override void Initialize()
{
base.Initialize();
var cache = IoCManager.Resolve<IResourceCache>();
_smallFont = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Italic.ttf"), 10);
_mediumFont = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Italic.ttf"), 12);
_largeFont = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-BoldItalic.ttf"), 14);
}
public void OnStateEntered(GameplayState state)
{
_popupControl = new PopupRootControl(_popup, this);
UIManager.RootControl.AddChild(_popupControl);
}
public void OnStateExited(GameplayState state)
{
if (_popupControl == null)
return;
UIManager.RootControl.RemoveChild(_popupControl);
_popupControl = null;
}
public void DrawPopup(PopupSystem.PopupLabel popup, DrawingHandleScreen handle, Vector2 position, float scale, float horizontalDirection = 0f)
{
var lifetime = PopupSystem.GetPopupLifetime(popup);
// Keep alpha at 1 until TotalTime passes half its lifetime, then gradually decrease to 0.
var alpha = MathF.Min(1f, 1f - MathF.Max(0f, popup.TotalTime - lifetime / 2) * 2 / lifetime);
var font = _smallFont;
var color = Color.White.WithAlpha(alpha);
var useHorizontalDirection = false;
switch (popup.Type)
{
// Sunrise start
case PopupType.SmallFloating:
useHorizontalDirection = true;
break;
// Sunrise end
case PopupType.SmallCaution:
color = Color.Red;
break;
case PopupType.Medium:
font = _mediumFont;
color = Color.LightGray;
break;
case PopupType.MediumCaution:
font = _mediumFont;
color = Color.Red;
break;
// Sunrise start
case PopupType.MediumCautionFloating:
font = _mediumFont;
color = Color.Red;
useHorizontalDirection = true;
break;
// Sunrise end
case PopupType.Large:
font = _largeFont;
color = Color.LightGray;
break;
case PopupType.LargeCaution:
font = _largeFont;
color = Color.Red;
break;
// Sunrise-Start
case PopupType.LargeGreen:
font = _largeFont;
color = Color.LightGreen;
break;
// Sunrise-End
}
// Sunrise edit start
if (!useHorizontalDirection)
horizontalDirection = 0f;
// Calculate horizontal offset based on direction.
var horizontalOffset = horizontalDirection * MathF.Min(8f, 12f * popup.TotalTime);
// Adjust position to move both vertically and horizontally.
var updatedPosition = position + new Vector2(horizontalOffset, -MathF.Min(8f, 12f * (popup.TotalTime * popup.TotalTime + popup.TotalTime)));
// Sunrise edit end
var dimensions = handle.GetDimensions(font, popup.Text, scale);
handle.DrawString(font, updatedPosition - dimensions / 2f, popup.Text, scale, color.WithAlpha(alpha));
}
/// <summary>
/// Handles drawing all screen popups.
/// </summary>
private sealed class PopupRootControl : Control
{
private readonly PopupSystem? _popup;
private readonly PopupUIController _controller;
public PopupRootControl(PopupSystem? system, PopupUIController controller)
{
_popup = system;
_controller = controller;
}
protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
if (_popup == null)
return;
// Different window
var windowId = UserInterfaceManager.RootControl.Window.Id;
foreach (var popup in _popup.CursorLabels)
{
if (popup.InitialPos.Window != windowId)
continue;
_controller.DrawPopup(popup, handle, popup.InitialPos.Position, UIScale);
}
}
}
}