using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Events; using UnityEngine.UI; namespace Rewired.UI.ControlMapper { [AddComponentMenu("")] [RequireComponent(typeof(CanvasGroup))] public class Window : MonoBehaviour { public class Timer { private bool _started; private float end; public bool started => _started; public bool finished { get { if (!started) { return false; } if (Time.realtimeSinceStartup < end) { return false; } _started = false; return true; } } public float remaining { get { if (!_started) { return 0f; } return end - Time.realtimeSinceStartup; } } public void Start(float length) { end = Time.realtimeSinceStartup + length; _started = true; } public void Stop() { _started = false; } } public Image backgroundImage; public GameObject content; private bool _initialized; private int _id = -1; private RectTransform _rectTransform; private Text _titleText; private List _contentText; private GameObject _defaultUIElement; private Action _updateCallback; private Func _isFocusedCallback; private Timer _timer; private CanvasGroup _canvasGroup; public UnityAction cancelCallback; private GameObject lastUISelection; public bool hasFocus { get { if (_isFocusedCallback == null) { return false; } return _isFocusedCallback(_id); } } public int id => _id; public RectTransform rectTransform { get { if (_rectTransform == null) { _rectTransform = base.gameObject.GetComponent(); } return _rectTransform; } } public Text titleText => _titleText; public List contentText => _contentText; public GameObject defaultUIElement { get { return _defaultUIElement; } set { _defaultUIElement = value; } } public Action updateCallback { get { return _updateCallback; } set { _updateCallback = value; } } public Timer timer => _timer; public int width { get { return (int)rectTransform.sizeDelta.x; } set { Vector2 sizeDelta = rectTransform.sizeDelta; sizeDelta.x = value; rectTransform.sizeDelta = sizeDelta; } } public int height { get { return (int)rectTransform.sizeDelta.y; } set { Vector2 sizeDelta = rectTransform.sizeDelta; sizeDelta.y = value; rectTransform.sizeDelta = sizeDelta; } } protected bool initialized => _initialized; private void OnEnable() { StartCoroutine("OnEnableAsync"); } protected virtual void Update() { if (_initialized && hasFocus) { CheckUISelection(); if (_updateCallback != null) { _updateCallback(_id); } } } public virtual void Initialize(int id, Func isFocusedCallback) { if (_initialized) { Debug.LogError("Window is already initialized!"); return; } _id = id; _isFocusedCallback = isFocusedCallback; _timer = new Timer(); _contentText = new List(); _canvasGroup = GetComponent(); _initialized = true; } public void SetSize(int width, int height) { rectTransform.sizeDelta = new Vector2(width, height); } public void CreateTitleText(GameObject prefab, Vector2 offset) { CreateText(prefab, ref _titleText, "Title Text", UIPivot.TopCenter, UIAnchor.TopHStretch, offset); } public void CreateTitleText(GameObject prefab, Vector2 offset, string text) { CreateTitleText(prefab, offset); SetTitleText(LanguageManager.Instance.GetText(text)); } public void AddContentText(GameObject prefab, UIPivot pivot, UIAnchor anchor, Vector2 offset) { Text textComponent = null; CreateText(prefab, ref textComponent, "Content Text", pivot, anchor, offset); _contentText.Add(textComponent); } public void AddContentText(GameObject prefab, UIPivot pivot, UIAnchor anchor, Vector2 offset, string text) { AddContentText(prefab, pivot, anchor, offset); SetContentText(text, _contentText.Count - 1); } public void AddContentImage(GameObject prefab, UIPivot pivot, UIAnchor anchor, Vector2 offset) { CreateImage(prefab, "Image", pivot, anchor, offset); } public void AddContentImage(GameObject prefab, UIPivot pivot, UIAnchor anchor, Vector2 offset, string text) { AddContentImage(prefab, pivot, anchor, offset); } public void CreateButton(GameObject prefab, UIPivot pivot, UIAnchor anchor, Vector2 offset, string buttonText, UnityAction confirmCallback, UnityAction cancelCallback, bool setDefault) { if (prefab == null) { return; } ButtonInfo buttonInfo; GameObject gameObject = CreateButton(prefab, "Button", anchor, pivot, offset, out buttonInfo); if (!(gameObject == null)) { Button component = gameObject.GetComponent