Lua - implement forms.button(), and the basic backend for calling lua functions on events from user generated controls

This commit is contained in:
andres.delikat 2012-07-10 20:23:19 +00:00
parent 3a8fe7c464
commit 827e2150d7
5 changed files with 95 additions and 4 deletions

View File

@ -332,6 +332,9 @@
<Compile Include="tools\HexFind.Designer.cs">
<DependentUpon>HexFind.cs</DependentUpon>
</Compile>
<Compile Include="tools\LuaButton.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="tools\LuaWinform.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -411,6 +411,7 @@ namespace BizHawk.MultiClient
"newform",
"destroy",
"destroyall",
"button",
};
/****************************************************/
@ -423,7 +424,14 @@ namespace BizHawk.MultiClient
public void console_output(object lua_input)
{
Global.MainForm.LuaConsole1.WriteToOutputWindow(lua_input.ToString());
if (lua_input == null)
{
Global.MainForm.LuaConsole1.WriteToOutputWindow("NULL");
}
else
{
Global.MainForm.LuaConsole1.WriteToOutputWindow(lua_input.ToString());
}
}
public void console_log(object lua_input)
@ -1674,7 +1682,6 @@ namespace BizHawk.MultiClient
public bool forms_destroy(object handle)
{
//TODO: try/catch, error handling, etc
IntPtr ptr = new IntPtr(LuaInt(handle));
foreach (LuaWinform form in LuaForms)
{
@ -1696,5 +1703,39 @@ namespace BizHawk.MultiClient
LuaForms.Remove(form);
}
}
public int forms_button(object form_handle, object caption, LuaFunction lua_event, object X = null, object Y = null)
{
IntPtr ptr = new IntPtr(LuaInt(form_handle));
foreach (LuaWinform form in LuaForms)
{
if (form.Handle == ptr)
{
LuaButton button = new LuaButton();
button.Text = caption.ToString();
form.Controls.Add(button);
form.Control_Events.Add(new LuaWinform.Lua_Event(button.Handle, lua_event));
//button.Click += new System.EventHandler(button.DoLuaClick);
try
{
if (X != null && Y != null)
{
int x = LuaInt(X);
int y = LuaInt(Y);
button.Location = new Point(x, y);
}
}
catch
{
//Do nothing
}
return (int)button.Handle;
}
}
return 0;
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BizHawk.MultiClient.tools
{
class LuaButton : Button
{
private void DoLuaClick(object sender, EventArgs e)
{
LuaWinform parent = this.Parent as LuaWinform;
parent.DoLuaEvent(this.Handle);
}
protected override void OnClick(EventArgs e)
{
DoLuaClick(this, e);
base.OnClick(e);
}
}
}

View File

@ -38,7 +38,7 @@
this.ClientSize = new System.Drawing.Size(284, 262);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "LuaWinform";
this.Text = "LuaWinform";
this.Text = "Lua Dialog";
this.Load += new System.EventHandler(this.LuaWinform_Load);
this.ResumeLayout(false);

View File

@ -12,7 +12,7 @@ namespace BizHawk.MultiClient.tools
{
public partial class LuaWinform : Form
{
public List<LuaFunction> Events = new List<LuaFunction>();
public List<Lua_Event> Control_Events = new List<Lua_Event>();
public LuaWinform()
{
@ -29,5 +29,29 @@ namespace BizHawk.MultiClient.tools
{
Global.MainForm.LuaConsole1.LuaImp.WindowClosed(Handle);
}
public void DoLuaEvent(IntPtr handle)
{
foreach (Lua_Event l_event in Control_Events)
{
if (l_event.Control == handle)
{
l_event.Event.Call();
}
}
}
public class Lua_Event
{
public LuaFunction Event;
public IntPtr Control;
public Lua_Event() { }
public Lua_Event(IntPtr handle, LuaFunction lfunction)
{
Event = lfunction;
Control = handle;
}
}
}
}