using PaintCore; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace game { /// This component fills the attached UI Image based on the total amount of opaque pixels that have been painted in all active and enabled CwChannelCounter components in the scene. [RequireComponent(typeof(Image))] public class BrushChannelCounterFill : MonoBehaviour { public enum ChannelType { Red, Green, Blue, Alpha } /// This allows you to specify the counters that will be used. /// Zero = All active and enabled counters in the scene. public List Counters { get { if (counters == null) counters = new List(); return counters; } } [SerializeField] private List counters; /// This allows you to choose which channel will be output to the UI Image. public ChannelType Channel { set { channel = value; } get { return channel; } } [SerializeField] private ChannelType channel; /// Inverse the fill? 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(); 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(new EventWashingMonitoringFoamProgressData(ratio)); if (1 - ratio <= BrushChannelCounterFill.m_OmitPercentage) { if (m_IsOver == false) { FishingYachtAct.Publish(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); } } } }