using UnityEngine.UIElements; namespace RosettaUI.UIToolkit { /// /// Referring to UIElement's Popup. /// // ReSharper disable once ClassWithVirtualMembersNeverInherited.Global public class ModalWindow : Window { private const string USSClassName = "rosettaui-modal-window"; private readonly VisualElement _eventBlockerElement; protected override VisualElement SelfRoot => _eventBlockerElement; public ModalWindow(bool resizable = false) : base(resizable, true) { _eventBlockerElement = new EventBlocker(); _eventBlockerElement.Add(this); AddToClassList(USSClassName); _eventBlockerElement.RegisterCallback(OnAttachToPanel); _eventBlockerElement.RegisterCallback(OnDetachFromPanel); } public override void Hide() { base.Hide(); _eventBlockerElement.RemoveFromHierarchy(); } protected virtual void OnAttachToPanel(AttachToPanelEvent evt) { if (evt.destinationPanel == null) return; _eventBlockerElement.RegisterCallback(OnPointerDownOnBlocker); } protected virtual void OnDetachFromPanel(DetachFromPanelEvent evt) { if (evt.originPanel == null) return; _eventBlockerElement.UnregisterCallback(OnPointerDownOnBlocker); } protected virtual void OnPointerDownOnBlocker(PointerDownEvent evt) { if (worldBound.Contains(evt.position)) return; Hide(); evt.StopPropagation(); } } }