Browse Source

Simplified adding new commands. Added Note command.

master
chrisgregan 10 years ago
parent
commit
b63a9ce3b9
  1. BIN
      Assets/Example/Scenes/Example.unity
  2. 3
      Assets/Fungus/FungusScript/Commands/Note.cs
  3. 0
      Assets/Fungus/FungusScript/Commands/Note.cs.meta
  4. 9
      Assets/Fungus/FungusScript/Editor/CommandEditor.cs
  5. 94
      Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs
  6. 91
      Assets/Fungus/FungusScript/Editor/SequenceEditor.cs
  7. 9
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs

BIN
Assets/Example/Scenes/Example.unity

Binary file not shown.

3
Assets/Fungus/FungusScript/Commands/Comment.cs → Assets/Fungus/FungusScript/Commands/Note.cs

@ -1,4 +1,5 @@
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
@ -7,7 +8,7 @@ namespace Fungus
[CommandInfo("Scripting",
"Note",
"Records design notes and reminders about your script.")]
public class Comment : Command
public class Note : Command
{
[TextArea(2,4)]
public string commentText = "";

0
Assets/Fungus/FungusScript/Commands/Comment.cs.meta → Assets/Fungus/FungusScript/Commands/Note.cs.meta

9
Assets/Fungus/FungusScript/Editor/CommandEditor.cs

@ -12,15 +12,6 @@ namespace Fungus
{
public static Command selectedCommand;
void OnEnable()
{
Command t = target as Command;
if (t != null)
{
t.hideFlags = HideFlags.HideInInspector;
}
}
public static CommandInfoAttribute GetCommandInfo(System.Type commandType)
{
object[] attributes = commandType.GetCustomAttributes(typeof(CommandInfoAttribute), false);

94
Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs

@ -5,12 +5,21 @@
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using System.Linq;
using Rotorz.ReorderableList;
namespace Fungus
{
public class CommandListAdaptor : IReorderableListAdaptor {
private class SetCommandOperation
{
public Sequence sequence;
public Type commandType;
public int index;
}
private SerializedProperty _arrayProperty;
public float fixedItemHeight;
@ -74,8 +83,7 @@ namespace Fungus
Command AddNewCommand()
{
FungusScript fungusScript = FungusScriptWindow.GetFungusScript();
if (fungusScript == null ||
fungusScript.selectedAddCommandType == null)
if (fungusScript == null)
{
return null;
}
@ -86,7 +94,7 @@ namespace Fungus
return null;
}
Command newCommand = sequence.gameObject.AddComponent(fungusScript.selectedAddCommandType) as Command;
Command newCommand = sequence.gameObject.AddComponent<Note>() as Command;
fungusScript.selectedCommand = newCommand;
return newCommand;
@ -174,8 +182,10 @@ namespace Fungus
}
string commandName = commandInfoAttr.CommandName;
GUIStyle commandStyle = new GUIStyle(EditorStyles.miniButtonLeft);
float buttonWidth = Mathf.Max(commandStyle.CalcSize(new GUIContent(commandName)).x, 100f);
GUIStyle commandLabelStyle = new GUIStyle(EditorStyles.miniButtonLeft);
float buttonWidth = Mathf.Max(commandLabelStyle.CalcSize(new GUIContent(commandName)).x, 100f);
float indentWidth = command.indentLevel * indentSize;
Rect buttonRect = position;
@ -186,7 +196,7 @@ namespace Fungus
Rect summaryRect = buttonRect;
summaryRect.x += buttonWidth - 1;
summaryRect.width = position.width - buttonWidth - indentWidth;
summaryRect.width = position.width - buttonWidth - indentWidth - 15;
if (!Application.isPlaying &&
Event.current.type == EventType.MouseDown &&
@ -220,18 +230,31 @@ namespace Fungus
}
GUI.backgroundColor = buttonBackgroundColor;
GUI.Label(buttonRect, commandName, commandStyle);
GUI.Label(buttonRect, commandName, commandLabelStyle);
GUIStyle labelStyle = new GUIStyle(EditorStyles.miniButtonRight);
labelStyle.alignment = TextAnchor.MiddleLeft;
GUIStyle summaryStyle = new GUIStyle(EditorStyles.miniButtonRight);
summaryStyle.alignment = TextAnchor.MiddleLeft;
if (error && !selected)
{
labelStyle.normal.textColor = Color.white;
summaryStyle.normal.textColor = Color.white;
}
GUI.backgroundColor = summaryBackgroundColor;
GUI.Box(summaryRect, summary, labelStyle);
GUI.Box(summaryRect, summary, summaryStyle);
GUI.backgroundColor = Color.white;
Rect menuRect = summaryRect;
menuRect.x += menuRect.width + 2;
menuRect.y = position.y + 1;
menuRect.width = 18;
menuRect.height = position.height;
GUIStyle menuButtonStyle = new GUIStyle(EditorStyles.popup);
if (GUI.Button(menuRect, new GUIContent("", "Select command type"), menuButtonStyle))
{
ShowCommandMenu(index, fungusScript.selectedSequence);
}
}
public virtual float GetItemHeight(int index) {
@ -277,6 +300,55 @@ namespace Fungus
break;
}
}
void ShowCommandMenu(int index, Sequence sequence)
{
GenericMenu commandMenu = new GenericMenu();
// Build menu list
List<System.Type> menuTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList();
foreach(System.Type type in menuTypes)
{
object[] attributes = type.GetCustomAttributes(false);
foreach (object obj in attributes)
{
CommandInfoAttribute infoAttr = obj as CommandInfoAttribute;
if (infoAttr != null)
{
SetCommandOperation commandOperation = new SetCommandOperation();
commandOperation.sequence = sequence;
commandOperation.commandType = type;
commandOperation.index = index;
commandMenu.AddItem (new GUIContent (infoAttr.Category + "/" + infoAttr.CommandName),
false, Callback, commandOperation);
}
}
}
commandMenu.ShowAsContext();
}
void Callback(object obj)
{
SetCommandOperation commandOperation = obj as SetCommandOperation;
Sequence sequence = commandOperation.sequence;
if (sequence == null)
{
return;
}
Command newCommand = Undo.AddComponent(sequence.gameObject, commandOperation.commandType) as Command;
sequence.GetFungusScript().selectedCommand = newCommand;
Command oldCommand = sequence.commandList[commandOperation.index];
Undo.DestroyObjectImmediate(oldCommand);
Undo.RecordObject(sequence, "Set command type");
sequence.commandList[commandOperation.index] = newCommand;
}
}
}

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

