Twitter-Like-Bootstrap widgets with a custom function

Hi, I’m liking Corona SDK amd Lua a lot. Been reading and experimenting with creating buttons but the procedure takes too many lines considering a page/scene with multiple buttons.

So I created a function that I can call with one line and it creates the buttons for me, I choose to create diff alternatives of the Twitter Bootstrap like framework (I mean the graphical part, how they look) and it works. But there is something I can’t solve… keeping the onliner to create the button and custom event. I read I can’t change the properties once the button has been created. Well this is what I have and it works, let me try to explain:

local botonhrPress = function( event ) t.text = "Button 1 pressed" end cbutton = function (nbutton, tbutton, blabel, bfs, bx, by) bfcolor={default = { 51, 51, 51, 215 }} local button1 = widget.newButton { defaultFile = 'assets/' .. tbutton .. '.png', overFile = 'assets/o' .. tbutton .. '.png', label = blabel, labelColor = bfcolor, fontSize = bfs, emboss = true, onPress=botonhrPress } button1.x = bx; button1.y = by end

PLEASE do not mind the details, just focus on the onPress = XXXXXX. Everything else works passsing the parameters to the function and it creates the buttons, but can’t make the event work. It works writing it directly inside the function and {}, but it doesn’t work passing parameters with the name of the corresponding function for the button. Tried placing a variable with the parameter… nope, is there some kind of operators? I now some languages expect an object-like thing and not a variable with a string, some use an &, some work using parentesis.

Any ideas will be helpful, have no problem sharing the code, but it doesn’t fully work by now (for that bit only).

Thanks

onPress expects the “address of a function”.  As long as botonhrPress is defined above your use of it,

cbutton = function (nbutton, tbutton, blabel, bfs, bx, by, handlerFunction)     bfcolor={default = { 51, 51, 51, 215 }}     local button1 = widget.newButton     {         defaultFile = 'assets/' .. tbutton .. '.png',         overFile = 'assets/o' .. tbutton .. '.png',         label = blabel,         labelColor = bfcolor,         fontSize = bfs,         emboss = true,         onPress=handlerFunction     }     button1.x = bx; button1.y = by end

Then call your button creator:  cbutton(…, botonhrPress)

where … is all your creator parameters that you’re passing.

Rob

Thanks Rob but that didn’t work.

The values work for the parameter but not for the handler function, it expects directly hand written, not a value inside a variable.

I will try to explain. The options for the function value where botonhrPress is placed are:

  • a variable with the name of the function

  • a direct reference to the function

In some coding languages some “options” reject variables or values and expect direct data, then some of those languages accept the variable to have an & before, so it knows it has to use the content of that variable because otherwise it rejects it. I wonder if there is anything related here.

none works, I will appreciate suggestions, thanks in advance,.

Can you show me the code where you are evoking your button maker with passing in the function parameter as I suggested?

Rob

Thanks Rob, I tried but didn’t work, I did something wrong there and just tried the code again from scratch, it works, it goes as follows.

-- The function handling the new button local target\_onRelease = function( event ) button\_id = event.target.id t.text = "The button ID: ".. button\_id .." has been pressed" end -- The function creating the buttons -- Parameters: name, label, type, font-size, handler, x, y, emboss local create\_button = function (id\_button, label\_button, type\_button, fsize\_button, handler\_button, x\_button, y\_button, emboss\_button) local id\_button = widget.newButton { id = id\_button, label = label\_button, fontSize = fsize\_button, defaultFile = "assets/".. type\_button ..".png", overFile = "assets/o".. type\_button ..".png", emboss = emboss\_button, onRelease = handler\_button } id\_button.x = x\_button ; id\_button.y = y\_button end -- Let's call the function and create a button create\_button ("b1", "I'm a button", "default-1", "19", target\_onRelease, 160, 290, true)

All the parameters work, I can send the name of the handling function as a parameter, it works. So I can now create multiple buttons with just one line of code calling that function. So, in order the code shows:

  1. The handling function for the button(s)

  2. The function creating the buttons

  3. The one line of code to create the button(s)

(note) There is a “problem”. I send the “name” of the button on the parameter but, even as the button is created, I can’t call it or move it later after it’s been created. This means the name of the button is not being recognized via variable. I don’t see a critical problem there unless one wants to disable or hide the button later.

