// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using FlaxEditor.Surface.Elements;
using FlaxEditor.Utilities;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.Surface.ContextMenu
{
///
/// Drop panel for group of . It represents .
///
///
[HideInEditor]
public sealed class VisjectCMGroup : DropPanel
{
///
/// The context menu.
///
public readonly VisjectCM ContextMenu;
///
/// The archetypes (one or more).
///
public readonly List Archetypes = new List();
///
/// A computed score for the context menu order.
///
public float SortScore;
///
/// The group name.
///
public string Name;
///
/// Initializes a new instance of the class.
///
/// The context menu.
/// The group archetype.
public VisjectCMGroup(VisjectCM cm, GroupArchetype archetype)
{
ContextMenu = cm;
Archetypes.Add(archetype);
Name = archetype.Name;
EnableDropDownIcon = true;
HeaderColor = Style.Current.Background;
ArrowImageOpened = new SpriteBrush(Style.Current.ArrowDown);
ArrowImageClosed = new SpriteBrush(Style.Current.ArrowRight);
CloseAnimationTime = 0;
}
///
/// Resets the view.
///
public void ResetView()
{
Profiler.BeginEvent("VisjectCMGroup.ResetView");
// Remove filter
SortScore = 0;
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is VisjectCMItem item)
{
item.UpdateFilter(null);
item.UpdateScore(null);
}
}
SortChildren();
if (ContextMenu.ShowExpanded)
Open(false);
else
Close(false);
Visible = true;
Profiler.EndEvent();
}
///
/// Updates the filter.
///
/// The filter text.
public void UpdateFilter(string filterText)
{
Profiler.BeginEvent("VisjectCMGroup.UpdateFilter");
// Update items
bool isAnyVisible = false;
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is VisjectCMItem item)
{
item.UpdateFilter(filterText);
isAnyVisible |= item.Visible;
}
}
// Update header title
if (QueryFilterHelper.Match(filterText, HeaderText))
{
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is VisjectCMItem item)
{
item.Visible = true;
}
}
isAnyVisible = true;
}
// Update itself
if (isAnyVisible)
{
if (!string.IsNullOrEmpty(filterText))
Open(false);
Visible = true;
}
else
{
// Hide group if none of the items matched the filter
Visible = false;
}
Profiler.EndEvent();
}
///
/// Updates the sorting of the s of this
/// Also updates the
///
/// The currently user-selected box
public void UpdateItemSort(Box selectedBox)
{
Profiler.BeginEvent("VisjectCMGroup.UpdateItemSort");
SortScore = 0;
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is VisjectCMItem item)
{
item.UpdateScore(selectedBox);
if (item.SortScore > SortScore)
SortScore = item.SortScore;
}
}
SortChildren();
Profiler.EndEvent();
}
///
public override int Compare(Control other)
{
if (other is VisjectCMGroup otherGroup)
{
int order = -1 * SortScore.CompareTo(otherGroup.SortScore);
if (order == 0)
{
order = string.Compare(Name, otherGroup.Name, StringComparison.InvariantCulture);
}
return order;
}
return base.Compare(other);
}
}
}