From 846253760e6679b220434c73d519c11da99570a4 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 25 Aug 2024 02:24:22 +1000 Subject: [PATCH] Extract more helpers for finding Lua forms/controls by handle --- .../tools/Lua/Libraries/FormsLuaLibrary.cs | 844 ++++++------------ 1 file changed, 273 insertions(+), 571 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs index a219d30515..10bd0eda65 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs @@ -56,18 +56,8 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("addclick", "adds the given lua function as a click event to the given control")] public void AddClick(long handle, LuaFunction clickEvent) { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - form.ControlEvents.Add(new LuaWinform.LuaEvent(control.Handle, clickEvent)); - return; - } - } - } + var found = FindControlWithHandle(handle, out var form); + if (found is not null) form.ControlEvents.Add(new(found.Handle, clickEvent)); } [LuaMethodExample("local inforbut = forms.button( 333, \"Caption\", function()\r\n\tconsole.log( \"Creates a button control on the given form. The caption property will be the text value on the button. clickEvent is the name of a Lua function that will be invoked when the button is clicked. x, and y are the optional location parameters for the position of the button within the given form. The function returns the handle of the created button. Width and Height are optional, if not specified they will be a default size\" );\r\nend, 2, 48, 18, 24 );")] @@ -122,35 +112,18 @@ namespace BizHawk.Client.EmuHawk public void ClearClicks(long handle) { var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - form.ControlEvents.RemoveAll(x => x.Control == ptr); - return; - } - } - } + var found = FindControlWithHandle(ptr, out var form); + if (found is not null) form.ControlEvents.RemoveAll(x => x.Control == ptr); } [LuaMethodExample("if ( forms.destroy( 332 ) ) then\r\n\tconsole.log( \"Closes and removes a Lua created form with the specified handle. If a dialog was found and removed true is returned, else false\" );\r\nend;")] [LuaMethod("destroy", "Closes and removes a Lua created form with the specified handle. If a dialog was found and removed true is returned, else false")] public bool Destroy(long handle) { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - form.Close(); - _luaForms.Remove(form); - return true; - } - } - - return false; + var form = GetForm(handle); + if (form is null) return false; + form.Close(); + return _luaForms.Remove(form); } [LuaMethodExample("forms.destroyall();")] @@ -198,22 +171,8 @@ namespace BizHawk.Client.EmuHawk { try { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - return form.GetType().GetProperty(property).GetValue(form, null).ToString(); - } - - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - return control.GetType().GetProperty(property).GetValue(control, null).ToString(); - } - } - } + var found = FindFormOrControlWithHandle(handle); + if (found is not null) return found.GetType().GetProperty(property).GetValue(found, null).ToString(); } catch (Exception ex) { @@ -229,22 +188,8 @@ namespace BizHawk.Client.EmuHawk { try { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - return form.Text; - } - - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - return control is LuaDropDown dd ? dd.SelectedItem.ToString() : control.Text; - } - } - } + var found = FindFormOrControlWithHandle(handle); + if (found is not null) return found is LuaDropDown dd ? dd.SelectedItem.ToString() : found.Text; } catch (Exception ex) { @@ -257,31 +202,7 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("if ( forms.ischecked( 332 ) ) then\r\n\tconsole.log( \"Returns the given checkbox's checked property\" );\r\nend;")] [LuaMethod("ischecked", "Returns the given checkbox's checked property")] public bool IsChecked(long handle) - { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - return false; - } - - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - if (control is LuaCheckbox checkbox) - { - return checkbox.Checked; - } - - return false; - } - } - } - - return false; - } + => FindControlWithHandle(handle) is LuaCheckbox { Checked: true }; [LuaMethodExample("local inforlab = forms.label( 333, \"Caption\", 2, 48, 18, 24, false );")] [LuaMethod( @@ -410,24 +331,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.ParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.Clear(color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.Clear(color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -443,24 +350,10 @@ namespace BizHawk.Client.EmuHawk { try { - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.Refresh(); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.Refresh(); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -477,24 +370,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.ParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.SetDefaultForegroundColor(color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.SetDefaultForegroundColor(color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -511,24 +390,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.ParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.SetDefaultBackgroundColor(color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.SetDefaultBackgroundColor(color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -545,24 +410,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.ParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.SetDefaultTextBackground(color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.SetDefaultTextBackground(color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -579,24 +430,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.ParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawBezier(points, color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.DrawBezier(points, color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -621,23 +458,18 @@ namespace BizHawk.Client.EmuHawk { var strokeColor = _th.SafeParseColor(line); var fillColor = _th.SafeParseColor(background); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawBox(x, y, x2, y2, strokeColor, fillColor); - return; + control.DrawBox(x: x, y: y, x2: x2, y2: y2, line: strokeColor, background: fillColor); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -663,23 +495,18 @@ namespace BizHawk.Client.EmuHawk { var strokeColor = _th.SafeParseColor(line); var fillColor = _th.SafeParseColor(background); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawEllipse(x, y, width, height, strokeColor, fillColor); - return; + control.DrawEllipse(x: x, y: y, width: width, height: height, line: strokeColor, background: fillColor); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -707,24 +534,10 @@ namespace BizHawk.Client.EmuHawk } try { - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawIcon(path, x, y, width, height); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.DrawIcon(path, x: x, y: y, width: width, height: height); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -752,23 +565,18 @@ namespace BizHawk.Client.EmuHawk } try { - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawImage(path, x, y, width, height, cache); - return; + control.DrawImage(path, x: x, y: y, width: width, height: height, cache: cache); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -785,24 +593,10 @@ namespace BizHawk.Client.EmuHawk { try { - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.ClearImageCache(); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.ClearImageCache(); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -833,23 +627,27 @@ namespace BizHawk.Client.EmuHawk } try { - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawImageRegion(path, source_x, source_y, source_width, source_height, dest_x, dest_y, dest_width, dest_height); - return; + control.DrawImageRegion( + path, + sourceX: source_x, + sourceY: source_y, + sourceWidth: source_width, + sourceHeight: source_height, + destX: dest_x, + destY: dest_y, + destWidth: dest_width, + destHeight: dest_height); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -867,24 +665,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.SafeParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawLine(x1, y1, x2, y2, color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.DrawLine(x1: x1, y1: y1, x2: x2, y2: y2, color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -901,24 +685,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.SafeParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawAxis(x, y, size, color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.DrawAxis(x: x, y: y, size, color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -944,23 +714,25 @@ namespace BizHawk.Client.EmuHawk try { var strokeColor = _th.SafeParseColor(line); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawArc(x, y, width, height, startangle, sweepangle, strokeColor); - return; + control.DrawArc( + x: x, + y: y, + width: width, + height: height, + startAngle: startangle, + sweepAngle: sweepangle, + strokeColor); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -988,23 +760,26 @@ namespace BizHawk.Client.EmuHawk { var strokeColor = _th.SafeParseColor(line); var fillColor = _th.SafeParseColor(background); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawPie(x, y, width, height, startangle, sweepangle, strokeColor, fillColor); - return; + control.DrawPie( + x: x, + y: y, + width: width, + height: height, + startAngle: startangle, + sweepAngle: sweepangle, + line: strokeColor, + background: fillColor); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -1022,24 +797,10 @@ namespace BizHawk.Client.EmuHawk try { var color1 = _th.SafeParseColor(color); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawPixel(x, y, color1); - return; - } + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) control.DrawPixel(x: x, y: y, color1); + else if (match is Form) LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + else if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } catch (Exception ex) { @@ -1063,23 +824,18 @@ namespace BizHawk.Client.EmuHawk { var strokeColor = _th.SafeParseColor(line); var fillColor = _th.SafeParseColor(background); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawPolygon(points, x, y, strokeColor, fillColor); - return; + control.DrawPolygon(points, x: x, y: y, line: strokeColor, background: fillColor); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -1106,23 +862,24 @@ namespace BizHawk.Client.EmuHawk { var strokeColor = _th.SafeParseColor(line); var fillColor = _th.SafeParseColor(background); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawRectangle(x, y, width, height, strokeColor, fillColor); - return; + control.DrawRectangle( + x: x, + y: y, + width: width, + height: height, + line: strokeColor, + background: fillColor); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -1181,23 +938,28 @@ namespace BizHawk.Client.EmuHawk { var fgColor = _th.SafeParseColor(forecolor); var bgColor = _th.SafeParseColor(backcolor); - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) continue; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return; - } - control.DrawText(x, y, message, fgColor, bgColor, fontsize, fontfamily, fontstyle, horizalign, vertalign); - return; + control.DrawText( + x: x, + y: y, + message: message, + foreColor: fgColor, + backColor: bgColor, + fontSize: fontsize, + fontFamily: fontfamily, + fontStyle: fontstyle, + horizAlign: horizalign, + vertAlign: vertalign); + } + else if (match is Form) + { + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + } + else if (match is not null) + { + LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); } } catch (Exception ex) @@ -1206,6 +968,46 @@ namespace BizHawk.Client.EmuHawk } } + private Control/*?*/ FindControlWithHandle(IntPtr handle) + { + foreach (var form in _luaForms) foreach (Control control in form.Controls) + { + if (control.Handle == handle) return control; + } + return null; + } + + private Control/*?*/ FindControlWithHandle(IntPtr handle, out LuaWinform parentForm) + { + foreach (var form in _luaForms) foreach (Control control in form.Controls) + { + if (control.Handle != handle) continue; + parentForm = form; + return control; + } + parentForm = null; + return null; + } + + private Control/*?*/ FindControlWithHandle(long handle) + => FindControlWithHandle(new IntPtr(handle)); + + private Control/*?*/ FindControlWithHandle(long handle, out LuaWinform parentForm) + => FindControlWithHandle(new IntPtr(handle), out parentForm); + + private Control/*?*/ FindFormOrControlWithHandle(IntPtr handle) + { + foreach (var form in _luaForms) + { + if (form.Handle == handle) return form; + foreach (Control control in form.Controls) if (control.Handle == handle) return control; + } + return null; + } + + private Control/*?*/ FindFormOrControlWithHandle(long handle) + => FindFormOrControlWithHandle(new IntPtr(handle)); + // It'd be great if these were simplified into 1 function, but I cannot figure out how to return a LuaTable from this class [LuaMethodExample("local inforget = forms.getMouseX( 334 );")] [LuaMethod( @@ -1215,23 +1017,15 @@ namespace BizHawk.Client.EmuHawk { try { - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) return control.GetMouse().X; + if (match is Form) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return 0; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) return default; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return default; - } - return control.GetMouse().X; + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + return 0; } + if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); + return default; } catch (Exception ex) { @@ -1249,23 +1043,15 @@ namespace BizHawk.Client.EmuHawk { try { - var ptr = new IntPtr(componentHandle); - foreach (var form in _luaForms) + var match = FindFormOrControlWithHandle(componentHandle); + if (match is LuaPictureBox control) return control.GetMouse().Y; + if (match is Form) { - if (form.Handle == ptr) - { - LogOutputCallback(ERR_MSG_DRAW_ON_FORM); - return 0; - } - var match = form.Controls().FirstOrDefault(c => c.Handle == ptr); - if (match is null) return default; - if (match is not LuaPictureBox control) - { - LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); - return default; - } - return control.GetMouse().Y; + LogOutputCallback(ERR_MSG_DRAW_ON_FORM); + return 0; } + if (match is not null) LogOutputCallback(ERR_MSG_CONTROL_NOT_LPB); + return default; } catch (Exception ex) { @@ -1293,27 +1079,11 @@ namespace BizHawk.Client.EmuHawk { try { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) + if (FindControlWithHandle(handle) is LuaDropDown ldd) { - if (form.Handle == ptr) - { - return; - } - - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - if (control is LuaDropDown ldd) - { - var dropdownItems = _th.EnumerateValues(items).ToList(); - if (alphabetize) dropdownItems.Sort(); - ldd.SetItems(dropdownItems); - } - return; - } - } + var dropdownItems = _th.EnumerateValues(items).ToList(); + if (alphabetize) dropdownItems.Sort(); + ldd.SetItems(dropdownItems); } } catch (Exception ex) @@ -1326,26 +1096,8 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("setlocation", "Sets the location of a control or form by passing in the handle of the created object")] public void SetLocation(long handle, int x, int y) { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - SetLocation(form, x, y); - return; - } - else - { - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - SetLocation(control, x, y); - return; - } - } - } - } + var found = FindFormOrControlWithHandle(handle); + if (found is not null) SetLocation(found, x: x, y: y); } /// @@ -1356,36 +1108,17 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("setproperty", "Attempts to set the given property of the widget with the given value. Note: not all properties will be able to be represented for the control to accept")] public void SetProperty(long handle, string property, object value) { + var c = FindFormOrControlWithHandle(handle); + if (c is null) return; // relying on exceptions for error handling here - void ParseAndSet(Control c) - { - var pi = c.GetType().GetProperty(property) ?? throw new Exception($"no property with the identifier {property}"); - var pt = pi.PropertyType; - 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); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - ParseAndSet(form); - return; - } - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - ParseAndSet(control); - return; - } - } - } + var pi = c.GetType().GetProperty(property) ?? throw new Exception($"no property with the identifier {property}"); + var pt = pi.PropertyType; + 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); } [LuaMethodExample("local coforcre = forms.createcolor( 0x7F, 0x3F, 0x1F, 0xCF );")] @@ -1399,52 +1132,21 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("setsize", "TODO")] public void SetSize(long handle, int width, int height) { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) + var control = FindFormOrControlWithHandle(handle); + if (control is Form form) { - if (form.Handle == ptr) - { - form.ClientSize = UIHelper.Scale(new Size(width, height)); - return; - } - else - { - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - SetSize(control, width, height); - return; - } - } - } + form.ClientSize = UIHelper.Scale(new Size(width: width, height: height)); + return; } + if (control is not null) SetSize(control, width: width, height: height); } [LuaMethodExample("forms.settext( 332, \"Caption\" );")] [LuaMethod("settext", "Sets the text property of a control or form by passing in the handle of the created object")] public void Settext(long handle, string caption) { - var ptr = new IntPtr(handle); - foreach (var form in _luaForms) - { - if (form.Handle == ptr) - { - SetText(form, caption); - return; - } - else - { - foreach (Control control in form.Controls) - { - if (control.Handle == ptr) - { - SetText(control, caption); - return; - } - } - } - } + var found = FindFormOrControlWithHandle(handle); + if (found is not null) SetText(found, caption); } [LuaMethodExample("local infortex = forms.textbox( 333, \"Caption\", 18, 24, \"HEX\", 2, 48, true, false, \"Both\" );")]