BITKit/Src/Unity/Scripts/UX/Library/DashBoardProgress.cs

112 lines
3.8 KiB
C#

using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
// An element that displays progress inside a partially filled circle
public class DashBoardProgress : VisualElement,INotifyValueChanged<float>
{
public new class UxmlTraits : VisualElement.UxmlTraits
{
// The progress property is exposed to UXML.
private readonly UxmlFloatAttributeDescription m_ProgressAttribute = new UxmlFloatAttributeDescription()
{
name = "progress"
};
// Use the Init method to assign the value of the progress UXML attribute to the C# progress property.
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((DashBoardProgress)ve).progress = m_ProgressAttribute.GetValueFromBag(bag, cc);
}
}
// Define a factory class to expose this control to UXML.
public new class UxmlFactory : UxmlFactory<DashBoardProgress, UxmlTraits> { }
// These are USS class names for the control overall and the label.
public static readonly string ussClassName = "radial-progress";
public static readonly string ussLabelClassName = "radial-progress__label";
private Color m_ProgressColor;
// This is the label that displays the percentage.
private Label m_Label;
// This is the number that the Label displays as a percentage.
private float m_Progress;
// A value between 0 and 100
public float progress
{
// The progress property is exposed in C#.
get => m_Progress;
set
{
// Whenever the progress property changes, MarkDirtyRepaint() is named. This causes a call to the
// generateVisualContents callback.
m_Progress = value;
MarkDirtyRepaint();
}
}
// This default constructor is RadialProgress's only constructor.
public DashBoardProgress()
{
// Add the USS class name for the overall control.
AddToClassList(ussClassName);
// Register a callback to generate the visual content of the control.
generateVisualContent += GenerateVisualContent;
progress = 0.5f;
}
private void GenerateVisualContent(MeshGenerationContext context)
{
var width = contentRect.width;
var height = contentRect.height;
var painter = context.painter2D;
painter.lineWidth = resolvedStyle.unityTextOutlineWidth;
painter.lineCap = LineCap.Butt;
var startAngle = -220f;
//startAngle = Mathf.Lerp(-90f, 480f, nextProgress);
//var endAngle = 360.0f * (progress) + startAngle - 30;
var endAngle = 40f;
endAngle = Mathf.Lerp(startAngle, endAngle, progress);
//计算progress
// Draw the track
painter.strokeColor = resolvedStyle.unityTextOutlineColor;
painter.BeginPath();
painter.Arc(new Vector2(width * 0.5f, height * 0.5f), width*0.5f - painter.lineWidth/2f, startAngle, 40f);
painter.Stroke();
// Draw the progress Out
painter.strokeColor = resolvedStyle.color;
painter.BeginPath();
painter.Arc(new Vector2(width * 0.5f, height * 0.5f), width*0.5f - painter.lineWidth/2f, startAngle, endAngle);
painter.Stroke();
}
public void SetValueWithoutNotify(float newValue)
{
value = newValue;
}
public float value
{
get => progress;
set => progress = value;
}
}
}