diff --git a/LuaScript.Tests/ScriptDirectiveTests.cs b/LuaScript.Tests/ScriptDirectiveTests.cs index 735394a..433592c 100644 --- a/LuaScript.Tests/ScriptDirectiveTests.cs +++ b/LuaScript.Tests/ScriptDirectiveTests.cs @@ -37,6 +37,9 @@ namespace LuaScript.Tests [InlineData("local pd = obj.getpixeldata()", "Native")] [InlineData("--!moonsharp\nobj.setpixel(0,0,1,1,1)", "MoonSharp")] [InlineData("--!native\nobj.rz = 1", "Native")] + [InlineData("obj.load(\"figure\", \"円\", 0xff0000, 100)", "MoonSharp")] + [InlineData("obj.load(\"figure\", \"円\", 0xff0000, 100)\nlocal r = obj.getpixel(0,0)", "MoonSharp")] + [InlineData("--!native\nobj.load(\"figure\", \"円\", 0xff0000, 100)", "Native")] public void ResolveAuto_RoutesPixelScriptsToNative(string script, string expected) { Assert.Equal(expected, ScriptDirective.ResolveAuto(script).ToString()); diff --git a/LuaScript/AviUtlScriptContext.cs b/LuaScript/AviUtlScriptContext.cs index 2b82c38..4ed5064 100644 --- a/LuaScript/AviUtlScriptContext.cs +++ b/LuaScript/AviUtlScriptContext.cs @@ -63,8 +63,10 @@ namespace LuaScript private byte[]? _pixelBuffer; private bool _pixelBufferLoaded; private bool _isPixelsDirty; + private bool _bufferReplaced; public bool IsPixelsDirty => _isPixelsDirty; + public bool BufferReplaced => _bufferReplaced; internal int TotalChannels => ImageWidth * ImageHeight * 4; @@ -76,6 +78,17 @@ namespace LuaScript _pixelBuffer = null; _pixelBufferLoaded = false; _isPixelsDirty = false; + _bufferReplaced = false; + } + + internal void ReplaceBuffer(byte[] buffer, int width, int height) + { + _pixelBuffer = buffer; + _pixelBufferLoaded = true; + _isPixelsDirty = true; + _bufferReplaced = true; + ImageWidth = width; + ImageHeight = height; } internal void EnsurePixelBuffer() diff --git a/LuaScript/Engine/ScriptEngineKind.cs b/LuaScript/Engine/ScriptEngineKind.cs index 53d69ee..3b55c42 100644 --- a/LuaScript/Engine/ScriptEngineKind.cs +++ b/LuaScript/Engine/ScriptEngineKind.cs @@ -19,12 +19,22 @@ namespace LuaScript.Engine { if (TryGetDirective(script, out var kind)) return kind; + if (UsesDrawingApi(script)) + return ScriptEngineKind.MoonSharp; return UsesPixelApi(script) ? ScriptEngineKind.Native : ScriptEngineKind.MoonSharp; } public static bool UsesPixelApi(string? script) => script is not null && (script.Contains("getpixel", StringComparison.Ordinal) || script.Contains("setpixel", StringComparison.Ordinal)); + public static bool UsesDrawingApi(string? script) => + script is not null && ( + script.Contains("obj.load", StringComparison.Ordinal) || + script.Contains("obj.draw", StringComparison.Ordinal) || + script.Contains("obj.effect", StringComparison.Ordinal) || + script.Contains("obj.setoption", StringComparison.Ordinal) || + script.Contains("obj.copybuffer", StringComparison.Ordinal)); + private static bool TryGetDirective(string? script, out ScriptEngineKind kind) { kind = ScriptEngineKind.MoonSharp; diff --git a/LuaScript/LuaAutoCompletionStrategy.cs b/LuaScript/LuaAutoCompletionStrategy.cs index bd3dd40..4947c51 100644 --- a/LuaScript/LuaAutoCompletionStrategy.cs +++ b/LuaScript/LuaAutoCompletionStrategy.cs @@ -42,7 +42,7 @@ namespace LuaScript "obj.time", "obj.totaltime", "obj.t", "obj.frame", "obj.totalframe", "obj.framerate", "obj.layer", "obj.index", "obj.num", "obj.getpixel", "obj.setpixel", - "obj.getpixeldata", "obj.rand" + "obj.getpixeldata", "obj.rand", "obj.load" ]), ("math", [ "math.abs", "math.ceil", "math.cos", "math.exp", "math.floor", diff --git a/LuaScript/LuaScriptEffectProcessor.cs b/LuaScript/LuaScriptEffectProcessor.cs index 2032acf..61bc50b 100644 --- a/LuaScript/LuaScriptEffectProcessor.cs +++ b/LuaScript/LuaScriptEffectProcessor.cs @@ -210,9 +210,14 @@ namespace LuaScript if (ctx.IsPixelsDirty) { - EnsureBitmaps(imgW, imgH); - WritePixelsToOutput(ctx.GetPixelBuffer()!, imgW); - UpdateTransformEffect(bounds); + int bufW = ctx.ImageWidth; + int bufH = ctx.ImageHeight; + EnsureBitmaps(bufW, bufH); + WritePixelsToOutput(ctx.GetPixelBuffer()!, bufW); + if (ctx.BufferReplaced) + UpdateTransformEffect(-bufW / 2f, -bufH / 2f); + else + UpdateTransformEffect(bounds.Left, bounds.Top); effectOutput = _transformEffect!.Output; pixelsModified = true; } @@ -398,7 +403,7 @@ namespace LuaScript { EnsureBitmaps(imgW, imgH); WritePixelsToOutput(buffer, imgW); - UpdateTransformEffect(bounds); + UpdateTransformEffect(bounds.Left, bounds.Top); effectOutput = _transformEffect!.Output; return true; } @@ -462,7 +467,7 @@ namespace LuaScript _outputBitmap!.CopyFromMemory(new nint(ptr), width * 4); } - private void UpdateTransformEffect(RawRectF bounds) + private void UpdateTransformEffect(float left, float top) { if (_transformEffect is null) { @@ -471,10 +476,10 @@ namespace LuaScript _cachedBounds = default; } - if (_cachedBounds.Left != bounds.Left || _cachedBounds.Top != bounds.Top) + if (_cachedBounds.Left != left || _cachedBounds.Top != top) { - _transformEffect.TransformMatrix = Matrix3x2.CreateTranslation(bounds.Left, bounds.Top); - _cachedBounds = bounds; + _transformEffect.TransformMatrix = Matrix3x2.CreateTranslation(left, top); + _cachedBounds = new RawRectF(left, top, left, top); } } diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index 035672e..a5c1951 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -463,6 +463,46 @@ namespace LuaScript double frame = args.Count > 3 ? args[3].CastToNumber() ?? frameDefault : frameDefault; return DynValue.NewNumber(AviUtlRandom.Next(a, b, seed, frame)); }); + + obj["load"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count < 4) + return DynValue.Void; + if (args[0].Type != DataType.String || args[0].String != "figure") + return DynValue.Void; + + string name = args[1].Type == DataType.String ? args[1].String : string.Empty; + int color = (int)(args[2].CastToNumber() ?? 0d); + double size = args[3].CastToNumber() ?? 0d; + double line = args.Count > 4 ? args[4].CastToNumber() ?? 0d : 0d; + double aspect = args.Count > 5 ? Math.Clamp(args[5].CastToNumber() ?? 0d, -1d, 1d) : 0d; + + LoadFigure(name, color, size, line, aspect); + return DynValue.Void; + }); + } + + private void LoadFigure(string name, int color, double size, double line, double aspect) + { + int boundingSize = Math.Max(1, (int)Math.Round(size)); + double width = aspect >= 0d ? boundingSize * (1d - aspect) : boundingSize; + double height = aspect <= 0d ? boundingSize * (1d + aspect) : boundingSize; + int w = Math.Max(1, (int)Math.Round(width)); + int h = Math.Max(1, (int)Math.Round(height)); + + var buffer = FigureRenderer.Render(name, w, h, color, line); + _activeContext!.ReplaceBuffer(buffer, w, h); + + var obj = _objTable!; + obj["w"] = w; + obj["h"] = h; + obj["hw"] = w / 2d; + obj["hh"] = h / 2d; + obj["cx"] = w / 2d; + obj["cy"] = h / 2d; + obj["cz"] = 0d; + obj["diagonal"] = Math.Sqrt((double)w * w + (double)h * h); } private static DynValue BuildObjectTable(Script script, SceneObjectInfo info)