-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathLuaPictureBox.cs
More file actions
426 lines (373 loc) · 11.3 KB
/
LuaPictureBox.cs
File metadata and controls
426 lines (373 loc) · 11.3 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using BizHawk.Bizware.Graphics;
using BizHawk.Client.Common;
using BizHawk.Common.CollectionExtensions;
using NLua;
namespace BizHawk.Client.EmuHawk
{
public class LuaPictureBox : PictureBox
{
private readonly Dictionary<string, Image> _imageCache = new Dictionary<string, Image>();
private readonly SolidBrush _brush = new(default);
private readonly Pen _pen = new(default(Color));
private readonly Action<string> LogOutputCallback;
private readonly NLuaTableHelper TableHelper;
private SolidBrush GetBrush([LuaColorParam] object color)
{
_brush.Color = TableHelper.ParseColor(color);
return _brush;
}
private Pen GetPen([LuaColorParam] object color)
{
_pen.Color = TableHelper.ParseColor(color);
return _pen;
}
private Color _defaultForeground = Color.Black;
private Color? _defaultBackground;
private Color? _defaultTextBackground = Color.FromArgb(128, 0, 0, 0);
public LuaPictureBox(NLuaTableHelper tableHelper, Action<string> logOutputCallback)
{
Image = BitmapBuffer.CreateBitmapObject(Size);
LogOutputCallback = logOutputCallback;
TableHelper = tableHelper;
}
public void LuaResize(int width, int height)
{
Width = width;
Height = height;
Image = BitmapBuffer.CreateBitmapObject(Size);
}
public void Clear([LuaColorParam] object color)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.Clear(TableHelper.ParseColor(color));
}
public void SetDefaultForegroundColor([LuaColorParam] object color)
{
_defaultForeground = TableHelper.ParseColor(color);
}
public void SetDefaultBackgroundColor([LuaColorParam] object color)
{
_defaultBackground = TableHelper.ParseColor(color);
}
public void SetDefaultTextBackground([LuaColorParam] object color)
{
_defaultTextBackground = TableHelper.ParseColor(color);
}
public void DrawBezier(LuaTable points, [LuaColorParam] object color)
{
var pointsArr = new Point[4];
var i = 0;
foreach (var point in TableHelper.EnumerateValues<LuaTable>(points)
.Select(table => TableHelper.EnumerateValues<long>(table).ToList()))
{
pointsArr[i] = new Point((int) point[0], (int) point[1]);
i++;
if (i >= 4)
{
break;
}
}
var boxBackground = Graphics.FromImage(Image);
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,
[LuaColorParam] object line,
[LuaColorParam] object background)
{
if (x < x2)
{
x2 = Math.Abs(x - x2);
}
else
{
x2 = x - x2;
x -= x2;
}
if (y < y2)
{
y2 = Math.Abs(y - y2);
}
else
{
y2 = y - y2;
y -= y2;
}
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawRectangle(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x, y, x2, y2);
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
if (bg.HasValue)
{
boxBackground = Graphics.FromImage(Image);
boxBackground.FillRectangle(GetBrush(bg.Value), x + 1, y + 1, x2 - 1, y2 - 1);
}
}
public void DrawEllipse(
int x,
int y,
int width,
int height,
[LuaColorParam] object line,
[LuaColorParam] object background)
{
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
var boxBackground = Graphics.FromImage(Image);
if (bg.HasValue)
{
var brush = GetBrush(bg.Value);
boxBackground.FillEllipse(brush, x, y, width, height);
boxBackground = Graphics.FromImage(Image);
}
boxBackground.DrawEllipse(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x, y, width, height);
}
public void DrawIcon(string path, int x, int y, int? width, int? height, string functionName)
{
Icon icon;
if (width is int w && height is int h)
{
icon = new Icon(path, width: w, height: h);
}
else
{
if (width is not null || height is not null) WarnForMismatchedPair(functionName: functionName, kind: "width and height");
icon = new Icon(path);
}
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawIcon(icon, x, y);
}
public void DrawImage(string path, int x, int y, int? width, int? height, bool cache)
{
if (!_imageCache.TryGetValue(path, out var img))
{
img = Image.FromFile(path);
if (cache)
{
_imageCache.Add(path, img);
}
}
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawImage(img, x, y, width ?? img.Width, height ?? img.Height);
}
public void ClearImageCache()
{
foreach (var image in _imageCache)
{
image.Value.Dispose();
}
_imageCache.Clear();
}
public void DrawImageRegion(
string path,
int sourceX,
int sourceY,
int sourceWidth,
int sourceHeight,
int destX,
int destY,
int? destWidth,
int? destHeight)
{
var img = _imageCache.GetValueOrPut(path, Image.FromFile);
var destRect = new Rectangle(destX, destY, destWidth ?? sourceWidth, destHeight ?? sourceHeight);
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawImage(img, destRect, sourceX, sourceY, sourceWidth, sourceHeight, GraphicsUnit.Pixel);
}
public void DrawLine(int x1, int y1, int x2, int y2, [LuaColorParam] object color)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawLine(GetPen(TableHelper.SafeParseColor(color) ?? _defaultForeground), x1, y1, x2, y2);
}
public void DrawAxis(int x, int y, int size, [LuaColorParam] object 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,
[LuaColorParam] object line)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawArc(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x, y, width, height, startAngle, sweepAngle);
}
public void DrawPie(
int x,
int y,
int width,
int height,
int startAngle,
int sweepAngle,
[LuaColorParam] object line,
[LuaColorParam] object background)
{
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
var boxBackground = Graphics.FromImage(Image);
if (bg.HasValue)
{
var brush = GetBrush(bg.Value);
boxBackground.FillPie(brush, x, y, width, height, startAngle, sweepAngle);
boxBackground = Graphics.FromImage(Image);
}
boxBackground.DrawPie(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x + 1, y + 1, width - 1, height - 1, startAngle, sweepAngle);
}
public void DrawPixel(int x, int y, [LuaColorParam] object color)
{
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawLine(GetPen(TableHelper.SafeParseColor(color) ?? _defaultForeground), x, y, x + 0.1F, y);
}
public void DrawPolygon(
LuaTable points,
int? x,
int? y,
[LuaColorParam] object line,
[LuaColorParam] object background)
{
var pointsList = TableHelper.EnumerateValues<LuaTable>(points)
.Select(table => TableHelper.EnumerateValues<long>(table).ToList()).ToList();
var pointsArr = new Point[pointsList.Count];
var x1 = x ?? 0;
var y1 = y ?? 0;
var i = 0;
foreach (var point in pointsList)
{
pointsArr[i++] = new(x1 + (int) point[0], y1 + (int) point[1]);
}
var boxBackground = Graphics.FromImage(Image);
boxBackground.DrawPolygon(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), pointsArr);
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
if (bg.HasValue)
{
boxBackground = Graphics.FromImage(Image);
boxBackground.FillPolygon(GetBrush(bg.Value), pointsArr);
}
}
public void DrawRectangle(
int x,
int y,
int width,
int height,
[LuaColorParam] object line,
[LuaColorParam] object background)
{
var bg = TableHelper.SafeParseColor(background) ?? _defaultBackground;
var boxBackground = Graphics.FromImage(Image);
if (bg.HasValue)
{
boxBackground.FillRectangle(GetBrush(bg.Value), x, y, width, height);
boxBackground = Graphics.FromImage(Image);
}
boxBackground.DrawRectangle(GetPen(TableHelper.SafeParseColor(line) ?? _defaultForeground), x, y, width, height);
}
public void DrawText(
int x,
int y,
string message,
[LuaColorParam] object foreColor,
[LuaColorParam] object backColor,
int? fontSize,
string fontFamily,
string fontStyle,
string horizAlign,
string vertAlign)
{
var family = FontFamily.GenericMonospace;
if (fontFamily != null)
{
family = new FontFamily(fontFamily);
}
var fStyle = FontStyle.Regular;
if (fontStyle != null)
{
switch (fontStyle.ToLowerInvariant())
{
default:
case "regular":
break;
case "bold":
fStyle = FontStyle.Bold;
break;
case "italic":
fStyle = FontStyle.Italic;
break;
case "strikethrough":
fStyle = FontStyle.Strikeout;
break;
case "underline":
fStyle = FontStyle.Underline;
break;
}
}
var f = new StringFormat(StringFormat.GenericDefault);
var font = new Font(family, fontSize ?? 12, fStyle, GraphicsUnit.Pixel);
var boxBackground = Graphics.FromImage(Image);
Size sizeOfText = boxBackground.MeasureString(message, font, 0, f).ToSize();
if (horizAlign != null)
{
switch (horizAlign.ToLowerInvariant())
{
default:
case "left":
break;
case "center":
case "middle":
x -= sizeOfText.Width / 2;
break;
case "right":
x -= sizeOfText.Width;
break;
}
}
if (vertAlign != null)
{
switch (vertAlign.ToLowerInvariant())
{
default:
case "top":
break;
case "center":
case "middle":
y -= sizeOfText.Height / 2;
break;
case "bottom":
y -= sizeOfText.Height;
break;
}
}
Rectangle rect = new Rectangle(new Point(x, y), sizeOfText);
boxBackground = Graphics.FromImage(Image);
boxBackground.FillRectangle(GetBrush(TableHelper.SafeParseColor(backColor) ?? _defaultTextBackground.Value), rect);
boxBackground = Graphics.FromImage(Image);
boxBackground.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
boxBackground.DrawString(message, font, GetBrush(TableHelper.SafeParseColor(foreColor) ?? Color.Black), x, y);
}
public Point GetMouse()
{
var p = PointToClient(MousePosition);
return p;
}
private void DoLuaClick(object sender, EventArgs e)
{
LuaWinform parent = Parent as LuaWinform;
parent?.DoLuaEvent(Handle);
}
protected override void OnClick(EventArgs e)
{
DoLuaClick(this, e);
base.OnClick(e);
}
private void WarnForMismatchedPair(string functionName, string kind)
=> LogOutputCallback($"{functionName}: both {kind} must be set to have any effect");
}
}