This commit is contained in:
CortexCore 2024-04-22 03:46:46 +08:00
parent 9bf8b2a1ea
commit 16f5c49b3d
7 changed files with 164 additions and 102 deletions

View File

@ -8,7 +8,8 @@
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:6de01b04fa4e14662b03fa46366da151",
"GUID:f19bbd83e3c264a5680926bf75d7e494"
"GUID:f19bbd83e3c264a5680926bf75d7e494",
"GUID:1c2aa13aa706ffc49a1a0044cad55436"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -1,99 +0,0 @@
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);
}
}
}

View File

@ -1,5 +1,5 @@
{
"name": "BITKit.UX.Chart.Tests",
"name": "BITKit.UX.Painter.Runtime",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
@ -9,7 +9,7 @@
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:6de01b04fa4e14662b03fa46366da151",
"GUID:f19bbd83e3c264a5680926bf75d7e494",
"GUID:994a3fb33a5627740b0712e7c483cc1f"
"GUID:1c2aa13aa706ffc49a1a0044cad55436"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets.Kcp;
using SkiaSharp;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class SkiaPainter : VisualElement
{
public new class UxmlFactory : UxmlFactory<SkiaPainter, UxmlTraits> { }
public SkiaPainter()
{
RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
RegisterCallback<MouseDownEvent>(OnMouseDown);
RegisterCallback<MouseUpEvent>(OnMouseUp);
RegisterCallback<MouseMoveEvent>(OnMouseMove);
}
private bool _isPainting;
private readonly List<Vector2> _points = new();
private SKImageInfo _info;
private SKCanvas _canvas;
private SKSurface _surface;
private Rect _rect;
private readonly SKPaint _linePaint = new()
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
StrokeWidth = 5
};
public void ClearCanvas()
{
_points.Clear();
Rebuild();
}
private void OnMouseMove(MouseMoveEvent evt)
{
if (_isPainting is false) return;
var pos = evt.localMousePosition;
pos.y = layout.height - pos.y;
if (_rect.Contains(pos) is false)
{
var last = _points.LastOrDefault();
if (last == default) return;
_points.Add(default);
return;
}
_points.Add(pos);
Rebuild();
}
private void OnMouseUp(MouseUpEvent evt)
{
_isPainting = false;
var last = _points.LastOrDefault();
if (last != default)
_points.Add(default);
}
private void OnMouseDown(MouseDownEvent evt)
{
_isPainting = true;
}
private void OnGeometryChanged(GeometryChangedEvent evt)
{
if(float.IsNaN(layout.width) || float.IsNaN(layout.height))
return;
_linePaint.Color = resolvedStyle.color.ToSKColor();
_surface?.Dispose();
_info = new SKImageInfo((int)layout.width, (int)layout.height);
_surface = SKSurface.Create(_info);
_canvas = _surface.Canvas;
_rect = new Rect(0, 0, layout.width, layout.height);
Rebuild();
}
private void Rebuild()
{
if (_canvas is null) return;
_canvas.Clear();
DoubleBuffer<Vector2> buffer = new();
foreach (var pos in _points)
{
if (pos == default)
{
buffer.Clear();
}
else
{
if (buffer.TryGetRelease(out var previousPoint))
{
_canvas.DrawLine(previousPoint.x, previousPoint.y, pos.x, pos.y, _linePaint);
}
buffer.Release(pos);
}
}
var texture = _info.ToTexture2D(_surface);
style.backgroundImage = texture;
}
public string Base64 => _surface?.GetBase64();
}
}

View File

@ -314,3 +314,6 @@ Label {
.armorPlate-container:hover {
background-color: rgba(255, 255, 255, 0.78);
}
.flex-center {
}

View File

@ -0,0 +1,24 @@
{
"name": "BITKit.Extensions.SkiaSharp",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:6de01b04fa4e14662b03fa46366da151",
"GUID:f19bbd83e3c264a5680926bf75d7e494"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"_SkiaSharp"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@ -1,5 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SkiaSharp;
using UnityEngine;
@ -31,5 +34,31 @@ namespace BITKit.UX
{
return new SKColor(color.r, color.g, color.b, color.a);
}
public static string GetBase64(this SKSurface self)
{
try
{
using var image = self.Snapshot();
using var data = image.Encode(SKEncodedImageFormat.Png, 50);
//using var stream = File.OpenWrite(exportPath.Value);
var ms = new MemoryStream();
// save the data to a stream
data.SaveTo(ms);
var bytes = ms.ToArray();
var base64 = Convert.ToBase64String(bytes);
return "data:image/jpeg;base64," + base64;
}
catch (Exception e)
{
StringBuilder exceptionBuilder = new();
exceptionBuilder.AppendLine($"Surface:{self is not null}");
BIT4Log.LogException( new InGameException(exceptionBuilder.ToString(),e));
throw;
}
}
}
}