// Copyright (c) Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine.GUI
{
///
/// Implementation of for .
///
///
public sealed class SpriteBrush : IBrush, IEquatable
{
///
/// The sprite.
///
[ExpandGroups, EditorOrder(0), Tooltip("The sprite.")]
public SpriteHandle Sprite;
///
/// The texture sampling filter mode.
///
[ExpandGroups, EditorOrder(1), Tooltip("The texture sampling filter mode.")]
public BrushFilter Filter = BrushFilter.Linear;
///
/// Initializes a new instance of the class.
///
public SpriteBrush()
{
}
///
/// Initializes a new instance of the struct.
///
/// The sprite.
public SpriteBrush(SpriteHandle sprite)
{
Sprite = sprite;
}
///
public Float2 Size => Sprite.IsValid ? Sprite.Size : Float2.Zero;
///
public void Draw(Rectangle rect, Color color)
{
if (Filter == BrushFilter.Point)
Render2D.DrawSpritePoint(Sprite, rect, color);
else
Render2D.DrawSprite(Sprite, rect, color);
}
///
public bool Equals(SpriteBrush other)
{
return other != null && Sprite == other.Sprite && Filter == other.Filter;
}
///
public override bool Equals(object obj)
{
return obj is SpriteBrush other && Equals(other);
}
///
public override int GetHashCode()
{
return HashCode.Combine(Sprite, (int)Filter);
}
///
public int CompareTo(object obj)
{
return Equals(obj) ? 1 : 0;
}
}
///
/// Implementation of for using 9-slicing.
///
///
public sealed class Sprite9SlicingBrush : IBrush
{
///
/// The sprite.
///
[ExpandGroups, EditorOrder(0), Tooltip("The sprite.")]
public SpriteHandle Sprite;
///
/// The texture sampling filter mode.
///
[ExpandGroups, EditorOrder(1), Tooltip("The texture sampling filter mode.")]
public BrushFilter Filter = BrushFilter.Linear;
///
/// The border size.
///
[ExpandGroups, EditorOrder(2), Limit(0), Tooltip("The border size.")]
public float BorderSize = 10.0f;
///
/// The sprite borders (in texture space, range 0-1).
///
[ExpandGroups, EditorOrder(3), Limit(0, 1), Tooltip("The sprite borders (in texture space, range 0-1).")]
public Margin Border = new Margin(0.1f);
#if FLAX_EDITOR
///
/// Displays borders (editor only).
///
[NoSerialize, EditorOrder(4), Tooltip("Displays borders (editor only).")]
public bool ShowBorders;
#endif
///
/// Initializes a new instance of the class.
///
public Sprite9SlicingBrush()
{
}
///
public Float2 Size => Sprite.IsValid ? Sprite.Size : Float2.Zero;
///
public unsafe void Draw(Rectangle rect, Color color)
{
if (!Sprite.IsValid)
return;
var border = Border;
var borderUV = *(Float4*)&border;
var borderSize = new Float4(BorderSize, BorderSize, BorderSize, BorderSize);
if (Filter == BrushFilter.Point)
Render2D.Draw9SlicingSpritePoint(Sprite, rect, borderSize, borderUV, color);
else
Render2D.Draw9SlicingSprite(Sprite, rect, borderSize, borderUV, color);
#if FLAX_EDITOR
if (ShowBorders)
{
var bordersRect = rect;
bordersRect.Location.X += borderSize.X;
bordersRect.Location.Y += borderSize.Z;
bordersRect.Size.X -= borderSize.X + borderSize.Y;
bordersRect.Size.Y -= borderSize.Z + borderSize.W;
Render2D.DrawRectangle(bordersRect, Color.YellowGreen, 2.0f);
}
#endif
}
///
public bool Equals(Sprite9SlicingBrush other)
{
return other != null && Sprite == other.Sprite && Filter == other.Filter;
}
///
public override bool Equals(object obj)
{
return obj is Sprite9SlicingBrush other && Equals(other);
}
///
public override int GetHashCode()
{
return HashCode.Combine(Sprite, (int)Filter);
}
///
public int CompareTo(object obj)
{
return Equals(obj) ? 1 : 0;
}
}
}