Files
beyond/Assets/Scripts/UI/ScrollBase.cs
2024-11-20 15:21:28 +01:00

217 lines
6.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
//using UnityEngine.InputSystem.UI;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Video;
namespace Beyond
{
public class ScrollBase : MonoBehaviour
{
/// <summary>
/// if true, canvas should be displayed directly on the screen
/// </summary>
[SerializeField] private bool m_DirectDisplay;
/// <summary>
/// Input that is used to pass touch/click events from/to 3D scroll
/// </summary>
public UVInputModule m_uvInputModule;
#if USE_NEW_INPUT
/// <summary>
/// Standard unity input that needs to be switched off while scroll is on
/// </summary>
public InputSystemUIInputModule m_inputSystemUIInputModule;
#endif
/// <summary>
/// Animates scroll opening and closing
/// </summary>
public Animator m_scrollAnimator;
/// <summary>
/// Canvas used to render 2D scroll elements
/// </summary>
public Canvas m_canvas;
public Camera m_CanvasCamera;
public Camera m_ScrollCamera;
public bool m_burnIn = false;
public bool m_isOpen = false;
public float m_burnInDelay = 1f;
public float m_moveInTime = 1f;
public float m_burnInTime = 1f;
public float m_burnThreshold;
[Tooltip("Motion curve for first move in")]
public AnimationCurve m_MotionCurve;
public AnimationCurve m_BurnInCurve;
public Vector3 m_startPositionShift = new Vector3(-3f, 0f, 0f);
private Vector3 m_startPosition;
private Vector3 m_endPosition;
private Transform m_scrollTransform;
public Material m_scrollMat;
private Volume m_volume;
public VolumeProfile m_volumeProfile;
private VolumeProfile m_defaultProfile;
void ActivateObjects(bool activate)
{
m_canvas.gameObject.SetActive(activate);
if (!m_DirectDisplay)
{
m_CanvasCamera.gameObject.SetActive(activate);
m_ScrollCamera.gameObject.SetActive(activate);
}
if (m_volume && m_volumeProfile != null)
m_volume.profile = activate ? m_volumeProfile : m_defaultProfile;
}
void BlockUI(bool block)
{
HideUI.InvokeSetActiveUI(!block);
if (m_uvInputModule != null)
{
m_uvInputModule.enabled = block;
}
#if USE_NEW_INPUT
if (m_inputSystemUIInputModule)
m_inputSystemUIInputModule.enabled = !block;
#endif
}
virtual protected void OnScrollOpened()
{
}
virtual protected void OnScrollClosed()
{
}
IEnumerator OpenAndBurnIn()
{
BlockUI(true);
ActivateObjects(true);
SetBurnInTh(0f);
float time = 0f;
while (time < m_moveInTime)
{
time += Time.deltaTime;
float val = m_MotionCurve.Evaluate(time / m_moveInTime);
m_scrollTransform.position = m_startPosition - m_startPositionShift * val;
yield return null;
}
m_scrollAnimator.SetBool("Open", true);
yield return new WaitForSeconds(m_burnInDelay);
time = 0f;
while (time < m_burnInTime)
{
time += Time.deltaTime;
float val = m_BurnInCurve.Evaluate(time / m_burnInTime);
SetBurnInTh(val);
yield return null;
}
m_isOpen = true;
OnScrollOpened();
yield return true;
}
IEnumerator CloseAndBurnOut()
{
float time = 0f;
m_isOpen = false;
while (time < m_burnInTime)
{
time += Time.deltaTime;
float val = m_BurnInCurve.Evaluate(1.0f - time / m_burnInTime);
SetBurnInTh(val);
yield return null;
}
m_scrollAnimator.SetBool("Open", false);
time = 0f;
yield return new WaitForSeconds(m_burnInDelay);
while (time < m_moveInTime)
{
time += Time.deltaTime;
float val = m_MotionCurve.Evaluate(1.0f - time / m_moveInTime);
m_scrollTransform.position = m_startPosition - m_startPositionShift * val;
yield return null;
}
BlockUI(false);
ActivateObjects(false);
OnScrollClosed();
yield return true;
}
void SetBurnInTh(float burnTh)
{
m_scrollMat.SetFloat("_Threshold", burnTh);
}
protected virtual void Awake()
{
if (!m_scrollAnimator)
{
m_scrollAnimator = transform.GetComponentInChildren<Animator>();
}
m_scrollAnimator.SetBool("Open", false);
m_scrollTransform = m_scrollAnimator.transform.parent;
if (m_scrollMat == null)
m_scrollMat = m_scrollAnimator.transform.GetComponentInChildren<Renderer>().material;
if (m_volume == null)
m_volume = GameObject.FindObjectOfType<Volume>();
if (m_volume != null)
m_defaultProfile = m_volume.profile;
if (m_uvInputModule != null)
m_uvInputModule.forceModuleActive = true;
}
[Button]
public void OpenCloseScroll(bool open)
{
if (open)
StartCoroutine(OpenAndBurnIn());
else
StartCoroutine(CloseAndBurnOut());
}
protected virtual void Start()
{
m_endPosition = m_scrollTransform.position;
m_startPosition = m_endPosition + m_startPositionShift;
m_scrollTransform.position = m_startPosition;
var camera = Camera.main;
if (camera != null)
{
var cameraData = camera.GetUniversalAdditionalCameraData();
if (cameraData.cameraStack.Count == 0)
cameraData.cameraStack.Add(m_ScrollCamera);
}
if (m_DirectDisplay)
{
m_ScrollCamera.gameObject.SetActive(false);
m_CanvasCamera.gameObject.SetActive(false);
m_canvas.renderMode = RenderMode.ScreenSpaceOverlay;
}
//OpenCloseScroll(true);
//StartCoroutine(DisplayPages());
//ActivateObjects(false);
}
}
}