Browse Source

Add Variable moved from FlowchartEditor to popup

master
desktop-maesty/steve 7 years ago
parent
commit
507eb490d0
  1. 33
      Assets/Fungus/Scripts/Editor/BasePopupWindowContent.cs
  2. 84
      Assets/Fungus/Scripts/Editor/FlowchartEditor.cs
  3. 8
      Assets/Fungus/Scripts/Editor/PopupContent.meta
  4. 23
      Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs
  5. 0
      Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs.meta
  6. 9
      Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs
  7. 0
      Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs.meta
  8. 125
      Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs
  9. 11
      Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs.meta

33
Assets/Fungus/Scripts/Editor/BasePopupWindowContent.cs

@ -42,7 +42,6 @@ namespace Fungus.EditorUtils
public GUIContent content; public GUIContent content;
} }
protected Block block;
protected int hoverIndex; protected int hoverIndex;
protected readonly string SEARCH_CONTROL_NAME = "PopupSearchControlName"; protected readonly string SEARCH_CONTROL_NAME = "PopupSearchControlName";
protected readonly float ROW_HEIGHT = EditorGUIUtility.singleLineHeight; protected readonly float ROW_HEIGHT = EditorGUIUtility.singleLineHeight;
@ -55,16 +54,25 @@ namespace Fungus.EditorUtils
protected int currentIndex; protected int currentIndex;
protected Vector2 size; protected Vector2 size;
static readonly char[] SEARCH_SPLITS = new char[]{ '/', ' ' }; static readonly char[] SEARCH_SPLITS = new char[]{ CATEGORY_CHAR, ' ' };
protected static readonly char CATEGORY_CHAR = '/';
public BasePopupWindowContent(string currentHandlerName, Block block, int width, int height) public BasePopupWindowContent(string currentHandlerName, int width, int height)
{ {
this.block = block;
this.size = new Vector2(width, height); this.size = new Vector2(width, height);
PrepareAllItems(); PrepareAllItems();
allItems.Sort((lhs, rhs) => lhs.lowerName.CompareTo(rhs.lowerName)); allItems.Sort((lhs, rhs) =>
{
//order root level objects first
var islhsRoot = lhs.lowerName.IndexOf(CATEGORY_CHAR) != -1;
var isrhsRoot = rhs.lowerName.IndexOf(CATEGORY_CHAR) != -1;
if(islhsRoot == isrhsRoot)
return lhs.lowerName.CompareTo(rhs.lowerName);
return islhsRoot ? 1 : -1;
});
UpdateFilter(); UpdateFilter();
currentIndex = Mathf.Max(0, visibleItems.FindIndex(x=>x.name.Contains(currentHandlerName))); currentIndex = Mathf.Max(0, visibleItems.FindIndex(x=>x.name.Contains(currentHandlerName)));
hoverIndex = currentIndex; hoverIndex = currentIndex;
@ -120,12 +128,13 @@ namespace Fungus.EditorUtils
{ {
visibleItems = allItems.Where(x => visibleItems = allItems.Where(x =>
{ {
//we want all tokens
foreach (var item in lowers) foreach (var item in lowers)
{ {
if (x.lowerName.Contains(currentFilter)) if (!x.lowerName.Contains(item))
return true; return false;
} }
return false; return true;
}).ToList(); }).ToList();
} }
@ -246,13 +255,5 @@ namespace Fungus.EditorUtils
} }
} }
} }
//protected void SelectByName(string name)
//{
// var loc = allItems.FindIndex(x => x.name == name);
// SelectByOrigIndex(loc);
//}
} }
} }

84
Assets/Fungus/Scripts/Editor/FlowchartEditor.cs

@ -13,11 +13,6 @@ namespace Fungus.EditorUtils
[CustomEditor (typeof(Flowchart))] [CustomEditor (typeof(Flowchart))]
public class FlowchartEditor : Editor public class FlowchartEditor : Editor
{ {
protected class AddVariableInfo
{
public Flowchart flowchart;
public System.Type variableType;
}
protected SerializedProperty descriptionProp; protected SerializedProperty descriptionProp;
protected SerializedProperty colorCommandsProp; protected SerializedProperty colorCommandsProp;
@ -179,90 +174,15 @@ namespace Fungus.EditorUtils
if (!Application.isPlaying && if (!Application.isPlaying &&
GUI.Button(plusRect, addTexture)) GUI.Button(plusRect, addTexture))
{ {
GenericMenu menu = new GenericMenu (); Rect popRect = plusRect;
List<System.Type> types = FindAllDerivedTypes<Variable>(); VariableSelectPopupWindowContent.DoAddVariable(popRect, "", t);
// Add variable types without a category
foreach (var type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category != "")
{
continue;
}
AddVariableInfo addVariableInfo = new AddVariableInfo();
addVariableInfo.flowchart = t;
addVariableInfo.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, addVariableInfo);
}
// Add types with a category
foreach (var type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category == "")
{
continue;
}
AddVariableInfo info = new AddVariableInfo();
info.flowchart = t;
info.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, info);
}
menu.ShowAsContext ();
} }
} }
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }
protected virtual void AddVariable(object obj)
{
AddVariableInfo addVariableInfo = obj as AddVariableInfo;
if (addVariableInfo == null)
{
return;
}
var flowchart = addVariableInfo.flowchart;
System.Type variableType = addVariableInfo.variableType;
Undo.RecordObject(flowchart, "Add Variable");
Variable newVariable = flowchart.gameObject.AddComponent(variableType) as Variable;
newVariable.Key = flowchart.GetUniqueVariableKey("");
flowchart.Variables.Add(newVariable);
// Because this is an async call, we need to force prefab instances to record changes
PrefabUtility.RecordPrefabInstancePropertyModifications(flowchart);
}
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();
}
/// <summary> /// <summary>
/// When modifying custom editor code you can occasionally end up with orphaned editor instances. /// When modifying custom editor code you can occasionally end up with orphaned editor instances.

