// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using Doozy.Editor.EditorUI.Utils; using Doozy.Runtime.UIElements.Extensions; using UnityEngine; using UnityEngine.UIElements; namespace Doozy.Editor.EditorUI.Components { /// Component with two labels (title and description) that can be set in either horizontal or vertical layout public class FluidDualLabel : VisualElement { public enum Layout { Horizontal, Vertical } internal static Font titleFont => EditorFonts.Inter.Light; internal static Font descriptionFont => EditorFonts.Ubuntu.Regular; internal static Color titleColor => EditorColors.Default.TextTitle; internal static Color descriptionColor => EditorColors.Default.TextSubtitle; private ElementSize elementSize { get; set; } public VisualElement root { get; private set; } public Label titleLabel { get; private set; } public Label descriptionLabel { get; private set; } public FluidDualLabel() { Initialize(); Compose(); this .ResetTitleColor() .ResetDescriptionColor() .SetLayout(Layout.Horizontal) .SetElementSize(ElementSize.Normal); } public FluidDualLabel(string title, string description) : this() { this.titleLabel.text = title; this.descriptionLabel.text = description; } protected virtual void Initialize() { Add(root = new VisualElement().SetStyleFlexGrow(1)); titleLabel = new Label() .ResetLayout() .SetStyleUnityFont(titleFont); descriptionLabel = new Label() .ResetLayout() .SetStyleUnityFont(descriptionFont); } protected virtual void Compose() { root .AddChild(titleLabel) .AddChild(descriptionLabel); } internal void UpdateElementSize(ElementSize size) { int titleSize = 10; int descriptionSize = 9; switch (size) { case ElementSize.Tiny: titleSize = 8; descriptionSize = 7; break; case ElementSize.Small: titleSize = 9; descriptionSize = 8; break; case ElementSize.Normal: titleSize = 10; descriptionSize = 9; break; case ElementSize.Large: titleSize = 11; descriptionSize = 10; break; default: throw new ArgumentOutOfRangeException(nameof(size), size, null); } titleLabel.SetStyleFontSize(titleSize); descriptionLabel.SetStyleFontSize(descriptionSize); } } public static class FluidInfoLabelExtensions { /// Set title text color /// Target /// Text color public static T SetTitleColor(this T target, Color color) where T : FluidDualLabel { target.titleLabel.SetStyleColor(color); return target; } /// Set description text color /// Target /// Text color public static T SetDescriptionTextColor(this T target, Color color) where T : FluidDualLabel { target.descriptionLabel.SetStyleColor(color); return target; } /// Reset title color /// Target public static T ResetTitleColor(this T target) where T : FluidDualLabel => target.SetTitleColor(FluidDualLabel.titleColor); /// Reset description color /// Target public static T ResetDescriptionColor(this T target) where T : FluidDualLabel => target.SetDescriptionTextColor(FluidDualLabel.descriptionColor); /// Show title /// Target public static T ShowTitle(this T target) where T : FluidDualLabel { target.titleLabel.SetStyleDisplay(DisplayStyle.Flex); return target; } /// Hide title /// Target public static T HideTitle(this T target) where T : FluidDualLabel { target.titleLabel.SetStyleDisplay(DisplayStyle.None); return target; } /// Show description /// Target public static T ShowDescription(this T target) where T : FluidDualLabel { target.descriptionLabel.SetStyleDisplay(DisplayStyle.Flex); return target; } /// Hide description /// Target public static T HideDescription(this T target) where T : FluidDualLabel { target.descriptionLabel.SetStyleDisplay(DisplayStyle.None); return target; } /// Set element size /// Target /// Element size public static T SetElementSize(this T target, ElementSize size) where T : FluidDualLabel { target.UpdateElementSize(size); return target; } /// Set title text /// Target /// Title text public static T SetTitle(this T target, string text) where T : FluidDualLabel { target.titleLabel.text = text; return target; } /// Set description text /// Target /// Description text public static T SetDescription(this T target, string text) where T : FluidDualLabel { target.descriptionLabel.text = text; return target; } /// Set horizontal or vertical layout /// Target /// Horizontal or Vertical layout public static T SetLayout(this T target, FluidDualLabel.Layout layout) where T : FluidDualLabel { target.titleLabel.ClearMargins(); switch (layout) { case FluidDualLabel.Layout.Horizontal: target.root .SetStyleFlexDirection(FlexDirection.Row); target.titleLabel .SetStyleMarginRight(DesignUtils.k_Spacing) .SetStyleTextAlign(TextAnchor.MiddleLeft) .SetStyleFlexGrow(0); target.descriptionLabel .SetStyleTextAlign(TextAnchor.MiddleLeft) .SetStyleFlexGrow(1); break; case FluidDualLabel.Layout.Vertical: target.root .SetStyleFlexDirection(FlexDirection.Column); target.titleLabel .SetStyleMarginBottom(DesignUtils.k_Spacing) .SetStyleTextAlign(TextAnchor.UpperLeft) .SetStyleFlexGrow(1); target.descriptionLabel .SetStyleTextAlign(TextAnchor.UpperLeft) .SetStyleFlexGrow(1); break; default: throw new ArgumentOutOfRangeException(nameof(layout), layout, null); } return target; } } }