Browse Source

Label & Jump commands #78

Use label to specify a location in a command list, and the jump to
command to move execution to that point.
master
chrisgregan 10 years ago
parent
commit
739a69207b
  1. 32
      Assets/Fungus/FungusScript/Editor/JumpEditor.cs
  2. 8
      Assets/Fungus/FungusScript/Editor/JumpEditor.cs.meta
  3. 53
      Assets/Fungus/FungusScript/Editor/LabelEditor.cs
  4. 8
      Assets/Fungus/FungusScript/Editor/LabelEditor.cs.meta
  5. 6
      Assets/Fungus/FungusScript/Editor/SequenceEditor.cs
  6. 3
      Assets/Fungus/FungusScript/Scripts/Command.cs
  7. 16
      Assets/Fungus/FungusScript/Scripts/Commands/Jump.cs
  8. 6
      Assets/Fungus/FungusScript/Scripts/Commands/Label.cs

32
Assets/Fungus/FungusScript/Editor/JumpEditor.cs

@ -0,0 +1,32 @@
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Fungus
{
[CustomEditor (typeof(Jump))]
public class JumpEditor : CommandEditor
{
protected SerializedProperty targetLabelProp;
protected virtual void OnEnable()
{
targetLabelProp = serializedObject.FindProperty("targetLabel");
}
public override void DrawCommandGUI()
{
serializedObject.Update();
Jump t = target as Jump;
LabelEditor.LabelField(targetLabelProp,
new GUIContent("Target Label", "Label to jump to"),
t.parentSequence);
serializedObject.ApplyModifiedProperties();
}
}
}

8
Assets/Fungus/FungusScript/Editor/JumpEditor.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c1fd0ccd416054df994af1949fa6fce3
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

53
Assets/Fungus/FungusScript/Editor/LabelEditor.cs

@ -0,0 +1,53 @@
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Fungus
{
public class LabelEditor
{
static public void LabelField(SerializedProperty property,
GUIContent labelText,
Sequence sequence)
{
List<string> labelKeys = new List<string>();
List<Label> labelObjects = new List<Label>();
labelKeys.Add("<None>");
labelObjects.Add(null);
Label selectedLabel = property.objectReferenceValue as Label;
int index = 0;
int selectedIndex = 0;
foreach (Command command in sequence.commandList)
{
Label label = command as Label;
if (label == null)
{
continue;
}
labelKeys.Add(label.key);
labelObjects.Add(label);
index++;
if (label == selectedLabel)
{
selectedIndex = index;
}
}
selectedIndex = EditorGUILayout.Popup(labelText.text, selectedIndex, labelKeys.ToArray());
property.objectReferenceValue = labelObjects[selectedIndex];
}
}
}

8
Assets/Fungus/FungusScript/Editor/LabelEditor.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 788b0e4d9a7584649a4c608bbacc8ada
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

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

@ -55,6 +55,12 @@ namespace Fungus
sequenceNameProperty.stringValue = uniqueName;
}
// Make sure each command has a reference to its parent sequence
foreach (Command command in sequence.commandList)
{
command.parentSequence = sequence;
}
SerializedProperty commandListProperty = serializedObject.FindProperty("commandList");
ReorderableListGUI.Title("Commands");

3
Assets/Fungus/FungusScript/Scripts/Command.cs

@ -42,7 +42,8 @@ namespace Fungus
/**
* Reference to the Sequence object that this command belongs to.
* This reference is only set at runtime (null in editor).
* This reference is only populated at runtime and in the editor when the
* sequence is selected.
*/
[NonSerialized]
public Sequence parentSequence;

16
Assets/Fungus/FungusScript/Scripts/Commands/Jump.cs

@ -10,11 +10,11 @@ namespace Fungus
[AddComponentMenu("")]
public class Jump : Command
{
public string labelName;
public Label targetLabel;
public override void OnEnter()
{
if (labelName.Length == 0)
if (targetLabel == null)
{
Continue();
return;
@ -24,7 +24,7 @@ namespace Fungus
{
Label label = command as Label;
if (label != null &&
label.labelName == labelName)
label == targetLabel)
{
Continue(label.commandIndex + 1);
break;
@ -32,6 +32,16 @@ namespace Fungus
}
}
public override string GetSummary()
{
if (targetLabel == null)
{
return "Error: No label selected";
}
return "To " + targetLabel.key;
}
public override Color GetButtonColor()
{
return new Color32(253, 253, 150, 255);

6
Assets/Fungus/FungusScript/Scripts/Commands/Label.cs

@ -10,7 +10,7 @@ namespace Fungus
[AddComponentMenu("")]
public class Label : Command
{
public string labelName = "";
public string key = "";
public override void OnEnter()
{
@ -19,12 +19,12 @@ namespace Fungus
public override string GetSummary()
{
return labelName;
return key;
}
public override Color GetButtonColor()
{
return new Color32(253, 253, 150, 255);
return new Color32(200, 200, 253, 255);
}
}

Loading…
Cancel
Save