Browse Source

Reorderable lists for commands and variables now use EditorGUI determined LineHeight

master
desktop-maesty/steve 7 years ago
parent
commit
c2e317ea5a
  1. 10
      Assets/Fungus/Scripts/Editor/CommandEditor.cs
  2. 110
      Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs
  3. 6
      Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

10
Assets/Fungus/Scripts/Editor/CommandEditor.cs

@ -171,8 +171,16 @@ namespace Fungus.EditorUtils
drawHeaderCallback = (Rect rect) => drawHeaderCallback = (Rect rect) =>
{ {
EditorGUI.LabelField(rect, locSerProp.displayName); EditorGUI.LabelField(rect, locSerProp.displayName);
},
drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
EditorGUI.PropertyField(rect, locSerProp.GetArrayElementAtIndex(index));
},
elementHeightCallback = (int index) =>
{
return EditorGUI.GetPropertyHeight(locSerProp.GetArrayElementAtIndex(index), null, true);// + EditorGUIUtility.singleLineHeight;
} }
}; };
reorderableLists.Add(iterator.displayName, reordList); reorderableLists.Add(iterator.displayName, reordList);
} }

110
Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs

@ -8,8 +8,9 @@ using UnityEditorInternal;
namespace Fungus.EditorUtils namespace Fungus.EditorUtils
{ {
public class CommandListAdaptor { public class CommandListAdaptor
{
public void DrawCommandList() public void DrawCommandList()
{ {
if (block.CommandList.Count == 0) if (block.CommandList.Count == 0)
@ -29,61 +30,69 @@ namespace Fungus.EditorUtils
protected ReorderableList list; protected ReorderableList list;
protected Block block; protected Block block;
public float fixedItemHeight; public float fixedItemHeight;
public SerializedProperty this[int index] { public SerializedProperty this[int index]
{
get { return _arrayProperty.GetArrayElementAtIndex(index); } get { return _arrayProperty.GetArrayElementAtIndex(index); }
} }
public SerializedProperty arrayProperty { public SerializedProperty arrayProperty
{
get { return _arrayProperty; } get { return _arrayProperty; }
} }
public CommandListAdaptor(Block _block, SerializedProperty arrayProperty, float fixedItemHeight = 0) { public CommandListAdaptor(Block _block, SerializedProperty arrayProperty)
{
if (arrayProperty == null) if (arrayProperty == null)
throw new ArgumentNullException("Array property was null."); throw new ArgumentNullException("Array property was null.");
if (!arrayProperty.isArray) if (!arrayProperty.isArray)
throw new InvalidOperationException("Specified serialized propery is not an array."); throw new InvalidOperationException("Specified serialized propery is not an array.");
this._arrayProperty = arrayProperty; this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight;
this.block = _block; this.block = _block;
list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, true, false, false); list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, true, false, false);
list.drawHeaderCallback = DrawHeader; list.drawHeaderCallback = DrawHeader;
list.drawElementCallback = DrawItem; list.drawElementCallback = DrawItem;
//list.elementHeightCallback = GetElementHeight;
} }
//private float GetElementHeight(int index)
//{
// return EditorGUI.GetPropertyHeight(this[index], null, true);// + EditorGUIUtility.singleLineHeight;
//}
private void DrawHeader(Rect rect) private void DrawHeader(Rect rect)
{ {
EditorGUI.LabelField(rect, new GUIContent("Commands")); EditorGUI.LabelField(rect, new GUIContent("Commands"));
} }
public void DrawItem(Rect position, int index, bool selected, bool focused) public void DrawItem(Rect position, int index, bool selected, bool focused)
{ {
Command command = this[index].objectReferenceValue as Command; Command command = this[index].objectReferenceValue as Command;
if (command == null) if (command == null)
{ {
return; return;
} }
CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(command.GetType()); CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(command.GetType());
if (commandInfoAttr == null) if (commandInfoAttr == null)
{ {
return; return;
} }
var flowchart = (Flowchart)command.GetFlowchart(); var flowchart = (Flowchart)command.GetFlowchart();
if (flowchart == null) if (flowchart == 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)
@ -117,9 +126,9 @@ namespace Fungus.EditorUtils
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.CommandBackground; commandLabelStyle.normal.background = FungusEditorResources.CommandBackground;
int borderSize = 5; int borderSize = 5;
@ -131,8 +140,8 @@ namespace Fungus.EditorUtils
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)
{ {
Rect indentRect = position; Rect indentRect = position;
@ -143,10 +152,10 @@ namespace Fungus.EditorUtils
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;
@ -173,7 +182,8 @@ namespace Fungus.EditorUtils
// Command key and shift key is not pressed // Command key and shift key is not pressed
if (!EditorGUI.actionKey && !Event.current.shift) if (!EditorGUI.actionKey && !Event.current.shift)
{ {
BlockEditor.actionList.Add ( delegate { BlockEditor.actionList.Add(delegate
{
flowchart.SelectedCommands.Remove(command); flowchart.SelectedCommands.Remove(command);
flowchart.ClearSelectedCommands(); flowchart.ClearSelectedCommands();
}); });
@ -182,7 +192,8 @@ namespace Fungus.EditorUtils
// Command key pressed // Command key pressed
if (EditorGUI.actionKey) if (EditorGUI.actionKey)
{ {
BlockEditor.actionList.Add ( delegate { BlockEditor.actionList.Add(delegate
{
flowchart.SelectedCommands.Remove(command); flowchart.SelectedCommands.Remove(command);
}); });
Event.current.Use(); Event.current.Use();
@ -195,13 +206,15 @@ namespace Fungus.EditorUtils
// Left click and no command key // Left click and no command key
if (!shift && !EditorGUI.actionKey && Event.current.button == 0) if (!shift && !EditorGUI.actionKey && Event.current.button == 0)
{ {
BlockEditor.actionList.Add ( delegate { BlockEditor.actionList.Add(delegate
{
flowchart.ClearSelectedCommands(); flowchart.ClearSelectedCommands();
}); });
Event.current.Use(); Event.current.Use();
} }
BlockEditor.actionList.Add ( delegate { BlockEditor.actionList.Add(delegate
{
flowchart.AddSelectedCommand(command); flowchart.AddSelectedCommand(command);
}); });
@ -209,12 +222,12 @@ namespace Fungus.EditorUtils
int firstSelectedIndex = -1; int firstSelectedIndex = -1;
int lastSelectedIndex = -1; int lastSelectedIndex = -1;
if (flowchart.SelectedCommands.Count > 0) if (flowchart.SelectedCommands.Count > 0)
{ {
if ( flowchart.SelectedBlock != null) if (flowchart.SelectedBlock != null)
{ {
for (int i = 0; i < flowchart.SelectedBlock.CommandList.Count; i++) for (int i = 0; i < flowchart.SelectedBlock.CommandList.Count; i++)
{ {
Command commandInBlock = flowchart.SelectedBlock.CommandList[i]; Command commandInBlock = flowchart.SelectedBlock.CommandList[i];
foreach (Command selectedCommand in flowchart.SelectedCommands) foreach (Command selectedCommand in flowchart.SelectedCommands)
{ {
if (commandInBlock == selectedCommand) if (commandInBlock == selectedCommand)
@ -224,9 +237,9 @@ namespace Fungus.EditorUtils
} }
} }
} }
for (int i = flowchart.SelectedBlock.CommandList.Count - 1; i >=0; i--) for (int i = flowchart.SelectedBlock.CommandList.Count - 1; i >= 0; i--)
{ {
Command commandInBlock = flowchart.SelectedBlock.CommandList[i]; Command commandInBlock = flowchart.SelectedBlock.CommandList[i];
foreach (Command selectedCommand in flowchart.SelectedCommands) foreach (Command selectedCommand in flowchart.SelectedCommands)
{ {
if (commandInBlock == selectedCommand) if (commandInBlock == selectedCommand)
@ -239,7 +252,7 @@ namespace Fungus.EditorUtils
} }
} }
if (shift) if (shift)
{ {
int currentIndex = command.CommandIndex; int currentIndex = command.CommandIndex;
if (firstSelectedIndex == -1 || if (firstSelectedIndex == -1 ||
@ -264,7 +277,8 @@ namespace Fungus.EditorUtils
for (int i = Math.Min(firstSelectedIndex, lastSelectedIndex); i < Math.Max(firstSelectedIndex, lastSelectedIndex); ++i) for (int i = Math.Min(firstSelectedIndex, lastSelectedIndex); i < Math.Max(firstSelectedIndex, lastSelectedIndex); ++i)
{ {
var selectedCommand = flowchart.SelectedBlock.CommandList[i]; var selectedCommand = flowchart.SelectedBlock.CommandList[i];
BlockEditor.actionList.Add ( delegate { BlockEditor.actionList.Add(delegate
{
flowchart.AddSelectedCommand(selectedCommand); flowchart.AddSelectedCommand(selectedCommand);
}); });
} }
@ -274,13 +288,13 @@ namespace Fungus.EditorUtils
} }
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 (flowchart.ColorCommands) if (flowchart.ColorCommands)
{ {
commandLabelColor = command.GetButtonColor(); commandLabelColor = command.GetButtonColor();
} }
if (commandIsSelected) if (commandIsSelected)
{ {
commandLabelColor = Color.green; commandLabelColor = Color.green;
@ -293,9 +307,9 @@ namespace Fungus.EditorUtils
{ {
// TODO: Show warning icon // TODO: Show warning icon
} }
GUI.backgroundColor = commandLabelColor; GUI.backgroundColor = commandLabelColor;
if (isComment) if (isComment)
{ {
GUI.Label(commandLabelRect, "", commandLabelStyle); GUI.Label(commandLabelRect, "", commandLabelStyle);
@ -314,7 +328,7 @@ namespace Fungus.EditorUtils
GUI.Label(commandLabelRect, commandNameLabel, commandLabelStyle); GUI.Label(commandLabelRect, commandNameLabel, commandLabelStyle);
} }
if (command.ExecutingIconTimer > Time.realtimeSinceStartup) if (command.ExecutingIconTimer > Time.realtimeSinceStartup)
{ {
Rect iconRect = new Rect(commandLabelRect); Rect iconRect = new Rect(commandLabelRect);
@ -332,7 +346,7 @@ namespace Fungus.EditorUtils
GUI.color = storeColor; GUI.color = storeColor;
} }
Rect summaryRect = new Rect(commandLabelRect); Rect summaryRect = new Rect(commandLabelRect);
if (isComment) if (isComment)
{ {
@ -343,16 +357,16 @@ namespace Fungus.EditorUtils
summaryRect.x += commandNameWidth + 5; summaryRect.x += commandNameWidth + 5;
summaryRect.width -= commandNameWidth + 5; summaryRect.width -= commandNameWidth + 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; 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);
@ -363,17 +377,9 @@ namespace Fungus.EditorUtils
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) {
return fixedItemHeight != 0f
? fixedItemHeight
: EditorGUI.GetPropertyHeight(this[index], GUIContent.none, false)
;
}
} }
} }

6
Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

@ -60,6 +60,12 @@ namespace Fungus.EditorUtils
list.onRemoveCallback = RemoveItem; list.onRemoveCallback = RemoveItem;
list.onAddCallback = AddButton; list.onAddCallback = AddButton;
list.onRemoveCallback = RemoveItem; list.onRemoveCallback = RemoveItem;
list.elementHeightCallback = GetElementHeight;
}
private float GetElementHeight(int index)
{
return /*EditorGUI.GetPropertyHeight(this[index], null, true) +*/ EditorGUIUtility.singleLineHeight;
} }
private void RemoveItem(ReorderableList list) private void RemoveItem(ReorderableList list)

Loading…
Cancel
Save