// Filename: CDlgSkillTooltip.cs
// Creator: BrewMonster
// Date: 2024
// Purpose: Tooltip dialog for displaying skill information when clicking skill icons
using BrewMonster.Scripts.UI;
using BrewMonster.UI;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster
{
///
/// Tooltip dialog that displays skill information (description, requirements, etc.)
/// when clicking on skill icons. Uses EC_UIUtility for smart positioning.
///
public class CDlgSkillTooltip : AUIDialog
{
[Header("Tooltip Components")]
[SerializeField] private GameObject panelRoot;
[SerializeField] private EC_UIUtility.TextOutlet descriptionText;
[SerializeField] private Button closeButton;
[Header("Positioning Settings")]
[SerializeField] private Vector2 panelOffset = new Vector2(20f, 0f);
[SerializeField] private bool autoHideOnClick = true;
[SerializeField] private float autoHideDelay = 0f; // 0 = no auto-hide
private RectTransform sourceButton;
private float showTime;
public override void Awake()
{
base.Awake();
if (closeButton != null)
{
closeButton.onClick.RemoveAllListeners();
closeButton.onClick.AddListener(HideTooltip);
}
// Hide by default
if (panelRoot != null)
{
panelRoot.SetActive(false);
}
}
private void Update()
{
// Auto-hide after delay if enabled
if (autoHideDelay > 0f && panelRoot != null && panelRoot.activeSelf)
{
if (Time.time - showTime >= autoHideDelay)
{
HideTooltip();
}
}
// Click anywhere to hide if enabled
if (autoHideOnClick && panelRoot != null && panelRoot.activeSelf)
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
// Check if click is outside the tooltip
if (!RectTransformUtility.RectangleContainsScreenPoint(
panelRoot.transform as RectTransform,
Input.mousePosition,
null))
{
HideTooltip();
}
}
}
}
///
/// Show the tooltip with skill information near the clicked button.
///
/// The skill description and information to display
/// The button RectTransform to position near
public void ShowTooltip(string hintText, RectTransform buttonRect)
{
if (string.IsNullOrEmpty(hintText))
{
Debug.LogWarning("[CDlgSkillTooltip] ShowTooltip called with empty hint text");
return;
}
// Set description text
if (descriptionText != null)
{
// Text is already formatted by the caller
descriptionText.Set(hintText);
}
else
{
Debug.LogWarning("[CDlgSkillTooltip] descriptionText is not assigned");
}
sourceButton = buttonRect;
showTime = Time.time;
// Position near button using utility
if (panelRoot != null && buttonRect != null)
{
var panelRect = panelRoot.transform as RectTransform;
if (panelRect != null)
{
// Show first so we can get accurate size for positioning
EC_UIUtility.ShowPanel(panelRoot, true);
// Position using the utility function
EC_UIUtility.PositionPanelNearButton(panelRect, buttonRect, panelOffset);
}
else
{
Debug.LogWarning("[CDlgSkillTooltip] panelRoot does not have a RectTransform component");
EC_UIUtility.ShowPanel(panelRoot, true);
}
}
else
{
if (panelRoot == null)
{
Debug.LogError("[CDlgSkillTooltip] panelRoot is not assigned");
}
if (buttonRect == null)
{
Debug.LogWarning("[CDlgSkillTooltip] buttonRect is null, showing tooltip at default position");
if (panelRoot != null)
{
EC_UIUtility.ShowPanel(panelRoot, true);
}
}
}
}
///
/// Hide the tooltip.
///
public void HideTooltip()
{
EC_UIUtility.ShowPanel(panelRoot, false);
sourceButton = null;
}
///
/// Check if the tooltip is currently visible.
///
/// True if tooltip is visible
public bool IsVisible()
{
return panelRoot != null && panelRoot.activeSelf;
}
private void OnDisable()
{
HideTooltip();
}
}
}