diff --git a/Directory.Packages.props b/Directory.Packages.props index 6328d7f..e7bf014 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -19,13 +19,13 @@ --> - + - + diff --git a/Progrart.Core/Graphics/Circle.cs b/Progrart.Core/Graphics/Circle.cs index 29cfb69..a0a4477 100644 --- a/Progrart.Core/Graphics/Circle.cs +++ b/Progrart.Core/Graphics/Circle.cs @@ -5,7 +5,7 @@ using SkiaSharp; namespace Progrart.Core.Graphics { - public class Circle : BaseElement + public class Circle : BaseElement { float StrokeWidth; diff --git a/Progrart.Core/Graphics/Text.cs b/Progrart.Core/Graphics/Text.cs new file mode 100644 index 0000000..a9109fd --- /dev/null +++ b/Progrart.Core/Graphics/Text.cs @@ -0,0 +1,105 @@ +using Jint; +using Jint.Native; +using Progrart.Core.JSExecution; +using SkiaSharp; + +namespace Progrart.Core.Graphics +{ + public class Text : BaseElement + { + + float StrokeWidth; + SKPoint Position; + float Size; + SKColorF Color; + string str = ""; + bool IsStroke; + SKShader? shader = null; + string? fontFamily = null; + public override void SetupProperties(Engine engine) + { + base.SetupProperties(engine); + if (__object != null) + { + { + JsObject point = new JsObject(engine); + point.Set("x", 0); + point.Set("y", 0); + __object.Set("Position", point); + } + __object.Set("StrokeWidth", 1); + __object.Set("Size", 1); + __object.Set("Text", ""); + __object.Set("IsStroke", true); + __object.Set("Color", ProgrartFunctions.color(engine, 1, 1, 1, 1)); + + } + + } + public override void LoadProperties() + { + if (__object is not null) + { + StrokeWidth = (float)__object.Get("StrokeWidth").AsNumber(); + str = __object.Get("Text").AsString(); + Size = (float)__object.Get("Size").AsNumber(); + IsStroke = (bool)__object.Get("IsStroke").AsBoolean(); + { + if (__object.Get("Position") is JsObject Start) + { + this.Position = ProgrartConversion.ObtainSKPointFromJsObject(Start); + } + } + { + if (__object.Get("Color") is JsObject Color) + { + this.Color = ProgrartConversion.ObtainSKColorFFromJsObject(Color); + } + } + + if (__object.Get("Shader") is JsObject shaderObj) + shader = ProgrartConversion.ObtainFromJsObject(shaderObj); + if (__object.Get("Font") is JsString fontObj) + fontFamily = fontObj.AsString(); + } + } + public override void Render(RenderContext context) + { + base.Render(context); + LoadProperties(); + SKPoint pos = context.TranslatePoint(Position); + float Size = context.TranslateSize(this.Size); + if (fontFamily is not null) + { + var face = context.GetFont(fontFamily); + if (face != null) + { + + context.canvas.DrawText(str, + pos.X, pos.Y, SKTextAlign.Center, new SKFont(face, Size) { }, + new SKPaint() + { + ColorF = Color, + StrokeWidth = context.TranslateSize(StrokeWidth), + Shader = shader, + IsStroke = IsStroke, + IsAntialias = true + } + ); + return; + } + } + context.canvas.DrawText(str, + pos.X, pos.Y, SKTextAlign.Center, new SKFont(SKTypeface.Default, Size) { }, + new SKPaint() + { + ColorF = Color, + StrokeWidth = context.TranslateSize(StrokeWidth), + Shader = shader, + IsStroke = IsStroke, + IsAntialias = true + } + ); + } + } +} diff --git a/Progrart.Core/Graphics/VisualRoot.cs b/Progrart.Core/Graphics/VisualRoot.cs index 5ea9fd4..dd255cf 100644 --- a/Progrart.Core/Graphics/VisualRoot.cs +++ b/Progrart.Core/Graphics/VisualRoot.cs @@ -25,7 +25,7 @@ namespace Progrart.Core.Graphics float rotate; void Transform(RenderContext context) { - Trace.WriteLine($"Visual Root: Rotation:{rotate}"); + //Trace.WriteLine($"Visual Root: Rotation:{rotate}"); context.canvas.Translate(tx, ty); context.canvas.RotateDegrees(rotate, context.DrawingCore.Width / 2, context.DrawingCore.Height / 2); if (scale != 1) diff --git a/Progrart.Core/JSExecution/ExecutionEngine.cs b/Progrart.Core/JSExecution/ExecutionEngine.cs index 51cca06..26057bd 100644 --- a/Progrart.Core/JSExecution/ExecutionEngine.cs +++ b/Progrart.Core/JSExecution/ExecutionEngine.cs @@ -1,5 +1,6 @@ using Jint; using Jint.Native; +using Jint.Runtime.Modules; using System; using System.Collections.Generic; using System.Diagnostics; @@ -43,6 +44,7 @@ namespace Progrart.Core.JSExecution JsObject _obj = new JsObject(Engine); Engine.SetValue("math", _obj); Random r = new Random(); + _obj.Set("random", JsObject.FromObject(Engine, new Func(() => { return r.NextDouble(); diff --git a/Progrart.Core/JSExecution/ProgrartExecutor.cs b/Progrart.Core/JSExecution/ProgrartExecutor.cs index f8a205b..c290e63 100644 --- a/Progrart.Core/JSExecution/ProgrartExecutor.cs +++ b/Progrart.Core/JSExecution/ProgrartExecutor.cs @@ -29,6 +29,26 @@ namespace Progrart.Core.JSExecution else Trace.WriteLine(v); })); + engine.Engine.SetValue("require", (string str) => + { + require(str, () => + { + if (str.IndexOf(".") < 0) + { + require(str + ".progrart", () => + { + require(str + ".js", () => + { + throw new Exception("Module not found: " + str); + }); + }); + } + else + { + throw new Exception("Module not found: " + str); + } + }); + }); engine.Engine.SetValue("visual_root", visual_root); engine.Engine.SetValue("line", line); engine.Engine.SetValue("rectangle", rectangle); @@ -40,10 +60,26 @@ namespace Progrart.Core.JSExecution engine.Engine.SetValue("circle", circle); engine.Engine.SetValue("color4", color4); engine.Engine.SetValue("color3", color3); + engine.Engine.SetValue("text", text); engine.Engine.SetValue("color_hex", color_hex); engine.Engine.SetValue("linear_gradient", linear_gradient); engine.Engine.SetValue("radial_gradient", radial_gradient); } + + private void require(string str, Action onNoFound) + { + var task = StorageProvider.TryOpenRead(str); + task.Wait(); + var stream = task.GetAwaiter().GetResult(); + if (stream is null) + { + onNoFound(); + return; + } + using var sr = new StreamReader(stream); + engine.Engine.Execute(sr.ReadToEnd()); + } + public JsObject color4(JsNumber r, JsNumber g, JsNumber b, JsNumber a) { return ProgrartFunctions.color(engine.Engine @@ -93,6 +129,7 @@ namespace Progrart.Core.JSExecution public JsObject circle() => ProgrartFunctions.CreateElement(this); public JsObject oval() => ProgrartFunctions.CreateElement(this); public JsObject triangle() => ProgrartFunctions.CreateElement(this); + public JsObject text() => ProgrartFunctions.CreateElement(this); public RenderContext RenderImage(int Scale, string script, ExecuteArguments arguments) { diff --git a/Progrart.Core/PrimitiveDrawingCore.cs b/Progrart.Core/PrimitiveDrawingCore.cs index 7c916a4..d500ce2 100644 --- a/Progrart.Core/PrimitiveDrawingCore.cs +++ b/Progrart.Core/PrimitiveDrawingCore.cs @@ -4,7 +4,7 @@ using System.Diagnostics; namespace Progrart.Core { - public class PrimitiveDrawingCore : IDisposable + public class PrimitiveDrawingCore : IDisposable { internal bool isDisposed = false; SKSurface surface; @@ -13,6 +13,26 @@ namespace Progrart.Core public readonly int Height; public SKCanvas canvas { get; } internal IStorageProvider StorageProvider; + Dictionary Fonts = new Dictionary(); + public SKTypeface? GetFont(string fontName) + { + if (Fonts.ContainsKey(fontName)) + return Fonts[fontName]; + else + { + var task=StorageProvider.TryOpenRead(fontName); + task.Wait(); + var stream=task.Result; + if(stream != null) + { + + SKTypeface typeface = SKTypeface.FromStream(stream); + Fonts[fontName] = typeface; + return typeface; + } + } + return null; + } public PrimitiveDrawingCore(int W, int H, IStorageProvider storageProvider) { Width = W; diff --git a/Progrart.Core/RenderContext.cs b/Progrart.Core/RenderContext.cs index 8dbfa26..cbf7132 100644 --- a/Progrart.Core/RenderContext.cs +++ b/Progrart.Core/RenderContext.cs @@ -14,6 +14,10 @@ namespace Progrart.Core { this.DrawingCore = core; } + public SKTypeface? GetFont(string fontName) + { + return DrawingCore.GetFont(fontName); + } public SKPoint TranslatePoint(float x, float y) { return new SKPoint(x * DrawingCore.Width, y * DrawingCore.Height); diff --git a/Progrart.slnx b/Progrart.slnx new file mode 100644 index 0000000..2b08454 --- /dev/null +++ b/Progrart.slnx @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/Progrart/Controls/ConfigEditor.axaml b/Progrart/Controls/ConfigEditor.axaml index a25aa2b..365b2c3 100644 --- a/Progrart/Controls/ConfigEditor.axaml +++ b/Progrart/Controls/ConfigEditor.axaml @@ -4,8 +4,7 @@ xmlns:local="using:Progrart.Controls" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - x:Class="Progrart.Controls.ConfigEditor"> - + x:Class="Progrart.Controls.ConfigEditor" Margin="5"> @@ -31,7 +30,7 @@ Targets - + diff --git a/Progrart/Controls/ConfigEditor.axaml.cs b/Progrart/Controls/ConfigEditor.axaml.cs index 863b173..d6fa1cd 100644 --- a/Progrart/Controls/ConfigEditor.axaml.cs +++ b/Progrart/Controls/ConfigEditor.axaml.cs @@ -1,11 +1,16 @@ using Avalonia.Controls; +using AvaloniaEdit.Editing; namespace Progrart.Controls; public partial class ConfigEditor : UserControl { - public ConfigEditor() - { - InitializeComponent(); - } + public ConfigEditor() + { + InitializeComponent(); + AddButton.Click += (_, _) => + { + TargetHolder.Children.Add(new TargetEditor()); + }; + } } \ No newline at end of file diff --git a/Progrart/Controls/ImageViewer.axaml b/Progrart/Controls/ImageViewer.axaml index fbea71e..2ca347b 100644 --- a/Progrart/Controls/ImageViewer.axaml +++ b/Progrart/Controls/ImageViewer.axaml @@ -5,7 +5,8 @@ xmlns:paz="using:Avalonia.Controls.PanAndZoom" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Progrart.Controls.ImageViewer"> - + - diff --git a/Progrart/Controls/TargetEditor.axaml b/Progrart/Controls/TargetEditor.axaml new file mode 100644 index 0000000..9f1e960 --- /dev/null +++ b/Progrart/Controls/TargetEditor.axaml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/Progrart/Controls/TargetEditor.axaml.cs b/Progrart/Controls/TargetEditor.axaml.cs new file mode 100644 index 0000000..7690aa1 --- /dev/null +++ b/Progrart/Controls/TargetEditor.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace Progrart; + +public partial class TargetEditor : UserControl +{ + public TargetEditor() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/Progrart/Progrart.csproj b/Progrart/Progrart.csproj index 63ff767..e70eee8 100644 --- a/Progrart/Progrart.csproj +++ b/Progrart/Progrart.csproj @@ -36,4 +36,10 @@ + + + + TargetEditor.axaml + +