-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathGuiLuaLibrary.cs
More file actions
389 lines (355 loc) · 18.5 KB
/
GuiLuaLibrary.cs
File metadata and controls
389 lines (355 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System.Drawing;
using System.Linq;
using BizHawk.Client.Common;
using NLua;
namespace BizHawk.Client.EmuHawk
{
public sealed class GuiLuaLibrary : LuaLibraryBase, IDisposable
{
private DisplaySurfaceID _rememberedSurfaceID = DisplaySurfaceID.EmuCore;
public GuiLuaLibrary(ILuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
public override string Name => "gui";
private DisplaySurfaceID UseOrFallback(string surfaceName)
=> DisplaySurfaceIDParser.Parse(surfaceName) ?? _rememberedSurfaceID;
#pragma warning disable CS0612
#pragma warning disable CS0618
[LuaDeprecatedMethod]
[LuaMethod("DrawNew", "Changes drawing target to the specified lua surface name. This may clobber any previous drawing to this surface (pass false if you don't want it to)")]
public void DrawNew(string name, bool? clear = true)
=> APIs.Gui.DrawNew(name, clear ?? true);
[LuaDeprecatedMethod]
[LuaMethod("DrawFinish", "Finishes drawing to the current lua surface and causes it to get displayed.")]
public void DrawFinish()
=> APIs.Gui.DrawFinish();
#pragma warning restore CS0612
#pragma warning restore CS0618
[LuaMethodExample("gui.addmessage( \"Some message\" );")]
[LuaMethod("addmessage", "Adds a message to the OSD's message area")]
public void AddMessage(string message)
=> APIs.Gui.AddMessage(message);
[LuaMethodExample("gui.clearGraphics( );")]
[LuaMethod("clearGraphics", "clears all lua drawn graphics from the screen")]
public void ClearGraphics(string surfaceName = null)
=> APIs.Gui.ClearGraphics(surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.cleartext( );")]
[LuaMethod("cleartext", "clears all text created by gui.text()")]
public void ClearText()
=> APIs.Gui.ClearText();
[LuaMethodExample("gui.defaultForeground( 0x000000FF );")]
[LuaMethod("defaultForeground", "Sets the default foreground color to use in drawing methods, white by default")]
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([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([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")]
public void SetDefaultPixelFont(string fontfamily)
=> APIs.Gui.SetDefaultPixelFont(fontfamily);
[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,
[LuaColorParam] object color,
string surfaceName = null)
{
try
{
var pointsArr = new Point[4];
var i = 0;
foreach (var point in _th.EnumerateValues<LuaTable>(points)
.Select(table => _th.EnumerateValues<long>(table).ToList()))
{
pointsArr[i] = new Point((int) point[0], (int) point[1]);
i++;
if (i >= 4)
{
break;
}
}
APIs.Gui.DrawBezier(pointsArr[0], pointsArr[1], pointsArr[2], pointsArr[3], _th.ParseColor(color), surfaceID: UseOrFallback(surfaceName));
}
catch (Exception)
{
// ignored
}
}
[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,
[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,
[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(
name: "drawIcon",
description: "Draws the image in the given .ico file to the surface specified by the surfaceName parameter, or the current surface if nil/unset."
+ " The image will be positioned such that its top-left corner will be at (x, y) on the surface."
+ " If width and height are both nil/unset, the image will be drawn at full size (100%). If both are specified, the image will be stretched to that size.")]
public void DrawIcon(
string path,
int x,
int y,
int? width = null,
int? height = null,
string surfaceName = null)
=> APIs.Gui.DrawIcon(path, x, y, width, height, surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("""
gui.drawImage("C:\\sample.bmp", 16, 32, 18, 24, false);
""")]
[LuaMethod(
name: "drawImage",
description: "Draws the image in the given file (.bmp, .gif, .jpg, .png, or .tif) to the surface specified by the surfaceName parameter, or the current surface if nil/unset."
+ " The image will be positioned such that its top-left corner will be at (x, y) on the surface."
+ " If width and height are both nil/unset, the image will be drawn at full size (100%). If both are specified, the image will be stretched to that size." // technically width or height can be specified w/o the other but let's leave that as UB
+ " If true is passed for the cache parameter, or if it's omitted, the file contents will be cached and re-used next time this function is called with the same path. The cache can be cleared with gui.clearImageCache.")]
public void DrawImage(
string path,
int x,
int y,
int? width = null,
int? height = null,
bool cache = true,
string surfaceName = null)
=> APIs.Gui.DrawImage(path, x, y, width, height, cache, surfaceID: UseOrFallback(surfaceName));
[LuaMethodExample("gui.clearImageCache( );")]
[LuaMethod("clearImageCache", "clears the image cache that is built up by using gui.drawImage, also releases the file handle for cached images")]
public void ClearImageCache()
=> APIs.Gui.ClearImageCache();
[LuaMethodExample("""
gui.drawImageRegion("C:\\sample.png", 11, 22, 33, 44, 21, 43, 34, 45);
""")]
[LuaMethod(
name: "drawImageRegion",
description: "Draws part of the image in the given file (.bmp, .gif, .jpg, .png, or .tif) to the surface specified by the surfaceName parameter, or the current surface if nil/unset."
+ " Consult this diagram to see its usage (renders embedded on the TASVideos Wiki): [https://user-images.githubusercontent.com/13409956/198868522-55dc1e5f-ae67-4ebb-a75f-558656cb4468.png|alt=Diagram showing how to use forms.drawImageRegion]"
+ " The file contents will be cached and re-used next time this function is called with the same path. The cache can be cleared with gui.clearImageCache.")]
public void DrawImageRegion(
string path,
int source_x,
int source_y,
int source_width,
int source_height,
int dest_x,
int dest_y,
int? dest_width = null,
int? dest_height = null,
string surfaceName = null)
=> APIs.Gui.DrawImageRegion(path, source_x, source_y, source_width, source_height, dest_x, dest_y, dest_width, dest_height, surfaceID: UseOrFallback(surfaceName));
[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,
[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,
[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,
[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,
[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(
name: "drawPolygon",
description: "Draws a polygon (cyclic polyline) to the surface specified by the surfaceName parameter, or the current surface if nil/unset."
+ " The polygon must be given as a list of length-2 lists (co-ordinate pairs). Each pair is interpreted as the absolute co-ordinates of one of the vertices, and these are joined together in sequence to form a polygon. The last is connected to the first; you DON'T need to end with a copy of the first to close the cycle."
+ " If the offsetX and offsetY parameters are both specified, the whole polygon will be offset by that amount." // technically x or y can be specified w/o the other but let's leave that as UB
+ " If a value is passed for the line parameter, the polygon's edges are drawn in that color (i.e. the stroke color)."
+ " If a value is passed for the background parameter, the polygon's face is filled in that color.")]
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<long>(table).ToList()).ToList();
try
{
var pointsArr = new Point[pointsList.Count];
var i = 0;
foreach (var point in pointsList)
{
pointsArr[i] = new Point((int) point[0] + (offsetX ?? 0), (int) point[1] + (offsetY ?? 0));
i++;
}
APIs.Gui.DrawPolygon(pointsArr, _th.SafeParseColor(line), _th.SafeParseColor(background), surfaceID: UseOrFallback(surfaceName));
}
catch (Exception)
{
// ignored
}
}
[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,
[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", "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 DrawString(
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: x,
y: y,
message: message,
forecolor: _th.SafeParseColor(forecolor),
backcolor: _th.SafeParseColor(backcolor),
fontsize: fontsize,
fontfamily: fontfamily,
fontstyle: fontstyle,
horizalign: horizalign,
vertalign: vertalign,
surfaceID: UseOrFallback(surfaceName));
/// <remarks>TODO do this in Lua binding code?</remarks>
[LuaMethodExample("gui.drawText( 16, 32, \"Some message\", 0x7F0000FF, 0x00007FFF, 8, \"Arial Narrow\", \"bold\", \"center\", \"middle\" );")]
[LuaMethod("drawText", "alias for gui.drawString")]
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)
=> DrawString(
x: x,
y: y,
message: message,
forecolor: forecolor,
backcolor: backcolor,
fontsize: fontsize,
fontfamily: fontfamily,
fontstyle: fontstyle,
horizalign: horizalign,
vertalign: vertalign,
surfaceName: 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 PixelText(
int x,
int y,
string message,
[LuaColorParam] object forecolor = null,
[LuaColorParam] object backcolor = null,
string fontfamily = null,
string surfaceName = null)
=> APIs.Gui.PixelText(x, y, message, _th.SafeParseColor(forecolor), _th.SafeParseColor(backcolor) ?? APIs.Gui.GetDefaultTextBackground(), 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. This function is generally much faster than other text drawing functions, at the cost of customization.")]
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 canvas = gui.createcanvas(77, 99, 2, 48);
""")]
[LuaMethod(
name: "createcanvas",
description: "Creates a dedicated canvas window, returning a table containing some callbacks for drawing. These are the LuaCanvas functions in the API reference."
+ " The width and height parameters determine the size of the canvas."
+ " If the x and y parameters are both nil/unset, the form (window) will appear at the default position. If both are specified, the form will be positioned at (x, y) on the screen.")] // technically x can be specified w/o y but let's leave that as UB
[return: LuaCatsType("LuaCanvas")]
public LuaTable CreateCanvas(int width, int height, int? x = null, int? y = null)
{
var canvas = new LuaCanvas(APIs.Emulation, PathEntries, width, height, x, y, _th, LogOutputCallback);
canvas.Show();
LuaFile lf = _luaLibsImpl.CurrentFile;
lf.AddDisposable(canvas);
canvas.FormClosed += (s, e) => lf.RemoveDisposable(canvas);
return _th.ObjectToTable(canvas);
}
[LuaMethodExample("gui.use_surface( \"client\" );")]
[LuaMethod("use_surface", "Stores the name of a surface to draw on, so you don't need to pass it to every draw function. The default is \"emucore\", and the other valid value is \"client\".")]
public void UseSurface(string surfaceName)
{
if (surfaceName == null)
{
Log("Surface name cannot be nil. Pass \"emucore\" to `gui.use_surface` to restore the default.");
return;
}
_rememberedSurfaceID = DisplaySurfaceIDParser.Parse(surfaceName).Value;
}
public void Dispose()
=> APIs.Gui.Dispose();
}
}