Use NLuaTableHelper for more conversion to/from LuaTable

also fix indexing that I broke in c8e10120d
This commit is contained in:
YoshiRulz 2020-12-04 21:20:43 +10:00
parent 46504b4880
commit b541fe5b40
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
5 changed files with 23 additions and 12 deletions

View File

@ -63,9 +63,10 @@ namespace BizHawk.Client.Common
{ {
var pointsArr = new Point[4]; var pointsArr = new Point[4];
var i = 0; var i = 0;
foreach (var point in _th.EnumerateValues<LuaTable>(points)) foreach (var point in _th.EnumerateValues<LuaTable>(points)
.Select(table => _th.EnumerateValues<double>(table).ToList()))
{ {
pointsArr[i] = new Point(LuaInt(point[1]), LuaInt(point[2])); pointsArr[i] = new Point(LuaInt(point[0]), LuaInt(point[1]));
i++; i++;
if (i >= 4) if (i >= 4)
{ {
@ -124,14 +125,15 @@ namespace BizHawk.Client.Common
[LuaMethod("drawPolygon", "Draws a polygon using the table of coordinates specified in points. This should be a table of tables(each of size 2). If x or y is passed, the polygon will be translated by the passed coordinate pair. Line is the color of the polygon. Background is the optional fill color")] [LuaMethod("drawPolygon", "Draws a polygon using the table of coordinates specified in points. This should be a table of tables(each of size 2). If x or y is passed, the polygon will be translated by the passed coordinate pair. Line is the color of the polygon. Background is the optional fill color")]
public void DrawPolygon(LuaTable points, int? offsetX = null, int? offsetY = null, Color? line = null, Color? background = null) public void DrawPolygon(LuaTable points, int? offsetX = null, int? offsetY = null, Color? line = null, Color? background = null)
{ {
var pointsList = _th.EnumerateValues<LuaTable>(points).ToList(); var pointsList = _th.EnumerateValues<LuaTable>(points)
.Select(table => _th.EnumerateValues<double>(table).ToList()).ToList();
try try
{ {
var pointsArr = new Point[pointsList.Count]; var pointsArr = new Point[pointsList.Count];
var i = 0; var i = 0;
foreach (var point in pointsList) foreach (var point in pointsList)
{ {
pointsArr[i] = new Point(LuaInt(point[1]) + (offsetX ?? 0), LuaInt(point[2]) + (offsetY ?? 0)); pointsArr[i] = new Point(LuaInt(point[0]) + (offsetX ?? 0), LuaInt(point[1]) + (offsetY ?? 0));
i++; i++;
} }
APIs.Gui.DrawPolygon(pointsArr, line, background); APIs.Gui.DrawPolygon(pointsArr, line, background);

View File

@ -422,7 +422,7 @@ namespace BizHawk.Client.EmuHawk
return 0; return 0;
} }
var pictureBox = new LuaPictureBox(); var pictureBox = new LuaPictureBox { TableHelper = _th };
form.Controls.Add(pictureBox); form.Controls.Add(pictureBox);
if (x.HasValue && y.HasValue) if (x.HasValue && y.HasValue)

View File

@ -15,10 +15,11 @@ namespace BizHawk.Client.EmuHawk
{ {
private readonly Action<string> LogOutputCallback; private readonly Action<string> LogOutputCallback;
public LuaCanvas(int width, int height, int? x, int? y, Action<string> logOutputCallback) public LuaCanvas(int width, int height, int? x, int? y, NLuaTableHelper tableHelper, Action<string> logOutputCallback)
{ {
LogOutputCallback = logOutputCallback; LogOutputCallback = logOutputCallback;
InitializeComponent(); InitializeComponent();
luaPictureBox.TableHelper = tableHelper;
luaPictureBox.Image = Properties.Resources.LuaPictureBox; luaPictureBox.Image = Properties.Resources.LuaPictureBox;
luaPictureBox.Width = width; luaPictureBox.Width = width;
luaPictureBox.Height = height; luaPictureBox.Height = height;

View File

@ -1,8 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Client.Common;
using NLua; using NLua;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
@ -14,6 +17,8 @@ namespace BizHawk.Client.EmuHawk
private readonly Dictionary<Color, SolidBrush> _solidBrushes = new Dictionary<Color, SolidBrush>(); private readonly Dictionary<Color, SolidBrush> _solidBrushes = new Dictionary<Color, SolidBrush>();
private readonly Dictionary<Color, Pen> _pens = new Dictionary<Color, Pen>(); private readonly Dictionary<Color, Pen> _pens = new Dictionary<Color, Pen>();
internal NLuaTableHelper TableHelper { private get; set; }
private SolidBrush GetBrush(Color color) private SolidBrush GetBrush(Color color)
{ {
if (!_solidBrushes.TryGetValue(color, out var b)) if (!_solidBrushes.TryGetValue(color, out var b))
@ -78,9 +83,10 @@ namespace BizHawk.Client.EmuHawk
var pointsArr = new Point[4]; var pointsArr = new Point[4];
var i = 0; var i = 0;
foreach (LuaTable point in points.Values) foreach (var point in TableHelper.EnumerateValues<LuaTable>(points)
.Select(table => TableHelper.EnumerateValues<double>(table).ToList()))
{ {
pointsArr[i] = new Point((int)(double)(point[1]), (int)(double)(point[2])); pointsArr[i] = new Point((int) point[0], (int) point[1]);
i++; i++;
if (i >= 4) if (i >= 4)
{ {
@ -252,11 +258,13 @@ namespace BizHawk.Client.EmuHawk
public void DrawPolygon(LuaTable points, int? x = null, int? y = null, Color? line = null, Color? background = null) public void DrawPolygon(LuaTable points, int? x = null, int? y = null, Color? line = null, Color? background = null)
{ {
var pointsArr = new Point[points.Values.Count]; var pointsList = TableHelper.EnumerateValues<LuaTable>(points)
.Select(table => TableHelper.EnumerateValues<double>(table).ToList()).ToList();
var pointsArr = new Point[pointsList.Count];
var i = 0; var i = 0;
foreach (LuaTable point in points.Values) foreach (var point in pointsList)
{ {
pointsArr[i] = new Point((int)(double)(point[1]) + x ?? 0, (int)(double)(point[2]) + y ?? 0); pointsArr[i] = new Point((int) point[0] + x ?? 0, (int) point[1] + y ?? 0);
i++; i++;
} }

View File

@ -78,7 +78,7 @@ namespace BizHawk.Client.EmuHawk
{ {
guiLib.CreateLuaCanvasCallback = (width, height, x, y) => guiLib.CreateLuaCanvasCallback = (width, height, x, y) =>
{ {
var canvas = new LuaCanvas(width, height, x, y, LogToLuaConsole); var canvas = new LuaCanvas(width, height, x, y, _th, LogToLuaConsole);
canvas.Show(); canvas.Show();
return _th.ObjectToTable(canvas); return _th.ObjectToTable(canvas);
}; };