100 lines
2.4 KiB
C#
100 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using SkiaSharp;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.UX
|
|
{
|
|
public class SKPaintComponent : MonoBehaviour
|
|
{
|
|
[SerializeField] private Texture2D texture;
|
|
[SerializeField] private float[] data;
|
|
|
|
[BIT]
|
|
private void Draw()
|
|
{
|
|
var info = new SKImageInfo(
|
|
width: 384,
|
|
height: 128,
|
|
colorType: SKColorType.Rgba8888,
|
|
alphaType: SKAlphaType.Premul
|
|
);
|
|
|
|
using var surface = SKSurface.Create(info);
|
|
|
|
|
|
using var canvas = surface.Canvas;
|
|
|
|
//canvas.Clear(new Color32(31,31,31,255).ToSKColor());
|
|
|
|
using var linePaint = new SKPaint();
|
|
linePaint.Color = new SKColor(255, 0, 0, 255);
|
|
linePaint.StrokeWidth = 8;
|
|
linePaint.IsAntialias = true;
|
|
linePaint.Style = SKPaintStyle.Stroke;
|
|
|
|
using var helpLinePaint = new SKPaint();
|
|
helpLinePaint.Color = new SKColor(200, 200, 200, 200);
|
|
helpLinePaint.StrokeWidth = 4;
|
|
helpLinePaint.Style = SKPaintStyle.Stroke;
|
|
|
|
using var textPaint = new SKPaint();
|
|
textPaint.TextAlign = SKTextAlign.Center;
|
|
textPaint.TextSize = 14;
|
|
textPaint.ColorF = new SKColor(0, 255, 0, 255);
|
|
|
|
using var filePaint = new SKPaint();
|
|
filePaint.Color = new SKColor(200, 200, 200, 200);
|
|
filePaint.Style=SKPaintStyle.Fill;
|
|
|
|
var min = data.Min();
|
|
var max = data.Max();
|
|
|
|
DoubleBuffer<SKPoint> buffer = new();
|
|
|
|
var path = new SKPath { FillType = SKPathFillType.EvenOdd };
|
|
|
|
path.MoveTo(0,0);
|
|
path.LineTo(0,0);
|
|
|
|
for (var i = 0; i < data.Length; i++)
|
|
{
|
|
var value = data[i];
|
|
var posX = (float)info.Width / (data.Length - 1) * (i);
|
|
var d = max - min;
|
|
var p = (value - min) / d;
|
|
var poxY = info.Height * p;
|
|
|
|
var currentPoint = new SKPoint(posX, poxY);
|
|
if (buffer.TryGetRelease(out var previousPoint))
|
|
{
|
|
canvas.DrawLine(previousPoint, currentPoint, linePaint);
|
|
}
|
|
|
|
canvas.DrawText(
|
|
value.ToString()
|
|
, currentPoint
|
|
, new SKFont(
|
|
SKTypeface.FromFile(@"D:\Iris\Documents\GitHub\iFactory-YL106.Unity\Assets\BITKit\Unity\Art\Fonts\TTF\Roboto\Roboto-Regular.ttf")
|
|
), textPaint);
|
|
canvas.DrawLine(posX, 0, posX, poxY, helpLinePaint);
|
|
|
|
path.LineTo(posX,poxY);
|
|
|
|
buffer.Release(currentPoint);
|
|
}
|
|
|
|
path.LineTo(info.Width,0);
|
|
path.Close();
|
|
|
|
|
|
//canvas.DrawPath(path,filePaint);
|
|
|
|
texture = info.ToTexture2D(surface);
|
|
}
|
|
}
|
|
}
|
|
|