mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-02 18:17:09 +02:00
* Refactor UI system. Deferred updating is used for styling & layout. This fixes the awful time complexity of containers. Removed SetDefaults and Initialize. They were a bad idea alright. * Fix build on .NET Framework.
100 lines
2.4 KiB
C#
100 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using JetBrains.Annotations;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Client.Graphics.Drawing;
|
|
using Robust.Shared.Maths;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Robust.Client.UserInterface.Controls
|
|
{
|
|
public class RichTextLabel : Control
|
|
{
|
|
private FormattedMessage _message;
|
|
private RichTextEntry _entry;
|
|
|
|
public float? MaxWidth { get; set; }
|
|
|
|
public void SetMessage(FormattedMessage message)
|
|
{
|
|
_message = message;
|
|
_entry = new RichTextEntry(_message);
|
|
_updateEntry();
|
|
}
|
|
|
|
public void SetMessage(string message)
|
|
{
|
|
var msg = new FormattedMessage();
|
|
msg.AddText(message);
|
|
SetMessage(msg);
|
|
}
|
|
|
|
protected override Vector2 CalculateMinimumSize()
|
|
{
|
|
if (_message == null)
|
|
{
|
|
return Vector2.Zero;
|
|
}
|
|
|
|
var width = 0f;
|
|
if (MaxWidth.HasValue)
|
|
{
|
|
width = _entry.Width / UIScale;
|
|
}
|
|
return (width, _entry.Height / UIScale);
|
|
}
|
|
|
|
private void _updateEntry()
|
|
{
|
|
var font = _getFont();
|
|
|
|
if (_message != null)
|
|
{
|
|
var oldHeight = _entry.Height;
|
|
var oldWidth = _entry.Width;
|
|
_entry.Update(font, MaxWidth ?? Width, UIScale);
|
|
if (oldHeight != _entry.Height || MaxWidth != null && _entry.Width != oldWidth)
|
|
{
|
|
MinimumSizeChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void StylePropertiesChanged()
|
|
{
|
|
base.StylePropertiesChanged();
|
|
|
|
_updateEntry();
|
|
}
|
|
|
|
protected internal override void Draw(DrawingHandleScreen handle)
|
|
{
|
|
base.Draw(handle);
|
|
|
|
if (_message == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_entry.Draw(handle, _getFont(), SizeBox, 0, new Stack<FormattedMessage.Tag>(), UIScale);
|
|
}
|
|
|
|
protected override void Resized()
|
|
{
|
|
base.Resized();
|
|
|
|
_updateEntry();
|
|
}
|
|
|
|
[Pure]
|
|
private Font _getFont()
|
|
{
|
|
if (TryGetStyleProperty("font", out Font font))
|
|
{
|
|
return font;
|
|
}
|
|
|
|
return UserInterfaceManager.ThemeDefaults.DefaultFont;
|
|
}
|
|
}
|
|
}
|