123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
-
- namespace Unity.VisualScripting
- {
- [FuzzyOption(typeof(IUnit))]
- public class UnitOption<TUnit> : IUnitOption where TUnit : IUnit
- {
- public UnitOption()
- {
- sourceScriptGuids = new HashSet<string>();
- }
-
- public UnitOption(TUnit unit) : this()
- {
- this.unit = unit;
-
- FillFromUnit();
- }
-
- [DoNotSerialize]
- protected bool filled { get; private set; }
-
- private TUnit _unit;
-
- protected UnitOptionRow source { get; private set; }
-
- public TUnit unit
- {
- get
- {
- // Load the node on demand to avoid deserialization overhead
- // Deserializing the entire database takes many seconds,
- // which is the reason why UnitOptionRow and SQLite are used
- // in the first place.
-
- if (_unit == null)
- {
- _unit = (TUnit)new SerializationData(source.unit).Deserialize();
- }
-
- return _unit;
- }
- protected set
- {
- _unit = value;
- }
- }
-
- IUnit IUnitOption.unit => unit;
-
- public Type unitType { get; private set; }
-
- protected IUnitDescriptor descriptor => unit.Descriptor<IUnitDescriptor>();
-
- // Avoid using the descriptions for each option, because we don't need all fields described until the option is hovered
-
- protected UnitDescription description => unit.Description<UnitDescription>();
-
- protected UnitPortDescription PortDescription(IUnitPort port)
- {
- return port.Description<UnitPortDescription>();
- }
-
- public virtual IUnit InstantiateUnit()
- {
- var instance = unit.CloneViaSerialization();
- instance.Define();
- return instance;
- }
-
- void IUnitOption.PreconfigureUnit(IUnit unit)
- {
- PreconfigureUnit((TUnit)unit);
- }
-
- public virtual void PreconfigureUnit(TUnit unit)
- {
- }
-
- protected virtual void FillFromUnit()
- {
- unit.EnsureDefined();
- unitType = unit.GetType();
-
- labelHuman = Label(true);
- haystackHuman = Haystack(true);
-
- labelProgrammer = Label(false);
- haystackProgrammer = Haystack(false);
-
- category = Category();
- order = Order();
- favoriteKey = FavoriteKey();
- UnityAPI.Async(() => icon = Icon());
-
- showControlInputsInFooter = ShowControlInputsInFooter();
- showControlOutputsInFooter = ShowControlOutputsInFooter();
- showValueInputsInFooter = ShowValueInputsInFooter();
- showValueOutputsInFooter = ShowValueOutputsInFooter();
-
- controlInputCount = unit.controlInputs.Count;
- controlOutputCount = unit.controlOutputs.Count;
- valueInputTypes = unit.valueInputs.Select(vi => vi.type).ToHashSet();
- valueOutputTypes = unit.valueOutputs.Select(vo => vo.type).ToHashSet();
-
- filled = true;
- }
-
- protected virtual void FillFromData()
- {
- unit.EnsureDefined();
- unitType = unit.GetType();
- UnityAPI.Async(() => icon = Icon());
-
- showControlInputsInFooter = ShowControlInputsInFooter();
- showControlOutputsInFooter = ShowControlOutputsInFooter();
- showValueInputsInFooter = ShowValueInputsInFooter();
- showValueOutputsInFooter = ShowValueOutputsInFooter();
-
- filled = true;
- }
-
- public virtual void Deserialize(UnitOptionRow row)
- {
- source = row;
-
- if (row.sourceScriptGuids != null)
- {
- sourceScriptGuids = row.sourceScriptGuids.Split(',').ToHashSet();
- }
-
- unitType = Codebase.DeserializeType(row.unitType);
-
- category = row.category == null ? null : new UnitCategory(row.category);
- labelHuman = row.labelHuman;
- labelProgrammer = row.labelProgrammer;
- order = row.order;
- haystackHuman = row.haystackHuman;
- haystackProgrammer = row.haystackProgrammer;
- favoriteKey = row.favoriteKey;
-
- controlInputCount = row.controlInputCount;
- controlOutputCount = row.controlOutputCount;
- }
-
- public virtual UnitOptionRow Serialize()
- {
- var row = new UnitOptionRow();
-
- if (sourceScriptGuids.Count == 0)
- {
- // Important to set to null here, because the code relies on
- // null checks, not empty string checks.
- row.sourceScriptGuids = null;
- }
- else
- {
- row.sourceScriptGuids = string.Join(",", sourceScriptGuids.ToArray());
- }
-
- row.optionType = Codebase.SerializeType(GetType());
- row.unitType = Codebase.SerializeType(unitType);
- row.unit = unit.Serialize().json;
-
- row.category = category?.fullName;
- row.labelHuman = labelHuman;
- row.labelProgrammer = labelProgrammer;
- row.order = order;
- row.haystackHuman = haystackHuman;
- row.haystackProgrammer = haystackProgrammer;
- row.favoriteKey = favoriteKey;
-
- row.controlInputCount = controlInputCount;
- row.controlOutputCount = controlOutputCount;
- row.valueInputTypes = valueInputTypes.Select(Codebase.SerializeType).ToSeparatedString("|").NullIfEmpty();
- row.valueOutputTypes = valueOutputTypes.Select(Codebase.SerializeType).ToSeparatedString("|").NullIfEmpty();
-
- return row;
- }
-
- public virtual void OnPopulate()
- {
- if (!filled)
- {
- FillFromData();
- }
- }
-
- public virtual void Prewarm() { }
-
-
- #region Configuration
-
- public object value => this;
-
- public bool parentOnly => false;
-
- public virtual string headerLabel => label;
-
- public virtual bool showHeaderIcon => false;
-
- public virtual bool favoritable => true;
-
- #endregion
-
-
- #region Properties
-
- public HashSet<string> sourceScriptGuids { get; protected set; }
-
- protected string labelHuman { get; set; }
-
- protected string labelProgrammer { get; set; }
-
- public string label => BoltCore.Configuration.humanNaming ? labelHuman : labelProgrammer;
-
- public UnitCategory category { get; private set; }
-
- public int order { get; private set; }
-
- public EditorTexture icon { get; private set; }
-
- protected string haystackHuman { get; set; }
-
- protected string haystackProgrammer { get; set; }
-
- public string haystack => BoltCore.Configuration.humanNaming ? haystackHuman : haystackProgrammer;
-
- public string favoriteKey { get; private set; }
-
- public virtual string formerHaystack => BoltFlowNameUtility.UnitPreviousTitle(unitType);
-
- GUIStyle IFuzzyOption.style => Style();
-
- #endregion
-
-
- #region Contextual Filtering
-
- public int controlInputCount { get; private set; }
-
- public int controlOutputCount { get; private set; }
-
- private HashSet<Type> _valueInputTypes;
-
- private HashSet<Type> _valueOutputTypes;
-
- // On demand loading for initialization performance (type deserialization is expensive)
-
- public HashSet<Type> valueInputTypes
- {
- get
- {
- if (_valueInputTypes == null)
- {
- if (string.IsNullOrEmpty(source.valueInputTypes))
- {
- _valueInputTypes = new HashSet<Type>();
- }
- else
- {
- _valueInputTypes = source.valueInputTypes.Split('|').Select(Codebase.DeserializeType).ToHashSet();
- }
- }
-
- return _valueInputTypes;
- }
- private set
- {
- _valueInputTypes = value;
- }
- }
-
- public HashSet<Type> valueOutputTypes
- {
- get
- {
- if (_valueOutputTypes == null)
- {
- if (string.IsNullOrEmpty(source.valueOutputTypes))
- {
- _valueOutputTypes = new HashSet<Type>();
- }
- else
- {
- _valueOutputTypes = source.valueOutputTypes.Split('|').Select(Codebase.DeserializeType).ToHashSet();
- }
- }
-
- return _valueOutputTypes;
- }
- private set
- {
- _valueOutputTypes = value;
- }
- }
-
- #endregion
-
-
- #region Providers
-
- protected virtual string Label(bool human)
- {
- return BoltFlowNameUtility.UnitTitle(unitType, false, true);
- }
-
- protected virtual UnitCategory Category()
- {
- return unitType.GetAttribute<UnitCategory>();
- }
-
- protected virtual int Order()
- {
- return unitType.GetAttribute<UnitOrderAttribute>()?.order ?? int.MaxValue;
- }
-
- protected virtual string Haystack(bool human)
- {
- return Label(human);
- }
-
- protected virtual EditorTexture Icon()
- {
- return descriptor.Icon();
- }
-
- protected virtual GUIStyle Style()
- {
- return FuzzyWindow.defaultOptionStyle;
- }
-
- protected virtual string FavoriteKey()
- {
- return unit.GetType().FullName;
- }
-
- #endregion
-
-
- #region Search
-
- public virtual string SearchResultLabel(string query)
- {
- var label = SearchUtility.HighlightQuery(haystack, query);
-
- if (category != null)
- {
- label += $" <color=#{ColorPalette.unityForegroundDim.ToHexString()}>(in {category.fullName})</color>";
- }
-
- return label;
- }
-
- #endregion
-
-
- #region Footer
-
- private string summary => description.summary;
-
- public bool hasFooter => !StringUtility.IsNullOrWhiteSpace(summary) || footerPorts.Any();
-
- protected virtual bool ShowControlInputsInFooter()
- {
- return unitType.GetAttribute<UnitFooterPortsAttribute>()?.ControlInputs ?? false;
- }
-
- protected virtual bool ShowControlOutputsInFooter()
- {
- return unitType.GetAttribute<UnitFooterPortsAttribute>()?.ControlOutputs ?? false;
- }
-
- protected virtual bool ShowValueInputsInFooter()
- {
- return unitType.GetAttribute<UnitFooterPortsAttribute>()?.ValueInputs ?? true;
- }
-
- protected virtual bool ShowValueOutputsInFooter()
- {
- return unitType.GetAttribute<UnitFooterPortsAttribute>()?.ValueOutputs ?? true;
- }
-
- [DoNotSerialize]
- protected bool showControlInputsInFooter { get; private set; }
-
- [DoNotSerialize]
- protected bool showControlOutputsInFooter { get; private set; }
-
- [DoNotSerialize]
- protected bool showValueInputsInFooter { get; private set; }
-
- [DoNotSerialize]
- protected bool showValueOutputsInFooter { get; private set; }
-
- private IEnumerable<IUnitPort> footerPorts
- {
- get
- {
- if (showControlInputsInFooter)
- {
- foreach (var controlInput in unit.controlInputs)
- {
- yield return controlInput;
- }
- }
-
- if (showControlOutputsInFooter)
- {
- foreach (var controlOutput in unit.controlOutputs)
- {
- yield return controlOutput;
- }
- }
-
- if (showValueInputsInFooter)
- {
- foreach (var valueInput in unit.valueInputs)
- {
- yield return valueInput;
- }
- }
-
- if (showValueOutputsInFooter)
- {
- foreach (var valueOutput in unit.valueOutputs)
- {
- yield return valueOutput;
- }
- }
- }
- }
-
- public float GetFooterHeight(float width)
- {
- var hasSummary = !StringUtility.IsNullOrWhiteSpace(summary);
- var hasIcon = icon != null;
- var hasPorts = footerPorts.Any();
-
- var height = 0f;
-
- width -= 2 * FooterStyles.padding;
-
- height += FooterStyles.padding;
-
- if (hasSummary)
- {
- if (hasIcon)
- {
- height += Mathf.Max(FooterStyles.unitIconSize, GetFooterSummaryHeight(width - FooterStyles.unitIconSize - FooterStyles.spaceAfterUnitIcon));
- }
- else
- {
- height += GetFooterSummaryHeight(width);
- }
- }
-
- if (hasSummary && hasPorts)
- {
- height += FooterStyles.spaceBetweenDescriptionAndPorts;
- }
-
- foreach (var port in footerPorts)
- {
- height += GetFooterPortHeight(width, port);
- height += FooterStyles.spaceBetweenPorts;
- }
-
- if (hasPorts)
- {
- height -= FooterStyles.spaceBetweenPorts;
- }
-
- height += FooterStyles.padding;
-
- return height;
- }
-
- public void OnFooterGUI(Rect position)
- {
- var hasSummary = !StringUtility.IsNullOrWhiteSpace(summary);
- var hasIcon = icon != null;
- var hasPorts = footerPorts.Any();
-
- var y = position.y;
-
- y += FooterStyles.padding;
-
- position.x += FooterStyles.padding;
- position.width -= FooterStyles.padding * 2;
-
- if (hasSummary)
- {
- if (hasIcon)
- {
- var iconPosition = new Rect
- (
- position.x,
- y,
- FooterStyles.unitIconSize,
- FooterStyles.unitIconSize
- );
-
- var summaryWidth = position.width - iconPosition.width - FooterStyles.spaceAfterUnitIcon;
-
- var summaryPosition = new Rect
- (
- iconPosition.xMax + FooterStyles.spaceAfterUnitIcon,
- y,
- summaryWidth,
- GetFooterSummaryHeight(summaryWidth)
- );
-
- GUI.DrawTexture(iconPosition, icon?[FooterStyles.unitIconSize]);
-
- OnFooterSummaryGUI(summaryPosition);
-
- y = Mathf.Max(iconPosition.yMax, summaryPosition.yMax);
- }
- else
- {
- OnFooterSummaryGUI(position.VerticalSection(ref y, GetFooterSummaryHeight(position.width)));
- }
- }
-
- if (hasSummary && hasPorts)
- {
- y += FooterStyles.spaceBetweenDescriptionAndPorts;
- }
-
- foreach (var port in footerPorts)
- {
- OnFooterPortGUI(position.VerticalSection(ref y, GetFooterPortHeight(position.width, port)), port);
- y += FooterStyles.spaceBetweenPorts;
- }
-
- if (hasPorts)
- {
- y -= FooterStyles.spaceBetweenPorts;
- }
-
- y += FooterStyles.padding;
- }
-
- private float GetFooterSummaryHeight(float width)
- {
- return FooterStyles.description.CalcHeight(new GUIContent(summary), width);
- }
-
- private void OnFooterSummaryGUI(Rect position)
- {
- EditorGUI.LabelField(position, summary, FooterStyles.description);
- }
-
- private string GetFooterPortLabel(IUnitPort port)
- {
- string type;
-
- if (port is ValueInput)
- {
- type = ((IUnitValuePort)port).type.DisplayName() + " Input";
- }
- else if (port is ValueOutput)
- {
- type = ((IUnitValuePort)port).type.DisplayName() + " Output";
- }
- else if (port is ControlInput)
- {
- type = "Trigger Input";
- }
- else if (port is ControlOutput)
- {
- type = "Trigger Output";
- }
- else
- {
- throw new NotSupportedException();
- }
-
- var portDescription = PortDescription(port);
-
- if (!StringUtility.IsNullOrWhiteSpace(portDescription.summary))
- {
- return $"<b>{portDescription.label}:</b> {portDescription.summary} {LudiqGUIUtility.DimString($"({type})")}";
- }
- else
- {
- return $"<b>{portDescription.label}:</b> {LudiqGUIUtility.DimString($"({type})")}";
- }
- }
-
- private float GetFooterPortDescriptionHeight(float width, IUnitPort port)
- {
- return FooterStyles.portDescription.CalcHeight(new GUIContent(GetFooterPortLabel(port)), width);
- }
-
- private void OnFooterPortDescriptionGUI(Rect position, IUnitPort port)
- {
- GUI.Label(position, GetFooterPortLabel(port), FooterStyles.portDescription);
- }
-
- private float GetFooterPortHeight(float width, IUnitPort port)
- {
- var descriptionWidth = width - FooterStyles.portIconSize - FooterStyles.spaceAfterPortIcon;
-
- return GetFooterPortDescriptionHeight(descriptionWidth, port);
- }
-
- private void OnFooterPortGUI(Rect position, IUnitPort port)
- {
- var iconPosition = new Rect
- (
- position.x,
- position.y,
- FooterStyles.portIconSize,
- FooterStyles.portIconSize
- );
-
- var descriptionWidth = position.width - FooterStyles.portIconSize - FooterStyles.spaceAfterPortIcon;
-
- var descriptionPosition = new Rect
- (
- iconPosition.xMax + FooterStyles.spaceAfterPortIcon,
- position.y,
- descriptionWidth,
- GetFooterPortDescriptionHeight(descriptionWidth, port)
- );
-
- var portDescription = PortDescription(port);
-
- var icon = portDescription.icon?[FooterStyles.portIconSize];
-
- if (icon != null)
- {
- GUI.DrawTexture(iconPosition, icon);
- }
-
- OnFooterPortDescriptionGUI(descriptionPosition, port);
- }
-
- public static class FooterStyles
- {
- static FooterStyles()
- {
- description = new GUIStyle(EditorStyles.label);
- description.padding = new RectOffset(0, 0, 0, 0);
- description.wordWrap = true;
- description.richText = true;
-
- portDescription = new GUIStyle(EditorStyles.label);
- portDescription.padding = new RectOffset(0, 0, 0, 0);
- portDescription.wordWrap = true;
- portDescription.richText = true;
- portDescription.imagePosition = ImagePosition.TextOnly;
- }
-
- public static readonly GUIStyle description;
- public static readonly GUIStyle portDescription;
- public static readonly float spaceAfterUnitIcon = 7;
- public static readonly int unitIconSize = IconSize.Medium;
- public static readonly float spaceAfterPortIcon = 6;
- public static readonly int portIconSize = IconSize.Small;
- public static readonly float spaceBetweenDescriptionAndPorts = 8;
- public static readonly float spaceBetweenPorts = 8;
- public static readonly float padding = 8;
- }
-
- #endregion
- }
- }
|