diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index 2957991..0b0266a 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -312,6 +312,44 @@ namespace LuaScript.Tests Assert.Equal(0.5d, commands[0].Alpha); } + [Fact] + public void SetOption_GetOption_RoundTrips() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[16]; + var fields = Fields(2, 2, 0d); + + bool ok = _worker.Execute( + "obj.setoption('antialias', 0) obj.x = obj.getoption('antialias') obj.y = obj.getoption('unset')", + fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, NoAddDraw, + out _, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.Equal(0d, fields[NativeProtocol.X]); + Assert.Equal(0d, fields[NativeProtocol.Y]); + } + + [Fact] + public void Draw_CarriesAntialiasFromOption() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[16]; + var commands = new System.Collections.Generic.List(); + Action addDraw = commands.Add; + + bool ok = _worker.Execute( + "obj.draw() obj.setoption('antialias', 0) obj.draw()", + Fields(2, 2, 0d), pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoAddEffect, addDraw, + out _, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.Equal(2, commands.Count); + Assert.Equal(1d, commands[0].Antialias); + Assert.Equal(0d, commands[1].Antialias); + } + [Fact] public void GetValue_ReturnsObjFields() { diff --git a/LuaScript/DrawCommand.cs b/LuaScript/DrawCommand.cs index 56d307b..4cd162c 100644 --- a/LuaScript/DrawCommand.cs +++ b/LuaScript/DrawCommand.cs @@ -7,5 +7,6 @@ namespace LuaScript double Zoom, double Alpha, double Aspect, - double[]? Poly = null); + double[]? Poly = null, + double Antialias = 1d); } diff --git a/LuaScript/DrawCompositor.cs b/LuaScript/DrawCompositor.cs index 422a71f..5a0e08d 100644 --- a/LuaScript/DrawCompositor.cs +++ b/LuaScript/DrawCompositor.cs @@ -74,12 +74,16 @@ namespace LuaScript for (int i = 0; i < commands.Count; i++) { var cmd = commands[i]; + var interpolation = cmd.Antialias != 0d + ? BitmapInterpolationMode.Linear + : BitmapInterpolationMode.NearestNeighbor; + if (cmd.Poly is { } poly) { if (!DrawPolyMath.TrySolveAffine(poly, out var affine)) continue; rt.Transform = affine * toTarget; - rt.DrawBitmap(_source, (float)Math.Clamp(poly[20], 0d, 1d), BitmapInterpolationMode.Linear); + rt.DrawBitmap(_source, (float)Math.Clamp(poly[20], 0d, 1d), interpolation); continue; } @@ -94,7 +98,7 @@ namespace LuaScript Matrix3x2.CreateTranslation((float)(cmd.Ox - originX), (float)(cmd.Oy - originY)); rt.Transform = matrix; - rt.DrawBitmap(_source, opacity, BitmapInterpolationMode.Linear); + rt.DrawBitmap(_source, opacity, interpolation); } rt.Transform = Matrix3x2.Identity; dc.EndDraw(); diff --git a/LuaScript/Engine/LuaJitWorker.cs b/LuaScript/Engine/LuaJitWorker.cs index a11e7c3..afb3155 100644 --- a/LuaScript/Engine/LuaJitWorker.cs +++ b/LuaScript/Engine/LuaJitWorker.cs @@ -181,8 +181,9 @@ namespace LuaScript.Engine double zoom = view.ReadDouble(rOff + 3 * 8); double alpha = view.ReadDouble(rOff + 4 * 8); double aspect = view.ReadDouble(rOff + 5 * 8); + double antialias = view.ReadDouble(rOff + 6 * 8); - try { addDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect)); } + try { addDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect, null, antialias)); } catch { } view.Write(NativeProtocol.OffCallbackFound, 1); } @@ -192,8 +193,9 @@ namespace LuaScript.Engine var poly = new double[DrawPolyMath.Length]; for (int i = 0; i < DrawPolyMath.Length; i++) poly[i] = view.ReadDouble(NativeProtocol.CallbackTagOffset + i * 8); + double antialias = view.ReadDouble(NativeProtocol.CallbackTagOffset + DrawPolyMath.Length * 8); - try { addDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly)); } + try { addDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly, antialias)); } catch { } view.Write(NativeProtocol.OffCallbackFound, 1); } diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index 995b27e..913ecce 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -53,6 +53,9 @@ namespace LuaScript private readonly List _removalBuffer = []; + private readonly Dictionary _options = new(StringComparer.Ordinal); + private readonly Dictionary _pixelOptions = new(StringComparer.Ordinal); + private Script? _script; private DynValue? _compiledChunk; private string _lastCompiledCode = string.Empty; @@ -203,6 +206,9 @@ namespace LuaScript var script = _script!; bool isFirstSetup = _objTable is null; + _options.Clear(); + _pixelOptions.Clear(); + if (isFirstSetup) { var sceneTable = new Table(script); @@ -528,7 +534,7 @@ namespace LuaScript double alpha = args.Count > 4 ? args[4].CastToNumber() ?? 1d : 1d; double aspect = args.Count > 5 ? args[5].CastToNumber() ?? 0d : 0d; - _activeContext.AddDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect)); + _activeContext.AddDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect, null, CurrentAntialias())); return DynValue.Void; }); @@ -564,7 +570,7 @@ namespace LuaScript _ => 1d, }; - _activeContext.AddDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly)); + _activeContext.AddDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly, CurrentAntialias())); return DynValue.Void; }); @@ -588,8 +594,39 @@ namespace LuaScript var value = _objTable!.Get(args[0].String); return value.Type == DataType.Number ? value : DynValue.NewNumber(0d); }); + + obj["setoption"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Void; + _options[args[0].String] = args.Count > 1 ? args[1] : DynValue.True; + return DynValue.Void; + }); + + obj["getoption"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.NewNumber(0d); + return _options.TryGetValue(args[0].String, out var value) ? value : DynValue.NewNumber(0d); + }); + + obj["pixeloption"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + if (args.Count == 0 || args[0].Type != DataType.String) + return DynValue.Void; + _pixelOptions[args[0].String] = args.Count > 1 ? args[1] : DynValue.True; + return DynValue.Void; + }); } + private double CurrentAntialias() => + _options.TryGetValue("antialias", out var value) && value.Type == DataType.Number + ? value.Number + : 1d; + private void LoadFigure(string name, int color, double size, double line, double aspect) { int boundingSize = Math.Max(1, (int)Math.Round(size)); diff --git a/native/worker.lua b/native/worker.lua index b15e7af..cf45be1 100644 --- a/native/worker.lua +++ b/native/worker.lua @@ -78,6 +78,8 @@ local cbResult = ffi.cast("double*", base + CB_RESULT_OFFSET) local cbTagD = ffi.cast("double*", base + CB_TAG_OFFSET) local pixelCapacity = mapSize - PIXEL_OFFSET local buffers = {} +local options = {} +local pixeloptions = {} local dirty = false local function bufferKind(id) @@ -250,6 +252,7 @@ function obj.draw(ox, oy, oz, zoom, alpha, aspect) cbResult[3] = zoom or 1 cbResult[4] = alpha or 1 cbResult[5] = aspect or 0 + cbResult[6] = options.antialias or 1 i32[OFF_CB_KIND] = CB_KIND_DRAW i32[OFF_STATUS] = STATUS_CALLBACK k32.SetEvent(doneEvent) @@ -276,6 +279,7 @@ function obj.drawpoly(...) alpha = a[21] or 1 end cbTagD[20] = alpha + cbTagD[21] = options.antialias or 1 i32[OFF_CB_KIND] = CB_KIND_DRAWPOLY i32[OFF_STATUS] = STATUS_CALLBACK k32.SetEvent(doneEvent) @@ -289,6 +293,25 @@ function obj.getvalue(target) return 0 end +function obj.setoption(name, value) + if type(name) ~= "string" then return end + if value == nil then value = true end + options[name] = value +end + +function obj.getoption(name) + if type(name) ~= "string" then return 0 end + local v = options[name] + if v == nil then return 0 end + return v +end + +function obj.pixeloption(name, value) + if type(name) ~= "string" then return end + if value == nil then value = true end + pixeloptions[name] = value +end + function obj.copybuffer(dst, src) if type(dst) ~= "string" or type(src) ~= "string" then return end local sk, skey = bufferKind(src) @@ -442,6 +465,8 @@ while true do if compiledChunk and code == compiledCode then loadFields() cacheTag = nil + for k in pairs(options) do options[k] = nil end + for k in pairs(pixeloptions) do pixeloptions[k] = nil end math.randomseed(f64[31]) setfenv(compiledChunk, sandbox) local ok, err = pcall(compiledChunk)