Files
2025-05-18 22:39:39 +03:00

349 lines
8.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Golems.Attributes;
using RogueUtils.Data;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.Serialization;
using Golems;
[DisallowMultipleComponent]
//This was used over outline entity due to it already being on the objects
public partial class Outline2 : MonoBehaviour
{
[Header("HighEndOutlineSettingsBelow ==================")]
/// <summary>
/// Renderer's attached to this GameObject
/// </summary>
///
[OnValueChanged("UpdateRendererBundle")]
[SerializeField]
private Renderer[] m_Renderers;
public Renderer[] Renderers
{
get => m_Renderers;
set => m_Renderers = value;
}
/// <summary>
/// Outline Colour
/// </summary>
[FormerlySerializedAs("outlineColor")]
[SerializeField]
private Color m_OutlineColor = Color.white;
[Header("The color variable will override local color if it exists")]
public ColorVariable OutlineColorVariable;
[SerializeField]
[Range(0, 10)]
private float m_BlurRadius = 2.5f;
public float BlurRadius
{
get => m_BlurRadius;
set => m_BlurRadius = value;
}
[SerializeField]
[Range(0, 1)]
private float m_Alpha = 1;
public float Alpha
{
get => m_Alpha;
set => m_Alpha = value;
}
[SerializeField] private float m_FadeSpeed = 7.0f;
/// <summary>
/// Returns True if Outline is currently active (m_Alpha >=1).
/// Otherwise, returns False.
/// </summary>
public bool IsOutlining => m_Alpha >= 1.0f;
/// <summary>
/// The current active Fade Coroutine
/// </summary>
private Coroutine m_FadeCoroutine = default;
/// <summary>
/// WaitForSeconds used before Fade in/out begins
/// </summary>
private WaitForSeconds m_FadeDelay = new WaitForSeconds(.1f);
[Header("OutlineTypeToggle ==================")]
[SerializeField]
private bool m_UseLowEndOutline = false;
private void Awake()
{
#if UNITY_SWITCH
m_UseLowEndOutline = true;
#endif
//if (GolemsGameManager.IsPlayerXR())
if (false)
{
m_UseLowEndOutline = true;
}
if (m_UseLowEndOutline)
{
}
else
{
HighEndAwake();
}
}
private void HighEndAwake()
{
if (m_Renderers.Length == 0)
{
Debug.LogWarning("No renderers setup for outline in editor on " + gameObject.name +
". Grabbing them through code");
var renderers = new List<Renderer>();
//
// Check OutlineEntity for Renderer
//
if (TryGetComponent<Renderer>(out var r))
{
renderers.Add(r);
}
SetRenderers(renderers);
}
OutlineCollection.AddRenderer(this);
Alpha = 0;
}
void OnEnable()
{
if (m_UseLowEndOutline)
{
}
else
{
HighEndOnEnable();
}
}
private void HighEndOnEnable()
{
}
private void OnValidate()
{
if (m_UseLowEndOutline)
{
}
else
{
}
}
private void Update()
{
if (m_UseLowEndOutline)
{
}
else
{
}
}
private void OnDisable()
{
Debug.Log("Disabling Outline");
Alpha = 0;
}
private void OnDestroy()
{
if (m_UseLowEndOutline)
{
}
else
{
HighEndOnDestroy();
}
}
private void HighEndOnDestroy()
{
OutlineCollection.RemoveRenderer(this);
Alpha = 0;
}
public void ToggleOutlining()
{
if (m_Alpha > 0)
{
StopOutlining();
}
else
{
StartOutlining();
}
}
/// <summary>
/// Activates/Deactivates the Outline,
/// depending on the given true or false value.
/// </summary>
/// <param name="active"> Activate or deactivate the Outline,
/// where true activates the Outline and false deactivates the Outline.</param>
public void SetOutlineActive(bool active)
{
if (active)
{
StartOutlining();
}
else
{
StopOutlining();
}
}
[Button]
public void StartOutlining()
{
if (m_UseLowEndOutline)
{
Debug.Log("Start Outlining");
}
else
{
StartHighEndOutlining();
}
}
public void StartHighEndOutlining()
{
//Debug.Log("StartOutlining");
if (m_FadeCoroutine != null)
{
StopCoroutine(m_FadeCoroutine);
m_FadeCoroutine = null;
}
//
// protect against running on disabled game objects
//
if (gameObject.activeInHierarchy)
{
m_FadeCoroutine = StartCoroutine(Fade(1));
}
}
[Button]
public void StopOutlining()
{
if (m_UseLowEndOutline)
{
Debug.Log("Stop Outlining");
}
else
{
StopHighEndOutlining();
}
}
public void StopHighEndOutlining()
{
if (m_FadeCoroutine != null)
{
StopCoroutine(m_FadeCoroutine);
m_FadeCoroutine = null;
}
//
// protect against running on disabled game objects
//
if (gameObject.activeInHierarchy)
{
m_FadeCoroutine = StartCoroutine(Fade(-1));
}
}
private IEnumerator Fade(int dir)
{
yield return new WaitForSeconds(0.1f);
bool faded = false;
while (!faded)
{
var alpha = Alpha;
alpha += (dir * m_FadeSpeed) * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
Alpha = alpha;
faded = dir < 0 ? alpha <= 0.0f : alpha >= 1.0f;
yield return null;
}
m_FadeCoroutine = null;
}
[ContextMenu("Force Stop Outlining")]
/// <summary>
/// Forces alpha to 0 to avoid showing if moved immediately
/// </summary>
public void ForceStopOutlining()
{
if (m_UseLowEndOutline)
{
}
else
{
Alpha = 0;
}
}
public void SetRenderers(List<Renderer> renderers)
{
m_Renderers = renderers.ToArray();
}
/// <summary>
/// Get the current Color for this Outline instance.
/// If the Outline has a OutlineColorVariable the value
/// of the passed Color variable will be set to the scriptable
/// variable value. Otherwise, it will be set to this Outlines m_OutlineColor.
/// </summary>
/// <param name="colour">Color variable to store this instances current outline colour</param>
public void GetColour(out Color colour)
{
colour = OutlineColorVariable == null ? m_OutlineColor : OutlineColorVariable.Value;
}
}