1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- namespace Unity.VisualScripting
- {
- [Widget(typeof(IUnit))]
- public class UnitWidget<TUnit> : NodeWidget<FlowCanvas, TUnit>, IUnitWidget where TUnit : class, IUnit
- {
- public UnitWidget(FlowCanvas canvas, TUnit unit) : base(canvas, unit)
- {
- unit.onPortsChanged += CacheDefinition;
- unit.onPortsChanged += SubWidgetsChanged;
- }
- public override void Dispose()
- {
- base.Dispose();
- unit.onPortsChanged -= CacheDefinition;
- unit.onPortsChanged -= SubWidgetsChanged;
- }
- public override IEnumerable<IWidget> subWidgets => unit.ports.Select(port => canvas.Widget(port));
- #region Model
- protected TUnit unit => element;
- IUnit IUnitWidget.unit => unit;
- protected IUnitDebugData unitDebugData => GetDebugData<IUnitDebugData>();
- private UnitDescription description;
- private UnitAnalysis analysis => unit.Analysis<UnitAnalysis>(context);
- protected readonly List<IUnitPortWidget> ports = new List<IUnitPortWidget>();
- protected readonly List<IUnitPortWidget> inputs = new List<IUnitPortWidget>();
- protected readonly List<IUnitPortWidget> outputs = new List<IUnitPortWidget>();
- private readonly List<string> settingNames = new List<string>();
- protected IEnumerable<Metadata> settings
- {
- get
- {
- foreach (var settingName in settingNames)
- {
- yield return metadata[settingName];
- }
- }
- }
- protected override void CacheItemFirstTime()
- {
- base.CacheItemFirstTime();
- CacheDefinition();
- }
- protected virtual void CacheDefinition()
- {
- inputs.Clear();
- outputs.Clear();
- ports.Clear();
- inputs.AddRange(unit.inputs.Select(port => canvas.Widget<IUnitPortWidget>(port)));
- outputs.AddRange(unit.outputs.Select(port => canvas.Widget<IUnitPortWidget>(port)));
- ports.AddRange(inputs);
- ports.AddRange(outputs);
- Reposition();
- }
- protected override void CacheDescription()
- {
- description = unit.Description<UnitDescription>();
- titleContent.text = description.shortTitle;
- titleContent.tooltip = description.summary;
- surtitleContent.text = description.surtitle;
- subtitleContent.text = description.subtitle;
- Reposition();
- }
- protected override void CacheMetadata()
- {
- settingNames.Clear();
- settingNames.AddRange(metadata.valueType
- .GetMembers()
- .Where(mi => mi.HasAttribute<UnitHeaderInspectableAttribute>())
- .OrderBy(mi => mi.GetAttributes<Attribute>().OfType<IInspectableAttribute>().FirstOrDefault()?.order ?? int.MaxValue)
- .ThenBy(mi => mi.MetadataToken)
- .Select(mi => mi.Name));
- lock (settingLabelsContents)
- {
- settingLabelsContents.Clear();
- foreach (var setting in settings)
- {
- var settingLabel = setting.GetAttribute<UnitHeaderInspectableAttribute>().label;
- GUIContent settingContent;
- if (string.IsNullOrEmpty(settingLabel))
- {
- settingContent = null;
- }
- else
- {
- settingContent = new GUIContent(settingLabel);
- }
- settingLabelsContents.Add(setting, settingContent);
- }
- }
- Reposition();
- }
- public virtual Inspector GetPortInspector(IUnitPort port, Metadata metadata)
- {
- return metadata.Inspector();
- }
- #endregion
- #region Lifecycle
- public override bool foregroundRequiresInput => showSettings || unit.valueInputs.Any(vip => vip.hasDefaultValue);
- public override void HandleInput()
- {
- if (canvas.isCreatingConnection)
- {
- if (e.IsMouseDown(MouseButton.Left))
- {
- var source = canvas.connectionSource;
- var destination = source.CompatiblePort(unit);
- if (destination != null)
- {
- UndoUtility.RecordEditedObject("Connect Nodes");
- source.ValidlyConnectTo(destination);
- canvas.connectionSource = null;
- canvas.Widget(source.unit).Reposition();
- canvas.Widget(destination.unit).Reposition();
- GUI.changed = true;
- }
- e.Use();
- }
- else if (e.IsMouseDown(MouseButton.Right))
- {
- canvas.CancelConnection();
- e.Use();
- }
- }
- base.HandleInput();
- }
- #endregion
- #region Contents
- protected readonly GUIContent titleContent = new GUIContent();
- protected readonly GUIContent surtitleContent = new GUIContent();
- protected readonly GUIContent subtitleContent = new GUIContent();
- protected readonly Dictionary<Metadata, GUIContent> settingLabelsContents = new Dictionary<Metadata, GUIContent>();
- #endregion
- #region Positioning
- protected override bool snapToGrid => BoltCore.Configuration.snapToGrid;
- public override IEnumerable<IWidget> positionDependers => ports.Cast<IWidget>();
- protected Rect _position;
- public override Rect position
- {
- get { return _position; }
- set { unit.position = value.position; }
- }
- public Rect titlePosition { get; private set; }
- public Rect surtitlePosition { get; private set; }
- public Rect subtitlePosition { get; private set; }
- public Rect iconPosition { get; private set; }
- public List<Rect> iconsPositions { get; private set; } = new List<Rect>();
- public Dictionary<Metadata, Rect> settingsPositions { get; } = new Dictionary<Metadata, Rect>();
- public Rect headerAddonPosition { get; private set; }
- public Rect portsBackgroundPosition { get; private set; }
- public override void CachePosition()
- {
- // Width
- var inputsWidth = 0f;
- var outputsWidth = 0f;
- foreach (var input in inputs)
- {
- inputsWidth = Mathf.Max(inputsWidth, input.GetInnerWidth());
- }
- foreach (var output in outputs)
- {
- outputsWidth = Mathf.Max(outputsWidth, output.GetInnerWidth());
- }
- var portsWidth = 0f;
- portsWidth += inputsWidth;
- portsWidth += Styles.spaceBetweenInputsAndOutputs;
- portsWidth += outputsWidth;
- settingsPositions.Clear();
- var settingsWidth = 0f;
- if (showSettings)
- {
- foreach (var setting in settings)
- {
- var settingWidth = 0f;
- var settingLabelContent = settingLabelsContents[setting];
- if (settingLabelContent != null)
- {
- settingWidth += Styles.settingLabel.CalcSize(settingLabelContent).x;
- }
- settingWidth += setting.Inspector().GetAdaptiveWidth();
- settingWidth = Mathf.Min(settingWidth, Styles.maxSettingsWidth);
- settingsPositions.Add(setting, new Rect(0, 0, settingWidth, 0));
- settingsWidth = Mathf.Max(settingsWidth, settingWidth);
- }
- }
- var headerAddonWidth = 0f;
- if (showHeaderAddon)
- {
- headerAddonWidth = GetHeaderAddonWidth();
- }
- var titleWidth = Styles.title.CalcSize(titleContent).x;
- var headerTextWidth = titleWidth;
- var surtitleWidth = 0f;
- if (showSurtitle)
- {
- surtitleWidth = Styles.surtitle.CalcSize(surtitleContent).x;
- headerTextWidth = Mathf.Max(headerTextWidth, surtitleWidth);
- }
- var subtitleWidth = 0f;
- if (showSubtitle)
- {
- subtitleWidth = Styles.subtitle.CalcSize(subtitleContent).x;
- headerTextWidth = Mathf.Max(headerTextWidth, subtitleWidth);
- }
- var iconsWidth = 0f;
- if (showIcons)
- {
- var iconsColumns = Mathf.Ceil((float)description.icons.Length / Styles.iconsPerColumn);
- iconsWidth = iconsColumns * Styles.iconsSize + ((iconsColumns - 1) * Styles.iconsSpacing);
- }
- var headerWidth = Mathf.Max(headerTextWidth + iconsWidth, Mathf.Max(settingsWidth, headerAddonWidth)) + Styles.iconSize + Styles.spaceAfterIcon;
- var innerWidth = Mathf.Max(portsWidth, headerWidth);
- var edgeWidth = InnerToEdgePosition(new Rect(0, 0, innerWidth, 0)).width;
- // Height & Positioning
- var edgeOrigin = unit.position;
- var edgeX = edgeOrigin.x;
- var edgeY = edgeOrigin.y;
- var innerOrigin = EdgeToInnerPosition(new Rect(edgeOrigin, Vector2.zero)).position;
- var innerX = innerOrigin.x;
- var innerY = innerOrigin.y;
- iconPosition = new Rect
- (
- innerX,
- innerY,
- Styles.iconSize,
- Styles.iconSize
- );
- var headerTextX = iconPosition.xMax + Styles.spaceAfterIcon;
- var y = innerY;
- var headerHeight = 0f;
- var surtitleHeight = 0f;
- if (showSurtitle)
- {
- surtitleHeight = Styles.surtitle.CalcHeight(surtitleContent, headerTextWidth);
- surtitlePosition = new Rect
- (
- headerTextX,
- y,
- headerTextWidth,
- surtitleHeight
- );
- headerHeight += surtitleHeight;
- y += surtitleHeight;
- headerHeight += Styles.spaceAfterSurtitle;
- y += Styles.spaceAfterSurtitle;
- }
- var titleHeight = 0f;
- if (showTitle)
- {
- titleHeight = Styles.title.CalcHeight(titleContent, headerTextWidth);
- titlePosition = new Rect
- (
- headerTextX,
- y,
- headerTextWidth,
- titleHeight
- );
- headerHeight += titleHeight;
- y += titleHeight;
- }
- var subtitleHeight = 0f;
- if (showSubtitle)
- {
- headerHeight += Styles.spaceBeforeSubtitle;
- y += Styles.spaceBeforeSubtitle;
- subtitleHeight = Styles.subtitle.CalcHeight(subtitleContent, headerTextWidth);
- subtitlePosition = new Rect
- (
- headerTextX,
- y,
- headerTextWidth,
- subtitleHeight
- );
- headerHeight += subtitleHeight;
- y += subtitleHeight;
- }
- iconsPositions.Clear();
- if (showIcons)
- {
- var iconRow = 0;
- var iconCol = 0;
- for (int i = 0; i < description.icons.Length; i++)
- {
- var iconPosition = new Rect
- (
- innerX + innerWidth - ((iconCol + 1) * Styles.iconsSize) - ((iconCol) * Styles.iconsSpacing),
- innerY + (iconRow * (Styles.iconsSize + Styles.iconsSpacing)),
- Styles.iconsSize,
- Styles.iconsSize
- );
- iconsPositions.Add(iconPosition);
- iconRow++;
- if (iconRow % Styles.iconsPerColumn == 0)
- {
- iconCol++;
- iconRow = 0;
- }
- }
- }
- var settingsHeight = 0f;
- if (showSettings)
- {
- headerHeight += Styles.spaceBeforeSettings;
- foreach (var setting in settings)
- {
- var settingWidth = settingsPositions[setting].width;
- using (LudiqGUIUtility.currentInspectorWidth.Override(settingWidth))
- {
- var settingHeight = LudiqGUI.GetInspectorHeight(null, setting, settingWidth, settingLabelsContents[setting] ?? GUIContent.none);
- var settingPosition = new Rect
- (
- headerTextX,
- y,
- settingWidth,
- settingHeight
- );
- settingsPositions[setting] = settingPosition;
- settingsHeight += settingHeight;
- y += settingHeight;
- settingsHeight += Styles.spaceBetweenSettings;
- y += Styles.spaceBetweenSettings;
- }
- }
- settingsHeight -= Styles.spaceBetweenSettings;
- y -= Styles.spaceBetweenSettings;
- headerHeight += settingsHeight;
- headerHeight += Styles.spaceAfterSettings;
- y += Styles.spaceAfterSettings;
- }
- if (showHeaderAddon)
- {
- var headerAddonHeight = GetHeaderAddonHeight(headerAddonWidth);
- headerAddonPosition = new Rect
- (
- headerTextX,
- y,
- headerAddonWidth,
- headerAddonHeight
- );
- headerHeight += headerAddonHeight;
- y += headerAddonHeight;
- }
- if (headerHeight < Styles.iconSize)
- {
- var difference = Styles.iconSize - headerHeight;
- var centeringOffset = difference / 2;
- if (showTitle)
- {
- var _titlePosition = titlePosition;
- _titlePosition.y += centeringOffset;
- titlePosition = _titlePosition;
- }
- if (showSubtitle)
- {
- var _subtitlePosition = subtitlePosition;
- _subtitlePosition.y += centeringOffset;
- subtitlePosition = _subtitlePosition;
- }
- if (showSettings)
- {
- foreach (var setting in settings)
- {
- var _settingPosition = settingsPositions[setting];
- _settingPosition.y += centeringOffset;
- settingsPositions[setting] = _settingPosition;
- }
- }
- if (showHeaderAddon)
- {
- var _headerAddonPosition = headerAddonPosition;
- _headerAddonPosition.y += centeringOffset;
- headerAddonPosition = _headerAddonPosition;
- }
- headerHeight = Styles.iconSize;
- }
- y = innerY + headerHeight;
- var innerHeight = 0f;
- innerHeight += headerHeight;
- if (showPorts)
- {
- innerHeight += Styles.spaceBeforePorts;
- y += Styles.spaceBeforePorts;
- var portsBackgroundY = y;
- var portsBackgroundHeight = 0f;
- portsBackgroundHeight += Styles.portsBackground.padding.top;
- innerHeight += Styles.portsBackground.padding.top;
- y += Styles.portsBackground.padding.top;
- var portStartY = y;
- var inputsHeight = 0f;
- var outputsHeight = 0f;
- foreach (var input in inputs)
- {
- input.y = y;
- var inputHeight = input.GetHeight();
- inputsHeight += inputHeight;
- y += inputHeight;
- inputsHeight += Styles.spaceBetweenPorts;
- y += Styles.spaceBetweenPorts;
- }
- if (inputs.Count > 0)
- {
- inputsHeight -= Styles.spaceBetweenPorts;
- y -= Styles.spaceBetweenPorts;
- }
- y = portStartY;
- foreach (var output in outputs)
- {
- output.y = y;
- var outputHeight = output.GetHeight();
- outputsHeight += outputHeight;
- y += outputHeight;
- outputsHeight += Styles.spaceBetweenPorts;
- y += Styles.spaceBetweenPorts;
- }
- if (outputs.Count > 0)
- {
- outputsHeight -= Styles.spaceBetweenPorts;
- y -= Styles.spaceBetweenPorts;
- }
- var portsHeight = Math.Max(inputsHeight, outputsHeight);
- portsBackgroundHeight += portsHeight;
- innerHeight += portsHeight;
- y = portStartY + portsHeight;
- portsBackgroundHeight += Styles.portsBackground.padding.bottom;
- innerHeight += Styles.portsBackground.padding.bottom;
- y += Styles.portsBackground.padding.bottom;
- portsBackgroundPosition = new Rect
- (
- edgeX,
- portsBackgroundY,
- edgeWidth,
- portsBackgroundHeight
- );
- }
- var edgeHeight = InnerToEdgePosition(new Rect(0, 0, 0, innerHeight)).height;
- _position = new Rect
- (
- edgeX,
- edgeY,
- edgeWidth,
- edgeHeight
- );
- }
- protected virtual float GetHeaderAddonWidth()
- {
- return 0;
- }
- protected virtual float GetHeaderAddonHeight(float width)
- {
- return 0;
- }
- #endregion
- #region Drawing
- protected virtual NodeColorMix baseColor => NodeColor.Gray;
- protected override NodeColorMix color
- {
- get
- {
- if (unitDebugData.runtimeException != null)
- {
- return NodeColor.Red;
- }
- var color = baseColor;
- if (analysis.warnings.Count > 0)
- {
- var mostSevereWarning = Warning.MostSevereLevel(analysis.warnings);
- switch (mostSevereWarning)
- {
- case WarningLevel.Error:
- color = NodeColor.Red;
- break;
- case WarningLevel.Severe:
- color = NodeColor.Orange;
- break;
- case WarningLevel.Caution:
- color = NodeColor.Yellow;
- break;
- }
- }
- if (EditorApplication.isPaused)
- {
- if (EditorTimeBinding.frame == unitDebugData.lastInvokeFrame)
- {
- return NodeColor.Blue;
- }
- }
- else
- {
- var mix = color;
- mix.blue = Mathf.Lerp(1, 0, (EditorTimeBinding.time - unitDebugData.lastInvokeTime) / Styles.invokeFadeDuration);
- return mix;
- }
- return color;
- }
- }
- protected override NodeShape shape => NodeShape.Square;
- protected virtual bool showTitle => !string.IsNullOrEmpty(description.shortTitle);
- protected virtual bool showSurtitle => !string.IsNullOrEmpty(description.surtitle);
- protected virtual bool showSubtitle => !string.IsNullOrEmpty(description.subtitle);
- protected virtual bool showIcons => description.icons.Length > 0;
- protected virtual bool showSettings => settingNames.Count > 0;
- protected virtual bool showHeaderAddon => false;
- protected virtual bool showPorts => ports.Count > 0;
- protected override bool dim
- {
- get
- {
- var dim = BoltCore.Configuration.dimInactiveNodes && !analysis.isEntered;
- if (isMouseOver || isSelected)
- {
- dim = false;
- }
- if (BoltCore.Configuration.dimIncompatibleNodes && canvas.isCreatingConnection)
- {
- dim = !unit.ports.Any(p => canvas.connectionSource == p || canvas.connectionSource.CanValidlyConnectTo(p));
- }
- return dim;
- }
- }
- public override void DrawForeground()
- {
- BeginDim();
- base.DrawForeground();
- DrawIcon();
- if (showSurtitle)
- {
- DrawSurtitle();
- }
- if (showTitle)
- {
- DrawTitle();
- }
- if (showSubtitle)
- {
- DrawSubtitle();
- }
- if (showIcons)
- {
- DrawIcons();
- }
- if (showSettings)
- {
- DrawSettings();
- }
- if (showHeaderAddon)
- {
- DrawHeaderAddon();
- }
- if (showPorts)
- {
- DrawPortsBackground();
- }
- EndDim();
- }
- protected void DrawIcon()
- {
- var icon = description.icon ?? BoltFlow.Icons.unit;
- if (icon != null && icon[(int)iconPosition.width])
- {
- GUI.DrawTexture(iconPosition, icon[(int)iconPosition.width]);
- }
- }
- protected void DrawTitle()
- {
- GUI.Label(titlePosition, titleContent, invertForeground ? Styles.titleInverted : Styles.title);
- }
- protected void DrawSurtitle()
- {
- GUI.Label(surtitlePosition, surtitleContent, invertForeground ? Styles.surtitleInverted : Styles.surtitle);
- }
- protected void DrawSubtitle()
- {
- GUI.Label(subtitlePosition, subtitleContent, invertForeground ? Styles.subtitleInverted : Styles.subtitle);
- }
- protected void DrawIcons()
- {
- for (int i = 0; i < description.icons.Length; i++)
- {
- var icon = description.icons[i];
- var position = iconsPositions[i];
- GUI.DrawTexture(position, icon?[(int)position.width]);
- }
- }
- private void DrawSettings()
- {
- if (graph.zoom < FlowCanvas.inspectorZoomThreshold)
- {
- return;
- }
- EditorGUI.BeginDisabledGroup(!e.IsRepaint && isMouseThrough && !isMouseOver);
- EditorGUI.BeginChangeCheck();
- foreach (var setting in settings)
- {
- DrawSetting(setting);
- }
- if (EditorGUI.EndChangeCheck())
- {
- unit.Define();
- Reposition();
- }
- EditorGUI.EndDisabledGroup();
- }
- protected void DrawSetting(Metadata setting)
- {
- var settingPosition = settingsPositions[setting];
- using (LudiqGUIUtility.currentInspectorWidth.Override(settingPosition.width))
- using (Inspector.expandTooltip.Override(false))
- {
- var label = settingLabelsContents[setting];
- if (label == null)
- {
- LudiqGUI.Inspector(setting, settingPosition, GUIContent.none);
- }
- else
- {
- using (Inspector.defaultLabelStyle.Override(Styles.settingLabel))
- using (LudiqGUIUtility.labelWidth.Override(Styles.settingLabel.CalcSize(label).x))
- {
- LudiqGUI.Inspector(setting, settingPosition, label);
- }
- }
- }
- }
- protected virtual void DrawHeaderAddon() { }
- protected void DrawPortsBackground()
- {
- if (canvas.showRelations)
- {
- foreach (var relation in unit.relations)
- {
- var start = ports.Single(pw => pw.port == relation.source).handlePosition.center;
- var end = ports.Single(pw => pw.port == relation.destination).handlePosition.center;
- var startTangent = start;
- var endTangent = end;
- if (relation.source is IUnitInputPort &&
- relation.destination is IUnitInputPort)
- {
- //startTangent -= new Vector2(20, 0);
- endTangent -= new Vector2(32, 0);
- }
- else
- {
- startTangent += new Vector2(innerPosition.width / 2, 0);
- endTangent += new Vector2(-innerPosition.width / 2, 0);
- }
- Handles.DrawBezier
- (
- start,
- end,
- startTangent,
- endTangent,
- new Color(0.136f, 0.136f, 0.136f, 1.0f),
- null,
- 3
- );
- }
- }
- else
- {
- if (e.IsRepaint)
- {
- Styles.portsBackground.Draw(portsBackgroundPosition, false, false, false, false);
- }
- }
- }
- #endregion
- #region Selecting
- public override bool canSelect => true;
- #endregion
- #region Dragging
- public override bool canDrag => true;
- public override void ExpandDragGroup(HashSet<IGraphElement> dragGroup)
- {
- if (BoltCore.Configuration.carryChildren)
- {
- foreach (var output in unit.outputs)
- {
- foreach (var connection in output.connections)
- {
- if (dragGroup.Contains(connection.destination.unit))
- {
- continue;
- }
- dragGroup.Add(connection.destination.unit);
- canvas.Widget(connection.destination.unit).ExpandDragGroup(dragGroup);
- }
- }
- }
- }
- #endregion
- #region Deleting
- public override bool canDelete => true;
- #endregion
- #region Clipboard
- public override void ExpandCopyGroup(HashSet<IGraphElement> copyGroup)
- {
- copyGroup.UnionWith(unit.connections.Cast<IGraphElement>());
- }
- #endregion
- #region Context
- protected override IEnumerable<DropdownOption> contextOptions
- {
- get
- {
- yield return new DropdownOption((Action)ReplaceUnit, "Replace...");
- foreach (var baseOption in base.contextOptions)
- {
- yield return baseOption;
- }
- }
- }
- private void ReplaceUnit()
- {
- var oldUnit = unit;
- var unitPosition = oldUnit.position;
- var preservation = UnitPreservation.Preserve(oldUnit);
- var options = new UnitOptionTree(new GUIContent("Node"));
- options.reference = reference;
- var activatorPosition = new Rect(e.mousePosition, new Vector2(200, 1));
- var context = this.context;
- LudiqGUI.FuzzyDropdown
- (
- activatorPosition,
- options,
- null,
- delegate (object _option)
- {
- var option = (IUnitOption)_option;
- context.BeginEdit();
- UndoUtility.RecordEditedObject("Replace Node");
- var graph = oldUnit.graph;
- oldUnit.graph.units.Remove(oldUnit);
- var newUnit = option.InstantiateUnit();
- newUnit.guid = Guid.NewGuid();
- newUnit.position = unitPosition;
- graph.units.Add(newUnit);
- preservation.RestoreTo(newUnit);
- option.PreconfigureUnit(newUnit);
- selection.Select(newUnit);
- GUI.changed = true;
- context.EndEdit();
- }
- );
- }
- #endregion
- public static class Styles
- {
- static Styles()
- {
- // Disabling word wrap because Unity's CalcSize and CalcHeight
- // are broken w.r.t. pixel-perfection and matrix
- title = new GUIStyle(BoltCore.Styles.nodeLabel);
- title.padding = new RectOffset(0, 5, 0, 2);
- title.margin = new RectOffset(0, 0, 0, 0);
- title.fontSize = 12;
- title.alignment = TextAnchor.MiddleLeft;
- title.wordWrap = false;
- surtitle = new GUIStyle(BoltCore.Styles.nodeLabel);
- surtitle.padding = new RectOffset(0, 5, 0, 0);
- surtitle.margin = new RectOffset(0, 0, 0, 0);
- surtitle.fontSize = 10;
- surtitle.alignment = TextAnchor.MiddleLeft;
- surtitle.wordWrap = false;
- subtitle = new GUIStyle(surtitle);
- subtitle.padding.bottom = 2;
- titleInverted = new GUIStyle(title);
- titleInverted.normal.textColor = ColorPalette.unityBackgroundDark;
- surtitleInverted = new GUIStyle(surtitle);
- surtitleInverted.normal.textColor = ColorPalette.unityBackgroundDark;
- subtitleInverted = new GUIStyle(subtitle);
- subtitleInverted.normal.textColor = ColorPalette.unityBackgroundDark;
- if (EditorGUIUtility.isProSkin)
- {
- portsBackground = new GUIStyle("In BigTitle")
- {
- padding = new RectOffset(0, 0, 6, 5)
- };
- }
- else
- {
- TextureResolution[] textureResolution = { 2 };
- var createTextureOptions = CreateTextureOptions.Scalable;
- EditorTexture normalTexture = BoltCore.Resources.LoadTexture($"NodePortsBackground.png", textureResolution, createTextureOptions);
- portsBackground = new GUIStyle
- {
- normal = { background = normalTexture.Single() },
- padding = new RectOffset(0, 0, 6, 5)
- };
- }
- settingLabel = new GUIStyle(BoltCore.Styles.nodeLabel);
- settingLabel.padding.left = 0;
- settingLabel.padding.right = 5;
- settingLabel.wordWrap = false;
- settingLabel.clipping = TextClipping.Clip;
- }
- public static readonly GUIStyle title;
- public static readonly GUIStyle surtitle;
- public static readonly GUIStyle subtitle;
- public static readonly GUIStyle titleInverted;
- public static readonly GUIStyle surtitleInverted;
- public static readonly GUIStyle subtitleInverted;
- public static readonly GUIStyle settingLabel;
- public static readonly float spaceAroundLineIcon = 5;
- public static readonly float spaceBeforePorts = 5;
- public static readonly float spaceBetweenInputsAndOutputs = 8;
- public static readonly float spaceBeforeSettings = 2;
- public static readonly float spaceBetweenSettings = 3;
- public static readonly float spaceBetweenPorts = 3;
- public static readonly float spaceAfterSettings = 0;
- public static readonly float maxSettingsWidth = 150;
- public static readonly GUIStyle portsBackground;
- public static readonly float iconSize = IconSize.Medium;
- public static readonly float iconsSize = IconSize.Small;
- public static readonly float iconsSpacing = 3;
- public static readonly int iconsPerColumn = 2;
- public static readonly float spaceAfterIcon = 6;
- public static readonly float spaceAfterSurtitle = 2;
- public static readonly float spaceBeforeSubtitle = 0;
- public static readonly float invokeFadeDuration = 0.5f;
- }
- }
- }
|