// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System.Runtime.CompilerServices;
namespace FlaxEngine
{
///
/// Font reference that defines the font asset and font size to use.
///
public class FontReference
{
[NoSerialize]
private FontAsset _font;
[NoSerialize]
private float _size;
[NoSerialize]
private Font _cachedFont;
///
/// Initializes a new instance of the struct.
///
public FontReference()
{
_font = null;
_size = 30;
_cachedFont = null;
}
///
/// Initializes a new instance of the struct.
///
/// The font.
/// The font size.
public FontReference(FontAsset font, float size)
{
_font = font;
_size = size;
_cachedFont = null;
}
///
/// Initializes a new instance of the struct.
///
/// The other font reference.
public FontReference(FontReference other)
{
_font = other._font;
_size = other._size;
_cachedFont = other._cachedFont;
}
///
/// Initializes a new instance of the struct.
///
/// The font.
public FontReference(Font font)
{
if (font)
{
_font = font.Asset;
_size = font.Size;
}
else
{
_font = null;
_size = 30;
}
_cachedFont = font;
}
///
/// The font asset.
///
[EditorOrder(0), Tooltip("The font asset to use as characters source.")]
public FontAsset Font
{
get => _font;
set
{
if (_font != value)
{
_font = value;
_cachedFont = null;
}
}
}
///
/// The size of the font characters.
///
[EditorOrder(10), Limit(1, 500, 0.5f), Tooltip("The size of the font characters.")]
public float Size
{
get => _size;
set
{
if (_size != value)
{
_size = value;
_cachedFont = null;
}
}
}
///
/// Gets the font object described by the structure.
///
/// The font or null if descriptor is invalid.
public Font GetFont()
{
if (_cachedFont)
return _cachedFont;
if (_font)
_cachedFont = _font.CreateFont(_size);
return _cachedFont;
}
///
/// Gets the bold font object described by the structure.
///
/// The bold font asset.
public FontReference GetBold()
{
return new FontReference(_font?.GetBold(), _size);
}
///
/// Gets the italic font object described by the structure.
///
/// The bold font asset.
public FontReference GetItalic()
{
return new FontReference(_font?.GetItalic(), _size);
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
/// true if the specified is equal to this instance; otherwise, false.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(FontReference other)
{
return !(other is null) && _font == other._font && _size == other._size;
}
///
/// Compares two font references.
///
/// The left.
/// The right.
/// True if font references are equal, otherwise false.
public static bool operator ==(FontReference lhs, FontReference rhs)
{
if (lhs is null)
return rhs is null;
return lhs.Equals(rhs);
}
///
/// Compares two font references.
///
/// The left.
/// The right.
/// True if font references are not equal, otherwise false.
public static bool operator !=(FontReference lhs, FontReference rhs)
{
if (lhs is null)
return !(rhs is null);
return !lhs.Equals(rhs);
}
///
public override bool Equals(object other)
{
if (!(other is FontReference))
return false;
var fontReference = (FontReference)other;
return Equals(fontReference);
}
///
public override int GetHashCode()
{
unchecked
{
int hashCode = _font ? _font.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ _size.GetHashCode();
return hashCode;
}
}
///
public override string ToString()
{
return string.Format("{0}, size {1}", _font ? _font.ToString() : string.Empty, _size);
}
}
}