Programming and Scripting :: dynamic flua button creation



It took me almost 2 hours of error messages before coming up with something to solve my current problem.  I was looking for a way to automatically draw multiple buttons according to a list of button labels. The drawing wasn't a big problem, but i couldn't seem to get the callback functions built into the loop.  I needed a way to avoid having to type a new callback every time I added a string to the list.  The answer was to create a global function and put "callback=<that function>" into the button properties.  The function tests to see which button is being pressed, and acts accordingly.

This script builds a pack of buttons and labels them according to what is in the button_labels table.  Add more items to the table, and you get more buttons.
It draws a single column of buttons, but i'm sure the script could be tweaked to pack them into multiple columns.  Also, the callback is limited...i was just looking for a way to do the same action on each item in the list.

Code Sample
ww = 360
wh = 300

button_labels = {"first button","second button","third button","the fourth"}

w = Window{ww,wh,"Lua FLTK Templates"}
display = Output{140,10,ww-150,25}

buttons = Pack{10,10,120,wh-30}

function do_butt(self)
for i = 1,getn(button_labels) do
if self.label == button_labels[i] then
display.value = "Button label: "..button_labels[i]
break
end
end
end

button = {}
for i = 1,getn(button_labels) do
button[i] = Button{25+i*10,25+i*10,25,25,button_labels[i];callback=do_butt}
end
buttons:end_layout()

w:show()

Very cool. Thanks for sharing.
I am adding that to my notebook of notes!

This is the project that had me trying to figure this out:
http://dsl-mirror.vectori.net/tools/mikshaw/flua_templates.uci
(you'll probably need to right-click, save as)
EDIT: package was renamed to something more appropriate...check the link further down in this thread.

It's a work in progress, which should eventually provide a slew of Lua FLTK templates for reference or for using as a script base.  Currently only the first three are mine, and the rest were taken from the flua package....eventually i hope to have every widget available added to it.

mikshaw, those flua example in the flua_templates.uci are a huge help.  Great examples with the code right there in another window so you can see what's going on.  I can wait till it's done.
It's never done.  Nothing is ever done.  At its current stage (as of last night) I'd say it's beta, but i have no idea how much better it will become....It could be finished now other than adding more widgets, or it might evolve into something more useful.  Typically if i run into a snag that i can't solve within a few hours, that project gets put in the fridge for a little while.  who knows.

One thing i do know is that i'd like to be able to select/copy text from the display window, but that will require loading the files in line by line...not something i want to script at the moment.

Next Page...
original here.