From e024897b7df1cd0f1b2f8a9390c10e2af008d383 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 18 Sep 2024 23:05:42 +0200 Subject: [PATCH] Refactor `Button` to inherit from `Label` and get all of its functionalities #2599 --- Source/Engine/UI/GUI/Common/Button.cs | 60 +++++++-------------------- 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/Source/Engine/UI/GUI/Common/Button.cs b/Source/Engine/UI/GUI/Common/Button.cs index aa25e0b1b..7d70dd3e3 100644 --- a/Source/Engine/UI/GUI/Common/Button.cs +++ b/Source/Engine/UI/GUI/Common/Button.cs @@ -8,7 +8,7 @@ namespace FlaxEngine.GUI /// Button control /// [ActorToolbox("GUI")] - public class Button : ContainerControl + public class Button : Label { /// /// The default height for the buttons. @@ -20,47 +20,16 @@ namespace FlaxEngine.GUI /// protected bool _isPressed; - /// - /// The font. - /// - protected FontReference _font; - - /// - /// The text. - /// - protected LocalizedString _text = new LocalizedString(); - - /// - /// Button text property. - /// - [EditorOrder(10), Tooltip("The button label text.")] - public LocalizedString Text - { - get => _text; - set => _text = value; - } - - /// - /// Gets or sets the font used to draw button text. - /// - [EditorDisplay("Text Style"), EditorOrder(2022), ExpandGroups] - public FontReference Font - { - get => _font; - set => _font = value; - } - /// /// Gets or sets the custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data. + /// [Deprecated on 18.09.2024, expires on 18.09.2026] /// - [EditorDisplay("Text Style"), EditorOrder(2021), Tooltip("Custom material used to render the text. It must has domain set to GUI and have a public texture parameter named Font used to sample font atlas texture with font characters data.")] - public MaterialBase TextMaterial { get; set; } - - /// - /// Gets or sets the color used to draw button text. - /// - [EditorDisplay("Text Style"), EditorOrder(2020)] - public Color TextColor; + [Serialize, Obsolete("Use Material property instead."), NoUndo] + public MaterialBase TextMaterial + { + get => Material; + set => Material = value; + } /// /// Gets or sets the background color when button is highlighted. @@ -79,7 +48,7 @@ namespace FlaxEngine.GUI /// [EditorDisplay("Border Style"), EditorOrder(2010), ExpandGroups] public bool HasBorder { get; set; } = true; - + /// /// Gets or sets the border thickness. /// @@ -147,6 +116,7 @@ namespace FlaxEngine.GUI public Button(float x, float y, float width = 120, float height = DefaultHeight) : base(x, y, width, height) { + AutoFocus = true; var style = Style.Current; if (style != null) { @@ -224,17 +194,14 @@ namespace FlaxEngine.GUI /// public override void DrawSelf() { - // Cache data Rectangle clientRect = new Rectangle(Float2.Zero, Size); bool enabled = EnabledInHierarchy; Color backgroundColor = BackgroundColor; Color borderColor = BorderColor; - Color textColor = TextColor; if (!enabled) { backgroundColor *= 0.5f; borderColor *= 0.5f; - textColor *= 0.6f; } else if (_isPressed) { @@ -256,7 +223,10 @@ namespace FlaxEngine.GUI Render2D.DrawRectangle(clientRect, borderColor, BorderThickness); // Draw text - Render2D.DrawText(_font?.GetFont(), TextMaterial, _text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center); + backgroundColor = BackgroundColor; + BackgroundColor = Color.Transparent; // Skip background drawing in Control + base.DrawSelf(); + BackgroundColor = backgroundColor; } /// @@ -321,7 +291,7 @@ namespace FlaxEngine.GUI OnClick(); return true; } - + if (button == MouseButton.Left && !_isPressed) { OnPressBegin();