Accept all 4 colour formats in all Lua functions, update docs/wikitext

resolves #2602
This commit is contained in:
YoshiRulz 2021-05-03 18:23:59 +10:00
parent 893396a009
commit 0a9d496ece
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
10 changed files with 250 additions and 198 deletions

View File

@ -43,15 +43,15 @@ namespace BizHawk.Client.Common
[LuaMethodExample("gui.defaultForeground( 0x000000FF );")]
[LuaMethod("defaultForeground", "Sets the default foreground color to use in drawing methods, white by default")]
public void SetDefaultForegroundColor(Color color) => APIs.Gui.SetDefaultForegroundColor(color);
public void SetDefaultForegroundColor([LuaColorParam] object color) => APIs.Gui.SetDefaultForegroundColor(_th.ParseColor(color));
[LuaMethodExample("gui.defaultBackground( 0xFFFFFFFF );")]
[LuaMethod("defaultBackground", "Sets the default background color to use in drawing methods, transparent by default")]
public void SetDefaultBackgroundColor(Color color) => APIs.Gui.SetDefaultBackgroundColor(color);
public void SetDefaultBackgroundColor([LuaColorParam] object color) => APIs.Gui.SetDefaultBackgroundColor(_th.ParseColor(color));
[LuaMethodExample("gui.defaultTextBackground( 0x000000FF );")]
[LuaMethod("defaultTextBackground", "Sets the default background color to use in text drawing methods, half-transparent black by default")]
public void SetDefaultTextBackground(Color color) => APIs.Gui.SetDefaultTextBackground(color);
public void SetDefaultTextBackground([LuaColorParam] object color) => APIs.Gui.SetDefaultTextBackground(_th.ParseColor(color));
[LuaMethodExample("gui.defaultPixelFont( \"Arial Narrow\");")]
[LuaMethod("defaultPixelFont", "Sets the default font to use in gui.pixelText(). Two font families are available, \"fceux\" and \"gens\" (or \"0\" and \"1\" respectively), \"gens\" is used by default")]
@ -59,7 +59,7 @@ namespace BizHawk.Client.Common
[LuaMethodExample("gui.drawBezier( { { 5, 10 }, { 10, 10 }, { 10, 20 }, { 5, 20 } }, 0x000000FF );")]
[LuaMethod("drawBezier", "Draws a Bezier curve using the table of coordinates provided in the given color")]
public void DrawBezier(LuaTable points, Color color, string surfaceName = null)
public void DrawBezier(LuaTable points, [LuaColorParam] object color, string surfaceName = null)
{
try
{
@ -75,7 +75,7 @@ namespace BizHawk.Client.Common
break;
}
}
APIs.Gui.DrawBezier(pointsArr[0], pointsArr[1], pointsArr[2], pointsArr[3], color, surfaceID: UseOrFallback(surfaceName));
APIs.Gui.DrawBezier(pointsArr[0], pointsArr[1], pointsArr[2], pointsArr[3], _th.ParseColor(color), surfaceID: UseOrFallback(surfaceName));
}
catch (Exception)
{
@ -85,11 +85,11 @@ namespace BizHawk.Client.Common
[LuaMethodExample("gui.drawBox( 16, 32, 162, 322, 0x007F00FF, 0x7F7F7FFF );")]
[LuaMethod("drawBox", "Draws a rectangle on screen from x1/y1 to x2/y2. Same as drawRectangle except it receives two points intead of a point and width/height")]
public void DrawBox(int x, int y, int x2, int y2, Color? line = null, Color? background = null, string surfaceName = null) => APIs.Gui.DrawBox(x, y, x2, y2, line, background, surfaceID: UseOrFallback(surfaceName));
public void DrawBox(int x, int y, int x2, int y2, [LuaColorParam] object line = null, [LuaColorParam] object background = null, string surfaceName = null) => APIs.Gui.DrawBox(x, y, x2, y2, _th.SafeParseColor(line), _th.SafeParseColor(background), surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.drawEllipse( 16, 32, 77, 99, 0x007F00FF, 0x7F7F7FFF );")]
[LuaMethod("drawEllipse", "Draws an ellipse at the given coordinates and the given width and height. Line is the color of the ellipse. Background is the optional fill color")]
public void DrawEllipse(int x, int y, int width, int height, Color? line = null, Color? background = null, string surfaceName = null) => APIs.Gui.DrawEllipse(x, y, width, height, line, background, surfaceID: UseOrFallback(surfaceName));
public void DrawEllipse(int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null, string surfaceName = null) => APIs.Gui.DrawEllipse(x, y, width, height, _th.SafeParseColor(line), _th.SafeParseColor(background), surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.drawIcon( \"C:\\sample.ico\", 16, 32, 18, 24 );")]
[LuaMethod("drawIcon", "draws an Icon (.ico) file from the given path at the given coordinate. width and height are optional. If specified, it will resize the image accordingly")]
@ -109,23 +109,23 @@ namespace BizHawk.Client.Common
[LuaMethodExample("gui.drawLine( 161, 321, 162, 322, 0xFFFFFFFF );")]
[LuaMethod("drawLine", "Draws a line from the first coordinate pair to the 2nd. Color is optional (if not specified it will be drawn black)")]
public void DrawLine(int x1, int y1, int x2, int y2, Color? color = null, string surfaceName = null) => APIs.Gui.DrawLine(x1, y1, x2, y2, color, surfaceID: UseOrFallback(surfaceName));
public void DrawLine(int x1, int y1, int x2, int y2, [LuaColorParam] object color = null, string surfaceName = null) => APIs.Gui.DrawLine(x1, y1, x2, y2, _th.SafeParseColor(color), surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.drawAxis( 16, 32, 15, 0xFFFFFFFF );")]
[LuaMethod("drawAxis", "Draws an axis of the specified size at the coordinate pair.)")]
public void DrawAxis(int x, int y, int size, Color? color = null, string surfaceName = null) => APIs.Gui.DrawAxis(x, y, size, color, surfaceID: UseOrFallback(surfaceName));
public void DrawAxis(int x, int y, int size, [LuaColorParam] object color = null, string surfaceName = null) => APIs.Gui.DrawAxis(x, y, size, _th.SafeParseColor(color), surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.drawPie( 16, 32, 77, 99, 180, 90, 0x007F00FF, 0x7F7F7FFF );")]
[LuaMethod("drawPie", "draws a Pie shape at the given coordinates and the given width and height")]
public void DrawPie(int x, int y, int width, int height, int startangle, int sweepangle, Color? line = null, Color? background = null, string surfaceName = null) => APIs.Gui.DrawPie(x, y, width, height, startangle, sweepangle, line, background, surfaceID: UseOrFallback(surfaceName));
public void DrawPie(int x, int y, int width, int height, int startangle, int sweepangle, [LuaColorParam] object line = null, [LuaColorParam] object background = null, string surfaceName = null) => APIs.Gui.DrawPie(x, y, width, height, startangle, sweepangle, _th.SafeParseColor(line), _th.SafeParseColor(background), surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.drawPixel( 16, 32, 0xFFFFFFFF );")]
[LuaMethod("drawPixel", "Draws a single pixel at the given coordinates in the given color. Color is optional (if not specified it will be drawn black)")]
public void DrawPixel(int x, int y, Color? color = null, string surfaceName = null) => APIs.Gui.DrawPixel(x, y, color, surfaceID: UseOrFallback(surfaceName));
public void DrawPixel(int x, int y, [LuaColorParam] object color = null, string surfaceName = null) => APIs.Gui.DrawPixel(x, y, _th.SafeParseColor(color), surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.drawPolygon( { { 5, 10 }, { 10, 10 }, { 10, 20 }, { 5, 20 } }, 10, 30, 0x007F00FF, 0x7F7F7FFF );")]
[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, string surfaceName = null)
public void DrawPolygon(LuaTable points, int? offsetX = null, int? offsetY = null, [LuaColorParam] object line = null, [LuaColorParam] object background = null, string surfaceName = null)
{
var pointsList = _th.EnumerateValues<LuaTable>(points)
.Select(table => _th.EnumerateValues<double>(table).ToList()).ToList();
@ -138,7 +138,7 @@ namespace BizHawk.Client.Common
pointsArr[i] = new Point(LuaInt(point[0]) + (offsetX ?? 0), LuaInt(point[1]) + (offsetY ?? 0));
i++;
}
APIs.Gui.DrawPolygon(pointsArr, line, background, surfaceID: UseOrFallback(surfaceName));
APIs.Gui.DrawPolygon(pointsArr, _th.SafeParseColor(line), _th.SafeParseColor(background), surfaceID: UseOrFallback(surfaceName));
}
catch (Exception)
{
@ -148,7 +148,7 @@ namespace BizHawk.Client.Common
[LuaMethodExample("gui.drawRectangle( 16, 32, 77, 99, 0x007F00FF, 0x7F7F7FFF );")]
[LuaMethod("drawRectangle", "Draws a rectangle at the given coordinate and the given width and height. Line is the color of the box. Background is the optional fill color")]
public void DrawRectangle(int x, int y, int width, int height, Color? line = null, Color? background = null, string surfaceName = null) => APIs.Gui.DrawRectangle(x, y, width, height, line, background, surfaceID: UseOrFallback(surfaceName));
public void DrawRectangle(int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null, string surfaceName = null) => APIs.Gui.DrawRectangle(x, y, width, height, _th.SafeParseColor(line), _th.SafeParseColor(background), surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.drawString( 16, 32, \"Some message\", 0x7F0000FF, 0x00007FFF, 8, \"Arial Narrow\", \"bold\", \"center\", \"middle\" );")]
[LuaMethod("drawString", "Alias of gui.drawText()")]
@ -156,8 +156,8 @@ namespace BizHawk.Client.Common
int x,
int y,
string message,
Color? forecolor = null,
Color? backcolor = null,
[LuaColorParam] object forecolor = null,
[LuaColorParam] object backcolor = null,
int? fontsize = null,
string fontfamily = null,
string fontstyle = null,
@ -165,20 +165,20 @@ namespace BizHawk.Client.Common
string vertalign = null,
string surfaceName = null)
{
DrawText(x, y, message, forecolor, backcolor, fontsize, fontfamily, fontstyle, horizalign, vertalign, surfaceName: surfaceName);
DrawText(x, y, message, _th.SafeParseColor(forecolor), _th.SafeParseColor(backcolor), fontsize, fontfamily, fontstyle, horizalign, vertalign, surfaceName: surfaceName);
}
[LuaMethodExample("gui.drawText( 16, 32, \"Some message\", 0x7F0000FF, 0x00007FFF, 8, \"Arial Narrow\", \"bold\", \"center\", \"middle\" );")]
[LuaMethod("drawText", "Draws the given message in the emulator screen space (like all draw functions) at the given x,y coordinates and the given color. The default color is white. A fontfamily can be specified and is monospace generic if none is specified (font family options are the same as the .NET FontFamily class). The fontsize default is 12. The default font style is regular. Font style options are regular, bold, italic, strikethrough, underline. Horizontal alignment options are left (default), center, or right. Vertical alignment options are bottom (default), middle, or top. Alignment options specify which ends of the text will be drawn at the x and y coordinates. For pixel-perfect font look, make sure to disable aspect ratio correction.")]
public void DrawText(int x, int y, string message, Color? forecolor = null, Color? backcolor = null, int? fontsize = null, string fontfamily = null, string fontstyle = null, string horizalign = null, string vertalign = null, string surfaceName = null) => APIs.Gui.DrawString(x, y, message, forecolor, backcolor, fontsize, fontfamily, fontstyle, horizalign, vertalign, surfaceID: UseOrFallback(surfaceName));
public void DrawText(int x, int y, string message, [LuaColorParam] object forecolor = null, [LuaColorParam] object backcolor = null, int? fontsize = null, string fontfamily = null, string fontstyle = null, string horizalign = null, string vertalign = null, string surfaceName = null) => APIs.Gui.DrawString(x, y, message, _th.SafeParseColor(forecolor), _th.SafeParseColor(backcolor), fontsize, fontfamily, fontstyle, horizalign, vertalign, surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.pixelText( 16, 32, \"Some message\", 0x7F0000FF, 0x00007FFF, \"Arial Narrow\" );")]
[LuaMethod("pixelText", "Draws the given message in the emulator screen space (like all draw functions) at the given x,y coordinates and the given color. The default color is white. Two font families are available, \"fceux\" and \"gens\" (or \"0\" and \"1\" respectively), both are monospace and have the same size as in the emulators they've been taken from. If no font family is specified, it uses \"gens\" font, unless that's overridden via gui.defaultPixelFont()")]
public void DrawText(int x, int y, string message, Color? forecolor = null, Color? backcolor = null, string fontfamily = null, string surfaceName = null) => APIs.Gui.DrawText(x, y, message, forecolor, backcolor ?? APIs.Gui.GetDefaultTextBackground().Value, fontfamily, surfaceID: UseOrFallback(surfaceName));
public void DrawText(int x, int y, string message, [LuaColorParam] object forecolor = null, [LuaColorParam] object backcolor = null, string fontfamily = null, string surfaceName = null) => APIs.Gui.DrawText(x, y, message, _th.SafeParseColor(forecolor), _th.SafeParseColor(backcolor) ?? APIs.Gui.GetDefaultTextBackground().Value, fontfamily, surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.text( 16, 32, \"Some message\", 0x7F0000FF, \"bottomleft\" );")]
[LuaMethod("text", "Displays the given text on the screen at the given coordinates. Optional Foreground color. The optional anchor flag anchors the text to one of the four corners. Anchor flag parameters: topleft, topright, bottomleft, bottomright")]
public void Text(int x, int y, string message, Color? forecolor = null, string anchor = null) => APIs.Gui.Text(x, y, message, forecolor, anchor);
public void Text(int x, int y, string message, [LuaColorParam] object forecolor = null, string anchor = null) => APIs.Gui.Text(x, y, message, _th.SafeParseColor(forecolor), anchor);
[LuaMethodExample("local nlguicre = gui.createcanvas( 77, 99, 2, 48 );")]
[LuaMethod("createcanvas", "Creates a canvas of the given size and, if specified, the given coordinates.")]

View File

@ -2,6 +2,9 @@
namespace BizHawk.Client.Common
{
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class LuaColorParamAttribute : Attribute {}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class LuaDeprecatedMethodAttribute : Attribute {}

View File

@ -12,35 +12,42 @@ namespace BizHawk.Client.Common
{
var sb = new StringBuilder();
sb
.AppendLine("[module:ListParents]")
sb.AppendLine("[module:ListParents]")
.AppendLine()
.AppendLine("This page documents the the behavior and parameters of Lua functions available for the [BizHawk] emulator.")
.AppendLine("__This is an autogenerated page, do not edit__")
.AppendLine("__This is an autogenerated page, do not edit.__ (To update, open the function list dialog on a Debug build and click the wiki button.)")
.AppendLine()
.AppendLine();
sb.AppendLine(@"All type names represent the standard .NET types of the same name. Except for func which represents a lua function and table which represents a lua table. For more information on .NET types can be found in MSDN documentation.
__Types and notation__
* ? (question mark)
** A question mark next to a value indicates that it is a Nullable type (only applies to types that are not normally nullable)
.AppendLine("This page documents the the behavior and parameters of Lua functions available for the [BizHawk] emulator. The function list is also available from within EmuHawk. From the Lua Console, click Help > Lua Functions List or press F1.")
.AppendLine()
.AppendLine(@"__Types and notation__
* [[]] (brackets)
** Brackets around a parameter indicate that the parameter is optional. Optional parameters have an equals sign followed by the value that will be used if no value is supplied.
** Brackets after a parameter type indicate it is an array
* null
** null is equivalent to the lua nil. Lua doesn't support named arguments, it checks the arguments by position. So if you're sending an optional argument that goes ''after'' other optional arguments you don't want to send, replace those with lua nil.
* Color
** This is a .NET System.Drawing.Color struct. The value passed from lua is any value acceptable in the Color constructor. This means either a string with the color name such as ""red"", or a 0xAARRGGBB integer value. Unless specified, this is not a nullable value
* ? (question mark)
** A question mark next to a value indicates that it is nullable i.e. null/nil may be passed instead of a real value. (Confusingly, many .NET types are nullable but omit the '?'. These aren't common in our Lua APIs.)
* null/nil
** null is equivalent to Lua's nil.
** Omitting the last parameter is equivalent to passing nil, same with the second-last parameter and so on. However, if you want to pass the last parameter but not one in the middle, you will need to explicitly pass nil.
* object
** A System.Object, literally anything qualifies for this parameter. However, the context of particular function may suggest a narrower range of useful values.
** A System.Object; any table or value may be passed, including nil.
** Usually you can infer what kinds of values would be useful to pass from the descriptions and examples.
* luacolor
** Any of:
** a 32-bit number in the format 0xAARRGGBB;
** a string in the format ""#AARRGGBB"";
** a string containing a CSS3/X11 color name e.g. ""blue"", ""palegoldenrod""; or
** a Color created with forms.createcolor.
** As noted above, luacolor? indicates nil may also be passed.
* luaf
** A Lua function. Note that these are always parameters, and never return values of a call
* table
** A standard Lua table
* something else
** check the .NET documentation on MSDN
");
foreach (var library in this.Select(lf => new { Name = lf.Library, Description = lf.LibraryDescription }).Distinct())
foreach (var library in this.Select(lf => (Name: lf.Library, Description: lf.LibraryDescription))
.Distinct()
.OrderBy(library => library.Name))
{
sb
.AppendFormat("%%TAB {0}%%", library.Name)
@ -199,10 +206,10 @@ __Types and notation__
for (var i = 0; i < parameters.Length; i++)
{
var p = TypeCleanup(parameters[i].ToString());
if (parameters[i].GetCustomAttribute<LuaColorParamAttribute>() != null) p = p.Replace("object", "luacolor");
if (parameters[i].IsOptional)
{
var def = parameters[i].DefaultValue != null ? parameters[i].DefaultValue.ToString() : "null";
list.AppendFormat("[{0} = {1}]", p, def);
list.Append($"[{p} = {parameters[i].DefaultValue?.ToString() ?? "nil"}]");
}
else
{

View File

@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Globalization;
using System.Threading;
namespace BizHawk.Client.Common
@ -58,17 +59,6 @@ namespace BizHawk.Client.Common
return (int)(double)luaArg;
}
protected static Color? ToColor(object o)
{
return o switch
{
null => null,
double d => Color.FromArgb((int) (long) d),
string s => Color.FromName(s),
_ => null
};
}
protected void Log(object message)
{
LogOutputCallback?.Invoke(message.ToString());

View File

@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using NLua;
@ -7,9 +10,15 @@ namespace BizHawk.Client.Common
{
public sealed class NLuaTableHelper
{
private readonly Action<string> _logCallback;
private readonly Lua _lua;
public NLuaTableHelper(Lua lua) => _lua = lua;
public NLuaTableHelper(Lua lua, Action<string> logCallback)
{
_logCallback = logCallback;
_lua = lua;
}
public LuaTable CreateTable() => _lua.NewTable();
@ -52,5 +61,33 @@ namespace BizHawk.Client.Common
}
return table;
}
public Color ParseColor(object o) => ParseColor(o, safe: false, _logCallback) ?? throw new ArgumentException("failed to parse Color", nameof(o));
public Color? SafeParseColor(object o) => ParseColor(o, safe: true, _logCallback);
private static Color? ParseColor(object o, bool safe, Action<string> logCallback)
{
switch (o)
{
case null:
return null;
case Color c:
return c;
case double d:
return ParseColor((int) (long) d, safe, logCallback);
case int i:
return Color.FromArgb(i);
case string s:
if (s[0] == '#' && s.Length == 9) return ParseColor(int.Parse(s.Substring(1), NumberStyles.HexNumber), safe, logCallback);
var fromName = Color.FromName(s);
if (fromName.IsNamedColor) return fromName;
if (safe) logCallback($"ParseColor: not a known color name (\"{s}\")");
return null;
default:
if (safe) logCallback("ParseColor: coercing object/table to string");
return ParseColor(o.ToString(), safe, logCallback);
}
}
}
}

View File

@ -434,10 +434,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"clear",
"Clears the canvas")]
public void Clear(int componentHandle, Color color)
public void Clear(int componentHandle, [LuaColorParam] object color)
{
try
{
var color1 = _th.ParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -449,7 +450,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.Clear(color);
control.Clear(color1);
}
}
}
@ -492,10 +493,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"setDefaultForegroundColor",
"Sets the default foreground color to use in drawing methods, white by default")]
public void SetDefaultForegroundColor(int componentHandle, Color color)
public void SetDefaultForegroundColor(int componentHandle, [LuaColorParam] object color)
{
try
{
var color1 = _th.ParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -507,7 +509,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.SetDefaultForegroundColor(color);
control.SetDefaultForegroundColor(color1);
}
}
}
@ -521,10 +523,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"setDefaultBackgroundColor",
"Sets the default background color to use in drawing methods, transparent by default")]
public void SetDefaultBackgroundColor(int componentHandle, Color color)
public void SetDefaultBackgroundColor(int componentHandle, [LuaColorParam] object color)
{
try
{
var color1 = _th.ParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -536,7 +539,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.SetDefaultBackgroundColor(color);
control.SetDefaultBackgroundColor(color1);
}
}
}
@ -550,10 +553,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"setDefaultTextBackground",
"Sets the default backgroiund color to use in text drawing methods, half-transparent black by default")]
public void SetDefaultTextBackground(int componentHandle, Color color)
public void SetDefaultTextBackground(int componentHandle, [LuaColorParam] object color)
{
try
{
var color1 = _th.ParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -565,7 +569,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.SetDefaultTextBackground(color);
control.SetDefaultTextBackground(color1);
}
}
}
@ -579,10 +583,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawBezier",
"Draws a Bezier curve using the table of coordinates provided in the given color")]
public void DrawBezier(int componentHandle, LuaTable points, Color color)
public void DrawBezier(int componentHandle, LuaTable points, [LuaColorParam] object color)
{
try
{
var color1 = _th.ParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -594,7 +599,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawBezier(points, color);
control.DrawBezier(points, color1);
}
}
}
@ -608,10 +613,12 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawBox",
"Draws a rectangle on screen from x1/y1 to x2/y2. Same as drawRectangle except it receives two points intead of a point and width/height")]
public void DrawBox(int componentHandle, int x, int y, int x2, int y2, Color? line = null, Color? background = null)
public void DrawBox(int componentHandle, int x, int y, int x2, int y2, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
try
{
var strokeColor = _th.SafeParseColor(line);
var fillColor = _th.SafeParseColor(background);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -623,7 +630,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawBox(x, y, x2, y2, line, background);
control.DrawBox(x, y, x2, y2, strokeColor, fillColor);
}
}
}
@ -637,10 +644,12 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawEllipse",
"Draws an ellipse at the given coordinates and the given width and height. Line is the color of the ellipse. Background is the optional fill color")]
public void DrawEllipse(int componentHandle, int x, int y, int width, int height, Color? line = null, Color? background = null)
public void DrawEllipse(int componentHandle, int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
try
{
var strokeColor = _th.SafeParseColor(line);
var fillColor = _th.SafeParseColor(background);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -652,7 +661,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawEllipse(x, y, width, height, line, background);
control.DrawEllipse(x, y, width, height, strokeColor, fillColor);
}
}
}
@ -792,10 +801,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawLine",
"Draws a line from the first coordinate pair to the 2nd. Color is optional (if not specified it will be drawn black)")]
public void DrawLine(int componentHandle, int x1, int y1, int x2, int y2, Color? color = null)
public void DrawLine(int componentHandle, int x1, int y1, int x2, int y2, [LuaColorParam] object color = null)
{
try
{
var color1 = _th.SafeParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -807,7 +817,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawLine(x1, y1, x2, y2, color);
control.DrawLine(x1, y1, x2, y2, color1);
}
}
}
@ -821,10 +831,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawAxis",
"Draws an axis of the specified size at the coordinate pair.)")]
public void DrawAxis(int componentHandle, int x, int y, int size, Color? color = null)
public void DrawAxis(int componentHandle, int x, int y, int size, [LuaColorParam] object color = null)
{
try
{
var color1 = _th.SafeParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -836,7 +847,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawAxis(x, y, size, color);
control.DrawAxis(x, y, size, color1);
}
}
}
@ -851,10 +862,11 @@ namespace BizHawk.Client.EmuHawk
"drawArc",
"draws a Arc shape at the given coordinates and the given width and height"
)]
public void DrawArc(int componentHandle, int x, int y, int width, int height, int startangle, int sweepangle, Color? line = null)
public void DrawArc(int componentHandle, int x, int y, int width, int height, int startangle, int sweepangle, [LuaColorParam] object line = null)
{
try
{
var strokeColor = _th.SafeParseColor(line);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -866,7 +878,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawArc(x, y, width, height, startangle, sweepangle, line);
control.DrawArc(x, y, width, height, startangle, sweepangle, strokeColor);
}
}
}
@ -888,11 +900,13 @@ namespace BizHawk.Client.EmuHawk
int height,
int startangle,
int sweepangle,
Color? line = null,
Color? background = null)
[LuaColorParam] object line = null,
[LuaColorParam] object background = null)
{
try
{
var strokeColor = _th.SafeParseColor(line);
var fillColor = _th.SafeParseColor(background);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -904,7 +918,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawPie(x, y, width, height, startangle, sweepangle, line, background);
control.DrawPie(x, y, width, height, startangle, sweepangle, strokeColor, fillColor);
}
}
}
@ -918,10 +932,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawPixel",
"Draws a single pixel at the given coordinates in the given color. Color is optional (if not specified it will be drawn black)")]
public void DrawPixel(int componentHandle, int x, int y, Color? color = null)
public void DrawPixel(int componentHandle, int x, int y, [LuaColorParam] object color = null)
{
try
{
var color1 = _th.SafeParseColor(color);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -933,7 +948,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawPixel(x, y, color);
control.DrawPixel(x, y, color1);
}
}
}
@ -947,10 +962,12 @@ namespace BizHawk.Client.EmuHawk
[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(int componentHandle, LuaTable points, int? x = null, int? y = null, Color? line = null, Color? background = null)
public void DrawPolygon(int componentHandle, LuaTable points, int? x = null, int? y = null, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
try
{
var strokeColor = _th.SafeParseColor(line);
var fillColor = _th.SafeParseColor(background);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -962,7 +979,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawPolygon(points, x, y, line, background);
control.DrawPolygon(points, x, y, strokeColor, fillColor);
}
}
}
@ -977,10 +994,12 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawRectangle",
"Draws a rectangle at the given coordinate and the given width and height. Line is the color of the box. Background is the optional fill color")]
public void DrawRectangle(int componentHandle, int x, int y, int width, int height, Color? line = null, Color? background = null)
public void DrawRectangle(int componentHandle, int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
try
{
var strokeColor = _th.SafeParseColor(line);
var fillColor = _th.SafeParseColor(background);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -992,7 +1011,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawRectangle(x, y, width, height, line, background);
control.DrawRectangle(x, y, width, height, strokeColor, fillColor);
}
}
}
@ -1011,8 +1030,8 @@ namespace BizHawk.Client.EmuHawk
int x,
int y,
string message,
Color? forecolor = null,
Color? backcolor = null,
[LuaColorParam] object forecolor = null,
[LuaColorParam] object backcolor = null,
int? fontsize = null,
string fontfamily = null,
string fontstyle = null,
@ -1021,6 +1040,8 @@ namespace BizHawk.Client.EmuHawk
{
try
{
var fgColor = _th.SafeParseColor(forecolor);
var bgColor = _th.SafeParseColor(backcolor);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -1032,7 +1053,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawText(x, y, message, forecolor, backcolor, fontsize, fontfamily, fontstyle, horizalign, vertalign);
control.DrawText(x, y, message, fgColor, bgColor, fontsize, fontfamily, fontstyle, horizalign, vertalign);
}
}
}
@ -1051,8 +1072,8 @@ namespace BizHawk.Client.EmuHawk
int x,
int y,
string message,
Color? forecolor = null,
Color? backcolor = null,
[LuaColorParam] object forecolor = null,
[LuaColorParam] object backcolor = null,
int? fontsize = null,
string fontfamily = null,
string fontstyle = null,
@ -1061,6 +1082,8 @@ namespace BizHawk.Client.EmuHawk
{
try
{
var fgColor = _th.SafeParseColor(forecolor);
var bgColor = _th.SafeParseColor(backcolor);
var ptr = new IntPtr(componentHandle);
foreach (var form in _luaForms)
{
@ -1072,7 +1095,7 @@ namespace BizHawk.Client.EmuHawk
foreach (var control in form.Controls.OfType<LuaPictureBox>())
{
control.DrawText(x, y, message, forecolor, backcolor, fontsize, fontfamily, fontstyle, horizalign, vertalign);
control.DrawText(x, y, message, fgColor, bgColor, fontsize, fontfamily, fontstyle, horizalign, vertalign);
}
}
}
@ -1218,21 +1241,12 @@ namespace BizHawk.Client.EmuHawk
{
var pi = c.GetType().GetProperty(property) ?? throw new Exception($"no property with the identifier {property}");
var pt = pi.PropertyType;
object value1;
if (pt.IsEnum)
{
value1 = Enum.Parse(pt, value.ToString(), true);
}
else if (pt == typeof(Color) && value is string s)
{
if (s[0] != '#' || s.Length != 9) throw new Exception("invalid format for Color, format the string as #AARRGGBB or pass a Color object");
value1 = Color.FromArgb(int.Parse(s.Substring(1), System.Globalization.NumberStyles.HexNumber));
}
else
{
value1 = value;
}
pi.SetValue(c, Convert.ChangeType(value1, pt), null);
var o = pt.IsEnum
? Enum.Parse(pt, value.ToString(), true)
: pt == typeof(Color)
? _th.ParseColor(value)
: Convert.ChangeType(value, pt);
pi.SetValue(c, o, null);
}
var ptr = new IntPtr(handle);

View File

@ -477,18 +477,7 @@ namespace BizHawk.Client.EmuHawk
{
if (Engaged())
{
Tastudio.QueryItemBgColorCallback = (index, name) =>
{
var result = luaf.Call(index, name);
if (result != null)
{
var color = ToColor(result[0]);
return color;
}
return null;
};
Tastudio.QueryItemBgColorCallback = (index, name) => _th.SafeParseColor(luaf.Call(index, name)?[0]);
}
}

View File

@ -13,12 +13,21 @@ namespace BizHawk.Client.EmuHawk
[Description("Represents a canvas object returned by the gui.createcanvas() method")]
public sealed class LuaCanvas : Form
{
private readonly NLuaTableHelper _th;
private readonly Action<string> LogOutputCallback;
private readonly LuaPictureBox luaPictureBox;
public LuaCanvas(int width, int height, int? x, int? y, NLuaTableHelper tableHelper, Action<string> logOutputCallback)
public LuaCanvas(
int width,
int height,
int? x,
int? y,
NLuaTableHelper tableHelper,
Action<string> logOutputCallback)
{
_th = tableHelper;
LogOutputCallback = logOutputCallback;
SuspendLayout();
@ -51,7 +60,7 @@ namespace BizHawk.Client.EmuHawk
Size = new Size(100, 50),
SizeMode = PictureBoxSizeMode.AutoSize,
TabIndex = 0,
TableHelper = tableHelper,
TableHelper = _th,
TabStop = false
};
Controls.Add(luaPictureBox);
@ -92,9 +101,9 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"clear",
"Clears the canvas")]
public void Clear(Color color)
public void Clear([LuaColorParam] object color)
{
luaPictureBox.Clear(color);
luaPictureBox.Clear(_th.ParseColor(color));
}
[LuaMethodExample(
@ -112,9 +121,9 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"setDefaultForegroundColor",
"Sets the default foreground color to use in drawing methods, white by default")]
public void SetDefaultForegroundColor(Color color)
public void SetDefaultForegroundColor([LuaColorParam] object color)
{
luaPictureBox.SetDefaultForegroundColor(color);
luaPictureBox.SetDefaultForegroundColor(_th.ParseColor(color));
}
[LuaMethodExample(
@ -122,9 +131,9 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"setDefaultBackgroundColor",
"Sets the default background color to use in drawing methods, transparent by default")]
public void SetDefaultBackgroundColor(Color color)
public void SetDefaultBackgroundColor([LuaColorParam] object color)
{
luaPictureBox.SetDefaultBackgroundColor(color);
luaPictureBox.SetDefaultBackgroundColor(_th.ParseColor(color));
}
[LuaMethodExample(
@ -132,9 +141,9 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"setDefaultTextBackground",
"Sets the default background color to use in text drawing methods, half-transparent black by default")]
public void SetDefaultTextBackground(Color color)
public void SetDefaultTextBackground([LuaColorParam] object color)
{
luaPictureBox.SetDefaultTextBackground(color);
luaPictureBox.SetDefaultTextBackground(_th.ParseColor(color));
}
[LuaMethodExample(
@ -142,11 +151,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawBezier",
"Draws a Bezier curve using the table of coordinates provided in the given color")]
public void DrawBezier(LuaTable points, Color color)
public void DrawBezier(LuaTable points, [LuaColorParam] object color)
{
try
{
luaPictureBox.DrawBezier(points, color);
luaPictureBox.DrawBezier(points, _th.ParseColor(color));
}
catch (Exception ex)
{
@ -159,11 +168,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawBox",
"Draws a rectangle on screen from x1/y1 to x2/y2. Same as drawRectangle except it receives two points intead of a point and width/height")]
public void DrawBox(int x, int y, int x2, int y2, Color? line = null, Color? background = null)
public void DrawBox(int x, int y, int x2, int y2, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
try
{
luaPictureBox.DrawBox(x, y, x2, y2, line, background);
luaPictureBox.DrawBox(x, y, x2, y2, _th.SafeParseColor(line), _th.SafeParseColor(background));
}
catch (Exception ex)
{
@ -176,11 +185,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawEllipse",
"Draws an ellipse at the given coordinates and the given width and height. Line is the color of the ellipse. Background is the optional fill color")]
public void DrawEllipse(int x, int y, int width, int height, Color? line = null, Color? background = null)
public void DrawEllipse(int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
try
{
luaPictureBox.DrawEllipse(x, y, width, height, line, background);
luaPictureBox.DrawEllipse(x, y, width, height, _th.SafeParseColor(line), _th.SafeParseColor(background));
}
catch (Exception ex)
{
@ -252,9 +261,9 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawLine",
"Draws a line from the first coordinate pair to the 2nd. Color is optional (if not specified it will be drawn black)")]
public void DrawLine(int x1, int y1, int x2, int y2, Color? color = null)
public void DrawLine(int x1, int y1, int x2, int y2, [LuaColorParam] object color = null)
{
luaPictureBox.DrawLine(x1, y1, x2, y2, color);
luaPictureBox.DrawLine(x1, y1, x2, y2, _th.SafeParseColor(color));
}
[LuaMethodExample(
@ -262,9 +271,9 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawAxis",
"Draws an axis of the specified size at the coordinate pair.)")]
public void DrawAxis(int x, int y, int size, Color? color = null)
public void DrawAxis(int x, int y, int size, [LuaColorParam] object color = null)
{
luaPictureBox.DrawAxis(x, y, size, color);
luaPictureBox.DrawAxis(x, y, size, _th.SafeParseColor(color));
}
[LuaMethodExample(
@ -273,9 +282,9 @@ namespace BizHawk.Client.EmuHawk
"drawArc",
"draws a Arc shape at the given coordinates and the given width and height"
)]
public void DrawArc(int x, int y, int width, int height, int startAngle, int sweepAngle, Color? line = null)
public void DrawArc(int x, int y, int width, int height, int startAngle, int sweepAngle, [LuaColorParam] object line = null)
{
luaPictureBox.DrawArc(x, y, width, height, startAngle, sweepAngle, line);
luaPictureBox.DrawArc(x, y, width, height, startAngle, sweepAngle, _th.SafeParseColor(line));
}
[LuaMethodExample(
@ -290,10 +299,10 @@ namespace BizHawk.Client.EmuHawk
int height,
int startAngle,
int sweepAngle,
Color? line = null,
Color? background = null)
[LuaColorParam] object line = null,
[LuaColorParam] object background = null)
{
luaPictureBox.DrawPie(x, y, width, height, startAngle, sweepAngle, line, background);
luaPictureBox.DrawPie(x, y, width, height, startAngle, sweepAngle, _th.SafeParseColor(line), _th.SafeParseColor(background));
}
[LuaMethodExample(
@ -301,11 +310,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawPixel",
"Draws a single pixel at the given coordinates in the given color. Color is optional (if not specified it will be drawn black)")]
public void DrawPixel(int x, int y, Color? color = null)
public void DrawPixel(int x, int y, [LuaColorParam] object color = null)
{
try
{
luaPictureBox.DrawPixel(x, y, color);
luaPictureBox.DrawPixel(x, y, _th.SafeParseColor(color));
}
catch (Exception ex)
{
@ -318,11 +327,11 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawPolygon",
"Draws a polygon using the table of coordinates specified in points. This should be a table of tables(each of size 2). Line is the color of the polygon. Background is the optional fill color")]
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, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
try
{
luaPictureBox.DrawPolygon(points, x, y, line, background);
luaPictureBox.DrawPolygon(points, x, y, _th.SafeParseColor(line), _th.SafeParseColor(background));
}
catch (Exception ex)
{
@ -336,9 +345,9 @@ namespace BizHawk.Client.EmuHawk
[LuaMethod(
"drawRectangle",
"Draws a rectangle at the given coordinate and the given width and height. Line is the color of the box. Background is the optional fill color")]
public void DrawRectangle(int x, int y, int width, int height, Color? line = null, Color? background = null)
public void DrawRectangle(int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
luaPictureBox.DrawRectangle(x, y, width, height, line, background);
luaPictureBox.DrawRectangle(x, y, width, height, _th.SafeParseColor(line), _th.SafeParseColor(background));
}
[LuaMethodExample(
@ -350,15 +359,15 @@ namespace BizHawk.Client.EmuHawk
int x,
int y,
string message,
Color? foreColor = null,
Color? backColor = null,
[LuaColorParam] object foreColor = null,
[LuaColorParam] object backColor = null,
int? fontSize = null,
string fontFamily = null,
string fontStyle = null,
string horizontalAlign = null,
string verticalAlign = null)
{
luaPictureBox.DrawText(x, y, message, foreColor, backColor, fontSize, fontFamily, fontStyle, horizontalAlign, verticalAlign);
luaPictureBox.DrawText(x, y, message, _th.SafeParseColor(foreColor), _th.SafeParseColor(backColor), fontSize, fontFamily, fontStyle, horizontalAlign, verticalAlign);
}
[LuaMethodExample(
@ -370,15 +379,15 @@ namespace BizHawk.Client.EmuHawk
int x,
int y,
string message,
Color? foreColor = null,
Color? backColor = null,
[LuaColorParam] object foreColor = null,
[LuaColorParam] object backColor = null,
int? fontSize = null,
string fontFamily = null,
string fontStyle = null,
string horizontalAlign = null,
string verticalAlign = null)
{
luaPictureBox.DrawText(x, y, message, foreColor, backColor, fontSize, fontFamily, fontStyle, horizontalAlign, verticalAlign);
luaPictureBox.DrawText(x, y, message, _th.SafeParseColor(foreColor), _th.SafeParseColor(backColor), fontSize, fontFamily, fontStyle, horizontalAlign, verticalAlign);
}

View File

@ -19,23 +19,25 @@ namespace BizHawk.Client.EmuHawk
internal NLuaTableHelper TableHelper { private get; set; }
private SolidBrush GetBrush(Color color)
private SolidBrush GetBrush([LuaColorParam] object color)
{
if (!_solidBrushes.TryGetValue(color, out var b))
var color1 = TableHelper.ParseColor(color);
if (!_solidBrushes.TryGetValue(color1, out var b))
{
b = new SolidBrush(color);
_solidBrushes[color] = b;
b = new SolidBrush(color1);
_solidBrushes[color1] = b;
}
return b;
}
private Pen GetPen(Color color)
private Pen GetPen([LuaColorParam] object color)
{
if (!_pens.TryGetValue(color, out var p))
var color1 = TableHelper.ParseColor(color);
if (!_pens.TryGetValue(color1, out var p))
{
p = new Pen(color);
_pens[color] = p;
p = new Pen(color1);
_pens[color1] = p;
}
return p;
@ -57,28 +59,28 @@ namespace BizHawk.Client.EmuHawk
Image = new Bitmap(width, height);
}
public void Clear(Color color)
public void Clear([LuaColorParam] object color)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.Clear(color);
boxBackground.Clear(TableHelper.ParseColor(color));
}
public void SetDefaultForegroundColor(Color color)
public void SetDefaultForegroundColor([LuaColorParam] object color)
{
_defaultForeground = color;
_defaultForeground = TableHelper.ParseColor(color);
}
public void SetDefaultBackgroundColor(Color color)
public void SetDefaultBackgroundColor([LuaColorParam] object color)
{
_defaultBackground = color;
_defaultBackground = TableHelper.ParseColor(color);
}
public void SetDefaultTextBackground(Color color)
public void SetDefaultTextBackground([LuaColorParam] object color)
{
_defaultTextBackground = color;
_defaultTextBackground = TableHelper.ParseColor(color);
}
public void DrawBezier(LuaTable points, Color color)
public void DrawBezier(LuaTable points, [LuaColorParam] object color)
{
var pointsArr = new Point[4];
@ -95,10 +97,10 @@ namespace BizHawk.Client.EmuHawk
}
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawBezier(GetPen(color), pointsArr[0], pointsArr[1], pointsArr[2], pointsArr[3]);
boxBackground.DrawBezier(GetPen(TableHelper.ParseColor(color)), pointsArr[0], pointsArr[1], pointsArr[2], pointsArr[3]);
}
public void DrawBox(int x, int y, int x2, int y2, Color? line = null, Color? background = null)
public void DrawBox(int x, int y, int x2, int y2, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
if (x < x2)
{
@ -121,9 +123,9 @@ namespace BizHawk.Client.EmuHawk
}
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawRectangle(GetPen(line ?? _defaultForeground), x, y, x2, y2);
boxBackground.DrawRectangle(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x, y, x2, y2);
var bg = background ?? _defaultBackground;
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
if (bg.HasValue)
{
boxBackground = Graphics.FromImage(Image);
@ -131,9 +133,9 @@ namespace BizHawk.Client.EmuHawk
}
}
public void DrawEllipse(int x, int y, int width, int height, Color? line = null, Color? background = null)
public void DrawEllipse(int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
var bg = background ?? _defaultBackground;
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
var boxBackground = Graphics.FromImage(Image);
if (bg.HasValue)
{
@ -142,7 +144,7 @@ namespace BizHawk.Client.EmuHawk
boxBackground = Graphics.FromImage(Image);
}
boxBackground.DrawEllipse(GetPen(line ?? _defaultForeground), x, y, width, height);
boxBackground.DrawEllipse(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x, y, width, height);
}
public void DrawIcon(string path, int x, int y, int? width = null, int? height = null)
@ -210,22 +212,23 @@ namespace BizHawk.Client.EmuHawk
boxBackground.DrawImage(img, destRect, sourceX, sourceY, sourceWidth, sourceHeight, GraphicsUnit.Pixel);
}
public void DrawLine(int x1, int y1, int x2, int y2, Color? color = null)
public void DrawLine(int x1, int y1, int x2, int y2, [LuaColorParam] object color = null)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawLine(GetPen(color ?? _defaultForeground), x1, y1, x2, y2);
boxBackground.DrawLine(GetPen(TableHelper.SafeParseColor(color) ?? _defaultForeground), x1, y1, x2, y2);
}
public void DrawAxis(int x, int y, int size, Color? color = null)
public void DrawAxis(int x, int y, int size, [LuaColorParam] object color = null)
{
DrawLine(x + size, y, x - size, y, color);
DrawLine(x, y + size, x, y - size, color);
var color1 = TableHelper.SafeParseColor(color);
DrawLine(x + size, y, x - size, y, color1);
DrawLine(x, y + size, x, y - size, color1);
}
public void DrawArc(int x, int y, int width, int height, int startAngle, int sweepAngle, Color? line = null)
public void DrawArc(int x, int y, int width, int height, int startAngle, int sweepAngle, [LuaColorParam] object line = null)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawArc(GetPen(line ?? _defaultForeground), x, y, width, height, startAngle, sweepAngle);
boxBackground.DrawArc(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x, y, width, height, startAngle, sweepAngle);
}
public void DrawPie(
@ -235,10 +238,10 @@ namespace BizHawk.Client.EmuHawk
int height,
int startAngle,
int sweepAngle,
Color? line = null,
Color? background = null)
[LuaColorParam] object line = null,
[LuaColorParam] object background = null)
{
var bg = background ?? _defaultBackground;
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
var boxBackground = Graphics.FromImage(Image);
if (bg.HasValue)
{
@ -247,16 +250,16 @@ namespace BizHawk.Client.EmuHawk
boxBackground = Graphics.FromImage(Image);
}
boxBackground.DrawPie(GetPen(line ?? _defaultForeground), x + 1, y + 1, width - 1, height - 1, startAngle, sweepAngle);
boxBackground.DrawPie(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x + 1, y + 1, width - 1, height - 1, startAngle, sweepAngle);
}
public void DrawPixel(int x, int y, Color? color = null)
public void DrawPixel(int x, int y, [LuaColorParam] object color = null)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawLine(GetPen(color ?? _defaultForeground), x, y, x + 0.1F, y);
boxBackground.DrawLine(GetPen(TableHelper.SafeParseColor(color) ?? _defaultForeground), x, y, x + 0.1F, y);
}
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, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
var pointsList = TableHelper.EnumerateValues<LuaTable>(points)
.Select(table => TableHelper.EnumerateValues<double>(table).ToList()).ToList();
@ -269,8 +272,8 @@ namespace BizHawk.Client.EmuHawk
}
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawPolygon(GetPen(line ?? _defaultForeground), pointsArr);
var bg = background ?? _defaultBackground;
boxBackground.DrawPolygon(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), pointsArr);
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
if (bg.HasValue)
{
boxBackground = Graphics.FromImage(Image);
@ -278,9 +281,9 @@ namespace BizHawk.Client.EmuHawk
}
}
public void DrawRectangle(int x, int y, int width, int height, Color? line = null, Color? background = null)
public void DrawRectangle(int x, int y, int width, int height, [LuaColorParam] object line = null, [LuaColorParam] object background = null)
{
var bg = background ?? _defaultBackground;
var bg = TableHelper.SafeParseColor(line) ?? _defaultBackground;
var boxBackground = Graphics.FromImage(Image);
if (bg.HasValue)
{
@ -288,15 +291,15 @@ namespace BizHawk.Client.EmuHawk
boxBackground = Graphics.FromImage(Image);
}
boxBackground.DrawRectangle(GetPen(line ?? _defaultForeground), x, y, width, height);
boxBackground.DrawRectangle(GetPen(TableHelper.SafeParseColor(background) ?? _defaultForeground), x, y, width, height);
}
public void DrawText(
int x,
int y,
string message,
Color? foreColor = null,
Color? backColor = null,
[LuaColorParam] object foreColor = null,
[LuaColorParam] object backColor = null,
int? fontSize = null,
string fontFamily = null,
string fontStyle = null,
@ -373,10 +376,10 @@ namespace BizHawk.Client.EmuHawk
}
Rectangle rect = new Rectangle(new Point(x, y), sizeOfText);
boxBackground = Graphics.FromImage(Image);
boxBackground.FillRectangle(GetBrush(backColor ?? _defaultTextBackground.Value), rect);
boxBackground.FillRectangle(GetBrush(TableHelper.SafeParseColor(backColor) ?? _defaultTextBackground.Value), rect);
boxBackground = Graphics.FromImage(Image);
boxBackground.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
boxBackground.DrawString(message, font, new SolidBrush(foreColor ?? Color.Black), x, y);
boxBackground.DrawString(message, font, new SolidBrush(TableHelper.SafeParseColor(foreColor) ?? Color.Black), x, y);
}
public Point GetMouse()

View File

@ -44,7 +44,7 @@ namespace BizHawk.Client.EmuHawk
}
if (true /*NLua.Lua.WhichLua == "NLua"*/) _lua["keepalives"] = _lua.NewTable();
_th = new NLuaTableHelper(_lua);
_th = new NLuaTableHelper(_lua, LogToLuaConsole);
_displayManager = displayManager;
_inputManager = inputManager;
_mainForm = mainForm;