Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
176 lines
4.9 KiB
C#
176 lines
4.9 KiB
C#
using System.Numerics;
|
|
using System.Text;
|
|
using Content.Client.UserInterface.Controls;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Client.UserInterface;
|
|
using Robust.Client.UserInterface.Controls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
|
|
namespace Content.Client.Lobby.UI;
|
|
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class LobbyCharacterPreviewPanel : Control
|
|
{
|
|
[Dependency] private readonly IEntityManager _entManager = default!;
|
|
|
|
public Button CharacterSetupButton => CharacterSetup;
|
|
|
|
public event Action? OnChangePetRequested;
|
|
|
|
private EntityUid? _previewDummy;
|
|
private EntityUid? _petPreviewDummy;
|
|
private SpriteView? _characterSpriteView;
|
|
private SpriteView? _petSpriteView;
|
|
|
|
public LobbyCharacterPreviewPanel()
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
IoCManager.InjectDependencies(this);
|
|
ChangePetButton.OnPressed += OnChangePetButtonPressed;
|
|
}
|
|
|
|
private void OnChangePetButtonPressed(BaseButton.ButtonEventArgs args)
|
|
{
|
|
OnChangePetRequested?.Invoke();
|
|
}
|
|
|
|
public void SetLoaded(bool value)
|
|
{
|
|
Loaded.Visible = value;
|
|
Unloaded.Visible = !value;
|
|
}
|
|
|
|
// Sunrise-Start
|
|
public void SetSummaryText(string value)
|
|
{
|
|
var wrappedText = WrapText(value, 350);
|
|
Summary.Text = wrappedText;
|
|
}
|
|
|
|
private string WrapText(string text, float maxWidth)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
return string.Empty;
|
|
|
|
var font = Summary.FontOverride ?? UserInterfaceManager.ThemeDefaults.LabelFont;
|
|
var uiScale = UIScale;
|
|
var words = text.Split(' ');
|
|
var lines = new List<string>();
|
|
var currentLine = new StringBuilder();
|
|
|
|
foreach (var word in words)
|
|
{
|
|
var testLine = currentLine.Length > 0 ? $"{currentLine} {word}" : word;
|
|
var testWidth = MeasureTextWidth(testLine, font, uiScale);
|
|
|
|
if (testWidth <= maxWidth * uiScale)
|
|
{
|
|
if (currentLine.Length > 0)
|
|
currentLine.Append(' ');
|
|
currentLine.Append(word);
|
|
}
|
|
else
|
|
{
|
|
if (currentLine.Length > 0)
|
|
{
|
|
lines.Add(currentLine.ToString());
|
|
currentLine.Clear();
|
|
}
|
|
currentLine.Append(word);
|
|
}
|
|
}
|
|
|
|
if (currentLine.Length > 0)
|
|
lines.Add(currentLine.ToString());
|
|
|
|
return string.Join("\n", lines);
|
|
}
|
|
|
|
private float MeasureTextWidth(string text, Font font, float uiScale)
|
|
{
|
|
float width = 0;
|
|
foreach (var rune in text.EnumerateRunes())
|
|
{
|
|
if (font.TryGetCharMetrics(rune, uiScale, out var metrics))
|
|
{
|
|
width += metrics.Advance;
|
|
}
|
|
}
|
|
return width;
|
|
}
|
|
// Sunrise-End
|
|
|
|
public void SetSprite(EntityUid uid)
|
|
{
|
|
if (_previewDummy != null)
|
|
{
|
|
_entManager.DeleteEntity(_previewDummy);
|
|
}
|
|
|
|
_previewDummy = uid;
|
|
|
|
// Удаляем старый спрайт персонажа
|
|
if (_characterSpriteView != null)
|
|
{
|
|
ViewBox.RemoveChild(_characterSpriteView);
|
|
_characterSpriteView = null;
|
|
}
|
|
|
|
_characterSpriteView = new SpriteView
|
|
{
|
|
OverrideDirection = Direction.South,
|
|
Scale = new Vector2(4f, 4f),
|
|
MaxSize = new Vector2(112, 112),
|
|
Stretch = SpriteView.StretchMode.Fill,
|
|
};
|
|
_characterSpriteView.SetEntity(uid);
|
|
ViewBox.AddChild(_characterSpriteView);
|
|
}
|
|
|
|
public void SetPetSprite(EntityUid? uid)
|
|
{
|
|
if (_petPreviewDummy != null && (uid == null || _petPreviewDummy != uid))
|
|
{
|
|
_entManager.DeleteEntity(_petPreviewDummy);
|
|
}
|
|
|
|
if (_petSpriteView != null)
|
|
{
|
|
ViewBox.RemoveChild(_petSpriteView);
|
|
_petSpriteView = null;
|
|
}
|
|
|
|
if (uid == null || !uid.Value.IsValid())
|
|
{
|
|
_petPreviewDummy = null;
|
|
return;
|
|
}
|
|
|
|
_petPreviewDummy = uid;
|
|
|
|
_petSpriteView = new SpriteView
|
|
{
|
|
OverrideDirection = Direction.South,
|
|
Scale = new Vector2(2f, 2f),
|
|
MaxSize = new Vector2(80, 80),
|
|
Stretch = SpriteView.StretchMode.Fill,
|
|
VerticalAlignment = Control.VAlignment.Bottom,
|
|
};
|
|
_petSpriteView.SetEntity(uid.Value);
|
|
ViewBox.AddChild(_petSpriteView);
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
if (!disposing)
|
|
return;
|
|
|
|
ChangePetButton.OnPressed -= OnChangePetButtonPressed;
|
|
_entManager.DeleteEntity(_previewDummy);
|
|
_entManager.DeleteEntity(_petPreviewDummy);
|
|
_previewDummy = null;
|
|
_petPreviewDummy = null;
|
|
}
|
|
}
|