Working on the progrart functions.

This commit is contained in:
Creeper Lv
2025-12-25 23:00:26 +11:00
parent 2d2fec0bed
commit 18b7b00fc3
17 changed files with 400 additions and 96 deletions

View File

@@ -0,0 +1,24 @@
using Jint;
using Jint.Native;
namespace Progrart.Core.Graphics
{
public class BaseElement
{
public List<BaseElement> Children = new List<BaseElement>();
public JsObject? __object = null;
public virtual void Add(BaseElement element)
{
Children.Add(element);
}
public virtual void Remove(BaseElement element)
{
Children.Remove(element);
}
public virtual void Render(RenderContext context)
{
}
public virtual void LoadProperties() { }
public virtual void SetupProperties(Engine engine) { }
}
}

View File

@@ -0,0 +1,65 @@
using Jint;
using Jint.Native;
using Progrart.Core.JSExecution;
using SkiaSharp;
namespace Progrart.Core.Graphics
{
public class Line : BaseElement
{
float Size;
float StartX;
float StartY;
float EndX;
float EndY;
float ColorR;
float ColorG;
float ColorB;
float ColorA;
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("Start", point);
}
__object.Set("Size", 1);
__object.Set("Color", ProgrartFunctions.color(engine, 1, 1, 1, 1));
{
JsObject point = new JsObject(engine);
point.Set("x", 0);
point.Set("y", 0);
__object.Set("End", point);
}
}
}
public override void LoadProperties()
{
if (__object is not null)
{
Size = (float)__object.Get("Size").AsNumber();
}
}
public override void Render(RenderContext context)
{
base.Render(context);
context.DrawingCore.canvas.DrawLine(
context.TranslatePoint((float)StartX, (float)StartY),
context.TranslatePoint((float)StartX, (float)StartY),
new SkiaSharp.SKPaint()
{
ColorF = new SkiaSharp.SKColorF(ColorR, ColorG, ColorB, ColorA),
StrokeWidth = Size,
}
);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Progrart.Core.Graphics
{
public enum ShaderType
{
LinearGradient,
RadialGradient,
Picture,
}
}

View File

@@ -1,27 +1,44 @@
using Jint.Native;
using System;
using System.Collections.Generic;
using System.Text;
using Jint;
using Jint.Native;
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
{
public override void Render(RenderContext context)
{
base.Render(context);
foreach (var item in Children)
{
item.Render(context);
}
}
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("translate", point);
}
{
JsObject point = new JsObject(engine);
point.Set("x", 0);
point.Set("y", 0);
__object.Set("scale", point);
}
{
__object.Set("rotation", 0);
}
__object.Set("Width", 1);
__object.Set("Height", 1);
}
}
}
}

View File

@@ -27,10 +27,6 @@ namespace Progrart.Core.JSExecution
{
Engine.Evaluate(formSymbol(Symbols));
Engine.Evaluate(content);
if (Engine.GetValue("config").AsObject().TryGetValue("width", out var w))
{
Trace.WriteLine(w);
}
}
public void Dispose()

View File

@@ -0,0 +1,16 @@
using Jint.Native;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Text;
namespace Progrart.Core.JSExecution
{
public static class ProgrartConversion
{
public static SKShader? ObtainFromJsObject(JsObject jsObject)
{
return null;
}
}
}

View File

