You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
288 lines
8.8 KiB
288 lines
8.8 KiB
10 years ago
|
using UnityEditor;
|
||
10 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using Fungus;
|
||
10 years ago
|
using Rotorz.ReorderableList;
|
||
|
using System.Linq;
|
||
10 years ago
|
|
||
10 years ago
|
namespace Fungus.Script
|
||
|
{
|
||
|
[CustomEditor (typeof(FungusScript))]
|
||
|
public class FungusScriptEditor : Editor
|
||
10 years ago
|
{
|
||
10 years ago
|
public void OnInspectorUpdate()
|
||
|
{
|
||
|
Repaint();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public override void OnInspectorGUI()
|
||
10 years ago
|
{
|
||
10 years ago
|
FungusScript t = target as FungusScript;
|
||
10 years ago
|
|
||
10 years ago
|
if (t != null)
|
||
|
{
|
||
10 years ago
|
// Hide the transform component if FungusScript & Variables are the only components on the game object
|
||
|
// Gives a bit more room in inspector for editing commands. The transform will become visible if any non-Fungus
|
||
|
// components are attached to the game object.
|
||
10 years ago
|
Component[] components = t.GetComponents(typeof(Component));
|
||
10 years ago
|
int count = 0;
|
||
|
foreach (Component component in components)
|
||
|
{
|
||
|
System.Type type = component.GetType();
|
||
|
if (type == typeof(Transform) ||
|
||
|
type == typeof(FungusScript) ||
|
||
|
type == typeof(BooleanVariable) ||
|
||
|
type == typeof(IntegerVariable) ||
|
||
|
type == typeof(FloatVariable) ||
|
||
|
type == typeof(StringVariable))
|
||
|
{
|
||
|
count++;
|
||
|
}
|
||
|
}
|
||
|
t.transform.hideFlags = (count == components.Length) ? HideFlags.HideInInspector : HideFlags.None;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
EditorGUI.BeginChangeCheck();
|
||
10 years ago
|
|
||
10 years ago
|
float stepTime = EditorGUILayout.FloatField(new GUIContent("Step Time", "Minimum time to execute each step"), t.stepTime);
|
||
|
|
||
10 years ago
|
Sequence startSequence = SequenceEditor.SequenceField(new GUIContent("Start Sequence", "Sequence to be executed when controller starts."),
|
||
|
new GUIContent("<None>"),
|
||
|
t,
|
||
|
t.startSequence);
|
||
10 years ago
|
if (t.startSequence == null)
|
||
|
{
|
||
|
GUIStyle style = new GUIStyle(GUI.skin.label);
|
||
|
style.normal.textColor = new Color(1,0,0);
|
||
|
EditorGUILayout.LabelField(new GUIContent("Error: Please select a Start Sequence"), style);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
bool startAutomatically = EditorGUILayout.Toggle(new GUIContent("Start Automatically", "Start this Fungus Script when the scene starts."), t.startAutomatically);
|
||
|
|
||
|
if (EditorGUI.EndChangeCheck())
|
||
|
{
|
||
|
Undo.RecordObject(t, "Set Fungus Script");
|
||
|
t.stepTime = stepTime;
|
||
|
t.startSequence = startSequence;
|
||
|
t.startAutomatically = startAutomatically;
|
||
|
}
|
||
|
|
||
|
EditorGUILayout.Separator();
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.BeginHorizontal();
|
||
|
GUILayout.FlexibleSpace();
|
||
10 years ago
|
if (GUILayout.Button("Open Editor"))
|
||
10 years ago
|
{
|
||
10 years ago
|
EditorWindow.GetWindow(typeof(FungusScriptWindow), false, "Fungus Script");
|
||
10 years ago
|
}
|
||
|
GUILayout.FlexibleSpace();
|
||
|
GUILayout.EndHorizontal();
|
||
|
|
||
10 years ago
|
EditorGUILayout.Separator();
|
||
10 years ago
|
GUI.backgroundColor = Color.yellow;
|
||
10 years ago
|
GUILayout.Box("Sequence Editor", GUILayout.ExpandWidth(true));
|
||
10 years ago
|
GUI.backgroundColor = Color.white;
|
||
|
|
||
10 years ago
|
GUILayout.BeginHorizontal();
|
||
|
|
||
10 years ago
|
if (t.selectedSequence == null)
|
||
|
{
|
||
|
GUILayout.FlexibleSpace();
|
||
|
}
|
||
|
|
||
10 years ago
|
if (GUILayout.Button(t.selectedSequence == null ? "Create Sequence" : "Create",
|
||
10 years ago
|
t.selectedSequence == null ? EditorStyles.miniButton : EditorStyles.miniButtonLeft))
|
||
10 years ago
|
{
|
||
10 years ago
|
Sequence newSequence = t.CreateSequence(t.scrollPos);
|
||
|
Undo.RegisterCreatedObjectUndo(newSequence, "New Sequence");
|
||
|
t.selectedSequence = newSequence;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if (t.selectedSequence == null)
|
||
|
{
|
||
|
GUILayout.FlexibleSpace();
|
||
|
}
|
||
|
|
||
10 years ago
|
if (t.selectedSequence != null)
|
||
|
{
|
||
10 years ago
|
if (GUILayout.Button("Delete", EditorStyles.miniButtonMid))
|
||
10 years ago
|
{
|
||
|
Undo.DestroyObjectImmediate(t.selectedSequence.gameObject);
|
||
|
t.selectedSequence = null;
|
||
|
}
|
||
10 years ago
|
if (GUILayout.Button("Duplicate", EditorStyles.miniButtonRight))
|
||
10 years ago
|
{
|
||
|
GameObject copy = GameObject.Instantiate(t.selectedSequence.gameObject) as GameObject;
|
||
|
copy.transform.parent = t.transform;
|
||
10 years ago
|
copy.transform.hideFlags = HideFlags.HideInHierarchy;
|
||
10 years ago
|
copy.name = t.selectedSequence.name;
|
||
|
|
||
|
Sequence sequenceCopy = copy.GetComponent<Sequence>();
|
||
|
sequenceCopy.nodeRect.x += sequenceCopy.nodeRect.width + 10;
|
||
|
|
||
|
Undo.RegisterCreatedObjectUndo(copy, "Duplicate Sequence");
|
||
|
t.selectedSequence = sequenceCopy;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
GUILayout.EndHorizontal();
|
||
|
|
||
|
EditorGUILayout.Separator();
|
||
|
|
||
10 years ago
|
if (t.selectedSequence != null)
|
||
|
{
|
||
|
DrawSequenceGUI(t.selectedSequence);
|
||
10 years ago
|
|
||
10 years ago
|
if (!Application.isPlaying)
|
||
|
{
|
||
|
EditorGUILayout.Separator();
|
||
|
DrawAddCommandGUI(t.selectedSequence);
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
public void DrawSequenceGUI(Sequence sequence)
|
||
|
{
|
||
|
EditorGUI.BeginChangeCheck();
|
||
|
|
||
10 years ago
|
string name = EditorGUILayout.TextField(new GUIContent("Sequence Name", "Name of sequence displayed in editor window"), sequence.name);
|
||
10 years ago
|
EditorGUILayout.PrefixLabel(new GUIContent("Description", "Sequence description displayed in editor window"));
|
||
|
GUIStyle descriptionStyle = new GUIStyle(EditorStyles.textArea);
|
||
|
descriptionStyle.wordWrap = true;
|
||
|
string desc = EditorGUILayout.TextArea(sequence.description, descriptionStyle);
|
||
10 years ago
|
|
||
10 years ago
|
EditorGUILayout.Separator();
|
||
|
|
||
10 years ago
|
if (name != sequence.name)
|
||
|
{
|
||
|
// The name is the gameobject name, so have to undo seperately
|
||
|
Undo.RecordObject(sequence.gameObject, "Set Sequence Name");
|
||
|
sequence.name = name;
|
||
|
}
|
||
|
|
||
|
if (desc != sequence.description)
|
||
10 years ago
|
{
|
||
10 years ago
|
Undo.RecordObject(sequence, "Set Sequence Description");
|
||
|
sequence.description = desc;
|
||
10 years ago
|
}
|
||
|
|
||
|
EditorGUILayout.PrefixLabel("Commands");
|
||
10 years ago
|
|
||
10 years ago
|
FungusCommand[] commands = sequence.GetComponents<FungusCommand>();
|
||
10 years ago
|
int index = 1;
|
||
10 years ago
|
foreach (FungusCommand command in commands)
|
||
|
{
|
||
10 years ago
|
FungusCommandEditor commandEditor = Editor.CreateEditor(command) as FungusCommandEditor;
|
||
|
commandEditor.DrawInspectorGUI(index++);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public void DrawAddCommandGUI(Sequence sequence)
|
||
|
{
|
||
10 years ago
|
FungusScript t = target as FungusScript;
|
||
10 years ago
|
|
||
|
GUI.backgroundColor = Color.yellow;
|
||
|
GUILayout.Box("Add Command", GUILayout.ExpandWidth(true));
|
||
|
GUI.backgroundColor = Color.white;
|
||
10 years ago
|
|
||
|
// Build list of categories
|
||
|
List<System.Type> commandTypes = EditorExtensions.FindDerivedTypes(typeof(FungusCommand)).ToList();
|
||
|
if (commandTypes.Count == 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
List<string> commandNames = new List<string>();
|
||
|
foreach(System.Type type in commandTypes)
|
||
|
{
|
||
|
commandNames.Add(type.Name);
|
||
|
}
|
||
|
|
||
|
EditorGUI.BeginChangeCheck();
|
||
|
|
||
|
int selectedCommandIndex = EditorGUILayout.Popup("Command", t.selectedCommandIndex, commandNames.ToArray());
|
||
|
|
||
|
if (EditorGUI.EndChangeCheck())
|
||
|
{
|
||
|
Undo.RecordObject(t, "Select Command");
|
||
|
t.selectedCommandIndex = selectedCommandIndex;
|
||
|
}
|
||
|
|
||
|
System.Type selectedType = commandTypes[selectedCommandIndex];
|
||
|
if (selectedType == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
GUILayout.BeginHorizontal();
|
||
|
GUILayout.FlexibleSpace();
|
||
|
if (GUILayout.Button(new GUIContent("Add Command", "Add the selected command to the sequence")))
|
||
|
{
|
||
|
Undo.AddComponent(sequence.gameObject, selectedType);
|
||
|
}
|
||
|
GUILayout.EndHorizontal();
|
||
|
|
||
|
EditorGUILayout.Separator();
|
||
|
|
||
|
object[] attributes = selectedType.GetCustomAttributes(typeof(HelpTextAttribute), false);
|
||
|
foreach (object obj in attributes)
|
||
|
{
|
||
|
HelpTextAttribute helpTextAttr = obj as HelpTextAttribute;
|
||
|
if (helpTextAttr != null)
|
||
|
{
|
||
|
GUIStyle labelStyle = new GUIStyle(EditorStyles.miniLabel);
|
||
|
labelStyle.wordWrap = true;
|
||
10 years ago
|
EditorGUILayout.HelpBox(helpTextAttr.HelpText, MessageType.Info);
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public void DrawVariablesGUI()
|
||
|
{
|
||
|
serializedObject.Update();
|
||
10 years ago
|
|
||
10 years ago
|
FungusScript t = target as FungusScript;
|
||
|
|
||
|
ReorderableListGUI.Title("Variables");
|
||
10 years ago
|
|
||
|
SerializedProperty variablesProperty = serializedObject.FindProperty("variables");
|
||
10 years ago
|
FungusVariableListAdaptor adaptor = new FungusVariableListAdaptor(variablesProperty, 0);
|
||
10 years ago
|
ReorderableListControl.DrawControlFromState(adaptor, null, ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton);
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.BeginHorizontal();
|
||
|
GUILayout.FlexibleSpace();
|
||
10 years ago
|
|
||
10 years ago
|
if (!Application.isPlaying && GUILayout.Button("Add Variable"))
|
||
10 years ago
|
{
|
||
|
GenericMenu menu = new GenericMenu ();
|
||
|
|
||
10 years ago
|
menu.AddItem(new GUIContent ("Boolean"), false, AddVariable<BooleanVariable>, t);
|
||
|
menu.AddItem (new GUIContent ("Integer"), false, AddVariable<IntegerVariable>, t);
|
||
|
menu.AddItem (new GUIContent ("Float"), false, AddVariable<FloatVariable>, t);
|
||
|
menu.AddItem (new GUIContent ("String"), false, AddVariable<StringVariable>, t);
|
||
10 years ago
|
|
||
|
menu.ShowAsContext ();
|
||
|
}
|
||
|
GUILayout.EndHorizontal();
|
||
10 years ago
|
|
||
10 years ago
|
serializedObject.ApplyModifiedProperties();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
void AddVariable<T>(object obj) where T : FungusVariable
|
||
10 years ago
|
{
|
||
|
FungusScript fungusScript = obj as FungusScript;
|
||
|
if (fungusScript == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
Undo.RecordObject(fungusScript, "Add Variable");
|
||
|
T variable = fungusScript.gameObject.AddComponent<T>();
|
||
10 years ago
|
variable.key = fungusScript.GetUniqueVariableKey("");
|
||
10 years ago
|
fungusScript.variables.Add(variable);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}
|