dynamic flua button creation


Forum: Programming and Scripting
Topic: dynamic flua button creation
started by: mikshaw

Posted by mikshaw on Jan. 05 2006,18:57
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()

Posted by roberts on Jan. 05 2006,19:40
Very cool. Thanks for sharing.
I am adding that to my notebook of notes!

Posted by mikshaw on Jan. 05 2006,21:52
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.

Posted by clacker on Jan. 07 2006,13:05
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.
Posted by mikshaw on Jan. 07 2006,15:12
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.

Posted by mikshaw on Jan. 07 2006,19:00
Another update to the package.
Now the display is editable.  The preview and save buttons now act on the text in the display window instead of on the original file, so you can make changes to the display and preview or save the changes.  If you are messing around with a script it's probably a good idea to turn on "in xterm" so you can receive any error messages it may generate.

Posted by mikshaw on Jan. 08 2006,04:54
I'm getting an increasing number of random segfaults as the number of scripts increases...don't know what's causing it at the moment, but hopefully it'll be fixed soon.  Flua loops seem to be a little buggy sometimes.
Posted by newOldUser on Jan. 08 2006,13:27
mikshaw,   very nice!  Good educational tool for Flua programmers. Thank you for sharing.  

Trying to program by reading the doco is one thing but being able to  see live examples is another...Wow.  In addition being able to 'fiddle' with the code and preview the change... just great.

What Flua documentation do you use? Are there any websites that seem to be helpful?

Posted by mikshaw on Jan. 08 2006,15:16
The only documentation I have are the widget docs at < http://lua-fltk.sourceforge.net/ > and the Lua4 manual at < http://www.lua.org/manual/4.0/ >
Most of my learning has come from other scripts.

Neither one will tell you everything about scripting flua, but the lua manual is probably the most helpful.  The widget manual is neither well written nor complete (no offense to the author, but it's sadly true). Its only use to me is to remind me of the widget names and some of their properties.  Once i get my reference tool finished, the widget manual will be useless to me.

I've goooogled at least a few times and there is very little mention of flua online, let alone documentation.  Sadly, it may remain a dead project.
I have hope, though, that as more people try DSL and its clones, this tiny interpreter will become more well-known. Maybe the author or someone else with programming experience will pick it up and continue development.  As far as I've found, the nearest thing still in development is tcl/tk, which, while still quite small, is several times larger than flua.

Note on the segmentation faults:
I changed the script a little (haven't uploaded yet) and the segfaults seem to have gone.  The number of items in the "button" table is now checked only once, and that number is used in the comparison loop.  Also, loading of the scripts into the display area is now done with a separate button (select the script, then press load).  This means more work for the user, but i think flua was choking when it had to run through a loop and load a file with every click....more so as the table grew in size.

Posted by mikshaw on Jan. 08 2006,17:09
Package updates:
Fixed (or at least reduced the occurrence of) the segfault problem.
Added more widgets.
Minor layout changes.
Renamed the package (most of the scripts are not templates).
< http://dsl-mirror.vectori.net/tools/mikshaw/flua_reference.uci >

Posted by John on Jan. 09 2006,04:08
Yeah, that UCI is really nice, that's the first working example dial that I've seen.  I think I'll find this very useful :-).

Lua-FLTK seemed to be sitting dormant until we picked it up here.  I am sure it has to do with the lack of good documentation.  It is really a shame because I don't think a faster gui-scripting language exists.



Posted by mikshaw on Jan. 09 2006,04:30
Thanks.  I'm really enjoying this....not only can it help others, but this is helping me a lot too.  There are still a few widgets i haven't explored yet, and a *lot* of lua that i haven't touched, but each day it gets easier, and you start to see more possibilities.

This project isn't being versioned, as you've probably noticed, but it does include a changelog.  All I'm doing right now is minor tweaks....tomorrow i'll put up what will probably be the last daily, and after that i'll put up a new one from time to time when i learn something new.

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.