Add Category attribute for types grouping in editor dialogs

This commit is contained in:
Wojtek Figat
2021-06-15 16:39:15 +02:00
parent 8816e4403d
commit c91c8b66ce
8 changed files with 116 additions and 52 deletions

View File

@@ -68,19 +68,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
var cm = new ItemsListContextMenu(180);
for (int i = 0; i < scripts.Count; i++)
{
var scriptType = scripts[i];
var item = new ItemsListContextMenu.Item(scriptType.Name, scriptType)
{
TooltipText = scriptType.TypeName,
};
var attributes = scriptType.GetAttributes(false);
var tooltipAttribute = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
if (tooltipAttribute != null)
{
item.TooltipText += '\n';
item.TooltipText += tooltipAttribute.Text;
}
cm.AddItem(item);
cm.AddItem(new TypeSearchPopup.TypeItemView(scripts[i]));
}
cm.ItemClicked += item => AddScript((ScriptType)item.Tag);
cm.SortChildren();

View File

@@ -639,21 +639,8 @@ namespace FlaxEditor.CustomEditors.Dedicated
var cm = new ItemsListContextMenu(180);
for (int i = 0; i < controlTypes.Count; i++)
{
var controlType = controlTypes[i];
var item = new ItemsListContextMenu.Item(controlType.Name, controlType)
{
TooltipText = controlType.TypeName,
};
var attributes = controlType.GetAttributes(false);
var tooltipAttribute = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
if (tooltipAttribute != null)
{
item.TooltipText += '\n';
item.TooltipText += tooltipAttribute.Text;
}
cm.AddItem(item);
cm.AddItem(new TypeSearchPopup.TypeItemView(controlTypes[i]));
}
cm.ItemClicked += controlType => SetType((ScriptType)controlType.Tag);
cm.SortChildren();
cm.Show(button.Parent, button.BottomLeft);

View File

@@ -10,6 +10,7 @@ namespace FlaxEditor.GUI
/// The label that contains events for mouse interaction.
/// </summary>
/// <seealso cref="FlaxEngine.GUI.Label" />
[HideInEditor]
public class ClickableLabel : Label
{
private bool _leftClick;

View File

@@ -32,9 +32,14 @@ namespace FlaxEditor.GUI
protected List<Rectangle> _highlights;
/// <summary>
/// Gets or sets the name.
/// The item name.
/// </summary>
public string Name { get; set; }
public string Name;
/// <summary>
/// The item category name (optional).
/// </summary>
public string Category;
/// <summary>
/// Occurs when items gets clicked by the user.
@@ -49,18 +54,6 @@ namespace FlaxEditor.GUI
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Item"/> class.
/// </summary>
/// <param name="name">The item name.</param>
/// <param name="tag">The item tag object.</param>
public Item(string name, object tag = null)
: base(0, 0, 120, 12)
{
Name = name;
Tag = tag;
}
/// <summary>
/// Updates the filter.
/// </summary>
@@ -181,6 +174,7 @@ namespace FlaxEditor.GUI
}
private readonly TextBox _searchBox;
private List<DropPanel> _categoryPanels;
private bool _waitingForInput;
/// <summary>
@@ -242,6 +236,23 @@ namespace FlaxEditor.GUI
if (items[i] is Item item)
item.UpdateFilter(_searchBox.Text);
}
if (_categoryPanels != null)
{
for (int i = 0; i < _categoryPanels.Count; i++)
{
var category = _categoryPanels[i];
bool anyVisible = false;
for (int j = 0; j < category.Children.Count; j++)
{
if (category.Children[j] is Item item2)
{
item2.UpdateFilter(_searchBox.Text);
anyVisible |= item2.Visible;
}
}
category.Visible = anyVisible;
}
}
UnlockChildrenRecursive();
PerformLayout(true);
@@ -254,8 +265,33 @@ namespace FlaxEditor.GUI
/// <param name="item">The item.</param>
public void AddItem(Item item)
{
item.Parent = ItemsPanel;
item.Clicked += OnClickItem;
ContainerControl parent = ItemsPanel;
if (!string.IsNullOrEmpty(item.Category))
{
if (_categoryPanels == null)
_categoryPanels = new List<DropPanel>();
for (int i = 0; i < _categoryPanels.Count; i++)
{
if (string.Equals(_categoryPanels[i].HeaderText, item.Category, StringComparison.Ordinal))
{
parent = _categoryPanels[i];
break;
}
}
if (parent == ItemsPanel)
{
var categoryPanel = new DropPanel
{
HeaderText = item.Category,
Parent = parent,
};
categoryPanel.Open(false);
_categoryPanels.Add(categoryPanel);
parent = categoryPanel;
}
}
item.Parent = parent;
}
/// <summary>
@@ -281,6 +317,19 @@ namespace FlaxEditor.GUI
if (items[i] is Item item)
item.UpdateFilter(null);
}
if (_categoryPanels != null)
{
for (int i = 0; i < _categoryPanels.Count; i++)
{
var category = _categoryPanels[i];
for (int j = 0; j < category.Children.Count; j++)
{
if (category.Children[j] is Item item2)
item2.UpdateFilter(null);
}
category.Visible = true;
}
}
_searchBox.Clear();
UnlockChildrenRecursive();

