123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122 |
- 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()
- {
- UnitWidgetHelper.ReplaceUnit(unit, reference, context, selection, e);
- }
-
- #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;
- }
- }
-
- internal class UnitWidgetHelper
- {
- internal static void ReplaceUnit(IUnit unit, GraphReference reference, IGraphContext context, GraphSelection selection, EventWrapper eventWrapper)
- {
- var oldUnit = unit;
- var unitPosition = oldUnit.position;
- var preservation = UnitPreservation.Preserve(oldUnit);
-
- var options = new UnitOptionTree(new GUIContent("Node"));
- options.filter = UnitOptionFilter.Any;
- options.filter.NoConnection = false;
- options.reference = reference;
-
- var activatorPosition = new Rect(eventWrapper.mousePosition, new Vector2(200, 1));
-
- 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();
- }
- );
- }
- }
- }
|