Browse Source

Force Sequence names to be unique within a Fungus Script

master
chrisgregan 10 years ago
parent
commit
366f6d62ac
  1. 2
      Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs
  2. 7
      Assets/Fungus/FungusScript/Editor/SequenceEditor.cs
  3. 44
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  4. 2
      Assets/Fungus/FungusScript/Scripts/Sequence.cs

2
Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs

@ -630,7 +630,7 @@ namespace Fungus
Sequence oldSequence = sequence;
Sequence newSequence = FungusScriptWindow.CreateSequence(fungusScript, newPosition);
newSequence.sequenceName = oldSequence.sequenceName + " (Copy)";
newSequence.sequenceName = fungusScript.GetUniqueSequenceKey(oldSequence.sequenceName + " (Copy)");
Undo.RecordObject(newSequence, "Duplicate Sequence");

7
Assets/Fungus/FungusScript/Editor/SequenceEditor.cs

@ -47,6 +47,13 @@ namespace Fungus
Rect sequenceNameRect = new Rect(45, 21, 180, 16);
EditorGUI.PropertyField(sequenceNameRect, sequenceNameProperty, new GUIContent(""));
// Ensure sequence name is unique for this Fungus Script
string uniqueName = fungusScript.GetUniqueSequenceKey(sequenceNameProperty.stringValue, sequence);
if (uniqueName != sequence.sequenceName)
{
sequenceNameProperty.stringValue = uniqueName;
}
SerializedProperty commandListProperty = serializedObject.FindProperty("commandList");
ReorderableListGUI.Title("Commands");

44
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -98,6 +98,7 @@ namespace Fungus
Sequence s = CreateSequenceComponent(gameObject);
s.nodeRect.x = position.x;
s.nodeRect.y = position.y;
s.sequenceName = GetUniqueSequenceKey(s.sequenceName, s);
return s;
}
@ -220,6 +221,49 @@ namespace Fungus
}
}
/**
* Returns a new Sequence key that is guaranteed not to clash with any existing Sequence in the Fungus Script.
*/
public virtual string GetUniqueSequenceKey(string originalKey, Sequence ignoreSequence = null)
{
int suffix = 0;
string baseKey = originalKey.Trim();
// No empty keys allowed
if (baseKey.Length == 0)
{
baseKey = "Sequence";
}
Sequence[] sequences = GetComponentsInChildren<Sequence>();
string key = baseKey;
while (true)
{
bool collision = false;
foreach(Sequence sequence in sequences)
{
if (sequence == ignoreSequence ||
sequence.sequenceName == null)
{
continue;
}
if (sequence.sequenceName.Equals(key, StringComparison.CurrentCultureIgnoreCase))
{
collision = true;
suffix++;
key = baseKey + suffix;
}
}
if (!collision)
{
return key;
}
}
}
/**
* Gets the value of a boolean variable.
* Returns false if the variable key does not exist.

2
Assets/Fungus/FungusScript/Scripts/Sequence.cs

@ -9,7 +9,7 @@ namespace Fungus
[RequireComponent(typeof(FungusScript))]
public class Sequence : Node
{
public string sequenceName = "New Sequence";
public string sequenceName = "Sequence";
[TextArea(2, 5)]
[Tooltip("Description text to display under the sequence node")]

Loading…
Cancel
Save