Working on the progrart functions.
This commit is contained in:
24
Progrart.Core/Graphics/BaseElement.cs
Normal file
24
Progrart.Core/Graphics/BaseElement.cs
Normal 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) { }
|
||||
}
|
||||
}
|
||||
65
Progrart.Core/Graphics/Line.cs
Normal file
65
Progrart.Core/Graphics/Line.cs
Normal 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,
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Progrart.Core/Graphics/ShaderType.cs
Normal file
13
Progrart.Core/Graphics/ShaderType.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Progrart.Core.Graphics
|
||||
{
|
||||
public enum ShaderType
|
||||
{
|
||||
LinearGradient,
|
||||
RadialGradient,
|
||||
Picture,
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
16
Progrart.Core/JSExecution/ProgrartConversion.cs
Normal file
16
Progrart.Core/JSExecution/ProgrartConversion.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
));
|
||||
engine.Engine.SetValue("color4", new Func<JsNumber, JsNumber, JsNumber, JsNumber, JsObject>((r, g, b, a) =>
|
||||
{
|
||||
return ProgrartFunctions.color(this
|
||||
, r.AsNumber()
|
||||
, g.AsNumber()
|
||||
, b.AsNumber()
|
||||
, a.AsNumber()
|
||||
);
|
||||
}
|
||||
));
|
||||
engine.Engine.SetValue("color3", new Func<JsNumber, JsNumber, JsNumber, JsObject>((r, g, b) =>
|
||||
{
|
||||
return ProgrartFunctions.color(this
|
||||
, r.AsNumber()
|
||||
, g.AsNumber()
|
||||
, b.AsNumber()
|
||||
);
|
||||
}
|
||||
));
|
||||
SetupCalls();
|
||||
}
|
||||
public RenderContext RenderImage(int Scale, string script, Dictionary<string, string> arguments)
|
||||
public void SetupCalls()
|
||||
{
|
||||
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()
|
||||
);
|
||||
}
|
||||
public JsObject color3(JsNumber r, JsNumber g, JsNumber b)
|
||||
{
|
||||
return ProgrartFunctions.color(engine.Engine
|
||||
, r.AsNumber()
|
||||
, g.AsNumber()
|
||||
, b.AsNumber()
|
||||
);
|
||||
}
|
||||
public JsObject visual_root()
|
||||
{
|
||||
return ProgrartFunctions.CreateVisualRoot(this);
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user