@@ -2,46 +2,69 @@
using Jint.Native;
using Jint.Native.Function;
using Progrart.Core.Graphics;
using System.Diagnostics;
using System.Text.Json;
namespace Progrart.Core.JSExecution
{
public class ProgrartExecutor
public class ProgrartExecutor : IDisposable
{
public ExecutionEngine engine;
public Dictionary<string, BaseElement> ObjectPool = new();
public Dictionary<string, object> ObjectPool = new();
public ProgrartExecutor()
{
engine = new ExecutionEngine();
engine.Engine.SetValue("visual_root", new Func<JsObject>(() =>
{
return ProgrartFunctions.CreateVisualRoot(this);
SetupCalls();
}
));
engine.Engine.SetValue("color4", new Func<JsNumber, JsNumber, JsNumber, JsNumber, JsObject>((r, g, b, a) =>
public void SetupCalls()
{
return ProgrartFunctions.color(this
engine.Engine.SetValue("visual_root", visual_root);
Jint.Native.Json.JsonSerializer serializer = new Jint.Native.Json.JsonSerializer(engine.Engine);
engine.Engine.SetValue("log", new Action<JsValue>((v) =>
{
if (v is JsObject obj)
{
Trace.WriteLine(serializer.Serialize(obj));
}
else
Trace.WriteLine(v);
}));
engine.Engine.SetValue("line", line);
engine.Engine.SetValue("color4", color4);
engine.Engine.SetValue("color3", color3);
engine.Engine.SetValue("linear_gradient", linear_gradient);
engine.Engine.SetValue("radial_gradient", radial_gradient);
}
public JsObject color4(JsNumber r, JsNumber g, JsNumber b, JsNumber a)
{
return ProgrartFunctions.color(engine.Engine
, r.AsNumber()
, g.AsNumber()
, b.AsNumber()
, a.AsNumber()
);
}
));
engine.Engine.SetValue("color3", new Func<JsNumber, JsNumber, JsNumber, JsObject>((r, g, b) =>
public JsObject color3(JsNumber r, JsNumber g, JsNumber b)
{
return ProgrartFunctions.color(this
return ProgrartFunctions.color(engine.Engine
, r.AsNumber()
, g.AsNumber()
, b.AsNumber()
);
}
));
public JsObject visual_root()
{
return ProgrartFunctions.CreateVisualRoot(this);
}
public RenderContext RenderImage(int Scale, string script, Dictionary<string, string> arguments)
public JsObject line()
{
return ProgrartFunctions.CreateLine(this);
}
public RenderContext RenderImage(int Scale, string script, ExecuteArguments arguments)
{
int width = 1;
int height = 1;
foreach (var item in arguments)
foreach (var item in arguments.data)
{
engine.Engine.SetValue(item.Key, item.Value);
}
@@ -55,15 +78,25 @@ namespace Progrart.Core.JSExecution
}
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")}"]);
if (ObjectPool[$"{img.Get("id")}"] is BaseElement element)
imageRoot.Add(element);
imageRoot.Render(renderContext);
return renderContext;
}
public int RegisterObject(object obj)
public string RegisterObject(object obj)
{
return obj.GetHashCode();
string v = $"{obj.GetHashCode()}";
ObjectPool[v] = obj;
return v;
}
public JsObject linear_gradient()=>ProgrartFunctions.linear_gradient(engine.Engine);
public JsObject radial_gradient()=>ProgrartFunctions.radial_gradient(engine.Engine);
public void Dispose()
{
engine.Dispose();
}
}
}

View File

