using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.AdaptivePerformance.UI.Editor
{
///
/// Usage Dial is a VisualElement to display a values with a color dial.
///
public class UsageDial : VisualElement
{
Texture2D m_BaseTexture;
Texture2D m_Texture;
///
/// A value the usage dial should represent.
///
public int Value
{
get { return m_Value; }
set
{
SetValue(value);
}
}
///
/// If the usage dial should show the percentage label.
///
public bool ShowLabel
{
get { return m_showLabel; }
set { m_showLabel = value; }
}
int m_Value = 55;
byte m_ValueCutoff = 0;
int m_ThresholdYellowPercentage = 50;
int m_ThresholdRedPercentage = 75;
byte m_ThresholdYellow = 0;
byte m_ThresholdRed = 0;
static readonly Color32 k_Green = new Color32(136, 176, 49, byte.MaxValue);
static readonly Color32 k_Yellow = new Color32(221, 124, 69, byte.MaxValue);
static readonly Color32 k_Red = new Color32(219, 89, 81, byte.MaxValue);
VisualElement m_Indicator;
VisualElement m_IndicatorRoot;
Label m_Label;
bool m_showLabel;
static readonly ushort[] k_Indices = new ushort[]
{
0, 1, 2,
1, 3, 2,
};
///
/// Constructor, setups the usage dial with its correct percentages.
///
public UsageDial() : base()
{
SetThresholds(m_ThresholdYellowPercentage, m_ThresholdRedPercentage, force: true);
RegenerateTexture();
RegisterCallback(OnCustomStyleResolved);
generateVisualContent = GenerateVisualContent;
}
///
/// Finalizer destorys the dial texture if still allocated.
///
~UsageDial()
{
if (m_Texture)
UnityEngine.Object.DestroyImmediate(m_Texture);
m_Texture = null;
}
///
/// Change the percentage levels of the dial.
///
///
///
/// Force and intemediate update.
public void SetThresholds(int yellowPercentage, int redPercentage, bool force = false)
{
if (!force && yellowPercentage == m_ThresholdYellowPercentage && redPercentage == m_ThresholdRedPercentage)
return;
m_ThresholdYellowPercentage = yellowPercentage;
m_ThresholdRedPercentage = redPercentage;
m_ThresholdYellow = (byte)(byte.MaxValue * (yellowPercentage / 100f));
m_ThresholdRed = (byte)(byte.MaxValue * (redPercentage / 100f));
RegenerateTexture();
}
void SetValue(int percentage, bool force = false)
{
if (!force && m_Value == percentage)
return;
if (m_IndicatorRoot == null)
{
InitVisualChildElements();
}
var f = percentage / 100f;
if (m_IndicatorRoot != null)
m_IndicatorRoot.transform.rotation = Quaternion.Euler(0, 0, 180 * f);
if (m_Label != null)
{
if (m_showLabel)
m_Label.text = string.Format("{0:0}%", percentage);
else
m_Label.text = "";
}
m_Value = percentage;
m_ValueCutoff = (byte)((percentage / 100f) * byte.MaxValue);
RegenerateTexture();
MarkDirtyRepaint();
}
void Init(int value, int yellow, int red, bool showLabel)
{
m_showLabel = showLabel;
InitVisualChildElements();
SetThresholds(yellow, red, force: true);
SetValue(value, force: true);
}
void InitVisualChildElements()
{
m_IndicatorRoot = this.Q("usage-dial__root");
m_Indicator = this.Q("usage-dial__indicator");
m_Label = this.Q