diff --git a/Assets/BITKit/Unity/Scripts/UX/Chart/BITKit.UX.Chart.Runtime.asmdef b/Assets/BITKit/Unity/Scripts/UX/Chart/BITKit.UX.Chart.Runtime.asmdef index b6b8f3940..863e43917 100644 --- a/Assets/BITKit/Unity/Scripts/UX/Chart/BITKit.UX.Chart.Runtime.asmdef +++ b/Assets/BITKit/Unity/Scripts/UX/Chart/BITKit.UX.Chart.Runtime.asmdef @@ -8,7 +8,8 @@ "GUID:49b49c76ee64f6b41bf28ef951cb0e50", "GUID:d8b63aba1907145bea998dd612889d6b", "GUID:6de01b04fa4e14662b03fa46366da151", - "GUID:f19bbd83e3c264a5680926bf75d7e494" + "GUID:f19bbd83e3c264a5680926bf75d7e494", + "GUID:1c2aa13aa706ffc49a1a0044cad55436" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Assets/BITKit/Unity/Scripts/UX/Chart/Test/SKPaintComponent.cs b/Assets/BITKit/Unity/Scripts/UX/Chart/Test/SKPaintComponent.cs deleted file mode 100644 index 86875066d..000000000 --- a/Assets/BITKit/Unity/Scripts/UX/Chart/Test/SKPaintComponent.cs +++ /dev/null @@ -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 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); - } - } -} - diff --git a/Assets/BITKit/Unity/Scripts/UX/Chart/Test/BITKit.UX.Chart.Tests.asmdef b/Assets/BITKit/Unity/Scripts/UX/Painter/BITKit.UX.Painter.Runtime.asmdef similarity index 88% rename from Assets/BITKit/Unity/Scripts/UX/Chart/Test/BITKit.UX.Chart.Tests.asmdef rename to Assets/BITKit/Unity/Scripts/UX/Painter/BITKit.UX.Painter.Runtime.asmdef index 27085d9b6..086a9147c 100644 --- a/Assets/BITKit/Unity/Scripts/UX/Chart/Test/BITKit.UX.Chart.Tests.asmdef +++ b/Assets/BITKit/Unity/Scripts/UX/Painter/BITKit.UX.Painter.Runtime.asmdef @@ -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": [], diff --git a/Assets/BITKit/Unity/Scripts/UX/Painter/SkiaPainter.cs b/Assets/BITKit/Unity/Scripts/UX/Painter/SkiaPainter.cs new file mode 100644 index 000000000..382c0a863 --- /dev/null +++ b/Assets/BITKit/Unity/Scripts/UX/Painter/SkiaPainter.cs @@ -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 { } + public SkiaPainter() + { + RegisterCallback(OnGeometryChanged); + RegisterCallback(OnMouseDown); + RegisterCallback(OnMouseUp); + RegisterCallback(OnMouseMove); + } + private bool _isPainting; + private readonly List _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 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(); + } +} + diff --git a/Assets/BITKit/Unity/UX/Common/Common.uss b/Assets/BITKit/Unity/UX/Common/Common.uss index ab2f7f7d4..e56cbbcf1 100644 --- a/Assets/BITKit/Unity/UX/Common/Common.uss +++ b/Assets/BITKit/Unity/UX/Common/Common.uss @@ -314,3 +314,6 @@ Label { .armorPlate-container:hover { background-color: rgba(255, 255, 255, 0.78); } + +.flex-center { +} diff --git a/Assets/BITKit/UnityPluginsSupport/SkiaSharp/BITKit.Extensions.SkiaSharp.asmdef b/Assets/BITKit/UnityPluginsSupport/SkiaSharp/BITKit.Extensions.SkiaSharp.asmdef new file mode 100644 index 000000000..25e21a165 --- /dev/null +++ b/Assets/BITKit/UnityPluginsSupport/SkiaSharp/BITKit.Extensions.SkiaSharp.asmdef @@ -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 +} \ No newline at end of file diff --git a/Assets/BITKit/Unity/Scripts/UX/Chart/SkiaExtensions.cs b/Assets/BITKit/UnityPluginsSupport/SkiaSharp/SkiaExtensions.cs similarity index 61% rename from Assets/BITKit/Unity/Scripts/UX/Chart/SkiaExtensions.cs rename to Assets/BITKit/UnityPluginsSupport/SkiaSharp/SkiaExtensions.cs index f849a7872..c466618ba 100644 --- a/Assets/BITKit/Unity/Scripts/UX/Chart/SkiaExtensions.cs +++ b/Assets/BITKit/UnityPluginsSupport/SkiaSharp/SkiaExtensions.cs @@ -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; + } + + } } }