Skip to content

yarn node attribute with node filtering #324

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

Open
wants to merge 1 commit into
base: main
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
12 changes: 12 additions & 0 deletions Editor/Editors/PropertyDrawers/YarnNodeAttributeDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
var name = node.Name;

if (!string.IsNullOrEmpty(attribute.filter))
{
var filter = attribute.filter.ToLower().Replace('-', '_');

switch (attribute.filterType)
{
case YarnNodeFilter.Contains: if (!name.ToLower().Contains(filter)) continue; break;
case YarnNodeFilter.Start: if (!name.ToLower().StartsWith(filter)) continue; break;
case YarnNodeFilter.End: if (!name.ToLower().EndsWith(filter)) continue; break;
}
}

menu.AddItem(new GUIContent(name), name == nodeName && !hasMixedNodeValues, () =>
{
property.stringValue = name;
Expand Down
16 changes: 15 additions & 1 deletion Runtime/Attributes/YarnNodeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.

namespace Yarn.Unity.Attributes
{
public enum YarnNodeFilter { None, Contains, Start, End }

/// <summary>
/// Specifies that a field represents a reference to a named Yarn node that
/// exists in a Yarn project.
Expand All @@ -28,17 +30,29 @@ public class YarnNodeAttribute : PropertyAttribute
/// </summary>
public readonly string yarnProjectAttribute;

/// <summary>
/// The text that specifies the Nodes you want to include in the dropdown.
/// </summary>
public readonly string filter;

/// <summary>
/// The filter type you'd like to use to find Nodes.
/// </summary>
public readonly YarnNodeFilter filterType;

public readonly bool requiresYarnProject;

/// <summary>
/// Initialises a new instance of <see cref="YarnNodeAttribute"/>.
/// </summary>
/// <param name="yarnProjectAttribute"><inheritdoc
/// cref="yarnProjectAttribute" path="/summary/node()"/></param>
public YarnNodeAttribute(string yarnProjectAttribute, bool requiresYarnProject = true)
public YarnNodeAttribute(string yarnProjectAttribute, bool requiresYarnProject = true, string filter = default, YarnNodeFilter filterType = YarnNodeFilter.Contains)
{
this.yarnProjectAttribute = yarnProjectAttribute;
this.requiresYarnProject = requiresYarnProject;
this.filter = filter;
this.filterType = filterType;
}
}
}