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.
232 lines
6.2 KiB
232 lines
6.2 KiB
10 years ago
|
using UnityEditor;
|
||
10 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
10 years ago
|
using Rotorz.ReorderableList;
|
||
|
using System.Linq;
|
||
10 years ago
|
using System.Reflection;
|
||
10 years ago
|
using System.IO;
|
||
10 years ago
|
|
||
10 years ago
|
namespace Fungus
|
||
10 years ago
|
{
|
||
10 years ago
|
[CustomEditor (typeof(Flowchart))]
|
||
|
public class FlowchartEditor : Editor
|
||
10 years ago
|
{
|
||
10 years ago
|
protected class AddVariableInfo
|
||
|
{
|
||
10 years ago
|
public Flowchart flowchart;
|
||
10 years ago
|
public System.Type variableType;
|
||
|
}
|
||
10 years ago
|
|
||
|
protected SerializedProperty descriptionProp;
|
||
10 years ago
|
protected SerializedProperty colorCommandsProp;
|
||
|
protected SerializedProperty hideComponentsProp;
|
||
10 years ago
|
protected SerializedProperty runSlowDurationProp;
|
||
10 years ago
|
protected SerializedProperty saveSelectionProp;
|
||
10 years ago
|
protected SerializedProperty variablesProp;
|
||
10 years ago
|
|
||
10 years ago
|
protected virtual void OnEnable()
|
||
10 years ago
|
{
|
||
10 years ago
|
descriptionProp = serializedObject.FindProperty("description");
|
||
10 years ago
|
colorCommandsProp = serializedObject.FindProperty("colorCommands");
|
||
|
hideComponentsProp = serializedObject.FindProperty("hideComponents");
|
||
10 years ago
|
runSlowDurationProp = serializedObject.FindProperty("runSlowDuration");
|
||
10 years ago
|
saveSelectionProp = serializedObject.FindProperty("saveSelection");
|
||
10 years ago
|
variablesProp = serializedObject.FindProperty("variables");
|
||
|
}
|
||
|
|
||
10 years ago
|
public override void OnInspectorGUI()
|
||
10 years ago
|
{
|
||
10 years ago
|
serializedObject.Update();
|
||
10 years ago
|
|
||
10 years ago
|
Flowchart flowchart = target as Flowchart;
|
||
10 years ago
|
|
||
10 years ago
|
flowchart.UpdateHideFlags();
|
||
10 years ago
|
|
||
10 years ago
|
EditorGUILayout.PropertyField(descriptionProp);
|
||
10 years ago
|
EditorGUILayout.PropertyField(colorCommandsProp);
|
||
|
EditorGUILayout.PropertyField(hideComponentsProp);
|
||
10 years ago
|
EditorGUILayout.PropertyField(runSlowDurationProp);
|
||
10 years ago
|
EditorGUILayout.PropertyField(saveSelectionProp);
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.BeginHorizontal();
|
||
|
GUILayout.FlexibleSpace();
|
||
10 years ago
|
if (GUILayout.Button("Flowchart Window"))
|
||
10 years ago
|
{
|
||
10 years ago
|
EditorWindow.GetWindow(typeof(FlowchartWindow), false, "Flowchart");
|
||
10 years ago
|
}
|
||
10 years ago
|
if (GUILayout.Button(new GUIContent("Export Text", "Export all story text in .fountain format.")))
|
||
|
{
|
||
10 years ago
|
FountainExporter.ExportStrings(flowchart);
|
||
10 years ago
|
}
|
||
|
if (GUILayout.Button(new GUIContent("Import Text", "Import story text from a file in .fountain format.")))
|
||
|
{
|
||
10 years ago
|
FountainExporter.ImportStrings(flowchart);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
GUILayout.FlexibleSpace();
|
||
|
GUILayout.EndHorizontal();
|
||
10 years ago
|
|
||
10 years ago
|
serializedObject.ApplyModifiedProperties();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public virtual void DrawVariablesGUI()
|
||
10 years ago
|
{
|
||
10 years ago
|
serializedObject.Update();
|
||
|
|
||
10 years ago
|
Flowchart t = target as Flowchart;
|
||
10 years ago
|
|
||
10 years ago
|
if (t.variables.Count == 0)
|
||
|
{
|
||
|
t.variablesExpanded = true;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (!t.variablesExpanded)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (GUILayout.Button ("Variables (" + t.variables.Count + ")", GUILayout.Height(24)))
|
||
10 years ago
|
{
|
||
|
t.variablesExpanded = true;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
Rect listRect = new Rect();
|
||
|
|
||
|
if (t.variables.Count > 0)
|
||
|
{
|
||
10 years ago
|
// Remove any null variables from the list
|
||
|
// Can sometimes happen when upgrading to a new version of Fungus (if .meta GUID changes for a variable class)
|
||
|
for (int i = t.variables.Count - 1; i >= 0; i--)
|
||
|
{
|
||
|
if (t.variables[i] == null)
|
||
|
{
|
||
|
t.variables.RemoveAt(i);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
ReorderableListGUI.Title("Variables");
|
||
|
VariableListAdaptor adaptor = new VariableListAdaptor(variablesProp, 0);
|
||
10 years ago
|
|
||
|
ReorderableListFlags flags = ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton;
|
||
|
|
||
|
ReorderableListControl.DrawControlFromState(adaptor, null, flags);
|
||
10 years ago
|
listRect = GUILayoutUtility.GetLastRect();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
GUILayoutUtility.GetRect(300, 24);
|
||
|
listRect = GUILayoutUtility.GetLastRect();
|
||
|
listRect.y += 20;
|
||
|
}
|
||
10 years ago
|
|
||
|
float plusWidth = 32;
|
||
|
float plusHeight = 24;
|
||
|
|
||
|
Rect buttonRect = listRect;
|
||
|
float buttonHeight = 24;
|
||
|
buttonRect.x = 4;
|
||
|
buttonRect.y -= buttonHeight - 1;
|
||
|
buttonRect.height = buttonHeight;
|
||
10 years ago
|
if (!Application.isPlaying)
|
||
|
{
|
||
|
buttonRect.width -= 30;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (GUI.Button (buttonRect, "Variables"))
|
||
|
{
|
||
|
t.variablesExpanded = false;
|
||
|
}
|
||
|
|
||
|
Rect plusRect = listRect;
|
||
|
plusRect.x += plusRect.width - plusWidth;
|
||
|
plusRect.y -= plusHeight - 1;
|
||
|
plusRect.width = plusWidth;
|
||
|
plusRect.height = plusHeight;
|
||
|
|
||
10 years ago
|
if (!Application.isPlaying &&
|
||
|
GUI.Button(plusRect, FungusEditorResources.texAddButton))
|
||
10 years ago
|
{
|
||
|
GenericMenu menu = new GenericMenu ();
|
||
10 years ago
|
List<System.Type> types = FindAllDerivedTypes<Variable>();
|
||
|
|
||
|
// Add variable types without a category
|
||
|
foreach (System.Type type in types)
|
||
|
{
|
||
|
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
|
||
|
if (variableInfo == null ||
|
||
|
variableInfo.Category != "")
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
AddVariableInfo addVariableInfo = new AddVariableInfo();
|
||
10 years ago
|
addVariableInfo.flowchart = t;
|
||
10 years ago
|
addVariableInfo.variableType = type;
|
||
|
|
||
|
GUIContent typeName = new GUIContent(variableInfo.VariableType);
|
||
|
|
||
|
menu.AddItem(typeName, false, AddVariable, addVariableInfo);
|
||
|
}
|
||
|
|
||
|
// Add types with a category
|
||
|
foreach (System.Type type in types)
|
||
|
{
|
||
|
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
|
||
|
if (variableInfo == null ||
|
||
|
variableInfo.Category == "")
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
AddVariableInfo info = new AddVariableInfo();
|
||
10 years ago
|
info.flowchart = t;
|
||
10 years ago
|
info.variableType = type;
|
||
|
|
||
|
GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType);
|
||
|
|
||
|
menu.AddItem(typeName, false, AddVariable, info);
|
||
|
}
|
||
10 years ago
|
|
||
|
menu.ShowAsContext ();
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
serializedObject.ApplyModifiedProperties();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
protected virtual void AddVariable(object obj)
|
||
10 years ago
|
{
|
||
10 years ago
|
AddVariableInfo addVariableInfo = obj as AddVariableInfo;
|
||
|
if (addVariableInfo == null)
|
||
10 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
Flowchart flowchart = addVariableInfo.flowchart;
|
||
10 years ago
|
System.Type variableType = addVariableInfo.variableType;
|
||
|
|
||
10 years ago
|
Undo.RecordObject(flowchart, "Add Variable");
|
||
|
Variable newVariable = flowchart.gameObject.AddComponent(variableType) as Variable;
|
||
|
newVariable.key = flowchart.GetUniqueVariableKey("");
|
||
|
flowchart.variables.Add(newVariable);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
public static List<System.Type> FindAllDerivedTypes<T>()
|
||
|
{
|
||
|
return FindAllDerivedTypes<T>(Assembly.GetAssembly(typeof(T)));
|
||
|
}
|
||
|
|
||
|
public static List<System.Type> FindAllDerivedTypes<T>(Assembly assembly)
|
||
|
{
|
||
|
var derivedType = typeof(T);
|
||
|
return assembly
|
||
|
.GetTypes()
|
||
|
.Where(t =>
|
||
|
t != derivedType &&
|
||
|
derivedType.IsAssignableFrom(t)
|
||
|
).ToList();
|
||
|
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}
|