// 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 System.Collections.Generic; using Doozy.Editor.EditorUI.Components.Internal; using Doozy.Editor.EditorUI.ScriptableObjects.Colors; using Doozy.Editor.Reactor.Internal; using Doozy.Editor.UIElements; using Doozy.Runtime.Colors; using Doozy.Runtime.Common.Extensions; using Doozy.Runtime.Reactor.Extensions; using Doozy.Runtime.Reactor.Internal; using Doozy.Runtime.Reactor.Reactions; using Doozy.Runtime.UIElements; using Doozy.Runtime.UIElements.Extensions; using UnityEngine; using UnityEngine.Events; using UnityEngine.UIElements; namespace Doozy.Editor.EditorUI.Components { public class FluidButton : PoolableElement { public override void Reset() { ResetElementSize(); ResetLayoutOrientation(); ResetButtonStyle(); ResetAnimationTrigger(); this.SetEnabled(true); this.ResetLayout(); this.SetTooltip(string.Empty); this.SetName(string.Empty); this.ResetAccentColor(); this.ClearIcon(); this.ClearLabelText(); this.ClearInfoContainer(); this.ClearOnClick(); this.SetSelectionState(SelectionState.Normal); buttonLabel.SetStyleTextAlign(TextAnchor.MiddleCenter); } public override void Dispose() { base.Dispose(); iconReaction?.Recycle(); } #region ElementSize private ElementSize elementSize { get; set; } private List elementSizeDependentElements { get; } public FluidButton SetElementSize(ElementSize value) { UIElementsUtils.RemoveClass(elementSize.ToString(), elementSizeDependentElements); UIElementsUtils.AddClass(value.ToString(), elementSizeDependentElements); elementSize = value; return this; } public FluidButton ResetElementSize() => SetElementSize(ElementSize.Normal); #endregion #region LayoutOrientation private LayoutOrientation layoutOrientation { get; set; } private List layoutOrientationDependentElements { get; } public FluidButton SetLayoutOrientation(LayoutOrientation value) { UIElementsUtils.RemoveClass(layoutOrientation.ToString(), layoutOrientationDependentElements); UIElementsUtils.AddClass(value.ToString(), layoutOrientationDependentElements); layoutOrientation = value; return this; } public FluidButton ResetLayoutOrientation() => SetLayoutOrientation(LayoutOrientation.Horizontal); #endregion #region ButtonStyle private ButtonStyle buttonStyle { get; set; } private List buttonStyleDependentElements { get; } public FluidButton SetButtonStyle(ButtonStyle value) { UIElementsUtils.RemoveClass(buttonStyle.ToString(), buttonStyleDependentElements); UIElementsUtils.AddClass(value.ToString(), buttonStyleDependentElements); buttonStyle = value; fluidElement.StateChanged(); return this; } public FluidButton ResetButtonStyle() => SetButtonStyle(ButtonStyle.Clear); #endregion #region ButtonAnimationTrigger private ButtonAnimationTrigger animationTrigger { get; set; } public FluidButton SetAnimationTrigger(ButtonAnimationTrigger value) { animationTrigger = value; return this; } public FluidButton ResetAnimationTrigger() => SetAnimationTrigger(ButtonAnimationTrigger.OnPointerEnter); #endregion #region IconType private IconType iconType { get; set; } = IconType.None; private List iconDependentComponents { get; } internal void UpdateIconType(IconType value) { if (iconType != IconType.None) UIElementsUtils.RemoveClass(iconType.ToString(), iconDependentComponents); if (value != IconType.None) UIElementsUtils.AddClass(value.ToString(), iconDependentComponents); iconType = value; } #endregion public FluidElement fluidElement { get; } public SelectionState selectionState { get => fluidElement.selectionState; set => fluidElement.selectionState = value; } private static Font font => EditorFonts.Ubuntu.Light; public TemplateContainer templateContainer { get; } public VisualElement layoutContainer { get; } public VisualElement buttonContainer { get; } public Image buttonIcon { get; } public Label buttonLabel { get; } public VisualElement infoContainer { get; } public Texture2DReaction iconReaction { get; internal set; } public UnityAction OnClick; public static FluidButton Get(string labelText, string tooltip = "") => Get().SetLabelText(labelText).SetTooltip(tooltip); public static FluidButton Get(string labelText, Texture2D icon, string tooltip = "") => Get().SetLabelText(labelText).SetIcon(icon).SetTooltip(tooltip); public static FluidButton Get(string labelText, IEnumerable iconTextures, string tooltip = "") => Get().SetLabelText(labelText).SetIcon(iconTextures).SetTooltip(tooltip); public static FluidButton Get(string labelText, EditorSelectableColorInfo accentColor, string tooltip = "") => Get().SetLabelText(labelText).SetAccentColor(accentColor).SetTooltip(tooltip); public static FluidButton Get(string labelText, Texture2D icon, EditorSelectableColorInfo accentColor, string tooltip = "") => Get().SetLabelText(labelText).SetIcon(icon).SetAccentColor(accentColor).SetTooltip(tooltip); public static FluidButton Get(string labelText, IEnumerable iconTextures, EditorSelectableColorInfo accentColor, string tooltip = "") => Get().SetLabelText(labelText).SetIcon(iconTextures).SetAccentColor(accentColor).SetTooltip(tooltip); public static FluidButton Get(Texture2D icon, string tooltip = "") => Get().SetIcon(icon).SetTooltip(tooltip); public static FluidButton Get(Texture2D icon, EditorSelectableColorInfo accentColor, string tooltip = "") => Get().SetIcon(icon).SetAccentColor(accentColor).SetTooltip(tooltip); public static FluidButton Get(IEnumerable iconTextures, string tooltip = "") => Get().SetIcon(iconTextures).SetTooltip(tooltip); public static FluidButton Get(IEnumerable iconTextures, EditorSelectableColorInfo accentColor, string tooltip = "") => Get().SetIcon(iconTextures).SetAccentColor(accentColor).SetTooltip(tooltip); /// /// Do not use this as new FluidButton() as that option does not use the internal pooling system /// use: 'FluidButton.Get()' to get a new instance (this uses the internal pooling system) /// public FluidButton() { Add(templateContainer = EditorLayouts.EditorUI.FluidButton.CloneTree()); templateContainer .AddStyle(EditorStyles.EditorUI.FieldIcon) .AddStyle(EditorStyles.EditorUI.FieldName) .AddStyle(EditorStyles.EditorUI.FluidButton); layoutContainer = templateContainer.Q(nameof(layoutContainer)); buttonContainer = layoutContainer.Q(nameof(buttonContainer)); buttonIcon = layoutContainer.Q(nameof(buttonIcon)); buttonLabel = layoutContainer.Q