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.
180 lines
4.1 KiB
180 lines
4.1 KiB
10 years ago
|
using UnityEditor;
|
||
10 years ago
|
using UnityEditorInternal;
|
||
10 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
10 years ago
|
namespace Fungus
|
||
10 years ago
|
{
|
||
|
|
||
10 years ago
|
[CustomEditor (typeof(Command), true)]
|
||
|
public class CommandEditor : Editor
|
||
10 years ago
|
{
|
||
10 years ago
|
public static Command selectedCommand;
|
||
10 years ago
|
|
||
10 years ago
|
public static CommandInfoAttribute GetCommandInfo(System.Type commandType)
|
||
|
{
|
||
|
object[] attributes = commandType.GetCustomAttributes(typeof(CommandInfoAttribute), false);
|
||
|
foreach (object obj in attributes)
|
||
|
{
|
||
|
CommandInfoAttribute commandInfoAttr = obj as CommandInfoAttribute;
|
||
|
if (commandInfoAttr != null)
|
||
|
{
|
||
|
return commandInfoAttr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
10 years ago
|
public virtual void DrawCommandInspectorGUI()
|
||
|
{
|
||
10 years ago
|
Command t = target as Command;
|
||
10 years ago
|
if (t == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
Flowchart flowchart = t.GetFlowchart();
|
||
|
if (flowchart == null)
|
||
10 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(t.GetType());
|
||
10 years ago
|
if (commandInfoAttr == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
GUILayout.BeginVertical(GUI.skin.box);
|
||
10 years ago
|
|
||
10 years ago
|
if (t.enabled)
|
||
|
{
|
||
10 years ago
|
if (flowchart.colorCommands)
|
||
10 years ago
|
{
|
||
|
GUI.backgroundColor = t.GetButtonColor();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
GUI.backgroundColor = Color.white;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
GUI.backgroundColor = Color.grey;
|
||
|
}
|
||
10 years ago
|
GUILayout.BeginHorizontal(GUI.skin.button);
|
||
10 years ago
|
|
||
10 years ago
|
string commandName = commandInfoAttr.CommandName;
|
||
|
GUILayout.Label(commandName, GUILayout.MinWidth(80), GUILayout.ExpandWidth(true));
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.FlexibleSpace();
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.Label(new GUIContent("(" + t.commandId + ")"));
|
||
|
|
||
|
GUILayout.Space(10);
|
||
|
|
||
10 years ago
|
GUI.backgroundColor = Color.white;
|
||
10 years ago
|
bool enabled = t.enabled;
|
||
10 years ago
|
enabled = GUILayout.Toggle(enabled, new GUIContent());
|
||
10 years ago
|
|
||
10 years ago
|
if (t.enabled != enabled)
|
||
|
{
|
||
|
Undo.RecordObject(t, "Set Enabled");
|
||
|
t.enabled = enabled;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.EndHorizontal();
|
||
10 years ago
|
GUI.backgroundColor = Color.white;
|
||
10 years ago
|
|
||
10 years ago
|
EditorGUILayout.Separator();
|
||
|
|
||
|
DrawCommandGUI();
|
||
10 years ago
|
|
||
10 years ago
|
EditorGUILayout.Separator();
|
||
10 years ago
|
|
||
10 years ago
|
if (t.errorMessage.Length > 0)
|
||
|
{
|
||
|
GUIStyle style = new GUIStyle(GUI.skin.label);
|
||
|
style.normal.textColor = new Color(1,0,0);
|
||
|
EditorGUILayout.LabelField(new GUIContent("Error: " + t.errorMessage), style);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.EndVertical();
|
||
10 years ago
|
|
||
|
// Display help text
|
||
|
CommandInfoAttribute infoAttr = CommandEditor.GetCommandInfo(t.GetType());
|
||
|
if (infoAttr != null)
|
||
|
{
|
||
10 years ago
|
EditorGUILayout.HelpBox(infoAttr.HelpText, MessageType.Info, true);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public virtual void DrawCommandGUI()
|
||
10 years ago
|
{
|
||
10 years ago
|
// Code below was copied from here
|
||
|
// http://answers.unity3d.com/questions/550829/how-to-add-a-script-field-in-custom-inspector.html
|
||
|
|
||
|
// Users should not be able to change the MonoScript for the command using the usual Script field.
|
||
10 years ago
|
// Doing so could cause block.commandList to contain null entries.
|
||
10 years ago
|
// To avoid this we manually display all properties, except for m_Script.
|
||
|
serializedObject.Update();
|
||
|
SerializedProperty iterator = serializedObject.GetIterator();
|
||
|
bool enterChildren = true;
|
||
|
while (iterator.NextVisible(enterChildren))
|
||
|
{
|
||
|
enterChildren = false;
|
||
|
|
||
|
if (iterator.name == "m_Script")
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
EditorGUILayout.PropertyField(iterator, true, new GUILayoutOption[0]);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
serializedObject.ApplyModifiedProperties();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
static public void ObjectField<T>(SerializedProperty property, GUIContent label, GUIContent nullLabel, List<T> objectList) where T : Object
|
||
10 years ago
|
{
|
||
10 years ago
|
if (property == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
List<GUIContent> objectNames = new List<GUIContent>();
|
||
10 years ago
|
|
||
|
T selectedObject = property.objectReferenceValue as T;
|
||
|
|
||
10 years ago
|
int selectedIndex = 0;
|
||
|
objectNames.Add(nullLabel);
|
||
10 years ago
|
for (int i = 0; i < objectList.Count; ++i)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (objectList[i] == null) continue;
|
||
|
objectNames.Add(new GUIContent(objectList[i].name));
|
||
10 years ago
|
|
||
10 years ago
|
if (selectedObject == objectList[i])
|
||
10 years ago
|
{
|
||
|
selectedIndex = i + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
T result;
|
||
|
|
||
|
selectedIndex = EditorGUILayout.Popup(label, selectedIndex, objectNames.ToArray());
|
||
|
if (selectedIndex == 0)
|
||
|
{
|
||
|
result = null; // Null option
|
||
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
result = objectList[selectedIndex - 1];
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
property.objectReferenceValue = result;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|