Changing Widget Button Table Options

Im trying to create a class for creating buttons… Something like this.

function newButton() newButton = widget.newButton{ width = 100, height = 70, sheet = buttonSheetDark, topLeftFrame = 1, topMiddleFrame = 2, topRightFrame = 3, middleRightFrame = 4, bottomRightFrame = 5, bottomMiddleFrame = 6, bottomLeftFrame = 7, middleLeftFrame = 8, middleFrame = 9, topLeftOverFrame = 10, topMiddleOverFrame = 11, topRightOverFrame = 12, middleRightOverFrame = 13, bottomRightOverFrame = 14, bottomMiddleOverFrame = 15, bottomLeftOverFrame = 16, middleLeftOverFrame = 17, middleOverFrame = 18, label = "", onPress = "", font = "", fontSize = "", labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } }, x = 0, --125 y = 0 } return newButton end

Im calling it with this

button = settings.newButton()

And then trying to set certain properties manually, which I cant figure out how to do. Ive tried the following with no luck.

button[23].onPress = onSoundTouch

button[24] = font1

button.fontSize = 36

How can I set these table options manually after the button has already been created?

Hi @havochare4,

Well, I think it works a bit differently than what you’re expecting. Once the button is created, you can set properties upon it (it’s a table, after all), but those properties won’t necessarily affect what the button does or how it behaves unless you rig it all up somehow using custom press functions and so forth.

If I were in your situation, I would suggest that you create all of your custom properties and button parameters in the class, but you don’t actually create the button in the class function. Instead, return that table of information back to where you called it from then use that data to create the button. This way, if you want, you can adjust some of the properties in that table before you create the button itself.

Not sure if that makes sense or not, but that’s the best I can describe it.

Brent

Ok, that makes sense… But Im having trouble getting it to work… Pretty much every aspect of it in fact.

I have my class built like so

btnClass.lua

module(..., package.seeall) function newOptions() options = {     width = 100,     height = 70,     sheet = buttonSheetDark, --omitted for brevity     topLeftFrame = 1,     topMiddleFrame = 2,     topRightFrame = 3,     middleRightFrame = 4,     bottomRightFrame = 5, bottomMiddleFrame = 6, bottomLeftFrame = 7,     middleLeftFrame = 8, middleFrame = 9,     topLeftOverFrame = 10,     topMiddleOverFrame = 11,     topRightOverFrame = 12,     middleRightOverFrame = 13,     bottomRightOverFrame = 14,     bottomMiddleOverFrame = 15,     bottomLeftOverFrame = 16,     middleLeftOverFrame = 17,     middleOverFrame = 18, labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } }, } return options end

And calling it like 

local options = btnClass.newOptions

It doesn’t seem to work though, as the options variable has only nil values

For example

print(options[1]) > outputs nil

Not sure what Im doing wrong here…

My next question is when I do get the options table returned successfully, how do I then utilize that options table in the creation of the widget button. Im not even going to venture a guess as to the syntax… it would be laughable I suspect.  

Hi @havochare4,

I’m not sure where you pulled this code from. The Lua “module(…, package.seeall)” method is deprecated and should no longer be used. Fixing that would be my first suggestion, then we can approach the next issue.

Best regards,

Brent

Ok, Ive made the changed you suggested. 

 

I have my class built like so

 

btnClass.lua

 

local btnClass = {} local function newOptions() options = {     width = 100,     height = 70,     sheet = buttonSheetDark, --omitted for brevity     topLeftFrame = 1,     topMiddleFrame = 2,     topRightFrame = 3,     middleRightFrame = 4,     bottomRightFrame = 5, bottomMiddleFrame = 6, bottomLeftFrame = 7,     middleLeftFrame = 8, middleFrame = 9,     topLeftOverFrame = 10,     topMiddleOverFrame = 11,     topRightOverFrame = 12,     middleRightOverFrame = 13,     bottomRightOverFrame = 14,     bottomMiddleOverFrame = 15,     bottomLeftOverFrame = 16,     middleLeftOverFrame = 17,     middleOverFrame = 18, labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } }, } return options end btnClass.newOptions = newOptions return btnClass

