Browse Source

Prevent invalid / confusing variable name key entry

master
chrisgregan 10 years ago
parent
commit
857184a8bb
  1. 41
      Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs
  2. 10
      Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs
  3. 50
      Assets/Fungus/VisualScripting/FungusScript.cs
  4. BIN
      Assets/Shuttle/ShuttleGame.unity

41
Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs

@ -11,16 +11,6 @@ namespace Fungus.Script
[CustomEditor (typeof(FungusScript))]
public class FungusScriptEditor : Editor
{
SerializedProperty variablesProperty;
void OnEnable()
{
if (this != null && serializedObject != null)
{
variablesProperty = serializedObject.FindProperty("variables");
}
}
public void OnInspectorUpdate()
{
Repaint();
@ -28,8 +18,6 @@ namespace Fungus.Script
public override void OnInspectorGUI()
{
serializedObject.Update();
FungusScript t = target as FungusScript;
if (t != null)
@ -136,8 +124,6 @@ namespace Fungus.Script
DrawAddCommandGUI(t.selectedSequence);
}
}
serializedObject.ApplyModifiedProperties();
}
public void DrawSequenceGUI(Sequence sequence)
@ -245,6 +231,7 @@ namespace Fungus.Script
ReorderableListGUI.Title("Variables");
SerializedProperty variablesProperty = serializedObject.FindProperty("variables");
FungusVariableListAdaptor adaptor = new FungusVariableListAdaptor(variablesProperty, 0);
ReorderableListControl.DrawControlFromState(adaptor, null, ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton);
@ -277,33 +264,9 @@ namespace Fungus.Script
Undo.RecordObject(fungusScript, "Add Variable");
T variable = fungusScript.gameObject.AddComponent<T>();
variable.key = MakeUniqueKey(fungusScript);
variable.key = fungusScript.GetUniqueVariableKey("");
fungusScript.variables.Add(variable);
}
string MakeUniqueKey(FungusScript fungusScript)
{
int index = 0;
while (true)
{
string key = "Var" + index;
bool found = false;
foreach(FungusVariable variable in fungusScript.GetComponents<FungusVariable>())
{
if (variable.key == key)
{
found = true;
index++;
}
}
if (!found)
{
return key;
}
}
}
}
}

10
Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs

@ -5,7 +5,6 @@
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using Rotorz.ReorderableList;
namespace Fungus.Script
@ -163,18 +162,17 @@ namespace Fungus.Script
VariableScope scope = (VariableScope)EditorGUI.EnumPopup(scopeRect, variable.scope);
FungusScript fungusScript = FungusScriptWindow.GetFungusScript();
if (EditorGUI.EndChangeCheck ())
{
Undo.RecordObject(variable, "Set Variable");
char[] arr = key.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray();
key = new string(arr);
variable.key = key;
// Modify the key if it clashes with an existing variable key
variable.key = fungusScript.GetUniqueVariableKey(key, variable);
variable.scope = scope;
}
FungusScript fungusScript = FungusScriptWindow.GetFungusScript();
if (fungusScript != null)
{
bool highlight = false;

50
Assets/Fungus/VisualScripting/FungusScript.cs

@ -1,8 +1,6 @@
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
@ -73,6 +71,50 @@ namespace Fungus.Script
selectedSequence = sequence;
sequence.ExecuteNextCommand();
}
public string GetUniqueVariableKey(string originalKey, FungusVariable ignoreVariable = null)
{
int suffix = 0;
string baseKey = originalKey;
// Only letters and digits allowed
char[] arr = baseKey.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray();
baseKey = new string(arr);
// No leading digits allowed
baseKey = baseKey.TrimStart('0','1','2','3','4','5','6','7','8','9');
// No empty keys allowed
if (baseKey.Length == 0)
{
baseKey = "Var";
}
string key = baseKey;
while (true)
{
bool collision = false;
foreach(FungusVariable variable in GetComponents<FungusVariable>())
{
if (variable == ignoreVariable)
{
continue;
}
if (variable.key.Equals(key, StringComparison.CurrentCultureIgnoreCase))
{
collision = true;
suffix++;
key = baseKey + suffix;
}
}
if (!collision)
{
return key;
}
}
}
}
}

BIN
Assets/Shuttle/ShuttleGame.unity

Binary file not shown.
Loading…
Cancel
Save