1
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "BITKit.UX.Painter.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:d525ad6bd40672747bde77962f1c401e",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b",
|
||||
"GUID:6de01b04fa4e14662b03fa46366da151",
|
||||
"GUID:f19bbd83e3c264a5680926bf75d7e494",
|
||||
"GUID:1c2aa13aa706ffc49a1a0044cad55436"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"_SkiaSharp"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5cbf6465b63af3e4da531a4e4d26376b
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
150
Src/Unity/Scripts/UX/Painter/SkiaPainter.cs
Normal file
150
Src/Unity/Scripts/UX/Painter/SkiaPainter.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
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 Optional<SKColor> _backgroundColor=new ();
|
||||
// private readonly SKPaint _linePaint = new()
|
||||
// {
|
||||
// Style = SKPaintStyle.Stroke,
|
||||
// Color = SKColors.Red,
|
||||
// StrokeWidth = 5,
|
||||
// };
|
||||
private readonly SKPaint _linePaint = new SKPaint
|
||||
{
|
||||
Style = SKPaintStyle.Stroke,
|
||||
Color = SKColors.Black, // 笔画颜色为黑色
|
||||
StrokeCap = SKStrokeCap.Round, // 笔画末端采用圆形
|
||||
StrokeJoin = SKStrokeJoin.Round, // 笔画连接处采用圆形
|
||||
StrokeWidth = 8, // 笔画宽度
|
||||
IsAntialias = true // 使用抗锯齿
|
||||
};
|
||||
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;
|
||||
|
||||
var last = _points.LastOrDefault();
|
||||
|
||||
if (_rect.Contains(pos) is false)
|
||||
{
|
||||
|
||||
if (last == default) return;
|
||||
|
||||
_points.Add(default);
|
||||
return;
|
||||
}
|
||||
|
||||
var originalPos = pos;
|
||||
if (last != default)
|
||||
{
|
||||
//pos = Vector2.Lerp(last, pos, 0.1f);
|
||||
|
||||
_points.Add(Vector2.Lerp(last, pos, 0.1f));
|
||||
_points.Add(Vector2.Lerp(last, pos, 0.2f));
|
||||
//_points.Add(Vector2.Lerp(last, pos, 1f));
|
||||
}
|
||||
else
|
||||
{
|
||||
_points.Add(pos);
|
||||
}
|
||||
|
||||
//_points.Add(pos);
|
||||
|
||||
if (originalPos != pos)
|
||||
{
|
||||
//_points.Add(originalPos);
|
||||
}
|
||||
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) || layout.width < 16 || float.IsNaN(layout.height) || layout.height<16)
|
||||
return;
|
||||
_linePaint.Color = resolvedStyle.color.ToSKColor();
|
||||
_surface?.Dispose();
|
||||
_canvas?.Dispose();
|
||||
_info = new SKImageInfo((int)layout.width, (int)layout.height);
|
||||
_info.AlphaType = SKAlphaType.Premul;
|
||||
|
||||
_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(resolvedStyle.backgroundColor.ToSKColor());
|
||||
DoubleBuffer<Vector2> buffer = new();
|
||||
|
||||
_linePaint.StrokeWidth = 4;
|
||||
|
||||
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();
|
||||
public byte[] Bytes => _surface?.GetBytes();
|
||||
}
|
||||
}
|
||||
|
11
Src/Unity/Scripts/UX/Painter/SkiaPainter.cs.meta
Normal file
11
Src/Unity/Scripts/UX/Painter/SkiaPainter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 380b69a479ae4d546a730e8c8ca136e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user