Added UI Toolkit Progress

This commit is contained in:
CortexCore
2024-12-13 16:14:20 +08:00
parent 21b4f9091a
commit d502501b27
20 changed files with 660 additions and 134 deletions

View File

@@ -7,8 +7,6 @@
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:6de01b04fa4e14662b03fa46366da151",
"GUID:f19bbd83e3c264a5680926bf75d7e494",
"GUID:1c2aa13aa706ffc49a1a0044cad55436"
],
"includePlatforms": [],
@@ -17,9 +15,7 @@
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"_SkiaSharp"
],
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -5,14 +5,13 @@ using System.Globalization;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SkiaSharp;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class SkiaChart : VisualElement
public class LineChart : VisualElement
{
public new class UxmlTraits : VisualElement.UxmlTraits
{
@@ -30,27 +29,21 @@ namespace BITKit.UX
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
var x = (SkiaChart)ve;
var x = (LineChart)ve;
x.Json = m_JsonAttribute.GetValueFromBag(bag, cc);
x.AllowWarning = m_allowWarningsAttribute.GetValueFromBag(bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<SkiaChart, UxmlTraits>
public new class UxmlFactory : UxmlFactory<LineChart, UxmlTraits>
{
}
public SkiaChart()
public LineChart()
{
RegisterCallback<GeometryChangedEvent>(x =>
{
if (visible)
{
Rebuild();
}
});
RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
style.flexDirection = FlexDirection.Row;
_dataContainer = this.Create<VisualElement>();
@@ -61,6 +54,26 @@ namespace BITKit.UX
_dataContainer.name = "Data";
_chartContainer.name = "Chart";
_chartContainer.generateVisualContent+= GenerateVisualContent;
_chartContainer.style.marginBottom = 0;
_chartContainer.style.marginLeft = 8;
_chartContainer.style.marginRight = 8;
_chartContainer.style.marginTop = 0 ;
pickingMode = PickingMode.Ignore;
_dataContainer.pickingMode = PickingMode.Ignore;
_chartContainer.style.flexDirection = FlexDirection.RowReverse;
_dataContainer.style.flexDirection = FlexDirection.ColumnReverse;
_chartContainer.RegisterCallback<PointerMoveEvent>(OnPointerMove);
}
private void OnPointerMove(PointerMoveEvent evt)
{
//_chartContainer.tooltip = evt.localPosition.ToString();
}
private void OnCustomStyleResolved(CustomStyleResolvedEvent evt)
@@ -71,7 +84,16 @@ namespace BITKit.UX
_secondaryColor = helpLineColor;
}
Rebuild();
for (int i = 0; i < 8; i++)
{
if (evt.customStyle.TryGetValue(new CustomStyleProperty<Color>($"--chart-line-color--{i}"),
out var lineColor))
{
_lineColors[i] = lineColor;
}
}
MarkDirtyRepaint();
}
public string Json
@@ -80,7 +102,7 @@ namespace BITKit.UX
set
{
_json = value;
Rebuild();
GenerateLayout();
}
}
@@ -93,7 +115,7 @@ namespace BITKit.UX
set
{
_secondaryColor = value;
Rebuild();
GenerateLayout();
}
}
@@ -102,12 +124,76 @@ namespace BITKit.UX
private readonly VisualElement _dataContainer;
private readonly VisualElement _chartContainer;
private void Rebuild()
private readonly Color[] _lineColors = new[]
{
Color.red,
Color.red,
Color.red,
Color.red,
Color.red,
Color.red,
Color.red,
Color.red,
};
private void GenerateLayout()
{
JArray[] array;
float[] data;
try
{
array = JsonConvert.DeserializeObject<JArray[]>(Json);
data = array.SelectMany(x => x.ToObject<float[]>()).ToArray();
}
catch (Exception e)
{
if (AllowWarning)
Debug.LogException(e);
return;
}
if (data is null or { Length: 0 })
{
if (AllowWarning)
BIT4Log.Log<LineChart>("Data is null or empty");
return;
}
//假如min是x,max是y,将中间的差用data.Length划分
var max = data.Max();
var min = data.Min();
var difference = max - min;
var maxLength = array.Select(x => x.Count).Max();
max += difference / maxLength;
min -= difference / maxLength;
//Debug.Log($"min:{min} max:{max} difference:{difference} max:{max}");
_dataContainer.Clear();
for (var i = 0; i <= maxLength; i++)
{
var label = _dataContainer.Create<Label>();
//var value = (max - min) / (data.Length - 1) * i + min;
var t = (float)i / maxLength;
var value = Mathf.Lerp(min, max, t);
label.text = Math.Round(value, 2).ToString();
}
MarkDirtyRepaint();
}
private void GenerateVisualContent(MeshGenerationContext context)
{
if (float.IsNaN(layout.width) || float.IsNaN(layout.height))
{
if (AllowWarning)
BIT4Log.Log<SkiaChart>("Width or height is NaN");
BIT4Log.Log<LineChart>("Width or height is NaN");
return;
}
@@ -128,139 +214,82 @@ namespace BITKit.UX
if (data is null or { Length: 0 })
{
if (AllowWarning)
BIT4Log.Log<SkiaChart>("Data is null or empty");
BIT4Log.Log<LineChart>("Data is null or empty");
return;
}
_dataContainer.Clear();
_chartContainer.Clear();
// _chartContainer.style.marginBottom = 8;
// _chartContainer.style.marginLeft = 8;
// _chartContainer.style.marginRight = 8;
// _chartContainer.style.marginTop = 8;
_dataContainer.style.flexDirection = FlexDirection.ColumnReverse;
//假如min是x,max是y,将中间的差用data.Length划分
var max = data.Max();
var min = data.Min();
var difference = max - min;
var maxLength = array.Select(x => x.Count).Max();
max = max + difference / maxLength;
min = min - difference / maxLength;
max += difference / maxLength;
min -= difference / maxLength;
difference = max - min;
//Debug.Log($"min:{min} max:{max} difference:{difference} max:{max}");
for (var i = 0; i <= maxLength; i++)
{
var label = _dataContainer.Create<Label>();
//var value = (max - min) / (data.Length - 1) * i + min;
var t = (float)i / maxLength;
var value = Mathf.Lerp(min,max,t);
label.text = Math.Round(value, 2).ToString();
}
if (_chartContainer.layout.width is float.NaN or < 10 || _chartContainer.layout.height is float.NaN or < 10)
{
if (AllowWarning)
BIT4Log.Log<SkiaChart>("Width or height is NaN or less than 10");
BIT4Log.Log<LineChart>("Width or height is NaN or less than 10");
return;
}
var info = new SKImageInfo(
width: (int)_chartContainer.layout.width,
height: (int)_chartContainer.layout.height,
colorType: SKColorType.Rgba8888,
alphaType: SKAlphaType.Premul
);
using var surface = SKSurface.Create(info);
using var canvas = surface.Canvas;
using var linePaint = new SKPaint();
linePaint.StrokeWidth = resolvedStyle.unityTextOutlineWidth;
linePaint.IsAntialias = true;
linePaint.Style = SKPaintStyle.Stroke;
using var helpLinePaint = new SKPaint();
helpLinePaint.Color = resolvedStyle.unityTextOutlineColor.ToSKColor();
helpLinePaint.StrokeWidth = 1;
helpLinePaint.Style = SKPaintStyle.Stroke;
using var fillPaint = new SKPaint();
fillPaint.Color = new SKColor(200, 200, 200, 200);
fillPaint.Style = SKPaintStyle.Fill;
var linePaint = context.painter2D;
linePaint.lineWidth = resolvedStyle.unityTextOutlineWidth;
var width = contentRect.width;
var height = contentRect.height;
for (var index = 0; index < array.Length; index++)
{
var _array = array[index];
DoubleBuffer<SKPoint> buffer = new();
var row = array[index];
DoubleBuffer<Vector2> buffer = new();
data = row.ToObject<float[]>();
data = _array.ToObject<float[]>();
var lastWidth = 0f;
var path = new SKPath { FillType = SKPathFillType.EvenOdd };
linePaint.strokeColor = _lineColors[index];
var tempColor = _lineColors[index];
tempColor.a *= 0.2f;
linePaint.fillColor = tempColor;
path.MoveTo(0, 0);
path.LineTo(0, 0);
if (_array.Count is not 1 && customStyle.TryGetValue(new CustomStyleProperty<Color>($"--color-{index}"),
out var _myStyle))
linePaint.BeginPath();
linePaint.MoveTo(new(0,height));
linePaint.lineJoin = LineJoin.Round;
for (var i = 0; i < data.Length; i++)
{
linePaint.Color = _myStyle.ToSKColor();
}
else
{
linePaint.Color = resolvedStyle.color.ToSKColor();
}
for (var i = 1; i <= data.Length; i++)
{
var value = data[i-1];
var posX = (float)info.Width / data.Length * (i);
var value = data[i];
var posX = width / data.Length * i;
var p = (value - min) / difference;
var poxY = info.Height * p;
var poxY = height-height * p;
var currentPoint = new SKPoint(posX, poxY);
var currentPoint = new Vector2(posX, poxY);
if (buffer.TryGetRelease(out var previousPoint))
{
canvas.DrawLine(previousPoint, currentPoint, linePaint);
linePaint.LineTo(previousPoint);
}
if (i == data.Length)
{
var label = _chartContainer.Create<Label>();
label.text = Math.Round(value, 2).ToString(CultureInfo.InvariantCulture);
label.style.position = Position.Absolute;
label.style.left = posX; //-label.layout.width/2;
label.style.bottom = poxY; //- label.layout.height/2;
//label.transform.position = new Vector3(-label.layout.width/2, poxY, 0);
}
canvas.DrawLine(posX, 0, posX, poxY, helpLinePaint);
path.LineTo(posX, poxY);
linePaint.LineTo(currentPoint);
buffer.Release(currentPoint);
if(i == data.Length-1)
{
lastWidth = posX;
}
}
path.LineTo(info.Width, 0);
path.Close();
linePaint.LineTo(new (lastWidth,height));
linePaint.ClosePath();
linePaint.Stroke();
linePaint.Fill();
}
var texture = info.ToTexture2D(surface);
_chartContainer.style.backgroundImage = texture;
}
}
}