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:
commit
1139be6fe4
23
Scripts/Attributes/OverrideTooltipAttribute.cs
Normal file
23
Scripts/Attributes/OverrideTooltipAttribute.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
Scripts/Attributes/OverrideTooltipAttribute.cs.meta
Normal file
11
Scripts/Attributes/OverrideTooltipAttribute.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b578945cab8b7c643ac937a49febb57e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -1,7 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using XNode;
|
||||||
|
|
||||||
namespace XNodeEditor {
|
namespace XNodeEditor {
|
||||||
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
|
/// <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>
|
/// <summary> Override to display custom tooltips </summary>
|
||||||
public virtual string GetPortTooltip(XNode.NodePort port) {
|
public virtual string GetPortTooltip(XNode.NodePort port) {
|
||||||
Type portType = port.ValueType;
|
// Allow tooltips to be overridden or to have their values hidden based on attribute usage
|
||||||
string tooltip = "";
|
// First, a port managed by a DynamicPortList will have a fieldName of "realName X", so we won't find it directly.
|
||||||
tooltip = portType.PrettyName();
|
FieldInfo portFieldInfo = null;
|
||||||
if (port.IsOutput) {
|
string[] fieldNameParts = port.fieldName.Split(' ');
|
||||||
object obj = port.node.GetValue(port);
|
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");
|
tooltip += " = " + (obj != null ? obj.ToString() : "null");
|
||||||
}
|
}
|
||||||
return tooltip;
|
return tooltip;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user