No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnitWidget.cs 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Unity.VisualScripting
  7. {
  8. [Widget(typeof(IUnit))]
  9. public class UnitWidget<TUnit> : NodeWidget<FlowCanvas, TUnit>, IUnitWidget where TUnit : class, IUnit
  10. {
  11. public UnitWidget(FlowCanvas canvas, TUnit unit) : base(canvas, unit)
  12. {
  13. unit.onPortsChanged += CacheDefinition;
  14. unit.onPortsChanged += SubWidgetsChanged;
  15. }
  16. public override void Dispose()
  17. {
  18. base.Dispose();
  19. unit.onPortsChanged -= CacheDefinition;
  20. unit.onPortsChanged -= SubWidgetsChanged;
  21. }
  22. public override IEnumerable<IWidget> subWidgets => unit.ports.Select(port => canvas.Widget(port));
  23. #region Model
  24. protected TUnit unit => element;
  25. IUnit IUnitWidget.unit => unit;
  26. protected IUnitDebugData unitDebugData => GetDebugData<IUnitDebugData>();
  27. private UnitDescription description;
  28. private UnitAnalysis analysis => unit.Analysis<UnitAnalysis>(context);
  29. protected readonly List<IUnitPortWidget> ports = new List<IUnitPortWidget>();
  30. protected readonly List<IUnitPortWidget> inputs = new List<IUnitPortWidget>();
  31. protected readonly List<IUnitPortWidget> outputs = new List<IUnitPortWidget>();
  32. private readonly List<string> settingNames = new List<string>();
  33. protected IEnumerable<Metadata> settings
  34. {
  35. get
  36. {
  37. foreach (var settingName in settingNames)
  38. {
  39. yield return metadata[settingName];
  40. }
  41. }
  42. }
  43. protected override void CacheItemFirstTime()
  44. {
  45. base.CacheItemFirstTime();
  46. CacheDefinition();
  47. }
  48. protected virtual void CacheDefinition()
  49. {
  50. inputs.Clear();
  51. outputs.Clear();
  52. ports.Clear();
  53. inputs.AddRange(unit.inputs.Select(port => canvas.Widget<IUnitPortWidget>(port)));
  54. outputs.AddRange(unit.outputs.Select(port => canvas.Widget<IUnitPortWidget>(port)));
  55. ports.AddRange(inputs);
  56. ports.AddRange(outputs);
  57. Reposition();
  58. }
  59. protected override void CacheDescription()
  60. {
  61. description = unit.Description<UnitDescription>();
  62. titleContent.text = description.shortTitle;
  63. titleContent.tooltip = description.summary;
  64. surtitleContent.text = description.surtitle;
  65. subtitleContent.text = description.subtitle;
  66. Reposition();
  67. }
  68. protected override void CacheMetadata()
  69. {
  70. settingNames.Clear();
  71. settingNames.AddRange(metadata.valueType
  72. .GetMembers()
  73. .Where(mi => mi.HasAttribute<UnitHeaderInspectableAttribute>())
  74. .OrderBy(mi => mi.GetAttributes<Attribute>().OfType<IInspectableAttribute>().FirstOrDefault()?.order ?? int.MaxValue)
  75. .ThenBy(mi => mi.MetadataToken)
  76. .Select(mi => mi.Name));
  77. lock (settingLabelsContents)
  78. {
  79. settingLabelsContents.Clear();
  80. foreach (var setting in settings)
  81. {
  82. var settingLabel = setting.GetAttribute<UnitHeaderInspectableAttribute>().label;
  83. GUIContent settingContent;
  84. if (string.IsNullOrEmpty(settingLabel))
  85. {
  86. settingContent = null;
  87. }
  88. else
  89. {
  90. settingContent = new GUIContent(settingLabel);
  91. }
  92. settingLabelsContents.Add(setting, settingContent);
  93. }
  94. }
  95. Reposition();
  96. }
  97. public virtual Inspector GetPortInspector(IUnitPort port, Metadata metadata)
  98. {
  99. return metadata.Inspector();
  100. }
  101. #endregion
  102. #region Lifecycle
  103. public override bool foregroundRequiresInput => showSettings || unit.valueInputs.Any(vip => vip.hasDefaultValue);
  104. public override void HandleInput()
  105. {
  106. if (canvas.isCreatingConnection)
  107. {
  108. if (e.IsMouseDown(MouseButton.Left))
  109. {
  110. var source = canvas.connectionSource;
  111. var destination = source.CompatiblePort(unit);
  112. if (destination != null)
  113. {
  114. UndoUtility.RecordEditedObject("Connect Nodes");
  115. source.ValidlyConnectTo(destination);
  116. canvas.connectionSource = null;
  117. canvas.Widget(source.unit).Reposition();
  118. canvas.Widget(destination.unit).Reposition();
  119. GUI.changed = true;
  120. }
  121. e.Use();
  122. }
  123. else if (e.IsMouseDown(MouseButton.Right))
  124. {
  125. canvas.CancelConnection();
  126. e.Use();
  127. }
  128. }
  129. base.HandleInput();
  130. }
  131. #endregion
  132. #region Contents
  133. protected readonly GUIContent titleContent = new GUIContent();
  134. protected readonly GUIContent surtitleContent = new GUIContent();
  135. protected readonly GUIContent subtitleContent = new GUIContent();
  136. protected readonly Dictionary<Metadata, GUIContent> settingLabelsContents = new Dictionary<Metadata, GUIContent>();
  137. #endregion
  138. #region Positioning
  139. protected override bool snapToGrid => BoltCore.Configuration.snapToGrid;
  140. public override IEnumerable<IWidget> positionDependers => ports.Cast<IWidget>();
  141. protected Rect _position;
  142. public override Rect position
  143. {
  144. get { return _position; }
  145. set { unit.position = value.position; }
  146. }
  147. public Rect titlePosition { get; private set; }
  148. public Rect surtitlePosition { get; private set; }
  149. public Rect subtitlePosition { get; private set; }
  150. public Rect iconPosition { get; private set; }
  151. public List<Rect> iconsPositions { get; private set; } = new List<Rect>();
  152. public Dictionary<Metadata, Rect> settingsPositions { get; } = new Dictionary<Metadata, Rect>();
  153. public Rect headerAddonPosition { get; private set; }
  154. public Rect portsBackgroundPosition { get; private set; }
  155. public override void CachePosition()
  156. {
  157. // Width
  158. var inputsWidth = 0f;
  159. var outputsWidth = 0f;
  160. foreach (var input in inputs)
  161. {
  162. inputsWidth = Mathf.Max(inputsWidth, input.GetInnerWidth());
  163. }
  164. foreach (var output in outputs)
  165. {
  166. outputsWidth = Mathf.Max(outputsWidth, output.GetInnerWidth());
  167. }
  168. var portsWidth = 0f;
  169. portsWidth += inputsWidth;
  170. portsWidth += Styles.spaceBetweenInputsAndOutputs;
  171. portsWidth += outputsWidth;
  172. settingsPositions.Clear();
  173. var settingsWidth = 0f;
  174. if (showSettings)
  175. {
  176. foreach (var setting in settings)
  177. {
  178. var settingWidth = 0f;
  179. var settingLabelContent = settingLabelsContents[setting];
  180. if (settingLabelContent != null)
  181. {
  182. settingWidth += Styles.settingLabel.CalcSize(settingLabelContent).x;
  183. }
  184. settingWidth += setting.Inspector().GetAdaptiveWidth();
  185. settingWidth = Mathf.Min(settingWidth, Styles.maxSettingsWidth);
  186. settingsPositions.Add(setting, new Rect(0, 0, settingWidth, 0));
  187. settingsWidth = Mathf.Max(settingsWidth, settingWidth);
  188. }
  189. }
  190. var headerAddonWidth = 0f;
  191. if (showHeaderAddon)
  192. {
  193. headerAddonWidth = GetHeaderAddonWidth();
  194. }
  195. var titleWidth = Styles.title.CalcSize(titleContent).x;
  196. var headerTextWidth = titleWidth;
  197. var surtitleWidth = 0f;
  198. if (showSurtitle)
  199. {
  200. surtitleWidth = Styles.surtitle.CalcSize(surtitleContent).x;
  201. headerTextWidth = Mathf.Max(headerTextWidth, surtitleWidth);
  202. }
  203. var subtitleWidth = 0f;
  204. if (showSubtitle)
  205. {
  206. subtitleWidth = Styles.subtitle.CalcSize(subtitleContent).x;
  207. headerTextWidth = Mathf.Max(headerTextWidth, subtitleWidth);
  208. }
  209. var iconsWidth = 0f;
  210. if (showIcons)
  211. {
  212. var iconsColumns = Mathf.Ceil((float)description.icons.Length / Styles.iconsPerColumn);
  213. iconsWidth = iconsColumns * Styles.iconsSize + ((iconsColumns - 1) * Styles.iconsSpacing);
  214. }
  215. var headerWidth = Mathf.Max(headerTextWidth + iconsWidth, Mathf.Max(settingsWidth, headerAddonWidth)) + Styles.iconSize + Styles.spaceAfterIcon;
  216. var innerWidth = Mathf.Max(portsWidth, headerWidth);
  217. var edgeWidth = InnerToEdgePosition(new Rect(0, 0, innerWidth, 0)).width;
  218. // Height & Positioning
  219. var edgeOrigin = unit.position;
  220. var edgeX = edgeOrigin.x;
  221. var edgeY = edgeOrigin.y;
  222. var innerOrigin = EdgeToInnerPosition(new Rect(edgeOrigin, Vector2.zero)).position;
  223. var innerX = innerOrigin.x;
  224. var innerY = innerOrigin.y;
  225. iconPosition = new Rect
  226. (
  227. innerX,
  228. innerY,
  229. Styles.iconSize,
  230. Styles.iconSize
  231. );
  232. var headerTextX = iconPosition.xMax + Styles.spaceAfterIcon;
  233. var y = innerY;
  234. var headerHeight = 0f;
  235. var surtitleHeight = 0f;
  236. if (showSurtitle)
  237. {
  238. surtitleHeight = Styles.surtitle.CalcHeight(surtitleContent, headerTextWidth);
  239. surtitlePosition = new Rect
  240. (
  241. headerTextX,
  242. y,
  243. headerTextWidth,
  244. surtitleHeight
  245. );
  246. headerHeight += surtitleHeight;
  247. y += surtitleHeight;
  248. headerHeight += Styles.spaceAfterSurtitle;
  249. y += Styles.spaceAfterSurtitle;
  250. }
  251. var titleHeight = 0f;
  252. if (showTitle)
  253. {
  254. titleHeight = Styles.title.CalcHeight(titleContent, headerTextWidth);
  255. titlePosition = new Rect
  256. (
  257. headerTextX,
  258. y,
  259. headerTextWidth,
  260. titleHeight
  261. );
  262. headerHeight += titleHeight;
  263. y += titleHeight;
  264. }
  265. var subtitleHeight = 0f;
  266. if (showSubtitle)
  267. {
  268. headerHeight += Styles.spaceBeforeSubtitle;
  269. y += Styles.spaceBeforeSubtitle;
  270. subtitleHeight = Styles.subtitle.CalcHeight(subtitleContent, headerTextWidth);
  271. subtitlePosition = new Rect
  272. (
  273. headerTextX,
  274. y,
  275. headerTextWidth,
  276. subtitleHeight
  277. );
  278. headerHeight += subtitleHeight;
  279. y += subtitleHeight;
  280. }
  281. iconsPositions.Clear();
  282. if (showIcons)
  283. {
  284. var iconRow = 0;
  285. var iconCol = 0;
  286. for (int i = 0; i < description.icons.Length; i++)
  287. {
  288. var iconPosition = new Rect
  289. (
  290. innerX + innerWidth - ((iconCol + 1) * Styles.iconsSize) - ((iconCol) * Styles.iconsSpacing),
  291. innerY + (iconRow * (Styles.iconsSize + Styles.iconsSpacing)),
  292. Styles.iconsSize,
  293. Styles.iconsSize
  294. );
  295. iconsPositions.Add(iconPosition);
  296. iconRow++;
  297. if (iconRow % Styles.iconsPerColumn == 0)
  298. {
  299. iconCol++;
  300. iconRow = 0;
  301. }
  302. }
  303. }
  304. var settingsHeight = 0f;
  305. if (showSettings)
  306. {
  307. headerHeight += Styles.spaceBeforeSettings;
  308. foreach (var setting in settings)
  309. {
  310. var settingWidth = settingsPositions[setting].width;
  311. using (LudiqGUIUtility.currentInspectorWidth.Override(settingWidth))
  312. {
  313. var settingHeight = LudiqGUI.GetInspectorHeight(null, setting, settingWidth, settingLabelsContents[setting] ?? GUIContent.none);
  314. var settingPosition = new Rect
  315. (
  316. headerTextX,
  317. y,
  318. settingWidth,
  319. settingHeight
  320. );
  321. settingsPositions[setting] = settingPosition;
  322. settingsHeight += settingHeight;
  323. y += settingHeight;
  324. settingsHeight += Styles.spaceBetweenSettings;
  325. y += Styles.spaceBetweenSettings;
  326. }
  327. }
  328. settingsHeight -= Styles.spaceBetweenSettings;
  329. y -= Styles.spaceBetweenSettings;
  330. headerHeight += settingsHeight;
  331. headerHeight += Styles.spaceAfterSettings;
  332. y += Styles.spaceAfterSettings;
  333. }
  334. if (showHeaderAddon)
  335. {
  336. var headerAddonHeight = GetHeaderAddonHeight(headerAddonWidth);
  337. headerAddonPosition = new Rect
  338. (
  339. headerTextX,
  340. y,
  341. headerAddonWidth,
  342. headerAddonHeight
  343. );
  344. headerHeight += headerAddonHeight;
  345. y += headerAddonHeight;
  346. }
  347. if (headerHeight < Styles.iconSize)
  348. {
  349. var difference = Styles.iconSize - headerHeight;
  350. var centeringOffset = difference / 2;
  351. if (showTitle)
  352. {
  353. var _titlePosition = titlePosition;
  354. _titlePosition.y += centeringOffset;
  355. titlePosition = _titlePosition;
  356. }
  357. if (showSubtitle)
  358. {
  359. var _subtitlePosition = subtitlePosition;
  360. _subtitlePosition.y += centeringOffset;
  361. subtitlePosition = _subtitlePosition;
  362. }
  363. if (showSettings)
  364. {
  365. foreach (var setting in settings)
  366. {
  367. var _settingPosition = settingsPositions[setting];
  368. _settingPosition.y += centeringOffset;
  369. settingsPositions[setting] = _settingPosition;
  370. }
  371. }
  372. if (showHeaderAddon)
  373. {
  374. var _headerAddonPosition = headerAddonPosition;
  375. _headerAddonPosition.y += centeringOffset;
  376. headerAddonPosition = _headerAddonPosition;
  377. }
  378. headerHeight = Styles.iconSize;
  379. }
  380. y = innerY + headerHeight;
  381. var innerHeight = 0f;
  382. innerHeight += headerHeight;
  383. if (showPorts)
  384. {
  385. innerHeight += Styles.spaceBeforePorts;
  386. y += Styles.spaceBeforePorts;
  387. var portsBackgroundY = y;
  388. var portsBackgroundHeight = 0f;
  389. portsBackgroundHeight += Styles.portsBackground.padding.top;
  390. innerHeight += Styles.portsBackground.padding.top;
  391. y += Styles.portsBackground.padding.top;
  392. var portStartY = y;
  393. var inputsHeight = 0f;
  394. var outputsHeight = 0f;
  395. foreach (var input in inputs)
  396. {
  397. input.y = y;
  398. var inputHeight = input.GetHeight();
  399. inputsHeight += inputHeight;
  400. y += inputHeight;
  401. inputsHeight += Styles.spaceBetweenPorts;
  402. y += Styles.spaceBetweenPorts;
  403. }
  404. if (inputs.Count > 0)
  405. {
  406. inputsHeight -= Styles.spaceBetweenPorts;
  407. y -= Styles.spaceBetweenPorts;
  408. }
  409. y = portStartY;
  410. foreach (var output in outputs)
  411. {
  412. output.y = y;
  413. var outputHeight = output.GetHeight();
  414. outputsHeight += outputHeight;
  415. y += outputHeight;
  416. outputsHeight += Styles.spaceBetweenPorts;
  417. y += Styles.spaceBetweenPorts;
  418. }
  419. if (outputs.Count > 0)
  420. {
  421. outputsHeight -= Styles.spaceBetweenPorts;
  422. y -= Styles.spaceBetweenPorts;
  423. }
  424. var portsHeight = Math.Max(inputsHeight, outputsHeight);
  425. portsBackgroundHeight += portsHeight;
  426. innerHeight += portsHeight;
  427. y = portStartY + portsHeight;
  428. portsBackgroundHeight += Styles.portsBackground.padding.bottom;
  429. innerHeight += Styles.portsBackground.padding.bottom;
  430. y += Styles.portsBackground.padding.bottom;
  431. portsBackgroundPosition = new Rect
  432. (
  433. edgeX,
  434. portsBackgroundY,
  435. edgeWidth,
  436. portsBackgroundHeight
  437. );
  438. }
  439. var edgeHeight = InnerToEdgePosition(new Rect(0, 0, 0, innerHeight)).height;
  440. _position = new Rect
  441. (
  442. edgeX,
  443. edgeY,
  444. edgeWidth,
  445. edgeHeight
  446. );
  447. }
  448. protected virtual float GetHeaderAddonWidth()
  449. {
  450. return 0;
  451. }
  452. protected virtual float GetHeaderAddonHeight(float width)
  453. {
  454. return 0;
  455. }
  456. #endregion
  457. #region Drawing
  458. protected virtual NodeColorMix baseColor => NodeColor.Gray;
  459. protected override NodeColorMix color
  460. {
  461. get
  462. {
  463. if (unitDebugData.runtimeException != null)
  464. {
  465. return NodeColor.Red;
  466. }
  467. var color = baseColor;
  468. if (analysis.warnings.Count > 0)
  469. {
  470. var mostSevereWarning = Warning.MostSevereLevel(analysis.warnings);
  471. switch (mostSevereWarning)
  472. {
  473. case WarningLevel.Error:
  474. color = NodeColor.Red;
  475. break;
  476. case WarningLevel.Severe:
  477. color = NodeColor.Orange;
  478. break;
  479. case WarningLevel.Caution:
  480. color = NodeColor.Yellow;
  481. break;
  482. }
  483. }
  484. if (EditorApplication.isPaused)
  485. {
  486. if (EditorTimeBinding.frame == unitDebugData.lastInvokeFrame)
  487. {
  488. return NodeColor.Blue;
  489. }
  490. }
  491. else
  492. {
  493. var mix = color;
  494. mix.blue = Mathf.Lerp(1, 0, (EditorTimeBinding.time - unitDebugData.lastInvokeTime) / Styles.invokeFadeDuration);
  495. return mix;
  496. }
  497. return color;
  498. }
  499. }
  500. protected override NodeShape shape => NodeShape.Square;
  501. protected virtual bool showTitle => !string.IsNullOrEmpty(description.shortTitle);
  502. protected virtual bool showSurtitle => !string.IsNullOrEmpty(description.surtitle);
  503. protected virtual bool showSubtitle => !string.IsNullOrEmpty(description.subtitle);
  504. protected virtual bool showIcons => description.icons.Length > 0;
  505. protected virtual bool showSettings => settingNames.Count > 0;
  506. protected virtual bool showHeaderAddon => false;
  507. protected virtual bool showPorts => ports.Count > 0;
  508. protected override bool dim
  509. {
  510. get
  511. {
  512. var dim = BoltCore.Configuration.dimInactiveNodes && !analysis.isEntered;
  513. if (isMouseOver || isSelected)
  514. {
  515. dim = false;
  516. }
  517. if (BoltCore.Configuration.dimIncompatibleNodes && canvas.isCreatingConnection)
  518. {
  519. dim = !unit.ports.Any(p => canvas.connectionSource == p || canvas.connectionSource.CanValidlyConnectTo(p));
  520. }
  521. return dim;
  522. }
  523. }
  524. public override void DrawForeground()
  525. {
  526. BeginDim();
  527. base.DrawForeground();
  528. DrawIcon();
  529. if (showSurtitle)
  530. {
  531. DrawSurtitle();
  532. }
  533. if (showTitle)
  534. {
  535. DrawTitle();
  536. }
  537. if (showSubtitle)
  538. {
  539. DrawSubtitle();
  540. }
  541. if (showIcons)
  542. {
  543. DrawIcons();
  544. }
  545. if (showSettings)
  546. {
  547. DrawSettings();
  548. }
  549. if (showHeaderAddon)
  550. {
  551. DrawHeaderAddon();
  552. }
  553. if (showPorts)
  554. {
  555. DrawPortsBackground();
  556. }
  557. EndDim();
  558. }
  559. protected void DrawIcon()
  560. {
  561. var icon = description.icon ?? BoltFlow.Icons.unit;
  562. if (icon != null && icon[(int)iconPosition.width])
  563. {
  564. GUI.DrawTexture(iconPosition, icon[(int)iconPosition.width]);
  565. }
  566. }
  567. protected void DrawTitle()
  568. {
  569. GUI.Label(titlePosition, titleContent, invertForeground ? Styles.titleInverted : Styles.title);
  570. }
  571. protected void DrawSurtitle()
  572. {
  573. GUI.Label(surtitlePosition, surtitleContent, invertForeground ? Styles.surtitleInverted : Styles.surtitle);
  574. }
  575. protected void DrawSubtitle()
  576. {
  577. GUI.Label(subtitlePosition, subtitleContent, invertForeground ? Styles.subtitleInverted : Styles.subtitle);
  578. }
  579. protected void DrawIcons()
  580. {
  581. for (int i = 0; i < description.icons.Length; i++)
  582. {
  583. var icon = description.icons[i];
  584. var position = iconsPositions[i];
  585. GUI.DrawTexture(position, icon?[(int)position.width]);
  586. }
  587. }
  588. private void DrawSettings()
  589. {
  590. if (graph.zoom < FlowCanvas.inspectorZoomThreshold)
  591. {
  592. return;
  593. }
  594. EditorGUI.BeginDisabledGroup(!e.IsRepaint && isMouseThrough && !isMouseOver);
  595. EditorGUI.BeginChangeCheck();
  596. foreach (var setting in settings)
  597. {
  598. DrawSetting(setting);
  599. }
  600. if (EditorGUI.EndChangeCheck())
  601. {
  602. unit.Define();
  603. Reposition();
  604. }
  605. EditorGUI.EndDisabledGroup();
  606. }
  607. protected void DrawSetting(Metadata setting)
  608. {
  609. var settingPosition = settingsPositions[setting];
  610. using (LudiqGUIUtility.currentInspectorWidth.Override(settingPosition.width))
  611. using (Inspector.expandTooltip.Override(false))
  612. {
  613. var label = settingLabelsContents[setting];
  614. if (label == null)
  615. {
  616. LudiqGUI.Inspector(setting, settingPosition, GUIContent.none);
  617. }
  618. else
  619. {
  620. using (Inspector.defaultLabelStyle.Override(Styles.settingLabel))
  621. using (LudiqGUIUtility.labelWidth.Override(Styles.settingLabel.CalcSize(label).x))
  622. {
  623. LudiqGUI.Inspector(setting, settingPosition, label);
  624. }
  625. }
  626. }
  627. }
  628. protected virtual void DrawHeaderAddon() { }
  629. protected void DrawPortsBackground()
  630. {
  631. if (canvas.showRelations)
  632. {
  633. foreach (var relation in unit.relations)
  634. {
  635. var start = ports.Single(pw => pw.port == relation.source).handlePosition.center;
  636. var end = ports.Single(pw => pw.port == relation.destination).handlePosition.center;
  637. var startTangent = start;
  638. var endTangent = end;
  639. if (relation.source is IUnitInputPort &&
  640. relation.destination is IUnitInputPort)
  641. {
  642. //startTangent -= new Vector2(20, 0);
  643. endTangent -= new Vector2(32, 0);
  644. }
  645. else
  646. {
  647. startTangent += new Vector2(innerPosition.width / 2, 0);
  648. endTangent += new Vector2(-innerPosition.width / 2, 0);
  649. }
  650. Handles.DrawBezier
  651. (
  652. start,
  653. end,
  654. startTangent,
  655. endTangent,
  656. new Color(0.136f, 0.136f, 0.136f, 1.0f),
  657. null,
  658. 3
  659. );
  660. }
  661. }
  662. else
  663. {
  664. if (e.IsRepaint)
  665. {
  666. Styles.portsBackground.Draw(portsBackgroundPosition, false, false, false, false);
  667. }
  668. }
  669. }
  670. #endregion
  671. #region Selecting
  672. public override bool canSelect => true;
  673. #endregion
  674. #region Dragging
  675. public override bool canDrag => true;
  676. public override void ExpandDragGroup(HashSet<IGraphElement> dragGroup)
  677. {
  678. if (BoltCore.Configuration.carryChildren)
  679. {
  680. foreach (var output in unit.outputs)
  681. {
  682. foreach (var connection in output.connections)
  683. {
  684. if (dragGroup.Contains(connection.destination.unit))
  685. {
  686. continue;
  687. }
  688. dragGroup.Add(connection.destination.unit);
  689. canvas.Widget(connection.destination.unit).ExpandDragGroup(dragGroup);
  690. }
  691. }
  692. }
  693. }
  694. #endregion
  695. #region Deleting
  696. public override bool canDelete => true;
  697. #endregion
  698. #region Clipboard
  699. public override void ExpandCopyGroup(HashSet<IGraphElement> copyGroup)
  700. {
  701. copyGroup.UnionWith(unit.connections.Cast<IGraphElement>());
  702. }
  703. #endregion
  704. #region Context
  705. protected override IEnumerable<DropdownOption> contextOptions
  706. {
  707. get
  708. {
  709. yield return new DropdownOption((Action)ReplaceUnit, "Replace...");
  710. foreach (var baseOption in base.contextOptions)
  711. {
  712. yield return baseOption;
  713. }
  714. }
  715. }
  716. private void ReplaceUnit()
  717. {
  718. UnitWidgetHelper.ReplaceUnit(unit, reference, context, selection, e);
  719. }
  720. #endregion
  721. public static class Styles
  722. {
  723. static Styles()
  724. {
  725. // Disabling word wrap because Unity's CalcSize and CalcHeight
  726. // are broken w.r.t. pixel-perfection and matrix
  727. title = new GUIStyle(BoltCore.Styles.nodeLabel);
  728. title.padding = new RectOffset(0, 5, 0, 2);
  729. title.margin = new RectOffset(0, 0, 0, 0);
  730. title.fontSize = 12;
  731. title.alignment = TextAnchor.MiddleLeft;
  732. title.wordWrap = false;
  733. surtitle = new GUIStyle(BoltCore.Styles.nodeLabel);
  734. surtitle.padding = new RectOffset(0, 5, 0, 0);
  735. surtitle.margin = new RectOffset(0, 0, 0, 0);
  736. surtitle.fontSize = 10;
  737. surtitle.alignment = TextAnchor.MiddleLeft;
  738. surtitle.wordWrap = false;
  739. subtitle = new GUIStyle(surtitle);
  740. subtitle.padding.bottom = 2;
  741. titleInverted = new GUIStyle(title);
  742. titleInverted.normal.textColor = ColorPalette.unityBackgroundDark;
  743. surtitleInverted = new GUIStyle(surtitle);
  744. surtitleInverted.normal.textColor = ColorPalette.unityBackgroundDark;
  745. subtitleInverted = new GUIStyle(subtitle);
  746. subtitleInverted.normal.textColor = ColorPalette.unityBackgroundDark;
  747. if (EditorGUIUtility.isProSkin)
  748. {
  749. portsBackground = new GUIStyle("In BigTitle")
  750. {
  751. padding = new RectOffset(0, 0, 6, 5)
  752. };
  753. }
  754. else
  755. {
  756. TextureResolution[] textureResolution = { 2 };
  757. var createTextureOptions = CreateTextureOptions.Scalable;
  758. EditorTexture normalTexture = BoltCore.Resources.LoadTexture($"NodePortsBackground.png", textureResolution, createTextureOptions);
  759. portsBackground = new GUIStyle
  760. {
  761. normal = { background = normalTexture.Single() },
  762. padding = new RectOffset(0, 0, 6, 5)
  763. };
  764. }
  765. settingLabel = new GUIStyle(BoltCore.Styles.nodeLabel);
  766. settingLabel.padding.left = 0;
  767. settingLabel.padding.right = 5;
  768. settingLabel.wordWrap = false;
  769. settingLabel.clipping = TextClipping.Clip;
  770. }
  771. public static readonly GUIStyle title;
  772. public static readonly GUIStyle surtitle;
  773. public static readonly GUIStyle subtitle;
  774. public static readonly GUIStyle titleInverted;
  775. public static readonly GUIStyle surtitleInverted;
  776. public static readonly GUIStyle subtitleInverted;
  777. public static readonly GUIStyle settingLabel;
  778. public static readonly float spaceAroundLineIcon = 5;
  779. public static readonly float spaceBeforePorts = 5;
  780. public static readonly float spaceBetweenInputsAndOutputs = 8;
  781. public static readonly float spaceBeforeSettings = 2;
  782. public static readonly float spaceBetweenSettings = 3;
  783. public static readonly float spaceBetweenPorts = 3;
  784. public static readonly float spaceAfterSettings = 0;
  785. public static readonly float maxSettingsWidth = 150;
  786. public static readonly GUIStyle portsBackground;
  787. public static readonly float iconSize = IconSize.Medium;
  788. public static readonly float iconsSize = IconSize.Small;
  789. public static readonly float iconsSpacing = 3;
  790. public static readonly int iconsPerColumn = 2;
  791. public static readonly float spaceAfterIcon = 6;
  792. public static readonly float spaceAfterSurtitle = 2;
  793. public static readonly float spaceBeforeSubtitle = 0;
  794. public static readonly float invokeFadeDuration = 0.5f;
  795. }
  796. }
  797. internal class UnitWidgetHelper
  798. {
  799. internal static void ReplaceUnit(IUnit unit, GraphReference reference, IGraphContext context, GraphSelection selection, EventWrapper eventWrapper)
  800. {
  801. var oldUnit = unit;
  802. var unitPosition = oldUnit.position;
  803. var preservation = UnitPreservation.Preserve(oldUnit);
  804. var options = new UnitOptionTree(new GUIContent("Node"));
  805. options.filter = UnitOptionFilter.Any;
  806. options.filter.NoConnection = false;
  807. options.reference = reference;
  808. var activatorPosition = new Rect(eventWrapper.mousePosition, new Vector2(200, 1));
  809. LudiqGUI.FuzzyDropdown
  810. (
  811. activatorPosition,
  812. options,
  813. null,
  814. delegate (object _option)
  815. {
  816. var option = (IUnitOption)_option;
  817. context.BeginEdit();
  818. UndoUtility.RecordEditedObject("Replace Node");
  819. var graph = oldUnit.graph;
  820. oldUnit.graph.units.Remove(oldUnit);
  821. var newUnit = option.InstantiateUnit();
  822. newUnit.guid = Guid.NewGuid();
  823. newUnit.position = unitPosition;
  824. graph.units.Add(newUnit);
  825. preservation.RestoreTo(newUnit);
  826. option.PreconfigureUnit(newUnit);
  827. selection.Select(newUnit);
  828. GUI.changed = true;
  829. context.EndEdit();
  830. }
  831. );
  832. }
  833. }
  834. }