Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Will i ever be able to pass data through a widget.newButton function call?
Started by InfiSnyp Mar 09 2016 08:32 PM

- - - - -
26 replies to this topic

Best Answer RedBeach , 12 March 2016 - 06:34 PM

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.

[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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 



[TOPIC: post.html]
#2

torbenratzlaff

[GLOBAL: userInfoPane.html]
torbenratzlaff
  • Contributor

  • 404 posts
  • Corona SDK

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



[TOPIC: post.html]
#3

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

@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



[TOPIC: post.html]
#6

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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

--SonicX278

[TOPIC: post.html]
#7

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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

[TOPIC: post.html]
#8

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

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.



[TOPIC: post.html]
#9

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

Oh. Thanks! --SonicX278

[TOPIC: post.html]
#10

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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 



[TOPIC: post.html]
#11

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

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.



[TOPIC: post.html]
#12

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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 



[TOPIC: post.html]
#13

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

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"



[TOPIC: post.html]
#14

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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 



[TOPIC: post.html]
#15

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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

--SonicX278

[TOPIC: post.html]
#16

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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 



[TOPIC: post.html]
#17

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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

 

--SonicX278 



[TOPIC: post.html]
#18

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

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.



[TOPIC: post.html]
#19

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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

--SonicX278

[TOPIC: post.html]
#20

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

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.



[TOPIC: post.html]
#21

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

Oh ok. I'll be home soon so I can try then.

--SonicX278

[TOPIC: post.html]
#22

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

Where would i add this function?  And how would i use it?

 

--SonicX278 



[TOPIC: post.html]
#23

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

  Best Answer

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.



[TOPIC: post.html]
#24

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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 



[TOPIC: post.html]
#25

RedBeach

[GLOBAL: userInfoPane.html]
RedBeach
  • Corona Geek

  • 1,132 posts
  • Corona SDK

Sorry. I fixed the error. I updated the code above.




[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]