I came up with an idea, even that I can send the name of the handling function, I could remove it and use one for all the buttons as I’m using the ID, I could use conditions to run diff code options there, I would be saving one parameter.

Thanks for the time, I did something wrong previously but it works now. Suggestions are welcome.

Thanks Rob for the time, just marked the thread as solved. I will continue to improve the function to make it shorter (to call). Emboss could be default and the font size could be setted depending on the type of button one wants to create, the oneliner can be shorter.

Thanks

I do almost something very similar, I have a library file that I include with each project with functions such as this for all sorts of things.

One thing I do though is return the ID of the button that is returned from widget.newButton, that way you then have a way of manipulating the button if you need to, such as moving it, making it invisible, etc.

Ha! thanks for that, I was doing something else trying to get a hold on the control of the button but returning the ID solves it even better, thanks a lot for that cublah.

onPress expects the “address of a function”.  As long as botonhrPress is defined above your use of it,

cbutton = function (nbutton, tbutton, blabel, bfs, bx, by, handlerFunction)     bfcolor={default = { 51, 51, 51, 215 }}     local button1 = widget.newButton     {         defaultFile = 'assets/' .. tbutton .. '.png',         overFile = 'assets/o' .. tbutton .. '.png',         label = blabel,         labelColor = bfcolor,         fontSize = bfs,         emboss = true,         onPress=handlerFunction     }     button1.x = bx; button1.y = by end

Then call your button creator:  cbutton(…, botonhrPress)

where … is all your creator parameters that you’re passing.

Rob

Thanks Rob but that didn’t work.

The values work for the parameter but not for the handler function, it expects directly hand written, not a value inside a variable.

I will try to explain. The options for the function value where botonhrPress is placed are:

  • a variable with the name of the function

  • a direct reference to the function

In some coding languages some “options” reject variables or values and expect direct data, then some of those languages accept the variable to have an & before, so it knows it has to use the content of that variable because otherwise it rejects it. I wonder if there is anything related here.

none works, I will appreciate suggestions, thanks in advance,.

Can you show me the code where you are evoking your button maker with passing in the function parameter as I suggested?

Rob

Thanks Rob, I tried but didn’t work, I did something wrong there and just tried the code again from scratch, it works, it goes as follows.

-- The function handling the new button local target\_onRelease = function( event ) button\_id = event.target.id t.text = "The button ID: ".. button\_id .." has been pressed" end -- The function creating the buttons -- Parameters: name, label, type, font-size, handler, x, y, emboss local create\_button = function (id\_button, label\_button, type\_button, fsize\_button, handler\_button, x\_button, y\_button, emboss\_button) local id\_button = widget.newButton { id = id\_button, label = label\_button, fontSize = fsize\_button, defaultFile = "assets/".. type\_button ..".png", overFile = "assets/o".. type\_button ..".png", emboss = emboss\_button, onRelease = handler\_button } id\_button.x = x\_button ; id\_button.y = y\_button end -- Let's call the function and create a button create\_button ("b1", "I'm a button", "default-1", "19", target\_onRelease, 160, 290, true)

All the parameters work, I can send the name of the handling function as a parameter, it works. So I can now create multiple buttons with just one line of code calling that function. So, in order the code shows:

  1. The handling function for the button(s)

  2. The function creating the buttons

  3. The one line of code to create the button(s)

(note) There is a “problem”. I send the “name” of the button on the parameter but, even as the button is created, I can’t call it or move it later after it’s been created. This means the name of the button is not being recognized via variable. I don’t see a critical problem there unless one wants to disable or hide the button later.

I came up with an idea, even that I can send the name of the handling function, I could remove it and use one for all the buttons as I’m using the ID, I could use conditions to run diff code options there, I would be saving one parameter.

Thanks for the time, I did something wrong previously but it works now. Suggestions are welcome.

Thanks Rob for the time, just marked the thread as solved. I will continue to improve the function to make it shorter (to call). Emboss could be default and the font size could be setted depending on the type of button one wants to create, the oneliner can be shorter.

Thanks

I do almost something very similar, I have a library file that I include with each project with functions such as this for all sorts of things.

One thing I do though is return the ID of the button that is returned from widget.newButton, that way you then have a way of manipulating the button if you need to, such as moving it, making it invisible, etc.

Ha! thanks for that, I was doing something else trying to get a hold on the control of the button but returning the ID solves it even better, thanks a lot for that cublah.