mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
Allow node input/output tooltips to be overriden through
the use of an attribute applied to custom classes.
This commit is contained in:
parent
5005b5e4f9
commit
7d37c9dfcc
23
Scripts/Attributes/OverrideTooltipAttribute.cs
Normal file
23
Scripts/Attributes/OverrideTooltipAttribute.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace XNodeEditor {
|
||||
|
||||
/// <summary>
|
||||
/// <para>When applied to the definition of a custom class, 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.Class)]
|
||||
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:
|
||||
@ -123,9 +123,17 @@ namespace XNodeEditor {
|
||||
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);
|
||||
|
||||
// Now allow tooltips to be overridden or to have their values hidden based on attribute usage
|
||||
var targetType = portType.HasElementType ? portType.GetElementType() : portType; // For arrays and lists.
|
||||
// ReSharper disable once PossibleNullReferenceException
|
||||
var attr = ((OverrideTooltipAttribute[])targetType
|
||||
.GetCustomAttributes(typeof(OverrideTooltipAttribute), false)).SingleOrDefault();
|
||||
tooltip = attr?.overrideTooltip ?? false ? attr.tooltip : targetType.PrettyName();
|
||||
var hideValue = attr?.hideValue ?? false;
|
||||
// ReSharper disable once InvertIf
|
||||
if (!hideValue && port.IsOutput) {
|
||||
var obj = port.node.GetValue(port);
|
||||
tooltip += " = " + (obj != null ? obj.ToString() : "null");
|
||||
}
|
||||
return tooltip;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user