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

added NodeTooltipAttribute that can be set on nodes. Gives a tooltip when hovering Node header label

This commit is contained in:
Simon Rodriguez 2020-05-24 18:38:51 +02:00
parent 7230861502
commit edfdff6b94
3 changed files with 26 additions and 1 deletions

View File

@ -25,7 +25,10 @@ namespace XNodeEditor {
#endif
public virtual void OnHeaderGUI() {
GUILayout.Label(target.name, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
Type type = target.GetType();
string tooltip;
type.TryGetAttributeTooltip(out tooltip);
GUILayout.Label(new GUIContent(target.name, tooltip), NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
}
/// <summary> Draws standard field editors for all public fields </summary>

View File

@ -11,6 +11,7 @@ namespace XNodeEditor {
public static class NodeEditorReflection {
[NonSerialized] private static Dictionary<Type, Color> nodeTint;
[NonSerialized] private static Dictionary<Type, int> nodeWidth;
[NonSerialized] private static Dictionary<Type, string> nodeTooltip;
/// <summary> All available node types </summary>
public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } }
@ -44,6 +45,14 @@ namespace XNodeEditor {
return nodeWidth.TryGetValue(nodeType, out width);
}
/// <summary> Get custom node tooltip defined with [NodeTooltip(tooltip)] </summary>
public static bool TryGetAttributeTooltip(this Type nodeType, out string tooltip) {
if (nodeTooltip == null) {
CacheAttributes<string, XNode.Node.NodeTooltipAttribute>(ref nodeTooltip, x => x.tooltip);
}
return nodeTooltip.TryGetValue(nodeType, out tooltip);
}
private static void CacheAttributes<V, A>(ref Dictionary<Type, V> dict, Func<A, V> getter) where A : Attribute {
dict = new Dictionary<Type, V>();
for (int i = 0; i < nodeTypes.Length; i++) {

View File

@ -385,6 +385,19 @@ namespace XNode {
this.width = width;
}
}
/// <summary> Specify a tooltip for this node type </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class NodeTooltipAttribute : Attribute
{
public string tooltip;
/// <summary> Specify a tooltip for this node type </summary>
/// <param name="tooltip"> The tooltip text </param>
public NodeTooltipAttribute(string tooltip)
{
this.tooltip = tooltip;
}
}
#endregion
[Serializable] private class NodePortDictionary : Dictionary<string, NodePort>, ISerializationCallbackReceiver {