namespace UIWidgets.Menu
{
using System;
using System.Collections.Generic;
using UIWidgets.Attributes;
using UIWidgets.l10n;
using UIWidgets.Styles;
using UnityEngine;
using UnityEngine.EventSystems;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
using UnityEngine.Serialization;
///
/// Context menu.
/// Contains menu items and reference to the menu template.
///
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
[AddComponentMenu("UI/New UI Widgets/ContextMenu")]
[DataBindSupport]
public partial class ContextMenu : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IStylable
#if !ENABLE_INPUT_SYSTEM
, IUpdatable
#endif
{
///
/// Serialized menu item.
///
[Serializable]
protected class MenuItemSerialized
{
///
/// Depth.
///
[SerializeField]
public int Depth;
///
/// Icon.
///
[SerializeField]
public Sprite Icon;
///
/// Checked.
///
[SerializeField]
public bool Checked;
///
/// Name.
///
[SerializeField]
public string Name;
///
/// Template.
///
[SerializeField]
public string Template;
///
/// Name.
///
[SerializeField]
public HotKey HotKey;
///
/// Is item visible?
///
[SerializeField]
public bool Visible;
///
/// Is item interactable.
///
[SerializeField]
public bool Interactable;
///
/// For the editor use only.
///
[SerializeField]
[HideInInspector]
#pragma warning disable 0414
bool showAction = false;
#pragma warning restore 0414
///
/// Action.
///
[SerializeField]
public MenuItemAction Action = new MenuItemAction();
///
/// Convert specified instance to the menu item.
///
/// Serialized instance.
/// Menu item.
public static MenuItem ToMenuItem(MenuItemSerialized serialized)
{
return serialized;
}
///
/// Convert specified instance to the menu item.
///
/// Serialized instance.
public static implicit operator MenuItem(MenuItemSerialized serialized)
{
return new MenuItem()
{
Icon = serialized.Icon,
Checked = serialized.Checked,
Name = serialized.Name,
Template = serialized.Template,
HotKey = serialized.HotKey,
Visible = serialized.Visible,
Interactable = serialized.Interactable,
Action = serialized.Action,
};
}
}
#region Interactable
[SerializeField]
bool interactable = true;
///
/// Is widget interactable.
///
/// true if interactable; otherwise, false.
[DataBindField]
public bool Interactable
{
get
{
return interactable;
}
set
{
if (interactable != value)
{
interactable = value;
InteractableChanged();
}
}
}
///
/// If the canvas groups allow interaction.
///
protected bool GroupsAllowInteraction = true;
///
/// The CanvasGroup cache.
///
protected List CanvasGroupCache = new List();
///
/// Process the CanvasGroupChanged event.
///
protected override void OnCanvasGroupChanged()
{
var groupAllowInteraction = true;
var t = transform;
while (t != null)
{
t.GetComponents(CanvasGroupCache);
var shouldBreak = false;
foreach (var canvas_group in CanvasGroupCache)
{
if (!canvas_group.interactable)
{
groupAllowInteraction = false;
shouldBreak = true;
}
shouldBreak |= canvas_group.ignoreParentGroups;
}
if (shouldBreak)
{
break;
}
t = t.parent;
}
if (groupAllowInteraction != GroupsAllowInteraction)
{
GroupsAllowInteraction = groupAllowInteraction;
InteractableChanged();
}
}
///
/// Returns true if the GameObject and the Component are active.
///
/// true if the GameObject and the Component are active; otherwise false.
public override bool IsActive()
{
return base.IsActive() && GroupsAllowInteraction && Interactable && isInited;
}
///
/// Process interactable change.
///
protected virtual void InteractableChanged()
{
if (!base.IsActive() || !isInited)
{
return;
}
OnInteractableChange(GroupsAllowInteraction && Interactable);
}
///
/// Process interactable change.
///
/// Current interactable state.
protected virtual void OnInteractableChange(bool interactableState)
{
}
#endregion
[SerializeField]
ContextMenuView template;
///
/// Menu template.
///
public ContextMenuView Template
{
get
{
return template;
}
set
{
template = value;
if (Instance != null)
{
Instance.Template = template;
}
}
}
///
/// Serialized menu items.
///
[SerializeField]
[HideInInspector]
protected List MenuItemsSerialized = new List();
ObservableList