163 lines
3.9 KiB
C#
163 lines
3.9 KiB
C#
|
using System.Collections;
|
|||
|
using UnityEngine;
|
|||
|
using System;
|
|||
|
|
|||
|
public class Window : MonoBehaviour
|
|||
|
{
|
|||
|
public event Action<Window> OnClose;
|
|||
|
public event Action<Window> OnHide;
|
|||
|
public event Action<Window> OnAfterClose;
|
|||
|
public event Action<Window> OnShow;
|
|||
|
|
|||
|
// public VideoWindowMediator videoMediator;
|
|||
|
|
|||
|
private bool _isClosed = false;
|
|||
|
|
|||
|
private Animator _animator;
|
|||
|
private WindowAnimationSettings _animationSettings;
|
|||
|
private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
|
|||
|
private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
|
|||
|
private static readonly int AnimationInteger = Animator.StringToHash("Animation");
|
|||
|
private Action _animationCompleteCallback;
|
|||
|
|
|||
|
public void Init()
|
|||
|
{
|
|||
|
if(TryGetComponent(out _animator))
|
|||
|
if (!TryGetComponent(out _animationSettings))
|
|||
|
throw new Exception("Window with Animator must have WindowAnimationSettings component.");
|
|||
|
|
|||
|
OnInit();
|
|||
|
}
|
|||
|
|
|||
|
protected virtual void OnInit() { }
|
|||
|
|
|||
|
private IEnumerator CloseRoutine()
|
|||
|
{
|
|||
|
yield return new WaitUntil(() => !gameObject.activeSelf);
|
|||
|
|
|||
|
_isClosed = true;
|
|||
|
OnClose?.Invoke(this);
|
|||
|
|
|||
|
if (_isClosed)
|
|||
|
{
|
|||
|
OnAfterClose?.Invoke(this);
|
|||
|
Destroy(gameObject);
|
|||
|
}
|
|||
|
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
|
|||
|
public void Close()
|
|||
|
{
|
|||
|
if (_isClosed) return; //FIX for NullReferenceException: Object reference not set to an instance of an object. Window+<ClearRoutine>d__19.MoveNext ()
|
|||
|
|
|||
|
Hide();
|
|||
|
UI.StartCoroutine(CloseRoutine());
|
|||
|
}
|
|||
|
|
|||
|
public void Show()
|
|||
|
{
|
|||
|
if (IsVisible())
|
|||
|
return;
|
|||
|
|
|||
|
SetVisible(true);
|
|||
|
|
|||
|
if (_animator != null)
|
|||
|
{
|
|||
|
_animator.SetInteger(AnimationInteger, _animationSettings.OpenAnimation);
|
|||
|
_animator.SetTrigger(OpenTrigger);
|
|||
|
OnShowed();
|
|||
|
OnShow?.Invoke(this);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
OnShowed();
|
|||
|
OnShow?.Invoke(this);
|
|||
|
}
|
|||
|
|
|||
|
// if(videoMediator != null && Settings.CrossPromoIsActive)
|
|||
|
// videoMediator.ShowVideo();
|
|||
|
}
|
|||
|
|
|||
|
private void SetVisible(bool value = true)
|
|||
|
{
|
|||
|
gameObject.SetActive(value); //debug
|
|||
|
|
|||
|
SetMouseInteraction(value);
|
|||
|
}
|
|||
|
|
|||
|
public void SetMouseInteraction(bool state)
|
|||
|
{
|
|||
|
gameObject.GetOrCreate<CanvasGroup>().blocksRaycasts = state;
|
|||
|
}
|
|||
|
|
|||
|
protected virtual void OnShowed() { }
|
|||
|
|
|||
|
public void Hide()
|
|||
|
{
|
|||
|
if (IsVisible() == false)
|
|||
|
return;
|
|||
|
|
|||
|
if (_animator != null)
|
|||
|
{
|
|||
|
_animator.SetInteger(AnimationInteger, _animationSettings.CloseAnimation);
|
|||
|
_animator.SetTrigger(CloseTrigger);
|
|||
|
SetMouseInteraction(false);
|
|||
|
_animationCompleteCallback = () =>
|
|||
|
{
|
|||
|
OnHidden();
|
|||
|
SetVisible(false);
|
|||
|
OnHide?.Invoke(this);
|
|||
|
};
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
OnHidden();
|
|||
|
SetVisible(false);
|
|||
|
OnHide?.Invoke(this);
|
|||
|
}
|
|||
|
|
|||
|
// if(videoMediator != null)
|
|||
|
// videoMediator.HideVideo();
|
|||
|
}
|
|||
|
|
|||
|
protected virtual void OnHidden() { }
|
|||
|
|
|||
|
public bool IsVisible()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (gameObject == null)
|
|||
|
return false;
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return gameObject.activeInHierarchy;
|
|||
|
}
|
|||
|
|
|||
|
public virtual bool NeedShowBackground => false;
|
|||
|
|
|||
|
public virtual int ZOrder => 0;
|
|||
|
|
|||
|
public virtual bool CanBeClosedByBackground => false;
|
|||
|
|
|||
|
protected virtual string WindowOpenSfx => "window_open";
|
|||
|
|
|||
|
protected virtual string WindowCloseSfx => "window_close";
|
|||
|
|
|||
|
public IEnumerator CloseWithDelay(float delay)
|
|||
|
{
|
|||
|
yield return new WaitForSeconds(delay);
|
|||
|
Close();
|
|||
|
}
|
|||
|
|
|||
|
public void AnimationComplete()
|
|||
|
{
|
|||
|
_animationCompleteCallback?.Invoke();
|
|||
|
_animationCompleteCallback = null;
|
|||
|
}
|
|||
|
}
|