@@ -1,33 +1,115 @@
using Jint.Native;
using Jint;
using Jint.Native;
using Progrart.Core.Graphics;
using SkiaSharp;
namespace Progrart.Core.JSExecution
{
public static class ProgrartFunctions
{
public static JsObject WrapObject(ProgrartExecutor executor, int Handle)
public static JsObject WrapObject(ProgrartExecutor executor, string 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<JsObject>((obj) =>
{
executor.ObjectPool[handle_str].Add(executor.ObjectPool[$"{obj.Get("id")}"]);
if (executor.ObjectPool[handle_str] is BaseElement self)
{
if (executor.ObjectPool[$"{obj.Get("id")}"] is BaseElement element)
{
self.Add(element);
return;
}
else
throw new Exception($"Object {obj} is not a Progrart Element!");
}
else
throw new Exception($"Object with id \"{handle_str}\" is not a Progrart Element!");
})));
return obj;
}
public static JsObject color(ProgrartExecutor executor, double r, double g, double b, double a)
public static JsObject linear_gradient(Engine engine)
{
var obj = new JsObject(executor.engine.Engine);
var obj = new JsObject(engine);
{
JsObject point = new JsObject(engine);
point.Set("x", 0);
point.Set("y", 0);
obj.Set("Start", point);
}
{
JsObject point = new JsObject(engine);
point.Set("x", 0);
point.Set("y", 0);
obj.Set("End", point);
}
{
obj.Set("Colors", new JsArray(engine, [
color(engine, 1, 1, 1, 1) ,
color(engine, 1, 1, 1, 1)
]
));
}
{
obj.Set("Colors", new JsArray(engine, [
color(engine, 1, 1, 1, 1) ,
color(engine, 1, 1, 1, 1)
]
));
}
{
obj.Set("Positions", new JsArray(engine, new[] { new JsNumber(0), new JsNumber(1) }));
}
obj.Set("TileMode", $"{SKShaderTileMode.Repeat}");
obj.Set("Type", $"{ShaderType.LinearGradient}");
return obj;
}
public static JsObject radial_gradient(Engine engine)
{
var obj = new JsObject(engine);
{
JsObject point = new JsObject(engine);
point.Set("x", 0);
point.Set("y", 0);
obj.Set("Center", point);
}
{
obj.Set("Colors", new JsArray(engine, [
color(engine, 1, 1, 1, 1) ,
color(engine, 1, 1, 1, 1)
]
));
}
{
obj.Set("Colors", new JsArray(engine, [
color(engine, 1, 1, 1, 1) ,
color(engine, 1, 1, 1, 1)
]
));
}
{
obj.Set("Positions", new JsArray(engine, new[] { new JsNumber(0), new JsNumber(1) }));
}
obj.Set("TileMode", $"{SKShaderTileMode.Repeat}");
obj.Set("Type", $"{ShaderType.RadialGradient}");
return obj;
}
public static JsObject color(Engine engine, double r, double g, double b, double a)
{
var obj = new JsObject(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)
public static JsObject color(Engine engine, double r, double g, double b)
{
var obj = new JsObject(executor.engine.Engine);
var obj = new JsObject(engine);
obj.Set("r", r);
obj.Set("g", g);
obj.Set("b", b);
@@ -39,6 +121,15 @@ namespace Progrart.Core.JSExecution
VisualRoot root = new();
var obj = WrapObject(executor, executor.RegisterObject(root));
root.__object = obj;
root.SetupProperties(executor.engine.Engine);
return obj;
}
public static JsObject CreateLine(ProgrartExecutor executor)
{
Line root = new();
var obj = WrapObject(executor, executor.RegisterObject(root));
root.__object = obj;
root.SetupProperties(executor.engine.Engine);
return obj;
}
}

View File

