Files
back_cantanBuilding/Assets/Scripts/EventWashing/Tools/BrushChannelCounterFill.cs
2026-05-26 16:15:54 +08:00

106 lines
3.5 KiB
C#

using PaintCore;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace game
{
/// <summary>This component fills the attached UI Image based on the total amount of opaque pixels that have been painted in all active and enabled <b>CwChannelCounter</b> components in the scene.</summary>
[RequireComponent(typeof(Image))]
public class BrushChannelCounterFill : MonoBehaviour
{
public enum ChannelType
{
Red,
Green,
Blue,
Alpha
}
/// <summary>This allows you to specify the counters that will be used.
/// Zero = All active and enabled counters in the scene.</summary>
public List<CwChannelCounter> Counters { get { if (counters == null) counters = new List<CwChannelCounter>(); return counters; } }
[SerializeField] private List<CwChannelCounter> counters;
/// <summary>This allows you to choose which channel will be output to the UI Image.</summary>
public ChannelType Channel { set { channel = value; } get { return channel; } }
[SerializeField] private ChannelType channel;
/// <summary>Inverse the fill?</summary>
public bool Inverse { set { inverse = value; } get { return inverse; } }
[SerializeField] private bool inverse;
private static float m_OmitPercentage = 0.1f;
public static float OmitPercentage { get => m_OmitPercentage; set => m_OmitPercentage = value; }
private bool m_IsOver = false;
[System.NonSerialized]
private Image cachedImage;
private bool m_IsStopping = false;
public void ResetData()
{
m_IsOver = false;
}
public void StopCounting()
{
m_IsStopping = true;
}
public void StartCounting()
{
m_IsStopping = false;
}
protected virtual void OnEnable()
{
cachedImage = GetComponent<Image>();
m_IsOver = false;
}
protected virtual void Update()
{
if (m_IsStopping) return;
var finalCounters = counters.Count > 0 ? counters : null;
var ratio = 0.0f;
switch (channel)
{
case ChannelType.Red: ratio = CwChannelCounter.GetRatioR(finalCounters); break;
case ChannelType.Green: ratio = CwChannelCounter.GetRatioG(finalCounters); break;
case ChannelType.Blue: ratio = CwChannelCounter.GetRatioB(finalCounters); break;
case ChannelType.Alpha: ratio = CwChannelCounter.GetRatioA(finalCounters); break;
}
if (inverse == true)
{
ratio = 1.0f - ratio;
}
FishingYachtAct.Publish<EventWashingMonitoringFoamProgressData>(new EventWashingMonitoringFoamProgressData(ratio));
if (1 - ratio <= BrushChannelCounterFill.m_OmitPercentage)
{
if (m_IsOver == false)
{
FishingYachtAct.Publish<EventWashingConclusionData>(new EventWashingConclusionData());
m_IsOver = true;
}
cachedImage.fillAmount = 1.0f;
}
else
{
float tmp = 1f - BrushChannelCounterFill.m_OmitPercentage;
float realProgress = ratio / tmp;
cachedImage.fillAmount = Mathf.Clamp01(realProgress);
}
}
}
}