chipmunkav.com Home Search Member List Faq Register Login  
Lua Scripting
LuaInterface: Scripting the .NET CLR with Lua

Thread Starter: Chipmunk   Started: 09-28-2006 8:45 AM   Replies: 0
 Chipmunk AV Forums » Chipmunk AV Controller » Lua Scripting » LuaInterface: Scripting the .NET CLR with Lua
 Printable Version    Next Thread »
  28 Sep 2006, 8:45 AM
Chipmunk is not online. Last active: 20/08/2009 20:17:19 Chipmunk



Top 10 Posts
Joined on 07-19-2006
London, England
Posts 167
LuaInterface: Scripting the .NET CLR with Lua

The Lua scripting engine is fully integrated with the .NET CLR. This means that you can script .NET without having to compile code. Here is a simple script you can attach to a command and run. It displays a .NET Windows Form with two buttons:

luanet.load_assembly("System.Windows.Forms")
luanet.load_assembly("System.Drawing)
Form = luanet.import_type("System.Windows.Forms.Form")
Button = luanet.import_type("System.Windows.Forms.Button")
Point = luanet.import_type("System.Drawing.Point")
 
form1=Form()
button1=Button()
button2=Button()

function handleClick(sender,data)
  if sender.Text=="OK" then
    sender.Text="Clicked"
  else
    sender.Text="OK"
  end
  button1.MouseUp:Remove(handler)
  print(sender:ToString())
end

button1.Text = "OK"
button1.Location=Point(10,10)
button2.Text = "Cancel"
button2.Location=Point(button1.Left, button1.Height + button1.Top + 10)
handler=button1.MouseUp:Add(handleClick)
form1.Text = "My Dialog Box"
form1.HelpButton = true
form1.MaximizeBox = false
form1.MinimizeBox = false
form1.AcceptButton = button1
form1.CancelButton = button2
form1.Controls:Add(button1)
form1.Controls:Add(button2)
form1:ShowDialog()

The next example creates and displays a simple Lua Editor that enables you to test and run scripts:

luanet.load_assembly("System.Windows.Forms")
luanet.load_assembly("System.Drawing")

Form=luanet.import_type("System.Windows.Forms.Form")
TextBox=luanet.import_type("System.Windows.Forms.TextBox")
Label=luanet.import_type("System.Windows.Forms.Label")
ListBox=luanet.import_type("System.Windows.Forms.ListBox")
Button=luanet.import_type("System.Windows.Forms.Button")
Point=luanet.import_type("System.Drawing.Point")
Size=luanet.import_type("System.Drawing.Size")
Lua=luanet.import_type("LuaInterface.Lua")
OpenFileDialog=luanet.import_type("System.Windows.Forms.OpenFileDialog")
File=luanet.import_type("System.IO.File")
StreamReader=luanet.import_type("System.IO.StreamReader")
FileMode=luanet.import_type("System.IO.FileMode")
ScrollBars=luanet.import_type("System.Windows.Forms.ScrollBars")
FormBorderStyle=luanet.import_type("System.Windows.Forms.FormBorderStyle")
FormStartPosition=luanet.import_type("System.Windows.Forms.FormStartPosition")

function clear_click(sender,args)
  code:Clear()
end

function execute_click(sender,args)
  results.Items:Clear()
  result=lua:DoString(code.Text)
  if result then
    for i=0,result.Length-1 do
      results.Items:Add(result[i])
    end
  end
end

function load_click(sender,args)
  open_file:ShowDialog()
  file=StreamReader(open_file.FileName)
  code.Text=file:ReadToEnd()
  file:Close()
end

form = Form()
code = TextBox()
label1 = Label()
execute = Button()
clear = Button()
results = ListBox()
label2 = Label()
load = Button()
lua = Lua()
lua:OpenBaseLib()
open_file = OpenFileDialog()

form:SuspendLayout()

code.Location = Point(16, 24)
code.Multiline = true
code.Name = "Code"
code.Size = Size(440, 128)
code.ScrollBars = ScrollBars.Vertical
code.TabIndex = 0
code.Text = ""

label1.Location = Point(16, 8)
label1.Name = "label1"
label1.Size = Size(100, 16)
label1.TabIndex = 1
label1.Text = "Lua Code:"

execute.Location = Point(96, 160)
execute.Name = "Execute"
execute.TabIndex = 2
execute.Text = "Execute"
execute.Click:Add(execute_click)

clear.Location = Point(176, 160)
clear.Name = "Clear"
clear.TabIndex = 3
clear.Text = "Clear"
clear.Click:Add(clear_click)

results.Location = Point(16, 208)
results.Name = "Results"
results.Size = Size(440, 95)
results.TabIndex = 4

label2.Location = Point(16, 192)
label2.Name = "label2"
label2.Size = Size(100, 16)
label2.TabIndex = 5
label2.Text = "Results:"

load.Location = Point(16, 160)
load.Name = "Load"
load.TabIndex = 6
load.Text = "Load..."
load.Click:Add(load_click)

open_file.DefaultExt = "lua"
open_file.Filter = "Lua Scripts|*.lua|All Files|*.*"
open_file.Title = "Pick a File"

form.AutoScaleBaseSize = Size(5, 13)
form.ClientSize = Size(472, 315)
form.Controls:Add(load)
form.Controls:Add(label2)
form.Controls:Add(results)
form.Controls:Add(clear)
form.Controls:Add(execute)
form.Controls:Add(label1)
form.Controls:Add(code)
form.Name = "MainForm"
form.Text = "LuaNet"
form.FormBorderStyle = FormBorderStyle.Fixed3D
form.StartPosition = FormStartPosition.CenterScreen
form:ResumeLayout(false)

form:ShowDialog()

If you are interested in learning more about scripting the .NET CLR, the following documentation is available:

http://www.chipmunkav.com/support/guide.pdf
http://www.chipmunkav.com/support/luainterface.pdf

Shaun




Chipmunk AV Controller Founder

  
 Page 1 of 1 (1 items)
Chipmunk AV Forums » Chipmunk AV Controller » Lua Scripting » LuaInterface: Scripting the .NET CLR with Lua

You can add attachments
You can post new topics
You can reply to topics
You can delete your posts
You can edit your posts
You can create polls
You can vote in polls
Forum statistics are enabled
Forum is unmoderated