8
Assets/Fungus/Scripts/Editor/PopupContent.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b99cf5f1ea08b914c94ee372bc4d76a7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

23
Assets/Fungus/Scripts/Editor/CommandSelectorPopupWindowContent.cs → Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs

@ -8,16 +8,9 @@ namespace Fungus.EditorUtils
{ {
/// <summary> /// <summary>
/// Searchable Popup Window for adding a command to a block /// Searchable Popup Window for adding a command to a block
///
/// Inspired by https://github.com/roboryantron/UnityEditorJunkie/blob/master/Assets/SearchableEnum/Code/Editor/SearchablePopup.cs
/// </summary> /// </summary>
public class CommandSelectorPopupWindowContent : BasePopupWindowContent public class CommandSelectorPopupWindowContent : BasePopupWindowContent
{ {
private static readonly char[] SPLIT_INPUT_ON = new char[] { ' ', '/', '\\' };
private static readonly int MAX_PREVIEW_GRID = 7;
private static readonly string ELIPSIS = "...";
static List<System.Type> commandTypes; static List<System.Type> commandTypes;
static void CacheCommandTypes() static void CacheCommandTypes()
@ -34,8 +27,8 @@ namespace Fungus.EditorUtils
static Block curBlock; static Block curBlock;
static protected List<KeyValuePair<System.Type, CommandInfoAttribute>> filteredAttributes; static protected List<KeyValuePair<System.Type, CommandInfoAttribute>> filteredAttributes;
public CommandSelectorPopupWindowContent(string currentHandlerName, Block block, int width, int height) public CommandSelectorPopupWindowContent(string currentHandlerName, int width, int height)
: base(currentHandlerName, block, width, height) : base(currentHandlerName, width, height)
{ {
} }
@ -53,12 +46,12 @@ namespace Fungus.EditorUtils
} }
filteredAttributes = GetFilteredSupportedCommands(block.GetFlowchart()); filteredAttributes = GetFilteredSupportedCommands(curBlock.GetFlowchart());
int i = 0; int i = 0;
foreach (var item in filteredAttributes) foreach (var item in filteredAttributes)
{ {
allItems.Add(new FilteredListItem(i, (item.Value.Category.Length > 0 ? item.Value.Category + "/" : "") + item.Value.CommandName, item.Value.HelpText)); allItems.Add(new FilteredListItem(i, (item.Value.Category.Length > 0 ? item.Value.Category + CATEGORY_CHAR : "") + item.Value.CommandName, item.Value.HelpText));
i++; i++;
} }
@ -68,7 +61,7 @@ namespace Fungus.EditorUtils
{ {
curBlock = block; curBlock = block;
var win = new CommandSelectorPopupWindowContent(currentHandlerName, block, var win = new CommandSelectorPopupWindowContent(currentHandlerName,
width, (int)(height - EditorGUIUtility.singleLineHeight * 3)); width, (int)(height - EditorGUIUtility.singleLineHeight * 3));
PopupWindow.Show(position, win); PopupWindow.Show(position, win);
@ -89,12 +82,8 @@ namespace Fungus.EditorUtils
} }
static protected void DoOlderMenu() static protected void DoOlderMenu()
{ {
var flowchart = (Flowchart)curBlock.GetFlowchart();
GenericMenu commandMenu = new GenericMenu(); GenericMenu commandMenu = new GenericMenu();
// Build menu list // Build menu list
@ -108,7 +97,7 @@ namespace Fungus.EditorUtils
} }
else else
{ {
menuItem = new GUIContent(keyPair.Value.Category + "/" + keyPair.Value.CommandName); menuItem = new GUIContent(keyPair.Value.Category + CATEGORY_CHAR + keyPair.Value.CommandName);
} }
commandMenu.AddItem(menuItem, false, AddCommandCallback, keyPair.Key); commandMenu.AddItem(menuItem, false, AddCommandCallback, keyPair.Key);

0
Assets/Fungus/Scripts/Editor/CommandSelectorPopupWindowContent.cs.meta → Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs.meta

9
Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs → Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs

@ -8,8 +8,6 @@ namespace Fungus.EditorUtils
{ {
/// <summary> /// <summary>
/// Searchable Popup Window for selecting Event type, used by block editor /// Searchable Popup Window for selecting Event type, used by block editor
///
/// Inspired by https://github.com/roboryantron/UnityEditorJunkie/blob/master/Assets/SearchableEnum/Code/Editor/SearchablePopup.cs
/// </summary> /// </summary>
public class EventSelectorPopupWindowContent : BasePopupWindowContent public class EventSelectorPopupWindowContent : BasePopupWindowContent
{ {
@ -32,10 +30,11 @@ namespace Fungus.EditorUtils
public Type eventHandlerType; public Type eventHandlerType;
} }
protected Block block;
public EventSelectorPopupWindowContent(string currentHandlerName, Block block, int width, int height) public EventSelectorPopupWindowContent(string currentHandlerName, Block block, int width, int height)
:base(currentHandlerName, block, width, height) :base(currentHandlerName, width, height)
{ {
this.block = block;
} }
protected override void PrepareAllItems() protected override void PrepareAllItems()
@ -51,7 +50,7 @@ namespace Fungus.EditorUtils
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type); EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type);
if (info != null) if (info != null)
{ {
allItems.Add(new FilteredListItem(i, (info.Category.Length > 0 ? info.Category + "/" : "") + info.EventHandlerName, info.HelpText)); allItems.Add(new FilteredListItem(i, (info.Category.Length > 0 ? info.Category + CATEGORY_CHAR : "") + info.EventHandlerName, info.HelpText));
} }
else else
{ {

0
Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs.meta → Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs.meta

125
Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs

@ -0,0 +1,125 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
namespace Fungus.EditorUtils
{
public class VariableSelectPopupWindowContent : BasePopupWindowContent
{
static readonly int POPUP_WIDTH = 200, POPUP_HEIGHT = 200;
static List<System.Type> types;
static void CacheVariableTypes()
{
var derivedType = typeof(Variable);
types = EditorExtensions.FindDerivedTypes(derivedType)
.Where(x => !x.IsAbstract && derivedType.IsAssignableFrom(x))
.ToList();
}
[UnityEditor.Callbacks.DidReloadScripts]
private static void OnScriptsReloaded()
{
CacheVariableTypes();
}
protected override void PrepareAllItems()
{
if(types == null || types.Count == 0)
{
CacheVariableTypes();
}
int i = 0;
foreach (var item in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(item);
if (variableInfo != null)
{
allItems.Add(new FilteredListItem(i, (variableInfo.Category.Length > 0 ? variableInfo.Category + CATEGORY_CHAR : "") + variableInfo.VariableType));
}
i++;
}
}
protected override void SelectByOrigIndex(int index)
{
AddVariable(types[index]);
}
static public void DoAddVariable(Rect position, string currentHandlerName, Flowchart flowchart)
{
curFlowchart = flowchart;
//new method
VariableSelectPopupWindowContent win = new VariableSelectPopupWindowContent(currentHandlerName, POPUP_WIDTH, POPUP_HEIGHT);
PopupWindow.Show(position, win);
//old method
DoOlderMenu(flowchart);
}
static protected void DoOlderMenu(Flowchart flowchart)
{
GenericMenu menu = new GenericMenu();
// Add variable types without a category
foreach (var type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category != "")
{
continue;
}
GUIContent typeName = new GUIContent(variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, type);
}
// Add types with a category
foreach (var type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category == "")
{
continue;
}
GUIContent typeName = new GUIContent(variableInfo.Category + CATEGORY_CHAR + variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, type);
}
menu.ShowAsContext();
}
private static Flowchart curFlowchart;
public VariableSelectPopupWindowContent(string currentHandlerName, int width, int height)
: base(currentHandlerName, width, height)
{
}
protected static void AddVariable(object obj)
{
System.Type t = obj as System.Type;
if (t == null)
{
return;
}
Undo.RecordObject(curFlowchart, "Add Variable");
Variable newVariable = curFlowchart.gameObject.AddComponent(t) as Variable;
newVariable.Key = curFlowchart.GetUniqueVariableKey("");
curFlowchart.Variables.Add(newVariable);
// Because this is an async call, we need to force prefab instances to record changes
PrefabUtility.RecordPrefabInstancePropertyModifications(curFlowchart);
}
}
}

11
Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 933b6fc31e5eac04ea3b2a1fa9a3b418
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save