@ -56,86 +56,6 @@ namespace Fungus
return;
}
EditorGUI.BeginChangeCheck();
EditorGUILayout.BeginHorizontal();
// Build list of categories
List<string> categories = new List<string>();
List<System.Type> subTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList();
foreach(System.Type type in subTypes)
{
object[] attributes = type.GetCustomAttributes(false);
foreach (object obj in attributes)
{
CommandInfoAttribute categoryAttr = obj as CommandInfoAttribute;
if (categoryAttr != null)
{
if (!categories.Contains(categoryAttr.Category))
{
categories.Add(categoryAttr.Category);
}
}
}
}
categories.Sort();
GUILayout.Label("New Command");
GUILayout.FlexibleSpace();
// We should probably use SerializedProperty for the category & command index but there's no real benefit to doing so
int selectedCategoryIndex = EditorGUILayout.Popup(fungusScript.selectedCommandCategoryIndex, categories.ToArray());
List<string> commandNames = new List<string>();
List<System.Type> commandTypes = new List<System.Type>();
string categoryName = categories[selectedCategoryIndex];
foreach (System.Type type in subTypes)
{
CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(type);
if (commandInfoAttr == null)
{
continue;
}
if (categoryName == commandInfoAttr.Category)
{
commandNames.Add(commandInfoAttr.CommandName);
commandTypes.Add(type);
}
}
int selectedCommandIndex = EditorGUILayout.Popup(fungusScript.selectedAddCommandIndex, commandNames.ToArray());
if (selectedCategoryIndex != fungusScript.selectedCommandCategoryIndex)
{
// Default to first item in list if category has changed
selectedCommandIndex = 0;
}
EditorGUILayout.EndHorizontal();
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(fungusScript, "Select Command");
fungusScript.selectedCommandCategoryIndex = selectedCategoryIndex;
fungusScript.selectedAddCommandIndex = selectedCommandIndex;
}
if (selectedCommandIndex >= commandTypes.Count)
{
return;
}
System.Type selectedType = commandTypes[selectedCommandIndex];
if (fungusScript.selectedSequence == null ||
selectedType == null)
{
serializedObject.ApplyModifiedProperties();
return;
}
fungusScript.selectedAddCommandType = selectedType;
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
@ -150,12 +70,13 @@ namespace Fungus
EditorGUILayout.EndHorizontal();
CommandInfoAttribute infoAttr = CommandEditor.GetCommandInfo(selectedType);
if (infoAttr != null)
if (fungusScript.selectedCommand != null)
{
GUIStyle labelStyle = new GUIStyle(EditorStyles.miniLabel);
labelStyle.wordWrap = true;
EditorGUILayout.HelpBox(infoAttr.HelpText, MessageType.Info);
CommandInfoAttribute infoAttr = CommandEditor.GetCommandInfo(fungusScript.selectedCommand.GetType());
if (infoAttr != null)
{
EditorGUILayout.HelpBox(infoAttr.HelpText, MessageType.Info);
}
}
serializedObject.ApplyModifiedProperties();

9
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -15,15 +15,6 @@ namespace Fungus
[System.NonSerialized]
public Command copyCommand;
[HideInInspector]
public int selectedAddCommandIndex;
[HideInInspector]
public System.Type selectedAddCommandType;
[HideInInspector]
public int selectedCommandCategoryIndex;
[HideInInspector]
public Vector2 scriptScrollPos;

Loading…
Cancel
Save