changed to use attribute to add items to the content context menu, this also allows users to add their own items to the CM
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FlaxEditor.Content;
|
||||
@@ -152,64 +153,78 @@ namespace FlaxEditor.Windows
|
||||
{
|
||||
cm.AddButton("New folder", NewFolder);
|
||||
}
|
||||
|
||||
// categorize the asset proxies according to their category name
|
||||
Dictionary<string, List<AssetProxy>> categorizedProxyList = new Dictionary<string, List<AssetProxy>>();
|
||||
for (int i = 0; i < Editor.ContentDatabase.Proxy.Count; i++)
|
||||
|
||||
// loop through each proxy and user defined json type and add them to the context menu
|
||||
foreach (var type in Editor.CodeEditing.All.Get())
|
||||
{
|
||||
var p = Editor.ContentDatabase.Proxy[i];
|
||||
if (type.IsAbstract || !type.HasAttribute(typeof(ContentContextMenuAttribute), true))
|
||||
continue;
|
||||
|
||||
ContentContextMenuAttribute attribute = null;
|
||||
foreach (var typeAttribute in type.GetAttributes(true))
|
||||
{
|
||||
if (typeAttribute is ContentContextMenuAttribute contentContextMenuAttribute)
|
||||
{
|
||||
attribute = contentContextMenuAttribute;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ContentProxy p;
|
||||
if (type.Type.IsSubclassOf(typeof(ContentProxy)))
|
||||
{
|
||||
p = Editor.ContentDatabase.Proxy.Find(T => T.GetType() == type.Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
// user can use attribute to put their own assets into the content context menu
|
||||
var generic = typeof(SpawnableJsonAssetProxy<>).MakeGenericType(type.Type);
|
||||
var instance = Activator.CreateInstance(generic);
|
||||
dynamic obj = instance;
|
||||
p = obj as AssetProxy;
|
||||
}
|
||||
|
||||
if (p == null)
|
||||
continue;
|
||||
|
||||
// create menus
|
||||
if (p.CanCreate(folder))
|
||||
{
|
||||
if (p is AssetProxy ap)
|
||||
var splitPath = attribute.Path.Split('/');
|
||||
ContextMenuChildMenu childCM = null;
|
||||
bool mainCM = true;
|
||||
for (int i = 0; i < splitPath?.Length; i++)
|
||||
{
|
||||
if (categorizedProxyList.ContainsKey(ap.CategoryName))
|
||||
if (i == splitPath.Length - 1)
|
||||
{
|
||||
categorizedProxyList.TryGetValue(ap.CategoryName, out var apList);
|
||||
apList?.Add(ap);
|
||||
if (mainCM)
|
||||
{
|
||||
cm.AddButton(splitPath[i].Trim(), () => NewItem(p));
|
||||
mainCM = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
childCM?.ContextMenu.AddButton(splitPath[i].Trim(), () => NewItem(p));
|
||||
childCM.ContextMenu.AutoSort = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
categorizedProxyList.Add(ap.CategoryName, new List<AssetProxy>() {ap});
|
||||
if (mainCM)
|
||||
{
|
||||
childCM = cm.GetOrAddChildMenu(splitPath[i].Trim());
|
||||
mainCM = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
childCM = childCM?.ContextMenu.GetOrAddChildMenu(splitPath[i].Trim());
|
||||
}
|
||||
childCM.ContextMenu.AutoSort = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c = cm.AddChildMenu("New");
|
||||
c.ContextMenu.Tag = item;
|
||||
c.ContextMenu.AutoSort = true;
|
||||
int newItems = 0;
|
||||
for (int i = 0; i < Editor.ContentDatabase.Proxy.Count; i++)
|
||||
{
|
||||
var p = Editor.ContentDatabase.Proxy[i];
|
||||
if (p.CanCreate(folder))
|
||||
{
|
||||
if (p is AssetProxy ap && categorizedProxyList.TryGetValue(ap.CategoryName, out var apList))
|
||||
{
|
||||
if (apList.Contains(ap))
|
||||
{
|
||||
if (apList.Count > 1)
|
||||
{
|
||||
var childMenu = c.ContextMenu.GetOrAddChildMenu(ap.CategoryName);
|
||||
childMenu.ContextMenu.AutoSort = true;
|
||||
childMenu.ContextMenu.AddButton(p.Name, () => NewItem(p));
|
||||
}
|
||||
else
|
||||
{
|
||||
c.ContextMenu.AddButton(p.Name, () => NewItem(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
c.ContextMenu.AddButton(p.Name, () => NewItem(p));
|
||||
}
|
||||
|
||||
newItems++;
|
||||
}
|
||||
}
|
||||
c.Enabled = newItems > 0;
|
||||
|
||||
if (folder.CanHaveAssets)
|
||||
{
|
||||
cm.AddButton("Import file", () =>
|
||||
|
||||
Reference in New Issue
Block a user