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.
169 lines
5.3 KiB
169 lines
5.3 KiB
6 years ago
|
using UnityEditor;
|
||
|
using UnityEngine;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
|
||
|
namespace Fungus.EditorUtils
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Searchable Popup Window for adding a command to a block
|
||
|
/// </summary>
|
||
6 years ago
|
public class CommandSelectorPopupWindowContent : BasePopupWindowContent
|
||
6 years ago
|
{
|
||
|
static List<System.Type> commandTypes;
|
||
|
|
||
6 years ago
|
static void CacheCommandTypes()
|
||
6 years ago
|
{
|
||
|
commandTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).Where(x => !x.IsAbstract).ToList();
|
||
|
}
|
||
|
|
||
|
[UnityEditor.Callbacks.DidReloadScripts]
|
||
|
private static void OnScriptsReloaded()
|
||
|
{
|
||
6 years ago
|
CacheCommandTypes();
|
||
6 years ago
|
}
|
||
|
|
||
|
static Block curBlock;
|
||
6 years ago
|
static protected List<KeyValuePair<System.Type, CommandInfoAttribute>> filteredAttributes;
|
||
6 years ago
|
|
||
6 years ago
|
public CommandSelectorPopupWindowContent(string currentHandlerName, int width, int height)
|
||
|
: base(currentHandlerName, width, height)
|
||
6 years ago
|
{
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
protected override void SelectByOrigIndex(int index)
|
||
|
{
|
||
|
var commandType = (index >= 0 && index < commandTypes.Count) ? commandTypes[index] : null;
|
||
|
AddCommandCallback(commandType);
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
protected override void PrepareAllItems()
|
||
|
{
|
||
|
if (commandTypes == null || commandTypes.Count == 0)
|
||
|
{
|
||
|
CacheCommandTypes();
|
||
|
}
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
filteredAttributes = GetFilteredSupportedCommands(curBlock.GetFlowchart());
|
||
6 years ago
|
|
||
|
int i = 0;
|
||
|
foreach (var item in filteredAttributes)
|
||
6 years ago
|
{
|
||
6 years ago
|
allItems.Add(new FilteredListItem(i, (item.Value.Category.Length > 0 ? item.Value.Category + CATEGORY_CHAR : "") + item.Value.CommandName, item.Value.HelpText));
|
||
6 years ago
|
|
||
6 years ago
|
i++;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
static public void ShowCommandMenu(Rect position, string currentHandlerName, Block block, int width, int height)
|
||
|
{
|
||
|
curBlock = block;
|
||
6 years ago
|
|
||
6 years ago
|
var win = new CommandSelectorPopupWindowContent(currentHandlerName,
|
||
6 years ago
|
width, (int)(height - EditorGUIUtility.singleLineHeight * 3));
|
||
6 years ago
|
PopupWindow.Show(position, win);
|
||
6 years ago
|
|
||
6 years ago
|
|
||
|
//old method
|
||
|
DoOlderMenu();
|
||
6 years ago
|
}
|
||
|
|
||
|
protected static List<KeyValuePair<System.Type, CommandInfoAttribute>> GetFilteredSupportedCommands(Flowchart flowchart)
|
||
|
{
|
||
|
List<KeyValuePair<System.Type, CommandInfoAttribute>> filteredAttributes = BlockEditor.GetFilteredCommandInfoAttribute(commandTypes);
|
||
|
|
||
|
filteredAttributes.Sort(BlockEditor.CompareCommandAttributes);
|
||
|
|
||
|
filteredAttributes = filteredAttributes.Where(x => flowchart.IsCommandSupported(x.Value)).ToList();
|
||
|
|
||
|
return filteredAttributes;
|
||
|
}
|
||
|
|
||
|
|
||
6 years ago
|
static protected void DoOlderMenu()
|
||
|
{
|
||
|
GenericMenu commandMenu = new GenericMenu();
|
||
|
|
||
|
// Build menu list
|
||
|
|
||
|
foreach (var keyPair in filteredAttributes)
|
||
|
{
|
||
|
GUIContent menuItem;
|
||
|
if (keyPair.Value.Category == "")
|
||
|
{
|
||
|
menuItem = new GUIContent(keyPair.Value.CommandName);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
6 years ago
|
menuItem = new GUIContent(keyPair.Value.Category + CATEGORY_CHAR + keyPair.Value.CommandName);
|
||
6 years ago
|
}
|
||
|
|
||
|
commandMenu.AddItem(menuItem, false, AddCommandCallback, keyPair.Key);
|
||
|
}
|
||
|
|
||
|
commandMenu.ShowAsContext();
|
||
|
}
|
||
|
|
||
6 years ago
|
//Used by GenericMenu Delegate
|
||
|
static protected void AddCommandCallback(object obj)
|
||
|
{
|
||
6 years ago
|
Type command = obj as Type;
|
||
|
if (command != null)
|
||
6 years ago
|
{
|
||
6 years ago
|
AddCommandCallback(command);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
static protected void AddCommandCallback(Type commandType)
|
||
|
{
|
||
|
var block = curBlock;
|
||
6 years ago
|
if (block == null || commandType == null)
|
||
6 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var flowchart = (Flowchart)block.GetFlowchart();
|
||
|
|
||
|
// Use index of last selected command in list, or end of list if nothing selected.
|
||
|
int index = -1;
|
||
|
foreach (var command in flowchart.SelectedCommands)
|
||
|
{
|
||
|
if (command.CommandIndex + 1 > index)
|
||
|
{
|
||
|
index = command.CommandIndex + 1;
|
||
|
}
|
||
|
}
|
||
|
if (index == -1)
|
||
|
{
|
||
|
index = block.CommandList.Count;
|
||
|
}
|
||
|
|
||
|
var newCommand = Undo.AddComponent(block.gameObject, commandType) as Command;
|
||
|
block.GetFlowchart().AddSelectedCommand(newCommand);
|
||
|
newCommand.ParentBlock = block;
|
||
|
newCommand.ItemId = flowchart.NextItemId();
|
||
|
|
||
|
// Let command know it has just been added to the block
|
||
|
newCommand.OnCommandAdded(block);
|
||
|
|
||
|
Undo.RecordObject(block, "Set command type");
|
||
|
if (index < block.CommandList.Count - 1)
|
||
|
{
|
||
|
block.CommandList.Insert(index, newCommand);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
block.CommandList.Add(newCommand);
|
||
|
}
|
||
|
|
||
|
// Because this is an async call, we need to force prefab instances to record changes
|
||
|
PrefabUtility.RecordPrefabInstancePropertyModifications(block);
|
||
|
|
||
|
flowchart.ClearSelectedCommands();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|