// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. namespace FlaxEngine.GUI { /// /// Implementation of for . /// /// public sealed class TextureBrush : IBrush { /// /// The texture. /// [ExpandGroups, EditorOrder(0), Tooltip("The texture asset.")] public Texture Texture; /// /// 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 TextureBrush() { } /// /// Initializes a new instance of the struct. /// /// The texture. public TextureBrush(Texture texture) { Texture = texture; } /// public Float2 Size => Texture != null && !Texture.WaitForLoaded() ? Texture.Size : Float2.Zero; /// public void Draw(Rectangle rect, Color color) { if (Filter == BrushFilter.Point) Render2D.DrawTexturePoint(Texture?.Texture, rect, color); else Render2D.DrawTexture(Texture, rect, color); } } /// /// Implementation of for using 9-slicing. /// /// public sealed class Texture9SlicingBrush : IBrush { /// /// The texture. /// [ExpandGroups, EditorOrder(0), Tooltip("The texture asset.")] public Texture Texture; /// /// 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 texture borders (in texture space, range 0-1). /// [ExpandGroups, EditorOrder(3), Limit(0, 1), Tooltip("The texture 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 Texture9SlicingBrush() { } /// /// Initializes a new instance of the struct. /// /// The texture. public Texture9SlicingBrush(Texture texture) { Texture = texture; } /// public Float2 Size => Texture?.Size ?? Float2.Zero; /// public unsafe void Draw(Rectangle rect, Color color) { if (Texture == null) return; var border = Border; var borderUV = *(Float4*)&border; var borderSize = borderUV * new Float4(BorderSize, BorderSize, BorderSize, BorderSize); if (Filter == BrushFilter.Point) Render2D.Draw9SlicingTexturePoint(Texture, rect, borderSize, borderUV, color); else Render2D.Draw9SlicingTexture(Texture, 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 } } }