View File

@@ -27,6 +27,15 @@ namespace FlaxEditor.GUI
/// </summary>
public ScriptType Type => _type;
/// <summary>
/// Initializes a new instance of the <see cref="TypeItemView"/> class.
/// </summary>
/// <param name="type">The type.</param>
public TypeItemView(ScriptType type)
: this(type, type.GetAttributes(false))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TypeItemView"/> class.
/// </summary>
@@ -38,12 +47,18 @@ namespace FlaxEditor.GUI
Name = type.Name;
TooltipText = type.TypeName;
Tag = type;
var tooltipAttribute = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
if (tooltipAttribute != null)
{
TooltipText += '\n';
TooltipText += tooltipAttribute.Text;
}
var categoryAttribute = (CategoryAttribute)attributes.FirstOrDefault(x => x is CategoryAttribute);
if (categoryAttribute != null)
{
Category = categoryAttribute.Name;
}
}
/// <inheritdoc />

View File

@@ -118,8 +118,10 @@ namespace FlaxEditor.Surface.Archetypes
var cm = new ItemsListContextMenu(180);
foreach (var module in modules)
{
cm.AddItem(new ItemsListContextMenu.Item(module.Title, module.TypeID)
cm.AddItem(new ItemsListContextMenu.Item
{
Name = module.Title,
Tag = module.TypeID,
TooltipText = module.Description,
});
}

View File

@@ -447,18 +447,13 @@ namespace FlaxEditor.Surface
var cm = new ItemsListContextMenu(180);
foreach (var newParameterType in newParameterTypes)
{
var name = newParameterType.Type != null ? window.VisjectSurface.GetTypeName(newParameterType) : newParameterType.Name;
var item = new ItemsListContextMenu.Item(name, newParameterType)
var item = new TypeSearchPopup.TypeItemView(newParameterType)
{
Tag = newParameterType,
TooltipText = newParameterType.TypeName,
};
var attributes = newParameterType.GetAttributes(false);
var tooltipAttribute = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
if (tooltipAttribute != null)
{
item.TooltipText += '\n';
item.TooltipText += tooltipAttribute.Text;
}
if (newParameterType.Type != null)
item.Name = window.VisjectSurface.GetTypeName(newParameterType);
cm.AddItem(item);
}
cm.ItemClicked += OnAddParameterItemClicked;

View File

@@ -0,0 +1,27 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
/// <summary>
/// Describes the category name for a type.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public sealed class CategoryAttribute : Attribute
{
/// <summary>
/// The category name.
/// </summary>
public string Name;
/// <summary>
/// Initializes a new instance of the <see cref="CategoryAttribute"/> class.
/// </summary>
/// <param name="name">The category name.</param>
public CategoryAttribute(string name)
{
Name = name;
}
}
}