Will i ever be able to pass data through a widget.newButton function call?

Well i want to pass data through a widget. Something like this.

local function showImg( event, \_fileName) print(\_fileName) end local button = widget.newButton { x = , y = , width = , height = , defaultFile = "imgs.png", onEvent = showImg( event, defualtFile ) }

But right now i can only do 

local function showImg() print() end local button = widget.newButton { x = , y = , width = , height = , defaultFile = "imgs.png", onEvent = showImg }

So my problem is that I have a widget button and when i press it i need that function to see what the file name of that widget is. How would i do this? Would i need to make make my own widget button like thing so i can get that data over to the function?

I found this thread. Not sure if this can and how apply to my problem.

https://forums.coronalabs.com/topic/50659-passing-information-through-onrelease-newwidgetbutton/

Or maybe one of you has there own button library that you wont mind sharing?

–SonicX278 

Hello Sonic,

you can add a value to the button table, after creating the button.

The onEvent function (or onPress and onRelease as well) recieve the parameter “event” which includes the slot “target” that refers to the button itself.

I would try it like this:

local function showImg(event) print(event.target.name) end local button = widget.newButton { x = , y = , width = , height = , defaultFile = "imgs.png", onEvent = showImg } button.name = "Button 1"

You can look at the button widgets code here:

https://github.com/coronalabs/framework-widget/blob/master/widgetLibrary/widget_button.lua

Well i dont want to get the name. I want to get the defualtFile with out have to write out hundreds of 

button.name = '"img.png"'

–SonicX278

Hi @SonicX278,

Internally, image-based widget buttons are sprite objects to allow for visual swapping between default and over states, so getting a direct name reference to the “defaultFile” string is tricky.

I’m not sure I understand the complication in stating button properties as @torbenratzlaff suggests. It’s just one line, and it’s relatively common to attach data to display objects and widgets in this way.

Brent

@SonicX278:

The widgets are open-source and you can easily do that by changing the widget button code to return that info on the release function.

https://github.com/coronalabs/framework-widget/blob/master/widgetLibrary/widget_button.lua

I’ll have to take a look at that when I get home.

–SonicX278

Although can you or someone point me in the correct direction in the widget library?

I mean if you know where it is why make me look through 2000 lines of code.??! Haha

–SonicX278

On line 143, change from:

view.\_onEvent( event )

to:

view.\_onEvent( event, M.\_options.defaultFile )

so when the library call the onEvent listener, it will pass as 2nd parameter the defaultFile.

Oh. Thanks! --SonicX278

Where can i implement this feature to just stay like that forever? Or i can’t? Do i have to take the code off git hub and just use it as a module? 

And will i be able to do 

view.\_onEvent

Instead of 

view.\_onEvent( event ) 

And then in the widget code just do 

onEvent( event, andSoOn ) 

–SonicX278 

You will have to download the code from github and use it as a module.  That same github (https://github.com/coronalabs/framework-widget) is a project example of how to use the a custom widget locally (instead of Corona default). So, just take a look on its main.lua to see the code that you need to use to make the project use your local custom widget button instead of Corona’s.

About your code example above, I didn’t understand what you meant.

I couldn’t figure out how to use the widget library locally. I tried to just get this code

https://github.com/coronalabs/framework-widget/blob/master/widgetLibrary/widget_button.lua

And put it into a file and require that file.

–SonicX278 

Yes, it will not work that way. That is why I mention for you look at the main.lua of the github project.

Add these lines to your main:

-- Override Corona's core widget libraries with the files contained in this project's subdirectory. -- Argument "name" will be set to the name of the library being loaded by the require() function. local function onRequireWidgetLibrary(name) return require("widgetLibrary." .. name) end package.preload.widget\_button = onRequireWidgetLibrary

and have your widget_button.lua saved inside the directory “widgetLibrary”

I tried 

local function onRequireWidgetLibrary(name) return require("module.widget\_button" .. name) end package.preload.widget\_button = onRequireWidgetLibrary

 In the main.lua and then 

local widget = require( "module.widget\_button" )

In my file i need to require it in. Still gives an error that

package.preload.widget\_button

Not found.

–SonicX278 

Still getting the error. Can someone take a look? Thanks!

–SonicX278

Okay i got the whole thing working. I added this in.

view.\_onEvent( event, M.\_options.defaultFile )

Now how do i print the defualtFile when i press a button?

–SonicX278 

Never mind. I got it. Thanks for the help!

–SonicX278 

Hi Sonic.

Sorry, I was out today. Glad that you make it work.

It just came out to me a simpler solution. Instead of modifier the widget button source, you could just wrap it in a function like:

local function myNewButton(options) local onEvent = function(e) options.onEvent(e, options.defaultFile) end options.onEvent = onEvent return require("widget").newButton(options) end

and you use that function instead of the widget.newButton.

Well I think I can just stick with the modified code. Can you check out my other question by any chance?

–SonicX278

Ok. Saw it. So, if you just use my simpler solution will solve that issue as well.

To fix the problem that you reported, you would need change some more code.  Go with the simpler solution, it is better.