Add warning message for Lua functions called with mismatched x/y/w/h

This commit is contained in:
YoshiRulz 2024-08-24 16:34:53 +10:00
parent 884fee6c6d
commit 170d1a62ed
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
1 changed files with 30 additions and 15 deletions

View File

@ -89,8 +89,9 @@ namespace BizHawk.Client.EmuHawk
SetText(button, caption); SetText(button, caption);
form.Controls.Add(button); form.Controls.Add(button);
form.ControlEvents.Add(new LuaWinform.LuaEvent(button.Handle, clickEvent)); form.ControlEvents.Add(new LuaWinform.LuaEvent(button.Handle, clickEvent));
ProcessPositionArguments(x: x, y: y, button); const string FUNC_NAME = "forms.button";
ProcessSizeArguments(width: width, height: height, button); ProcessPositionArguments(x: x, y: y, button, functionName: FUNC_NAME);
ProcessSizeArguments(width: width, height: height, button, functionName: FUNC_NAME);
return (long)button.Handle; return (long)button.Handle;
} }
@ -108,7 +109,8 @@ namespace BizHawk.Client.EmuHawk
var checkbox = new LuaCheckbox(); var checkbox = new LuaCheckbox();
form.Controls.Add(checkbox); form.Controls.Add(checkbox);
SetText(checkbox, caption); SetText(checkbox, caption);
ProcessPositionArguments(x: x, y: y, checkbox); const string FUNC_NAME = "forms.checkbox";
ProcessPositionArguments(x: x, y: y, checkbox, functionName: FUNC_NAME);
return (long)checkbox.Handle; return (long)checkbox.Handle;
} }
@ -180,8 +182,9 @@ namespace BizHawk.Client.EmuHawk
var dropdown = new LuaDropDown(dropdownItems); var dropdown = new LuaDropDown(dropdownItems);
form.Controls.Add(dropdown); form.Controls.Add(dropdown);
ProcessPositionArguments(x: x, y: y, dropdown); const string FUNC_NAME = "forms.dropdown";
ProcessSizeArguments(width: width, height: height, dropdown); ProcessPositionArguments(x: x, y: y, dropdown, functionName: FUNC_NAME);
ProcessSizeArguments(width: width, height: height, dropdown, functionName: FUNC_NAME);
return (long)dropdown.Handle; return (long)dropdown.Handle;
} }
@ -302,8 +305,9 @@ namespace BizHawk.Client.EmuHawk
SetText(label, caption); SetText(label, caption);
form.Controls.Add(label); form.Controls.Add(label);
ProcessPositionArguments(x: x, y: y, label); const string FUNC_NAME = "forms.label";
ProcessSizeArguments(width: width, height: height, label); ProcessPositionArguments(x: x, y: y, label, functionName: FUNC_NAME);
ProcessSizeArguments(width: width, height: height, label, functionName: FUNC_NAME);
return (long)label.Handle; return (long)label.Handle;
} }
@ -378,11 +382,16 @@ namespace BizHawk.Client.EmuHawk
var pictureBox = new LuaPictureBox { TableHelper = _th }; var pictureBox = new LuaPictureBox { TableHelper = _th };
form.Controls.Add(pictureBox); form.Controls.Add(pictureBox);
ProcessPositionArguments(x: x, y: y, pictureBox); const string FUNC_NAME = "forms.pictureBox";
if (width.HasValue && height.HasValue) ProcessPositionArguments(x: x, y: y, pictureBox, functionName: FUNC_NAME);
if (width is int w && height is int h)
{ {
pictureBox.LuaResize(width.Value, height.Value); pictureBox.LuaResize(width: w, height: h);
SetSize(pictureBox, width.Value, height.Value); SetSize(pictureBox, width: w, height: h);
}
else if (width.HasValue || height.HasValue)
{
WarnForMismatchedPair(functionName: FUNC_NAME, kind: "width and height");
} }
return (long)pictureBox.Handle; return (long)pictureBox.Handle;
@ -1242,14 +1251,16 @@ namespace BizHawk.Client.EmuHawk
return 0; return 0;
} }
private void ProcessPositionArguments(int? x, int? y, Control c) private void ProcessPositionArguments(int? x, int? y, Control c, string functionName)
{ {
if (x is int x1 && y is int y1) SetLocation(c, x: x1, y: y1); if (x is int x1 && y is int y1) SetLocation(c, x: x1, y: y1);
else if (x.HasValue || y.HasValue) WarnForMismatchedPair(functionName: functionName, kind: "x and y");
} }
private void ProcessSizeArguments(int? width, int? height, Control c) private void ProcessSizeArguments(int? width, int? height, Control c, string functionName)
{ {
if (width is int w && height is int h) SetSize(c, width: w, height: h); if (width is int w && height is int h) SetSize(c, width: w, height: h);
else if (width.HasValue || height.HasValue) WarnForMismatchedPair(functionName: functionName, kind: "width and height");
} }
[LuaMethodExample("forms.setdropdownitems(dropdown_handle, { \"item1\", \"item2\" });")] [LuaMethodExample("forms.setdropdownitems(dropdown_handle, { \"item1\", \"item2\" });")]
@ -1456,8 +1467,9 @@ namespace BizHawk.Client.EmuHawk
} }
SetText(textbox, caption); SetText(textbox, caption);
ProcessPositionArguments(x: x, y: y, textbox); const string FUNC_NAME = "forms.textbox";
ProcessSizeArguments(width: width, height: height, textbox); ProcessPositionArguments(x: x, y: y, textbox, functionName: FUNC_NAME);
ProcessSizeArguments(width: width, height: height, textbox, functionName: FUNC_NAME);
if (boxtype != null) if (boxtype != null)
{ {
@ -1483,5 +1495,8 @@ namespace BizHawk.Client.EmuHawk
form.Controls.Add(textbox); form.Controls.Add(textbox);
return (long)textbox.Handle; return (long)textbox.Handle;
} }
private void WarnForMismatchedPair(string functionName, string kind)
=> LogOutputCallback($"{functionName}: both {kind} must be set to have any effect");
} }
} }