@@ -11,7 +11,10 @@ namespace Progrart.Core
{
this.DrawingCore = core;
}
public SKPoint TranslatePoint(float x, float y)
{
return new SKPoint(x * DrawingCore.Width, y * DrawingCore.Height);
}
public RenderContext(int W, int H)
{
DrawingCore = new PrimitiveDrawingCore(W, H);
@@ -22,9 +25,13 @@ namespace Progrart.Core
internal bool isDisposed = false;
SKSurface surface;
SKImageInfo info;
public readonly int Width;
public readonly int Height;
public SKCanvas canvas { get; }
public PrimitiveDrawingCore(int W, int H)
{
Width = W;
Height = H;
info = new SKImageInfo(W, H);
surface = SKSurface.Create(info);
canvas = surface.Canvas;
@@ -38,6 +45,10 @@ namespace Progrart.Core
}
}
public class ExecuteArguments
{
public Dictionary<string, string> data = new Dictionary<string, string>();
}
[Serializable]
public class RenderConfig
{

View File

@@ -10,6 +10,17 @@ public partial class TabHost : UserControl
{
InitializeComponent();
}
public ITabPage? GetCurrentPage()
{
foreach (var item in PageContainer.Children)
{
if (item.IsVisible)
{
if (item is ITabPage page) return page;
}
}
return null;
}
public void AddPage(ITabPage page)
{
TabButton button = new TabButton(page, this);

View File

@@ -6,6 +6,7 @@ using Avalonia.Threading;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using Progrart.Controls.TabSystem;
using Progrart.Core;
using Progrart.Icons;
using System.IO;
using System.Threading.Tasks;

View File

@@ -1,6 +1,7 @@
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Progrart.Controls.TabSystem;
using Progrart.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;

View File

@@ -2,8 +2,4 @@
namespace Progrart.Pages
{
public class ExecuteArguments
{
public Dictionary<string, string> data = new Dictionary<string, string>();
}
}

View File

@@ -5,6 +5,7 @@ using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Progrart.Controls.TabSystem;
using Progrart.Core;
using System.Threading.Tasks;
namespace Progrart.Pages;

View File

@@ -4,13 +4,16 @@ using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Progrart.Controls.TabSystem;
using Progrart.Core;
using Progrart.Core.JSExecution;
using Progrart.Pages;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace Progrart;
public partial class ProgrartEditorPage : UserControl, ITabPage,IEditorPage
public partial class ProgrartEditorPage : UserControl, ITabPage, IEditorPage
{
IStorageFile? file = null;
TabButton? btn = null;
@@ -33,6 +36,16 @@ public partial class ProgrartEditorPage : UserControl, ITabPage,IEditorPage
public void Execute(ExecuteArguments? args = null)
{
using ProgrartExecutor executor = new ProgrartExecutor();
try
{
executor.RenderImage(100, CodeEditor.Text, new ExecuteArguments());
}
catch (System.Exception e)
{
Trace.WriteLine(e);
}
}
public bool IsModified()

View File

@@ -168,10 +168,24 @@
</Grid.RowDefinitions>
<Border CornerRadius="12" Margin="8" BorderBrush="#8888" BorderThickness="1,1,1,0" Background="#4888">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="8,4">Output</TextBlock>
<StackPanel Grid.Column="1" Margin="8,0" VerticalAlignment="Center" Orientation="Horizontal">
<Button CornerRadius="8" Name="OutputClear" Click="OutputClear_Click">
<Viewbox Height="12">
<PathIcon Data="F1 M 8.52 5.799999 L 11.52 5.799999 C 11.52 5.373333 11.373333 5.013332 11.08 4.719999 C 10.786666 4.426666 10.426666 4.286667 10 4.299999 C 9.573333 4.313334 9.219999 4.459999 8.94 4.74 C 8.66 5.02 8.52 5.373333 8.52 5.799999 Z M 7.52 5.799999 C 7.52 5.106667 7.759999 4.52 8.24 4.039999 C 8.719999 3.560001 9.306666 3.313334 10 3.299999 C 10.693333 3.286667 11.28 3.526667 11.759999 4.02 C 12.239999 4.513334 12.479999 5.106667 12.48 5.799999 L 17.52 5.799999 C 17.653332 5.799999 17.766666 5.846666 17.859999 5.94 C 17.953331 6.033334 18 6.153334 18 6.299999 C 18 6.446667 17.953331 6.566668 17.859999 6.66 C 17.766666 6.753334 17.653332 6.8 17.52 6.799999 L 16.440001 6.799999 L 15.24 17.16 C 15.159999 17.906666 14.833332 18.533333 14.259999 19.039999 C 13.686666 19.546665 13.026666 19.799999 12.28 19.799999 L 7.72 19.799999 C 6.973333 19.799999 6.313333 19.546665 5.74 19.039999 C 5.166667 18.533333 4.84 17.906666 4.76 17.16 L 3.56 6.799999 L 2.52 6.799999 C 2.36 6.8 2.233333 6.753334 2.14 6.66 C 2.046667 6.566668 2 6.446667 2 6.299999 C 2 6.153334 2.046667 6.033334 2.14 5.94 C 2.233333 5.846666 2.36 5.799999 2.52 5.799999 Z M 5.76 17.039999 C 5.813333 17.546665 6.026666 17.966665 6.4 18.299999 C 6.773333 18.633333 7.213333 18.799999 7.72 18.799999 L 12.28 18.799999 C 12.786666 18.799999 13.226665 18.633333 13.599999 18.299999 C 13.973333 17.966665 14.186666 17.546665 14.24 17.039999 L 15.44 6.799999 L 4.56 6.799999 Z M 8.52 9.28 C 8.653333 9.306667 8.766666 9.366667 8.86 9.459999 C 8.953333 9.553333 9 9.666667 9 9.799999 L 9 15.799999 C 9 15.933332 8.953333 16.053333 8.86 16.16 C 8.766666 16.266666 8.646667 16.313332 8.5 16.299999 C 8.353333 16.286667 8.233333 16.233334 8.139999 16.139999 C 8.046666 16.046665 8 15.933332 8 15.799999 L 8 9.799999 C 8 9.666667 8.046666 9.546667 8.139999 9.44 C 8.233333 9.333334 8.346666 9.280001 8.48 9.28 Z M 12 9.799999 C 12 9.666667 11.953333 9.546667 11.86 9.44 C 11.766666 9.333334 11.646666 9.280001 11.5 9.28 C 11.353333 9.280001 11.233333 9.333334 11.139999 9.44 C 11.046666 9.546667 11 9.666667 11 9.799999 L 11 15.799999 C 11 15.933332 11.046666 16.046665 11.139999 16.139999 C 11.233333 16.233334 11.353333 16.286667 11.5 16.299999 C 11.646666 16.313332 11.766666 16.266666 11.86 16.16 C 11.953333 16.053333 12 15.933332 12 15.799999 Z " VerticalAlignment="Center" HorizontalAlignment="Center" />
</Viewbox>
</Button>
</StackPanel>
</Grid>
</Border>
<ScrollViewer Grid.Row="1">
<TextBlock Name="Output"/>
<TextBlock Name="Output" TextWrapping="Wrap"/>
</ScrollViewer>
</Grid>
</Border>

View File

@@ -11,26 +11,12 @@ namespace Progrart.Views;
public partial class MainView : UserControl
{
ExecutionEngine engine;
static MainView? Instance;
public MainView()
{
Instance = this;
InitializeComponent();
engine = new ExecutionEngine();
//engine.Engine.SetValue("log", new Action<string>((s) => { Output.Text += $"{s}\n"; }));
RunButton.Click += (_, _) =>
{
try
{
//engine.Execute(CodePane.Text);
}
catch (Exception e)
{
WriteLine(e.Message);
}
};
//Trace.Listeners.Add(new ConsoleLogger());
Trace.Listeners.Add(new ConsoleLogger());
EditorProvider.setHost(MainTabHost);
BottomPanelToggle.IsCheckedChanged += (a, b) =>
{
@@ -89,11 +75,24 @@ public partial class MainView : UserControl
LeftPanelToggle.IsChecked = false;
};
LeftPanelToggle.IsChecked = true;
RunButton.Click += (_, _) =>
{
if (MainTabHost.GetCurrentPage() is IEditorPage editor)
{
editor.Execute(null);
}
};
}
private void OutputClear_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
Output.Text = "";
}
public void Write(string message)
{
//Dispatcher.UIThread.Invoke(() =>
//Output.Text += message);
Dispatcher.UIThread.Invoke(() =>
Output.Text += message);
try
{
Output.Text += message;
@@ -104,10 +103,12 @@ public partial class MainView : UserControl
}
public void WriteLine(string message)
{
//Dispatcher.UIThread.Invoke(() =>);
try
Dispatcher.UIThread.Invoke(() =>
{
Output.Text += $"{message}\n";
});
try
{
}
catch (Exception)
{