// Copyright (c) 2015 Augie R. Maddox, Guavaman Enterprises. All rights reserved. #pragma warning disable 0219 #pragma warning disable 0618 #pragma warning disable 0649 namespace Rewired.UI.ControlMapper { using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine.Events; using System.Collections.Generic; using System.Collections; [AddComponentMenu("")] [RequireComponent(typeof(CanvasGroup))] public class Window : MonoBehaviour { public Image backgroundImage; public GameObject content; // Working vars private bool _initialized; private int _id = -1; private RectTransform _rectTransform; private Text _titleText; private List _contentText; private GameObject _defaultUIElement; private System.Action _updateCallback; private System.Func _isFocusedCallback; private Timer _timer; private CanvasGroup _canvasGroup; public UnityAction cancelCallback; private GameObject lastUISelection; // Properties public bool hasFocus => _isFocusedCallback != null ? _isFocusedCallback(_id) : false; public int id => _id; public RectTransform rectTransform { get { if(_rectTransform == null) _rectTransform = gameObject.GetComponent(); return _rectTransform; } } public Text titleText => _titleText; public List contentText => _contentText; public GameObject defaultUIElement { get { return _defaultUIElement; } set { _defaultUIElement = value; } } public System.Action updateCallback { get { return _updateCallback; } set { _updateCallback = value; } } public Timer timer => _timer; public int width { get { return (int)rectTransform.sizeDelta.x; } set { Vector2 size = rectTransform.sizeDelta; size.x = value; rectTransform.sizeDelta = size; } } public int height { get { return (int)rectTransform.sizeDelta.y; } set { Vector2 size = rectTransform.sizeDelta; size.y = value; rectTransform.sizeDelta = size; } } protected bool initialized => _initialized; // Unity Events void OnEnable() { StartCoroutine("OnEnableAsync"); // Use coroutine to get around issue with OnEnable being called before GUI is ready and button not highlighting } protected virtual void Update() { if(!_initialized) return; if(!hasFocus) return; CheckUISelection(); if(_updateCallback != null) _updateCallback(_id); } // Public Methods public virtual void Initialize(int id, System.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(text); } public void AddContentText(GameObject prefab, UIPivot pivot, UIAnchor anchor, Vector2 offset) { Text text = null; CreateText(prefab, ref text, "Content Text", pivot, anchor, offset); _contentText.Add(text); } 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 instance = CreateButton(prefab, "Button", anchor, pivot, offset, out buttonInfo); if(instance == null) return; Button button = instance.GetComponent