using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
using System.Linq;
///
/// Beautiful UI popup for combat event encounters
/// Shows enemy details and provides options to attack or run away
/// Uses UXML template for cleaner UI structure
///
public class CombatEventPopupUXML : MonoBehaviour, IClickBlocker
{
[Header("UI References")]
public UIDocument uiDocument;
public VisualTreeAsset popupTemplate; // UXML template
public StyleSheet popupStyleSheet; // USS styles
[Header("Popup Settings")]
public float animationDuration = 0.3f;
public float backgroundBlurIntensity = 0.5f;
[Header("Debug")]
public bool debugMode = false;
// Events
public System.Action OnCombatDecision; // true = attack, false = run away
// UI Elements (from UXML)
private VisualElement popupContainer;
private VisualElement backgroundOverlay;
private VisualElement popupPanel;
private Label eventTitleLabel;
private Label eventDescriptionLabel;
private VisualElement enemyListContainer;
private VisualElement enemyItemTemplate;
private Button attackButton;
private Button runAwayButton;
// Current combat data
private BattleEventData currentBattleData;
private TravelEventContext currentContext;
private bool isPopupActive = false;
void Awake()
{
// Find UI Document if not assigned
if (uiDocument == null)
uiDocument = GetComponent();
if (uiDocument == null)
{
Debug.LogError("CombatEventPopupUXML: No UIDocument found! Please assign one.");
return;
}
SetupUI();
}
void OnEnable()
{
// Register with ClickManager when this component is enabled
if (ClickManager.Instance != null)
{
ClickManager.Instance.RegisterClickBlocker(this);
}
}
void OnDisable()
{
// Unregister from ClickManager when this component is disabled
if (ClickManager.Instance != null)
{
ClickManager.Instance.UnregisterClickBlocker(this);
}
}
void OnDestroy()
{
// Ensure we're unregistered when destroyed
if (ClickManager.Instance != null)
{
ClickManager.Instance.UnregisterClickBlocker(this);
}
}
void SetupUI()
{
var root = uiDocument.rootVisualElement;
if (debugMode) Debug.Log($"🔧 SetupUI: Root element size: {root.resolvedStyle.width}x{root.resolvedStyle.height}");
// Load and instantiate UXML template
if (popupTemplate != null)
{
// Clone the template
var template = popupTemplate.Instantiate();
root.Add(template);
if (debugMode) Debug.Log($"✅ UXML template instantiated and added to root");
// Apply style sheet
if (popupStyleSheet != null)
{
root.styleSheets.Add(popupStyleSheet);
if (debugMode) Debug.Log($"✅ USS stylesheet applied");
}
else
{
Debug.LogWarning("⚠️ No USS stylesheet assigned");
}
}
else
{
Debug.LogWarning("No UXML template assigned, creating UI programmatically as fallback");
CreateUIFallback(root);
}
// Cache UI element references
CacheUIElements(root);
// Setup event handlers
if (backgroundOverlay != null)
{
// Block all mouse events from reaching the map below
backgroundOverlay.RegisterCallback(OnBackgroundClick);
backgroundOverlay.RegisterCallback(OnBackgroundMouseDown);
backgroundOverlay.RegisterCallback(OnBackgroundMouseUp);
backgroundOverlay.RegisterCallback(OnBackgroundMouseMove);
backgroundOverlay.RegisterCallback(OnBackgroundWheelEvent);
}
if (attackButton != null)
attackButton.clicked += OnAttackClicked;
if (runAwayButton != null)
runAwayButton.clicked += OnRunAwayClicked;
// Initially hide the popup
HidePopup();
}
void CacheUIElements(VisualElement root)
{
// Cache references to UI elements by name/class
popupContainer = root.Q("combat-event-popup");
backgroundOverlay = root.Q("background-overlay");
popupPanel = root.Q("popup-panel");
eventTitleLabel = root.Q