Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special field attribute allowing for I/O tooltip overrides (or hiding the result) #220

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Scripts/Attributes/OverrideTooltipAttribute.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions Scripts/Attributes/OverrideTooltipAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions Scripts/Editor/NodeGraphEditor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
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>
Expand Down Expand Up @@ -121,11 +122,29 @@ public virtual Color GetTypeColor(Type type) {

/// <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;
Expand Down