Added find and load bitmap feature in PrimitiveDrawingCore.
Added a PixImage element.
This commit is contained in:
135
Progrart.Core/Graphics/PixImage.cs
Normal file
135
Progrart.Core/Graphics/PixImage.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using Jint;
|
||||
using Jint.Native;
|
||||
using Progrart.Core.JSExecution;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Progrart.Core.Graphics
|
||||
{
|
||||
public class PixImage : BaseElement
|
||||
{
|
||||
|
||||
float StrokeWidth;
|
||||
SKPoint Start;
|
||||
SKPoint Size;
|
||||
SKColorF Color;
|
||||
SKRect sourceRect;
|
||||
bool useClip = false;
|
||||
bool IsStroke;
|
||||
string? imgFile = null;
|
||||
SKShader? shader = 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);
|
||||
}
|
||||
{
|
||||
JsObject point = new JsObject(engine);
|
||||
point.Set("x", 0);
|
||||
point.Set("y", 0);
|
||||
point.Set("w", 0);
|
||||
point.Set("h", 0);
|
||||
__object.Set("Clip", point);
|
||||
}
|
||||
__object.Set("StrokeWidth", 1);
|
||||
__object.Set("IsStroke", true);
|
||||
__object.Set("UseClip", false);
|
||||
__object.Set("Source", "");
|
||||
__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("Size", point);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public override void LoadProperties()
|
||||
{
|
||||
if (__object is not null)
|
||||
{
|
||||
StrokeWidth = (float)__object.Get("StrokeWidth").AsNumber();
|
||||
IsStroke = (bool)__object.Get("IsStroke").AsBoolean();
|
||||
useClip = (bool)__object.Get("UseClip").AsBoolean();
|
||||
{
|
||||
if (__object.Get("Position") is JsObject Start)
|
||||
{
|
||||
this.Start = ProgrartConversion.ObtainSKPointFromJsObject(Start);
|
||||
}
|
||||
}
|
||||
{
|
||||
if (__object.Get("Size") is JsObject End)
|
||||
{
|
||||
this.Size = ProgrartConversion.ObtainSKPointFromJsObject(End);
|
||||
}
|
||||
}
|
||||
{
|
||||
if (__object.Get("Clip") is JsObject End)
|
||||
{
|
||||
this.sourceRect = ProgrartConversion.ObtainSKRectFromJsObject_XYWH_Style(End);
|
||||
}
|
||||
}
|
||||
{
|
||||
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("Source") is JsString source)
|
||||
this.imgFile = source.AsString();
|
||||
}
|
||||
}
|
||||
public override void Render(RenderContext context)
|
||||
{
|
||||
base.Render(context);
|
||||
LoadProperties();
|
||||
SKPoint FinalStartPos = context.TranslatePoint(Start);
|
||||
SKPoint Size = context.TranslatePoint(this.Size);
|
||||
if (imgFile == null) return;
|
||||
var bitmap = context.GetBitmap(imgFile);
|
||||
if (bitmap == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
SKRect dest = new(FinalStartPos.X, FinalStartPos.Y, FinalStartPos.X + Size.X, FinalStartPos.Y + Size.Y);
|
||||
if (useClip)
|
||||
{
|
||||
context.canvas.DrawBitmap(bitmap, sourceRect, dest,
|
||||
new SKPaint()
|
||||
{
|
||||
ColorF = Color,
|
||||
StrokeWidth = context.TranslateSize(StrokeWidth),
|
||||
Shader = shader,
|
||||
IsStroke = IsStroke,
|
||||
IsAntialias = true
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.canvas.DrawBitmap(bitmap, new SKRect(0, 0, bitmap.Width, bitmap.Height),
|
||||
dest,
|
||||
new SKPaint()
|
||||
{
|
||||
ColorF = Color,
|
||||
StrokeWidth = context.TranslateSize(StrokeWidth),
|
||||
Shader = shader,
|
||||
IsStroke = IsStroke,
|
||||
IsAntialias = true
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,22 @@ namespace Progrart.Core.JSExecution
|
||||
{
|
||||
return new SKPoint((float)jsObject.Get("x").AsNumber(), (float)jsObject.Get("y").AsNumber());
|
||||
}
|
||||
public static SKRect ObtainSKRectFromJsObject(JsObject jsObject)
|
||||
{
|
||||
return new SKRect((float)jsObject.Get("x").AsNumber(),
|
||||
(float)jsObject.Get("y").AsNumber(),
|
||||
(float)jsObject.Get("w").AsNumber(),
|
||||
(float)jsObject.Get("h").AsNumber()
|
||||
);
|
||||
}
|
||||
public static SKRect ObtainSKRectFromJsObject_XYWH_Style(JsObject jsObject)
|
||||
{
|
||||
float x = (float)jsObject.Get("x").AsNumber();
|
||||
float y = (float)jsObject.Get("y").AsNumber();
|
||||
float w = (float)jsObject.Get("w").AsNumber();
|
||||
float h = (float)jsObject.Get("h").AsNumber();
|
||||
return new SKRect(x, y, x + w, y + h);
|
||||
}
|
||||
public static SKColorF ObtainSKColorFFromJsObject(JsObject jsObject)
|
||||
{
|
||||
var r = (float)jsObject.Get("r").AsNumber();
|
||||
|
||||
@@ -61,7 +61,9 @@ namespace Progrart.Core.JSExecution
|
||||
engine.Engine.SetValue("path", path);
|
||||
engine.Engine.SetValue("path_element", path_element);
|
||||
engine.Engine.SetValue("circle", circle);
|
||||
engine.Engine.SetValue("color4", color4);
|
||||
engine.Engine.SetValue("circle", circle);
|
||||
engine.Engine.SetValue("piximage", piximage);
|
||||
engine.Engine.SetValue("piximg", piximage);
|
||||
engine.Engine.SetValue("color3", color3);
|
||||
engine.Engine.SetValue("text", text);
|
||||
engine.Engine.SetValue("color_hex", color_hex);
|
||||
@@ -124,6 +126,7 @@ namespace Progrart.Core.JSExecution
|
||||
{
|
||||
return ProgrartFunctions.CreateVisualRoot(this);
|
||||
}
|
||||
public JsObject piximage() => ProgrartFunctions.CreateElement<PixImage>(this);
|
||||
public JsObject line() => ProgrartFunctions.CreateElement<Line>(this);
|
||||
public JsObject rectangle() => ProgrartFunctions.CreateElement<Rectangle>(this);
|
||||
public JsObject roundrect() => ProgrartFunctions.CreateElement<RoundRectangle>(this);
|
||||
|
||||
@@ -14,6 +14,24 @@ namespace Progrart.Core
|
||||
public SKCanvas canvas { get; }
|
||||
internal IStorageProvider StorageProvider;
|
||||
Dictionary<string, SKTypeface> Fonts = new Dictionary<string, SKTypeface>();
|
||||
Dictionary<string, SKBitmap> Bitmaps = new Dictionary<string, SKBitmap>();
|
||||
public SKBitmap? GetBitmap(string sourceFile)
|
||||
{
|
||||
if(Bitmaps.ContainsKey(sourceFile))return Bitmaps[sourceFile];
|
||||
else
|
||||
{
|
||||
var task = StorageProvider.TryOpenRead(sourceFile);
|
||||
task.Wait();
|
||||
using var stream = task.Result;
|
||||
if (stream != null)
|
||||
{
|
||||
SKBitmap bitmap = SKBitmap.Decode(stream);
|
||||
Bitmaps[sourceFile] = bitmap;
|
||||
return bitmap;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public SKTypeface? GetFont(string fontName)
|
||||
{
|
||||
if (Fonts.ContainsKey(fontName))
|
||||
|
||||
@@ -14,6 +14,10 @@ namespace Progrart.Core
|
||||
{
|
||||
this.DrawingCore = core;
|
||||
}
|
||||
public SKBitmap? GetBitmap(string sourceFile)
|
||||
{
|
||||
return DrawingCore.GetBitmap(sourceFile);
|
||||
}
|
||||
public SKTypeface? GetFont(string fontName)
|
||||
{
|
||||
return DrawingCore.GetFont(fontName);
|
||||
|
||||
Reference in New Issue
Block a user