lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Make the widget a table instead?

Something like
{ 	_ud= userdata,
	leftclick= function

There's many ways you can attach the table and the userdata (which your C code may need) together. Or even forget the userdata and set x,y,w,h into the table itself?


Max van Rooij (van Rooij Electronics Design & Software Engineering) kirjoitti 7.7.2008 kello 21:35:

Dear all,

I'm new to Lua and very enthusiastic about it, especially in it's ability to interface with C. I've written a GUI in C and have given the users the ability to customize behaviours (such as mouse clicks) in Lua.

I've defined a so called "click-able" region as a struct in C. The user can create a region that's click-able in a lua script with a single line like this:

mywidget1 = widget.create(100,100,10,10) --create click-able region at (x,y) = 100,100 width 10 and height 10

By implementing the __index in C code, the user can write:

function mywidget1:leftclick
print("hurray! a left click!")

Now, the C code is capable to relate the clicked region to the right Lua object and a get a nice "hurray!" printed. Unfortunatly, if my user does:

mywidget1 = widget.create(100,100,10,10)
mywidget2 = widget.create(110,110,20,20)

function mywidget1:leftclick
print("left click widget 1")

function mywidget2:leftclick
print("left click widget 2")

I always get "left click widget 2". This is, because the widgets share the same method table, so the last definition simply overwrites all previous ones. How do I change my C code, so my users can have the above code working?