SHA256
forked from ADOFAIEditorForum/ADOFAIRenderer
129 lines
4.5 KiB
C#
129 lines
4.5 KiB
C#
using System.Diagnostics;
|
|
using ADOFAIRenderer.Core;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace ADOFAIRenderer.UI;
|
|
|
|
internal class EscapeHandlerUI : MonoBehaviour
|
|
{
|
|
private GameObject _containerObj = null!;
|
|
private TextMeshProUGUI _textComponent = null!;
|
|
private Stopwatch? _escapeStopwatch;
|
|
|
|
private void Start()
|
|
{
|
|
// Set UI layer (5)
|
|
gameObject.layer = 5;
|
|
|
|
// Canvas setup
|
|
var canvas = gameObject.AddComponent<Canvas>();
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
canvas.sortingOrder = 10000; // Placed above debug monitor
|
|
|
|
// Responsive Canvas Scaler setup
|
|
var scaler = gameObject.AddComponent<CanvasScaler>();
|
|
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
|
|
scaler.referenceResolution = new Vector2(1920, 1080);
|
|
scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
|
|
scaler.matchWidthOrHeight = 0.5f;
|
|
|
|
gameObject.AddComponent<GraphicRaycaster>();
|
|
|
|
// 1. Create container and background panel object
|
|
_containerObj = new GameObject("EscapeContainer");
|
|
_containerObj.transform.SetParent(transform, false);
|
|
_containerObj.layer = 5;
|
|
|
|
var bgImage = _containerObj.AddComponent<Image>();
|
|
bgImage.color = new Color(0.05f, 0.05f, 0.07f, 0.85f); // Dark tone acrylic style
|
|
|
|
var containerRect = _containerObj.GetComponent<RectTransform>();
|
|
containerRect.anchorMin = new Vector2(0.5f, 0.5f);
|
|
containerRect.anchorMax = new Vector2(0.5f, 0.5f);
|
|
containerRect.pivot = new Vector2(0.5f, 0.5f);
|
|
containerRect.anchoredPosition = Vector2.zero; // Screen center
|
|
containerRect.sizeDelta = new Vector2(460, 160);
|
|
|
|
// 2. Create text object
|
|
var textObj = new GameObject("EscapeText");
|
|
textObj.transform.SetParent(_containerObj.transform, false);
|
|
textObj.layer = 5;
|
|
|
|
_textComponent = textObj.AddComponent<TextMeshProUGUI>();
|
|
_textComponent.alignment = TextAlignmentOptions.Center;
|
|
_textComponent.fontSize = 60;
|
|
_textComponent.textWrappingMode = TextWrappingModes.NoWrap;
|
|
_textComponent.richText = true;
|
|
|
|
// Apply game's default font (RDString.fontData.fontTMP)
|
|
if (RDString.fontData.fontTMP != null)
|
|
_textComponent.font = RDString.fontData.fontTMP;
|
|
|
|
var textRect = textObj.GetComponent<RectTransform>();
|
|
textRect.anchorMin = Vector2.zero;
|
|
textRect.anchorMax = Vector2.one;
|
|
textRect.offsetMin = new Vector2(10, 10);
|
|
textRect.offsetMax = new Vector2(-10, -10);
|
|
|
|
// Initial state disabled
|
|
_containerObj.SetActive(false);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// Destroy self if rendering is no longer active
|
|
if (!RenderManager.IsActive)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
if (Input.GetKey(KeyCode.Escape))
|
|
{
|
|
if (_escapeStopwatch == null)
|
|
{
|
|
_escapeStopwatch = new Stopwatch();
|
|
_escapeStopwatch.Start();
|
|
_containerObj.SetActive(true);
|
|
}
|
|
|
|
var elapsed = (float)_escapeStopwatch.Elapsed.TotalSeconds;
|
|
var remaining = Mathf.Max(0f, 3f - elapsed);
|
|
|
|
// Update remaining time text
|
|
_textComponent.text = $"<color=#FF5252>Cancelling in {remaining:F1}s..</color>\n<size=40><color=#DDDDDD>Hold Esc to cancel render</color></size>";
|
|
|
|
// Micro pulse animation (container smoothly scales up while holding)
|
|
var scale = 1.0f + elapsed / 3.0f * 0.15f;
|
|
_containerObj.transform.localScale = new Vector3(scale, scale, 1f);
|
|
|
|
if (elapsed < 3f) return;
|
|
|
|
_escapeStopwatch.Stop();
|
|
_escapeStopwatch = null;
|
|
_containerObj.transform.localScale = Vector3.one; // Reset scale
|
|
_containerObj.SetActive(false);
|
|
|
|
// Cancel rendering and exit scene
|
|
if (RenderManager.Instance)
|
|
RenderManager.Instance.CancelRender();
|
|
}
|
|
else
|
|
{
|
|
if (_escapeStopwatch == null) return;
|
|
_escapeStopwatch.Stop();
|
|
_escapeStopwatch = null;
|
|
_containerObj.transform.localScale = Vector3.one; // Reset scale
|
|
_containerObj.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_escapeStopwatch == null) return;
|
|
_escapeStopwatch.Stop();
|
|
_escapeStopwatch = null;
|
|
}
|
|
} |