Browse Source

Added shift select support in command list #84

master
chrisgregan 10 years ago
parent
commit
93aa3e8779
  1. 185
      Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs

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

@ -12,13 +12,17 @@ using Rotorz.ReorderableList;
namespace Fungus namespace Fungus
{ {
public class CommandListAdaptor : IReorderableListAdaptor { public class CommandListAdaptor : IReorderableListAdaptor {
protected SerializedProperty _arrayProperty; protected SerializedProperty _arrayProperty;
public float fixedItemHeight; public float fixedItemHeight;
public Rect nodeRect = new Rect(); public Rect nodeRect = new Rect();
public static bool pinShiftToTop;
public static int firstSelectedIndex = 0;
public static int lastSelectedIndex = 0;
public SerializedProperty this[int index] { public SerializedProperty this[int index] {
get { return _arrayProperty.GetArrayElementAtIndex(index); } get { return _arrayProperty.GetArrayElementAtIndex(index); }
} }
@ -39,7 +43,7 @@ namespace Fungus
public CommandListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) { public CommandListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) {
} }
public int Count { public int Count {
get { return _arrayProperty.arraySize; } get { return _arrayProperty.arraySize; }
} }
@ -47,7 +51,7 @@ namespace Fungus
public virtual bool CanDrag(int index) { public virtual bool CanDrag(int index) {
return true; return true;
} }
public virtual bool CanRemove(int index) { public virtual bool CanRemove(int index) {
return true; return true;
} }
@ -58,23 +62,23 @@ namespace Fungus
{ {
return; return;
} }
int newIndex = _arrayProperty.arraySize; int newIndex = _arrayProperty.arraySize;
++_arrayProperty.arraySize; ++_arrayProperty.arraySize;
_arrayProperty.GetArrayElementAtIndex(newIndex).objectReferenceValue = newCommand; _arrayProperty.GetArrayElementAtIndex(newIndex).objectReferenceValue = newCommand;
} }
public void Insert(int index) { public void Insert(int index) {
Command newCommand = AddNewCommand(); Command newCommand = AddNewCommand();
if (newCommand == null) if (newCommand == null)
{ {
return; return;
} }
_arrayProperty.InsertArrayElementAtIndex(index); _arrayProperty.InsertArrayElementAtIndex(index);
_arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = newCommand; _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = newCommand;
} }
Command AddNewCommand() Command AddNewCommand()
{ {
FungusScript fungusScript = FungusScriptWindow.GetFungusScript(); FungusScript fungusScript = FungusScriptWindow.GetFungusScript();
@ -88,21 +92,21 @@ namespace Fungus
{ {
return null; return null;
} }
Command newCommand = Undo.AddComponent<Comment>(sequence.gameObject) as Command; Command newCommand = Undo.AddComponent<Comment>(sequence.gameObject) as Command;
fungusScript.ClearSelectedCommands(); fungusScript.ClearSelectedCommands();
fungusScript.AddSelectedCommand(newCommand); fungusScript.AddSelectedCommand(newCommand);
return newCommand; return newCommand;
} }
public void Duplicate(int index) { public void Duplicate(int index) {
Command command = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Command; Command command = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Command;
// Add the command as a new component // Add the command as a new component
Sequence parentSequence = command.GetComponent<Sequence>(); Sequence parentSequence = command.GetComponent<Sequence>();
System.Type type = command.GetType(); System.Type type = command.GetType();
Command newCommand = Undo.AddComponent(parentSequence.gameObject, type) as Command; Command newCommand = Undo.AddComponent(parentSequence.gameObject, type) as Command;
System.Reflection.FieldInfo[] fields = type.GetFields(); System.Reflection.FieldInfo[] fields = type.GetFields();
@ -110,11 +114,11 @@ namespace Fungus
{ {
field.SetValue(newCommand, field.GetValue(command)); field.SetValue(newCommand, field.GetValue(command));
} }
_arrayProperty.InsertArrayElementAtIndex(index); _arrayProperty.InsertArrayElementAtIndex(index);
_arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = newCommand; _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = newCommand;
} }
public void Remove(int index) { public void Remove(int index) {
// Remove the Fungus Command component // Remove the Fungus Command component
Command command = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Command; Command command = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Command;
@ -122,17 +126,17 @@ namespace Fungus
{ {
Undo.DestroyObjectImmediate(command); Undo.DestroyObjectImmediate(command);
} }
_arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null; _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null;
_arrayProperty.DeleteArrayElementAtIndex(index); _arrayProperty.DeleteArrayElementAtIndex(index);
} }
public void Move(int sourceIndex, int destIndex) { public void Move(int sourceIndex, int destIndex) {
if (destIndex > sourceIndex) if (destIndex > sourceIndex)
--destIndex; --destIndex;
_arrayProperty.MoveArrayElement(sourceIndex, destIndex); _arrayProperty.MoveArrayElement(sourceIndex, destIndex);
} }
public void Clear() { public void Clear() {
while (Count > 0) while (Count > 0)
{ {
@ -148,22 +152,22 @@ namespace Fungus
{ {
return; return;
} }
CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(command.GetType()); CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(command.GetType());
if (commandInfoAttr == null) if (commandInfoAttr == null)
{ {
return; return;
} }
FungusScript fungusScript = command.GetFungusScript(); FungusScript fungusScript = command.GetFungusScript();
if (fungusScript == null) if (fungusScript == null)
{ {
return; return;
} }
bool isComment = (command.GetType() == typeof(Comment)); bool isComment = command.GetType() == typeof(Comment);
bool isLabel = (command.GetType() == typeof(Label)); bool isLabel = (command.GetType() == typeof(Label));
bool error = false; bool error = false;
string summary = command.GetSummary(); string summary = command.GetSummary();
if (summary == null) if (summary == null)
@ -197,20 +201,21 @@ namespace Fungus
break; break;
} }
} }
string commandName = commandInfoAttr.CommandName; string commandName = commandInfoAttr.CommandName;
GUIStyle commandLabelStyle = new GUIStyle(GUI.skin.box); GUIStyle commandLabelStyle = new GUIStyle(GUI.skin.box);
commandLabelStyle.normal.background = FungusEditorResources.texCommandBackground; commandLabelStyle.normal.background = FungusEditorResources.texCommandBackground;
commandLabelStyle.border.top = 1; int borderSize = 5;
commandLabelStyle.border.bottom = 1; commandLabelStyle.border.top = borderSize;
commandLabelStyle.border.left = 1; commandLabelStyle.border.bottom = borderSize;
commandLabelStyle.border.right = 1; commandLabelStyle.border.left = borderSize;
commandLabelStyle.border.right = borderSize;
commandLabelStyle.alignment = TextAnchor.MiddleLeft; commandLabelStyle.alignment = TextAnchor.MiddleLeft;
commandLabelStyle.richText = true; commandLabelStyle.richText = true;
commandLabelStyle.fontSize = 11; commandLabelStyle.fontSize = 11;
commandLabelStyle.padding.top -= 1; commandLabelStyle.padding.top -= 1;
float indentSize = 20; float indentSize = 20;
for (int i = 0; i < command.indentLevel; ++i) for (int i = 0; i < command.indentLevel; ++i)
{ {
@ -222,16 +227,16 @@ namespace Fungus
GUI.backgroundColor = new Color(0.5f, 0.5f, 0.5f, 1f); GUI.backgroundColor = new Color(0.5f, 0.5f, 0.5f, 1f);
GUI.Box(indentRect, "", commandLabelStyle); GUI.Box(indentRect, "", commandLabelStyle);
} }
float commandNameWidth = Mathf.Max(commandLabelStyle.CalcSize(new GUIContent(commandName)).x, 90f); float commandNameWidth = Mathf.Max(commandLabelStyle.CalcSize(new GUIContent(commandName)).x, 90f);
float indentWidth = command.indentLevel * indentSize; float indentWidth = command.indentLevel * indentSize;
Rect commandLabelRect = position; Rect commandLabelRect = position;
commandLabelRect.x += indentWidth - 21; commandLabelRect.x += indentWidth - 21;
commandLabelRect.y -= 2; commandLabelRect.y -= 2;
commandLabelRect.width -= (indentSize * command.indentLevel - 22); commandLabelRect.width -= (indentSize * command.indentLevel - 22);
commandLabelRect.height += 5; commandLabelRect.height += 5;
// Select command via left click // Select command via left click
if (Event.current.type == EventType.MouseDown && if (Event.current.type == EventType.MouseDown &&
Event.current.button == 0 && Event.current.button == 0 &&
@ -239,28 +244,98 @@ namespace Fungus
{ {
if (fungusScript.selectedCommands.Contains(command) && Event.current.button == 0) if (fungusScript.selectedCommands.Contains(command) && Event.current.button == 0)
{ {
// Left click on an already selected command // Left click on already selected command
fungusScript.selectedCommands.Remove(command); // Command key and shift key is not pressed
if (!EditorGUI.actionKey && !Event.current.shift)
{
fungusScript.selectedCommands.Remove(command);
fungusScript.ClearSelectedCommands();
}
// Command key pressed
if (EditorGUI.actionKey)
{
fungusScript.selectedCommands.Remove(command);
}
// Shift key pressed
if (Event.current.shift)
{
fungusScript.ClearSelectedCommands();
if (pinShiftToTop)
{
for (int i = firstSelectedIndex; i < index+1; ++i)
{
fungusScript.AddSelectedCommand(fungusScript.selectedSequence.commandList[i]);
}
}
else
{
for (int i = index; i < lastSelectedIndex+1; ++i)
{
fungusScript.AddSelectedCommand(fungusScript.selectedSequence.commandList[i]);
}
}
}
} }
else else
{ {
// Left click and no command key // Left click and no command key
if (!EditorGUI.actionKey && Event.current.button == 0) if (!Event.current.shift && !EditorGUI.actionKey && Event.current.button == 0)
{ {
fungusScript.ClearSelectedCommands(); fungusScript.ClearSelectedCommands();
} }
fungusScript.AddSelectedCommand(command); fungusScript.AddSelectedCommand(command);
bool firstSelectedCommandFound = false;
if (fungusScript.selectedCommands.Count > 0)
{
if ( fungusScript.selectedSequence != null)
{
for (int i = 0; i < fungusScript.selectedSequence.commandList.Count; i++)
{
Command commandInSequence = fungusScript.selectedSequence.commandList[i];
foreach (Command selectedCommand in fungusScript.selectedCommands)
{
if (commandInSequence == selectedCommand)
{
if (!firstSelectedCommandFound)
{
firstSelectedIndex = i;
firstSelectedCommandFound = true;
}
lastSelectedIndex = i;
}
}
}
}
}
if (Event.current.shift)
{
for (int i = firstSelectedIndex; i < lastSelectedIndex; ++i)
{
fungusScript.AddSelectedCommand(fungusScript.selectedSequence.commandList[i]);
}
}
if (index == firstSelectedIndex)
{
pinShiftToTop = false;
}
else if (index == lastSelectedIndex)
{
pinShiftToTop = true;
}
} }
GUIUtility.keyboardControl = 0; // Fix for textarea not refeshing (change focus) GUIUtility.keyboardControl = 0; // Fix for textarea not refeshing (change focus)
} }
Color commandLabelColor = Color.white; Color commandLabelColor = Color.white;
if (fungusScript.colorCommands) if (fungusScript.colorCommands)
{ {
commandLabelColor = command.GetButtonColor(); commandLabelColor = command.GetButtonColor();
} }
if (commandIsSelected) if (commandIsSelected)
{ {
commandLabelColor = Color.green; commandLabelColor = Color.green;
@ -273,10 +348,10 @@ namespace Fungus
{ {
// TODO: Show warning icon // TODO: Show warning icon
} }
GUI.backgroundColor = commandLabelColor; GUI.backgroundColor = commandLabelColor;
if (isComment || isLabel) if (isComment)
{ {
GUI.Label(commandLabelRect, "", commandLabelStyle); GUI.Label(commandLabelRect, "", commandLabelStyle);
} }
@ -284,7 +359,7 @@ namespace Fungus
{ {
GUI.Label(commandLabelRect, commandName, commandLabelStyle); GUI.Label(commandLabelRect, commandName, commandLabelStyle);
} }
if (command.IsExecuting()) if (command.IsExecuting())
{ {
Rect iconRect = new Rect(commandLabelRect); Rect iconRect = new Rect(commandLabelRect);
@ -293,23 +368,28 @@ namespace Fungus
iconRect.height = 20; iconRect.height = 20;
GUI.Label(iconRect, FungusEditorResources.texPlaySmall, new GUIStyle()); GUI.Label(iconRect, FungusEditorResources.texPlaySmall, new GUIStyle());
} }
Rect summaryRect = new Rect(commandLabelRect); Rect summaryRect = new Rect(commandLabelRect);
if (!isComment && !isLabel) if (isComment)
{
summaryRect.x += 5;
}
else
{ {
summaryRect.x += commandNameWidth; summaryRect.x += commandNameWidth;
summaryRect.width -= commandNameWidth; summaryRect.width -= commandNameWidth;
summaryRect.width -= 5; summaryRect.width -= 5;
} }
GUIStyle summaryStyle = new GUIStyle(); GUIStyle summaryStyle = new GUIStyle();
summaryStyle.fontSize = 10; summaryStyle.fontSize = 10;
summaryStyle.padding.top += 5; summaryStyle.padding.top += 5;
summaryStyle.richText = true; summaryStyle.richText = true;
summaryStyle.wordWrap = false; summaryStyle.wordWrap = false;
summaryStyle.clipping = TextClipping.Clip; summaryStyle.clipping = TextClipping.Clip;
commandLabelStyle.alignment = TextAnchor.MiddleLeft;
GUI.Label(summaryRect, summary, summaryStyle); GUI.Label(summaryRect, summary, summaryStyle);
if (error) if (error)
{ {
GUISkin editorSkin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector); GUISkin editorSkin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector);
@ -320,10 +400,10 @@ namespace Fungus
GUI.Label(errorRect, editorSkin.GetStyle("CN EntryError").normal.background); GUI.Label(errorRect, editorSkin.GetStyle("CN EntryError").normal.background);
summaryRect.width -= 20; summaryRect.width -= 20;
} }
GUI.backgroundColor = Color.white; GUI.backgroundColor = Color.white;
} }
public virtual float GetItemHeight(int index) { public virtual float GetItemHeight(int index) {
return fixedItemHeight != 0f return fixedItemHeight != 0f
? fixedItemHeight ? fixedItemHeight
@ -369,4 +449,3 @@ namespace Fungus
} }
} }
} }

Loading…
Cancel
Save