// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.GUI
{
///
/// Popup that shows the list of actors to pick. Supports searching and basic type filtering.
///
///
public class ActorSearchPopup : ItemsListContextMenu
{
///
/// The actor item.
///
///
public class ActorItemView : Item
{
private Actor _actor;
///
/// Gets the actor.
///
public Actor Actor => _actor;
///
/// Initializes a new instance of the class.
///
/// The actor.
public ActorItemView(Actor actor)
{
_actor = actor;
Name = actor.Name;
TooltipText = Utilities.Utils.GetTooltip(actor);
}
///
public override void OnDestroy()
{
_actor = null;
base.OnDestroy();
}
}
///
/// Validates if the given actor item can be used to pick it.
///
/// The actor.
/// True if is valid.
public delegate bool IsValidDelegate(Actor actor);
private IsValidDelegate _isValid;
private Action _selected;
private ActorSearchPopup(IsValidDelegate isValid, Action selected)
{
_isValid = isValid;
_selected = selected;
ItemClicked += OnItemClicked;
// TODO: use async thread to search scenes
for (int i = 0; i < Level.ScenesCount; i++)
{
Find(Level.GetScene(i));
}
SortItems();
}
private void OnItemClicked(Item item)
{
_selected(((ActorItemView)item).Actor);
}
private void Find(Actor actor)
{
if (!actor)
return;
if (_isValid(actor))
{
AddItem(new ActorItemView(actor));
}
for (int i = 0; i < actor.ChildrenCount; i++)
{
Find(actor.GetChild(i));
}
}
///
/// Shows the popup.
///
/// The show target.
/// The show target location.
/// Event called to check if a given actor item is valid to be used.
/// Event called on actor item pick.
/// The dialog.
public static ActorSearchPopup Show(Control showTarget, Float2 showTargetLocation, IsValidDelegate isValid, Action selected)
{
var popup = new ActorSearchPopup(isValid, selected);
popup.Show(showTarget, showTargetLocation);
return popup;
}
///
public override void OnDestroy()
{
_isValid = null;
_selected = null;
base.OnDestroy();
}
}
}