And calling it like 

local options = btnClass.newOptions

 

After some more research I realized the options table does in fact have the data

print(options.width) > outputs 100

 

So how do I implement this options table into the widget.newButton table?

Hi @havochare4,

It should be as simple as this:

[lua]

widget.newButton( btnClass.newOptions )

–OR

local options = btnClass.newOptions

widget.newButton( options )

[/lua]

This is because the “btnClass” is returning a table of options, and the widget button constructor expects a table (of options) as its sole parameter. If this doesn’t work, then something else may be amiss in the setup.

Brent

Hi @havochare4,

Well, I think it works a bit differently than what you’re expecting. Once the button is created, you can set properties upon it (it’s a table, after all), but those properties won’t necessarily affect what the button does or how it behaves unless you rig it all up somehow using custom press functions and so forth.

If I were in your situation, I would suggest that you create all of your custom properties and button parameters in the class, but you don’t actually create the button in the class function. Instead, return that table of information back to where you called it from then use that data to create the button. This way, if you want, you can adjust some of the properties in that table before you create the button itself.

Not sure if that makes sense or not, but that’s the best I can describe it.

Brent

Ok, that makes sense… But Im having trouble getting it to work… Pretty much every aspect of it in fact.

I have my class built like so

btnClass.lua

module(..., package.seeall) function newOptions() options = {     width = 100,     height = 70,     sheet = buttonSheetDark, --omitted for brevity     topLeftFrame = 1,     topMiddleFrame = 2,     topRightFrame = 3,     middleRightFrame = 4,     bottomRightFrame = 5, bottomMiddleFrame = 6, bottomLeftFrame = 7,     middleLeftFrame = 8, middleFrame = 9,     topLeftOverFrame = 10,     topMiddleOverFrame = 11,     topRightOverFrame = 12,     middleRightOverFrame = 13,     bottomRightOverFrame = 14,     bottomMiddleOverFrame = 15,     bottomLeftOverFrame = 16,     middleLeftOverFrame = 17,     middleOverFrame = 18, labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } }, } return options end

And calling it like 

local options = btnClass.newOptions

It doesn’t seem to work though, as the options variable has only nil values

For example

print(options[1]) > outputs nil

Not sure what Im doing wrong here…

My next question is when I do get the options table returned successfully, how do I then utilize that options table in the creation of the widget button. Im not even going to venture a guess as to the syntax… it would be laughable I suspect.  

Hi @havochare4,

I’m not sure where you pulled this code from. The Lua “module(…, package.seeall)” method is deprecated and should no longer be used. Fixing that would be my first suggestion, then we can approach the next issue.

Best regards,

Brent

Ok, Ive made the changed you suggested. 

 

I have my class built like so

 

btnClass.lua

 

local btnClass = {} local function newOptions() options = {     width = 100,     height = 70,     sheet = buttonSheetDark, --omitted for brevity     topLeftFrame = 1,     topMiddleFrame = 2,     topRightFrame = 3,     middleRightFrame = 4,     bottomRightFrame = 5, bottomMiddleFrame = 6, bottomLeftFrame = 7,     middleLeftFrame = 8, middleFrame = 9,     topLeftOverFrame = 10,     topMiddleOverFrame = 11,     topRightOverFrame = 12,     middleRightOverFrame = 13,     bottomRightOverFrame = 14,     bottomMiddleOverFrame = 15,     bottomLeftOverFrame = 16,     middleLeftOverFrame = 17,     middleOverFrame = 18, labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } }, } return options end btnClass.newOptions = newOptions return btnClass

And calling it like 

local options = btnClass.newOptions

 

After some more research I realized the options table does in fact have the data

print(options.width) > outputs 100

 

So how do I implement this options table into the widget.newButton table?

Hi @havochare4,

It should be as simple as this:

[lua]

widget.newButton( btnClass.newOptions )

–OR

local options = btnClass.newOptions

widget.newButton( options )

[/lua]

This is because the “btnClass” is returning a table of options, and the widget button constructor expects a table (of options) as its sole parameter. If this doesn’t work, then something else may be amiss in the setup.

Brent