Oh ok. I’ll be home soon so I can try then.
–SonicX278
Oh ok. I’ll be home soon so I can try then.
–SonicX278
Where would i add this function? And how would i use it?
–SonicX278
you add that function on the same file that you need to use it.
If you will use the function in several different files, you can add it as a external module.
But to make things easier to you, just write this down in your main.lua:
local widget = require "widget" local newButton = widget.newButton widget.newButton = function(options) local onEvent = options.onEvent local onEventWrap = function(e) if onEvent then onEvent(e, options.defaultFile) end end options.onEvent = onEventWrap return newButton(options) end
now you can just use the widget.newButton as you were using before and your onEvent listener will receive a 2nd parameter that will be the defaultFile being used.
This gives me a error on
options.onEvent(e, options.defaultFile)
Just says
18:43:46.363 C:\Users\USER\Desktop\AppRebuilt - Copy\module\shortCuts.lua:10: in function '\_onEvent'
–SonicX278
Sorry. I fixed the error. I updated the code above.
I’m not home to test it right now. When are you up until?
–SonicX278
Well thanks a ton! Works like a charm! Now my lines of code in each file went from 700 to just over 100! Awesome!
–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