namespace UIWidgets.Styles { using System; using System.Collections.Generic; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; /// /// Style. /// public class Style : ScriptableObject, IStyleDefaultValues, IUpgradeable { /// /// Is default style? /// [SerializeField] [HideInInspector] [Obsolete("Replaced with PrefabsMenu.Instance.DefaultStyle.")] protected bool Default; /// /// Style for the collections. /// [SerializeField] [FormerlySerializedAs("Common")] [FormerlySerializedAs("Short")] public StyleFast Fast; /// /// Style for the collections. /// [Header("Collections")] [Tooltip("ListView's, TiveView's and TreeView's style")] [SerializeField] public StyleCollections Collections; /// /// Style for the TreeView. /// [SerializeField] public StyleTreeView TreeView; /// /// Style for the combobox. /// [SerializeField] public StyleCombobox Combobox; /// /// Style for the Table. /// [SerializeField] public StyleTable Table; /// /// Style for the FileListView. /// [SerializeField] public StyleFileListView FileListView; /// /// Style for the IO errors for collections. /// [SerializeField] public StyleText IOCollectionsErrors; /// /// Style for the DropIndicator. /// [SerializeField] public StyleDropIndicator DropIndicator; /// /// Style for the Connector. /// [SerializeField] public StyleConnector Connector; /// /// Style for the accordion. /// [Header("Containers")] [SerializeField] public StyleAccordion Accordion; /// /// Style for the tabs on top. /// [SerializeField] public StyleTabs TabsTop; /// /// Style for the tabs on left. /// [SerializeField] public StyleTabs TabsLeft; /// /// Style for the Dialog. /// [Header("Dialogs")] [SerializeField] public StyleDialog Dialog; /// /// Style for the Notify. /// [SerializeField] public StyleNotify Notify; /// /// Style for the Autocomplete. /// [Header("Input")] [SerializeField] public StyleAutocomplete Autocomplete; /// /// Style for the big button. /// [SerializeField] public StyleButton ButtonBig; /// /// Style for the small button. /// [SerializeField] public StyleButton ButtonSmall; /// /// Style for the calendar. /// [SerializeField] public StyleCalendar Calendar; /// /// Style for the ScrollBlock. /// [SerializeField] [FormerlySerializedAs("Scroller")] public StyleScroller ScrollBlock; /// /// Style for the horizontal centered slider. /// [SerializeField] public StyleCenteredSlider CenteredSliderHorizontal; /// /// Style for the vertical centered slider. /// [SerializeField] public StyleCenteredSlider CenteredSliderVertical; /// /// Style for the circular slider. /// [SerializeField] public StyleCircularSlider CircularSlider; /// /// Style for the ColorPicker. /// [SerializeField] public StyleColorPicker ColorPicker; /// /// Style for the horizontal color picker range. /// [SerializeField] public StyleColorPickerRange ColorPickerRangeHorizontal; /// /// Style for the vertical color picker range. /// [SerializeField] public StyleColorPickerRange ColorPickerRangeVertical; /// /// Style for the context menu. /// [SerializeField] public StyleContextMenu ContextMenu; /// /// Style for the horizontal range slider. /// [SerializeField] public StyleRangeSlider RangeSliderHorizontal; /// /// Style for the vertical range slider. /// [SerializeField] public StyleRangeSlider RangeSliderVertical; /// /// Style for the scale. /// [SerializeField] public StyleScale Scale; /// /// Style for the spinner. /// [SerializeField] public StyleSpinner Spinner; /// /// Style for the switch. /// [SerializeField] public StyleSwitch Switch; /// /// Style for the time widget. /// [SerializeField] public StyleTime Time; /// /// Style for the audio player. /// [Header("Misc")] [SerializeField] public StyleAudioPlayer AudioPlayer; /// /// Style for the determinate progress bar. /// [SerializeField] public StyleProgressbarDeterminate ProgressbarDeterminate; /// /// Style for the indeterminate progress bar. /// [SerializeField] public StyleProgressbarIndeterminate ProgressbarIndeterminate; /// /// Style for the paginator. /// [SerializeField] public StyleTooltip Tooltip; /// /// Style for the paginator. /// [SerializeField] public StylePaginator Paginator; /// /// Style for the close button. /// [SerializeField] public StyleButtonClose ButtonClose; /// /// Style for the Sidebar. /// [SerializeField] public StyleSidebar Sidebar; /// /// Style for the canvas. /// [Header("Default Unity Widgets")] [SerializeField] public StyleCanvas Canvas; /// /// Style for the text. /// [SerializeField] public StyleText Text; /// /// Style for the horizontal scrollbar. /// [SerializeField] public StyleScrollbar HorizontalScrollbar; /// /// Style for the vertical scrollbar. /// [SerializeField] public StyleScrollbar VerticalScrollbar; /// /// Style for the input field. /// [SerializeField] public StyleInputField InputField; /// /// Style for the button. /// [SerializeField] public StyleUnityButton Button; /// /// Style for the slider. /// [SerializeField] public StyleSlider Slider; /// /// Style for the toggle. /// [SerializeField] public StyleToggle Toggle; /// /// Style for the Dropdown. /// [SerializeField] public StyleDropdown Dropdown; static bool NoTMProSupport(StyleText style, GameObject go) { return false; } /// /// The function to process TMPro gameobject. /// public static readonly Func TMProSupport = #if UIWIDGETS_TMPRO_SUPPORT UIWidgets.TMProSupport.StyleTMPro.ApplyTo; #else NoTMProSupport; #endif /// /// The function to process TMPro gameobject. /// public static readonly Func TMProSupportGetFrom = #if UIWIDGETS_TMPRO_SUPPORT UIWidgets.TMProSupport.StyleTMPro.GetFrom; #else NoTMProSupport; #endif #region ApplyTo /// /// Apply style for the specified component. /// /// true, if style was applied for children gameobjects, false otherwise. /// Component. protected virtual bool ApplyTo(Component component) { var applied_for_children = false; Text.ApplyTo(component as Text); applied_for_children |= ApplyTo(component as Scrollbar); applied_for_children |= ApplyTo(component as InputField); applied_for_children |= ApplyTo(component as Button); applied_for_children |= ApplyTo(component as Slider); applied_for_children |= ApplyTo(component as Toggle); ApplyTo(component as Canvas); #if UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER applied_for_children |= ApplyTo(component as Dropdown); #endif return applied_for_children; } #if UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER /// /// Apply style for the Dropdown. /// /// true, if style was applied for children gameobjects, false otherwise. /// Component. protected virtual bool ApplyTo(Dropdown component) { if (component == null) { return false; } Dropdown.ApplyTo(component, this); return true; } #endif /// /// Apply style for the InputField. /// /// true, if style was applied for children gameobjects, false otherwise. /// Component. protected virtual bool ApplyTo(Toggle component) { if (component == null) { return false; } Toggle.ApplyTo(component); return true; } /// /// Apply style for the Canvas. /// /// Component. protected virtual void ApplyTo(Canvas component) { if (component != null) { Canvas.ApplyTo(component); } } /// /// Apply style for the Slider. /// /// true, if style was applied for children gameobjects, false otherwise. /// Component. protected virtual bool ApplyTo(Slider component) { if (component == null) { return false; } Slider.ApplyTo(component); return true; } /// /// Apply style for the InputField. /// /// true, if style was applied for children gameobjects, false otherwise. /// Component. protected virtual bool ApplyTo(InputField component) { if (component == null) { return false; } InputField.ApplyTo(component); return true; } /// /// Apply style for the button. /// /// true, if style was applied for children gameobjects, false otherwise. /// Component. protected virtual bool ApplyTo(Button component) { if (component == null) { return false; } Button.ApplyTo(component); return true; } /// /// Apply style for the scrollbar. /// /// true, if style was applied for children gameobjects, false otherwise. /// Component. protected virtual bool ApplyTo(Scrollbar component) { if (component == null) { return false; } var scrollbar_style = UtilitiesUI.IsHorizontal(component) ? HorizontalScrollbar : VerticalScrollbar; scrollbar_style.ApplyTo(component); return true; } /// /// Apply style for the gameobject with specified transform. /// /// Transform. /// Is style should be applied only for objects with IStylable component? public virtual void ApplyTo(Transform target, bool stylableOnly = false) { if (target == null) { return; } ApplyTo(target.gameObject, stylableOnly); } /// /// Apply style for the specified gameobject. /// /// Gameobject. /// Is style should be applied only for objects with IStylable component? public virtual void ApplyTo(GameObject target, bool stylableOnly = false) { Upgrade(); var stylable = new List(); Compatibility.GetComponents(target, stylable); var children_processed = false; if (!stylableOnly && (stylable.Count == 0)) { var components = new List(); Compatibility.GetComponents(target, components); foreach (var component in components) { children_processed |= ApplyTo(component); } if (!children_processed) { children_processed |= TMProSupport(Text, target); } } foreach (var component in stylable) { children_processed |= component.SetStyle(this); } if (!children_processed) { var t = target.transform; for (int i = 0; i < t.childCount; i++) { ApplyTo(t.GetChild(i).gameObject, stylableOnly); } } } /// /// Apply style for children gameobject. /// /// Parent. /// Is style should be applied only for objects with IStylable component? public virtual void ApplyForChildren(GameObject parent, bool stylableOnly = false) { var t = parent.transform; for (int i = 0; i < t.childCount; i++) { ApplyTo(t.GetChild(i).gameObject, stylableOnly); } } /// /// Apply style to specified target. /// /// Target. /// Style. public static void ApplyTo(GameObject tagret, Style style) { style.ApplyTo(tagret); } #endregion #region GetFrom /// /// Set style options from the specified component. /// /// true, if children gameobjects was processed, false otherwise. /// Component. protected virtual bool GetFrom(Component component) { var children_processed = false; Text.GetFrom(component as Text); children_processed |= GetFrom(component as Scrollbar); children_processed |= GetFrom(component as InputField); children_processed |= GetFrom(component as Button); children_processed |= GetFrom(component as Slider); children_processed |= GetFrom(component as Toggle); GetFrom(component as Canvas); #if UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER children_processed |= GetFrom(component as Dropdown); #endif return children_processed; } #if UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER /// /// Set style options from the Dropdown. /// /// true, if children gameobjects was processed, false otherwise. /// Component. protected virtual bool GetFrom(Dropdown component) { if (component == null) { return false; } Dropdown.GetFrom(component, this); return true; } #endif /// /// Set style options from the InputField. /// /// true, if children gameobjects was processed, false otherwise. /// Component. protected virtual bool GetFrom(Toggle component) { if (component == null) { return false; } Toggle.GetFrom(component); return true; } /// /// Set style options from the Canvas. /// /// Component. protected virtual void GetFrom(Canvas component) { if (component != null) { Canvas.GetFrom(component); } } /// /// Set style options from the Slider. /// /// true, if children gameobjects was processed, false otherwise. /// Component. protected virtual bool GetFrom(Slider component) { if (component == null) { return false; } Slider.GetFrom(component); return true; } /// /// Set style options from the InputField. /// /// true, if children gameobjects was processed, false otherwise. /// Component. protected virtual bool GetFrom(InputField component) { if (component == null) { return false; } InputField.GetFrom(component); return true; } /// /// Set style options from the button. /// /// true, if children gameobjects was processed, false otherwise. /// Component. protected virtual bool GetFrom(Button component) { if (component == null) { return false; } Button.GetFrom(component); return true; } /// /// Set style options from the scrollbar. /// /// true, if children gameobjects was processed, false otherwise. /// Component. protected virtual bool GetFrom(Scrollbar component) { if (component == null) { return false; } var scrollbar_style = UtilitiesUI.IsHorizontal(component) ? HorizontalScrollbar : VerticalScrollbar; scrollbar_style.GetFrom(component); return true; } /// /// Set style options from the specified gameobject with specified transform. /// /// Transform. /// Is style should be got only for objects with IStylable component? public virtual void GetFrom(Transform target, bool stylableOnly = false) { if (target == null) { return; } GetFrom(target.gameObject, stylableOnly); } /// /// Set style options from the specified gameobject. /// /// Gameobject. /// Is style should be got only for objects with IStylable component? public virtual void GetFrom(GameObject target, bool stylableOnly = false) { var stylable = new List(); Compatibility.GetComponents(target, stylable); var children_processed = false; if (!stylableOnly && (stylable.Count == 0)) { var components = new List(); Compatibility.GetComponents(target, components); foreach (var component in components) { children_processed |= GetFrom(component); } if (!children_processed) { children_processed |= TMProSupportGetFrom(Text, target); } } foreach (var component in stylable) { children_processed |= component.GetStyle(this); } if (!children_processed) { var t = target.transform; for (int i = 0; i < t.childCount; i++) { GetFrom(t.GetChild(i).gameObject, stylableOnly); } } } /// /// Set style options from the children gameobject. /// /// Parent. /// Is style should be got only for objects with IStylable component? public virtual void GetFromChildren(GameObject parent, bool stylableOnly = false) { var t = parent.transform; for (int i = 0; i < t.childCount; i++) { GetFrom(t.GetChild(i).gameObject, stylableOnly); } } /// /// Set style options from the specified target. /// /// Target. /// Style. public static void GetFrom(GameObject tagret, Style style) { style.GetFrom(tagret); } #endregion /// /// Set target if source is asset. /// /// Type of value. /// Source. /// Target. /// true if source is asset; otherwise false. public static bool SetValue(T source, ref T target) where T : UnityEngine.Object { #if UNITY_EDITOR if ((source == null) || AssetDatabase.Contains(source)) { target = source; return true; } #endif return false; } [SerializeField] [HideInInspector] int version; void UpgradeV1() { if (DropIndicator == null) { DropIndicator = new StyleDropIndicator(); #if UNITY_EDITOR DropIndicator.SetDefaultValues(); #endif } if (Collections != null) { DropIndicator.Image.Color = Collections.DefaultColor; } } void UpgradeV2() { if (Connector == null) { Connector = new StyleConnector(); #if UNITY_EDITOR Connector.SetDefaultValues(); #endif } if (Collections != null) { Connector.Color = Collections.DefaultColor; } } void UpgradeV3() { if (ContextMenu == null) { ContextMenu = new StyleContextMenu(); #if UNITY_EDITOR ContextMenu.SetDefaultValues(); #endif } if (Collections != null) { ContextMenu.MainBackground = Collections.MainBackground.Clone(); ContextMenu.ItemText = Collections.DefaultItemText.Clone(); ContextMenu.DelimiterImage.Color = Collections.DefaultColor; ContextMenu.ItemBackgroundSelectable.Colors.normalColor = Collections.DefaultBackgroundColor; ContextMenu.ItemBackgroundSelectable.Colors.highlightedColor = Collections.HighlightedBackgroundColor; ContextMenu.ItemBackgroundSelectable.Colors.pressedColor = Collections.SelectedBackgroundColor; #if UNITY_2019_1_OR_NEWER ContextMenu.ItemBackgroundSelectable.Colors.selectedColor = Collections.SelectedBackgroundColor; #endif ContextMenu.ItemTextSelectable.Colors.normalColor = Collections.DefaultColor; ContextMenu.ItemTextSelectable.Colors.highlightedColor = Collections.HighlightedColor; ContextMenu.ItemTextSelectable.Colors.pressedColor = Collections.SelectedColor; #if UNITY_2019_1_OR_NEWER ContextMenu.ItemTextSelectable.Colors.selectedColor = Collections.SelectedColor; #endif } } void UpgradeV4() { Accordion.ToggleActiveBackground = Paginator.ActiveBackground.Clone(); } void UpgradeV5() { CircularSlider = new StyleCircularSlider(); CircularSlider.Ring = new StyleImage(); CircularSlider.Handle = new StyleImage(); CircularSlider.Arrow = new StyleImage(); #if UNITY_EDITOR CircularSlider.SetDefaultValues(); #endif CircularSlider.RingColor = Collections.DefaultColor; CircularSlider.Arrow.Color = Collections.DefaultColor; CircularSlider.Handle = RangeSliderHorizontal.HandleMin.Clone(); } void UpgradeV6() { Scale = new StyleScale(); Scale.MainLine = new StyleImage(); Scale.MarkLine = new StyleImage(); Scale.MarkLabel = new StyleText(); #if UNITY_EDITOR Scale.SetDefaultValues(); #endif Scale.MainLine.Color = Collections.DefaultColor; Scale.MarkLine.Color = Collections.DefaultColor; Scale.MarkLabel.Color = Collections.DefaultColor; Scale.MarkLabel.Size = 14; } void UpgradeV7() { Time.HourLabel = new StyleText(); #if UNITY_EDITOR Time.HourLabel.SetDefaultValues(); #endif Time.HourLabel.ChangeColor = true; Time.HourLabel.Color = Collections.DefaultColor; } void UpgradeV8() { Accordion.ToggleActiveText = Accordion.ToggleDefaultText.Clone(); Accordion.ToggleActiveText.Color = Collections.DefaultColor; } /// public virtual void Upgrade() { if (version == 0) { UpgradeV1(); version = 1; } if (version == 1) { UpgradeV2(); version = 2; } if (version == 2) { UpgradeV3(); version = 3; } if (version == 3) { UpgradeV4(); version = 4; } if (version == 4) { UpgradeV5(); version = 5; } if (version == 5) { UpgradeV6(); version = 6; } if (version == 6) { UpgradeV7(); version = 7; } if (version == 7) { UpgradeV8(); version = 8; } } #if UNITY_EDITOR /// /// Validate this instance. /// protected virtual void OnValidate() { Upgrade(); } /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Reviewed.")] public virtual void SetDefaultValues() { Fast.SetDefaultValues(); Collections.SetDefaultValues(); TreeView.SetDefaultValues(); Combobox.SetDefaultValues(); Table.SetDefaultValues(); FileListView.SetDefaultValues(); IOCollectionsErrors.SetDefaultValues(); DropIndicator.SetDefaultValues(); Connector.SetDefaultValues(); Accordion.SetDefaultValues(); TabsTop.SetDefaultValues(); TabsLeft.SetDefaultValues(); Dialog.SetDefaultValues(); Notify.SetDefaultValues(); Autocomplete.SetDefaultValues(); ButtonBig.SetDefaultValues(); ButtonSmall.SetDefaultValues(); Calendar.SetDefaultValues(); CenteredSliderHorizontal.SetDefaultValues(); CenteredSliderVertical.SetDefaultValues(); ColorPicker.SetDefaultValues(); ColorPickerRangeHorizontal.SetDefaultValues(); ColorPickerRangeVertical.SetDefaultValues(); ContextMenu.SetDefaultValues(); RangeSliderHorizontal.SetDefaultValues(); RangeSliderVertical.SetDefaultValues(); Spinner.SetDefaultValues(); Switch.SetDefaultValues(); Time.SetDefaultValues(); AudioPlayer.SetDefaultValues(); ProgressbarDeterminate.SetDefaultValues(); ProgressbarIndeterminate.SetDefaultValues(); Tooltip.SetDefaultValues(); Paginator.SetDefaultValues(); ButtonClose.SetDefaultValues(); Canvas.SetDefaultValues(); Text.SetDefaultValues(); HorizontalScrollbar.SetDefaultValues(); VerticalScrollbar.SetDefaultValues(); InputField.SetDefaultValues(); Button.SetDefaultValues(); Slider.SetDefaultValues(); Toggle.SetDefaultValues(); Dropdown.SetDefaultValues(); } /// /// Is it default style? (Editor only) /// /// true if this style is default; otherwise, false. public bool IsDefault() { return PrefabsMenu.Instance.DefaultStyle == this; } /// /// Undo this style as default. (Editor only) /// public void SetAsNotDefault() { Undo.RecordObject(this, "Undo Set Style as Default"); Undo.RecordObject(PrefabsMenu.Instance, "Undo Set Style as Default"); PrefabsMenu.Instance.DefaultStyle = null; EditorUtility.SetDirty(PrefabsMenu.Instance.DefaultStyle); } /// /// Set this style as default. (Editor only) /// public void SetAsDefault() { Undo.RecordObject(PrefabsMenu.Instance, "Undo Set Style as Default"); PrefabsMenu.Instance.DefaultStyle = this; EditorUtility.SetDirty(PrefabsMenu.Instance.DefaultStyle); } /// /// Get the default style (Editor only). /// /// The default style. [Obsolete("Replaced with PrefabsMenu.Instance.DefaultStyle.")] public static Style DefaultStyle() { if (PrefabsMenu.Instance.DefaultStyle != null) { return PrefabsMenu.Instance.DefaultStyle; } foreach (var style in GetStyles()) { #pragma warning disable 0618 if (style.Default) #pragma warning restore 0618 { PrefabsMenu.Instance.DefaultStyle = style; EditorUtility.SetDirty(PrefabsMenu.Instance); return style; } } return null; } /// /// Get the list of all styles (Editor only). /// /// The styles. public static List