diff --git a/Directory.Packages.props b/Directory.Packages.props index fb4fc9e..351377d 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -23,6 +23,7 @@ + diff --git a/Progrart.Core/Graphics/VisualRoot.cs b/Progrart.Core/Graphics/VisualRoot.cs new file mode 100644 index 0000000..40e9a3d --- /dev/null +++ b/Progrart.Core/Graphics/VisualRoot.cs @@ -0,0 +1,27 @@ +using Jint.Native; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Progrart.Core.Graphics +{ + public class BaseElement + { + public JsObject? __object = null; + public virtual void Add(BaseElement element) { } + public virtual void Remove(BaseElement element) { } + public virtual void Render(RenderContext context) + { + } + public virtual void SetProperty(string key, string value) + { + + } + } + public class ImageRoot : BaseElement + { + } + public class VisualRoot : BaseElement + { + } +} diff --git a/Progrart.Core/JSExecution/ProgrartExecutor.cs b/Progrart.Core/JSExecution/ProgrartExecutor.cs new file mode 100644 index 0000000..8326b98 --- /dev/null +++ b/Progrart.Core/JSExecution/ProgrartExecutor.cs @@ -0,0 +1,69 @@ +using Jint; +using Jint.Native; +using Jint.Native.Function; +using Progrart.Core.Graphics; + +namespace Progrart.Core.JSExecution +{ + public class ProgrartExecutor + { + public ExecutionEngine engine; + public Dictionary ObjectPool = new(); + public ProgrartExecutor() + { + engine = new ExecutionEngine(); + engine.Engine.SetValue("visual_root", new Func(() => + { + return ProgrartFunctions.CreateVisualRoot(this); + } + )); + engine.Engine.SetValue("color4", new Func((r, g, b, a) => + { + return ProgrartFunctions.color(this + , r.AsNumber() + , g.AsNumber() + , b.AsNumber() + , a.AsNumber() + ); + } + )); + engine.Engine.SetValue("color3", new Func((r, g, b) => + { + return ProgrartFunctions.color(this + , r.AsNumber() + , g.AsNumber() + , b.AsNumber() + ); + } + )); + } + public RenderContext RenderImage(int Scale, string script, Dictionary arguments) + { + int width = 1; + int height = 1; + foreach (var item in arguments) + { + engine.Engine.SetValue(item.Key, item.Value); + } + if (engine.Engine.GetValue("Width") is JsNumber js_width) + { + width = (int)(js_width.AsNumber()); + } + if (engine.Engine.GetValue("Height") is JsNumber js_height) + { + height = (int)(js_height.AsNumber()); + } + RenderContext renderContext = new RenderContext(width * Scale, width * Scale); + ImageRoot imageRoot = new ImageRoot(); + engine.Execute(script); + var img = engine.Engine.Call("main"); + imageRoot.Add(ObjectPool[$"{img.Get("id")}"]); + imageRoot.Render(renderContext); + return renderContext; + } + public int RegisterObject(object obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Progrart.Core/JSExecution/ProgrartFunctions.cs b/Progrart.Core/JSExecution/ProgrartFunctions.cs new file mode 100644 index 0000000..eeab8cf --- /dev/null +++ b/Progrart.Core/JSExecution/ProgrartFunctions.cs @@ -0,0 +1,45 @@ +using Jint.Native; +using Progrart.Core.Graphics; + +namespace Progrart.Core.JSExecution +{ + public static class ProgrartFunctions + { + public static JsObject WrapObject(ProgrartExecutor executor, int Handle) + { + var obj = new JsObject(executor.engine.Engine); + string handle_str = $"{Handle}"; + obj.Set("id", handle_str); + obj.Set("add", JsValue.FromObject(executor.engine.Engine, new Action((obj) => + { + executor.ObjectPool[handle_str].Add(executor.ObjectPool[$"{obj.Get("id")}"]); + }))); + return obj; + } + public static JsObject color(ProgrartExecutor executor, double r, double g, double b, double a) + { + var obj = new JsObject(executor.engine.Engine); + obj.Set("r", r); + obj.Set("g", g); + obj.Set("b", b); + obj.Set("a", a); + return obj; + } + public static JsObject color(ProgrartExecutor executor, double r, double g, double b) + { + var obj = new JsObject(executor.engine.Engine); + obj.Set("r", r); + obj.Set("g", g); + obj.Set("b", b); + obj.Set("a", 1); + return obj; + } + public static JsObject CreateVisualRoot(ProgrartExecutor executor) + { + VisualRoot root = new(); + var obj = WrapObject(executor, executor.RegisterObject(root)); + root.__object = obj; + return obj; + } + } +} diff --git a/Progrart.Core/Progrart.Core.csproj b/Progrart.Core/Progrart.Core.csproj index 1b9edc4..d7dbea6 100644 --- a/Progrart.Core/Progrart.Core.csproj +++ b/Progrart.Core/Progrart.Core.csproj @@ -8,8 +8,7 @@ - - + diff --git a/Progrart.Core/RenderContext.cs b/Progrart.Core/RenderContext.cs index b42a5fd..6c7d5e8 100644 --- a/Progrart.Core/RenderContext.cs +++ b/Progrart.Core/RenderContext.cs @@ -1,16 +1,43 @@ -using Microsoft.Maui.Graphics.Skia; +using SkiaSharp; namespace Progrart.Core { - public class RenderContext - { - SkiaBitmapExportContext context; + public class RenderContext + { + public PrimitiveDrawingCore DrawingCore { get; } - public void Init() + public SKCanvas canvas { get => DrawingCore.canvas; } + public RenderContext(PrimitiveDrawingCore core) { + this.DrawingCore = core; + } + public RenderContext(int W, int H) + { + DrawingCore = new PrimitiveDrawingCore(W, H); } } + public class PrimitiveDrawingCore : IDisposable + { + internal bool isDisposed = false; + SKSurface surface; + SKImageInfo info; + public SKCanvas canvas { get; } + public PrimitiveDrawingCore(int W, int H) + { + info = new SKImageInfo(W, H); + surface = SKSurface.Create(info); + canvas = surface.Canvas; + } + public void Dispose() + { + if (isDisposed) return; + //GC.SuppressFinalize(this); + isDisposed = true; + surface.Dispose(); + } + } + [Serializable] public class RenderConfig { diff --git a/Progrart/Pages/AboutPage.axaml.cs b/Progrart/Pages/AboutPage.axaml.cs index a521682..4432eb3 100644 --- a/Progrart/Pages/AboutPage.axaml.cs +++ b/Progrart/Pages/AboutPage.axaml.cs @@ -1,7 +1,10 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; +using Avalonia.Media; +using Avalonia.Media.Imaging; using Progrart.Controls.TabSystem; +using SkiaSharp; namespace Progrart.Pages; @@ -10,9 +13,10 @@ public partial class AboutPage : UserControl,ITabPage public AboutPage() { InitializeComponent(); - } - public void BindButton(TabButton button) + } + + public void BindButton(TabButton button) { button.Title="About"; } diff --git a/Progrart/Views/MainView.axaml b/Progrart/Views/MainView.axaml index b9f0e5e..b2f2cb2 100644 --- a/Progrart/Views/MainView.axaml +++ b/Progrart/Views/MainView.axaml @@ -11,12 +11,6 @@ x:Class="Progrart.Views.MainView"> - - - - - -