1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

Merge cbda73ac2252734c149a5d40d7841b4d18fd451b into 286b9a167a539efdedd6c3d131b049edd0019d41

This commit is contained in:
Lumos 2021-06-26 14:21:22 +08:00 committed by GitHub
commit 1139be6fe4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 5 deletions

View File

@ -0,0 +1,23 @@
using System;
namespace XNode {
/// <summary>
/// <para>When applied to a field marked as node input/output, allows for the tooltip shown beside the nodes to be overriden.</para>
/// Optionally, whether an output node's value is shown in the tooltip can also be turned off.
/// Leaving the tooltip override blank or null will leave the normal tooltip (type name) in place.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public sealed class OverrideTooltipAttribute : Attribute {
public readonly bool overrideTooltip;
public readonly string tooltip;
public readonly bool hideValue;
public OverrideTooltipAttribute(string tooltip = "", bool hideValue = false) {
overrideTooltip = !string.IsNullOrEmpty(tooltip);
if (overrideTooltip) this.tooltip = tooltip;
this.hideValue = hideValue;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b578945cab8b7c643ac937a49febb57e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,9 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using XNode;
namespace XNodeEditor {
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
@ -184,11 +186,29 @@ namespace XNodeEditor {
/// <summary> Override to display custom tooltips </summary>
public virtual string GetPortTooltip(XNode.NodePort port) {
Type portType = port.ValueType;
string tooltip = "";
tooltip = portType.PrettyName();
if (port.IsOutput) {
object obj = port.node.GetValue(port);
// Allow tooltips to be overridden or to have their values hidden based on attribute usage
// First, a port managed by a DynamicPortList will have a fieldName of "realName X", so we won't find it directly.
FieldInfo portFieldInfo = null;
string[] fieldNameParts = port.fieldName.Split(' ');
if (port.IsDynamic && fieldNameParts.Length == 2) {
portFieldInfo = port.node.GetType().GetField(fieldNameParts[0]);
}
// If a port isn't dynamic, doesn't match the format, or we didn't find its field, try getting it directly
if (portFieldInfo == null) {
portFieldInfo = port.node.GetType().GetField(port.fieldName);
}
// If the fieldInfo is still null, abort mission; otherwise, look for our attribute.
OverrideTooltipAttribute attr = null;
if (portFieldInfo != null) {
attr = ((OverrideTooltipAttribute[])portFieldInfo
.GetCustomAttributes(typeof(OverrideTooltipAttribute), false)).SingleOrDefault();
}
string tooltip = attr != null && attr.overrideTooltip ? attr.tooltip : port.ValueType.PrettyName();
bool hideValue = attr != null && attr.hideValue;
// ReSharper disable once InvertIf
if (!hideValue && port.IsOutput) {
var obj = port.node.GetValue(port);
tooltip += " = " + (obj != null ? obj.ToString() : "null");
}
return tooltip;