From 51ee3de689b3aa3aa048d570309449b9e1d33f7f Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 12 Sep 2024 16:29:14 +0200 Subject: [PATCH] Add Background Brush to the control for more styling #1898 --- Source/Engine/UI/GUI/Control.cs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Source/Engine/UI/GUI/Control.cs b/Source/Engine/UI/GUI/Control.cs index cd7097dae..cb6e11ee0 100644 --- a/Source/Engine/UI/GUI/Control.cs +++ b/Source/Engine/UI/GUI/Control.cs @@ -81,6 +81,7 @@ namespace FlaxEngine.GUI // Style private Color _backgroundColor = Color.Transparent; + private IBrush _backgroundBrush = null; // Tooltip @@ -174,6 +175,25 @@ namespace FlaxEngine.GUI set => _backgroundColor = value; } + /// + /// Gets or sets control background brush used to fill the contents. Uses Background Color property as tint color. + /// + [EditorDisplay("Background Style"), EditorOrder(2001)] + public IBrush BackgroundBrush + { + get => _backgroundBrush; + set + { + _backgroundBrush = value; + +#if FLAX_EDITOR + // Auto-reset background color so brush is visible as it uses it for tint + if (value != null && _backgroundColor == Color.Transparent && FlaxEditor.CustomEditors.CustomEditor.IsSettingValue) + _backgroundColor = Color.White; +#endif + } + } + /// /// Gets or sets the anchor preset used by the control anchors (based on and ). /// @@ -416,9 +436,14 @@ namespace FlaxEngine.GUI public virtual void Draw() { // Paint Background - if (_backgroundColor.A > 0.0f) + var rect = new Rectangle(Float2.Zero, _bounds.Size); + if (BackgroundBrush != null) { - Render2D.FillRectangle(new Rectangle(Float2.Zero, Size), _backgroundColor); + BackgroundBrush.Draw(rect, _backgroundColor); + } + else if (_backgroundColor.A > 0.0f) + { + Render2D.FillRectangle(rect, _backgroundColor); } }