mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
Moved attribute to proper namespace and made it apply to fields. C# 4 compliant.
This commit is contained in:
parent
7d37c9dfcc
commit
cbda73ac22
@ -1,13 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace XNodeEditor {
|
||||
namespace XNode {
|
||||
|
||||
/// <summary>
|
||||
/// <para>When applied to the definition of a custom class, allows for the tooltip shown beside the nodes to be overriden.</para>
|
||||
/// <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.Class)]
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public sealed class OverrideTooltipAttribute : Attribute {
|
||||
public readonly bool overrideTooltip;
|
||||
public readonly string tooltip;
|
||||
|
||||
@ -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>
|
||||
@ -121,16 +122,26 @@ namespace XNodeEditor {
|
||||
|
||||
/// <summary> Override to display custom tooltips </summary>
|
||||
public virtual string GetPortTooltip(XNode.NodePort port) {
|
||||
Type portType = port.ValueType;
|
||||
string tooltip = "";
|
||||
|
||||
// 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;
|
||||
// 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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user