Widget.newButton() - stroke color.

I am creating a button using shape construction. It has a strokeColor parameter which is a table with two parameters - “default” and “over”. In the program, I can change the “default” color with object:setStrokeColor(). Can I also change the “over” color in some way?

Hi FreeWan, welcome to the forums!

You can access the table directly and assign a new color value like so:

object._view._strokeColor.over = {0,0,1,1}

Do note that these are considered “internal properties” and what I just showed is not really encouraged because it may no longer work in the future should they be changed.

On the other hand, the widget code can be found in a single Lua file, you can modify it, add a new function to change the overlay stroke color value as you see fit, and use your new modified widget file to create your buttons, or just create your own button widget module using the code from the linked file above as your starting base code.

1 Like

Thank you very much, everything worked. I understand that this is not entirely correct, it would be correct to write a function like object:setStrokeColor(), for example, object:setOverStrokeColor() specifically for a button. But I’m not very good at programming and if possible, could you guide me on how best to do this?

Actually, I had the wrong impression about the source, the link I provided on my previous post is not where it’s being loaded from and it uses old code, disregard about using that as base; I tried to patch up the code to see if it would be a quick fix but had no success.

For what it’s worth, the widget properties mentioned on my previous post haven’t changed in years, and not likely to change anytime soon either, but I still had to provide the heads-up in case something does break in a future update. :slightly_smiling_face:

As you’re working your way around programming and Solar2D, I was going to give you a rough draft on how the button widget works so that in the future you would make your own module for it, but a sample code is probably better…

Here’s a simple button module that you can use, see how it works and change it as you need:
m_button.lua (4.2 KB)

Place the file in your project directory.

Here’s a sample on how to use it:

local BUTTON = require "m_button"

local button1 = BUTTON:new(
    {
		x = display.contentCenterX,
		y = display.contentCenterY,
        label = "button",
        shape = "polygon",
		vertices = {0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35},
        fillColor = { default={1,0,0,1}, over={1,0.1,0.7,0.4} },
        strokeColor = { default={1,0.4,0,1}, over={0,1,0,1} },
        strokeWidth = 4,
		onPress = function() print("Button pressed") end,
		onRelease = function() print("Button released") end
    }
)

-- Uncoment lines below to change the button's properties after creation
--button1:setLabel( "Shape" )
--button1:setFillColor(1,1,0)
--button1:setOverFillColor(1,0.5,0.5)
--button1:setStrokeColor(1,1,1)
--button1:setOverStrokeColor(1,0,1)

-- To remove button do the following:
--button1:destroySelf()
--button1 = nil

I didn’t test the module thoroughly, and didn’t implement a function to change the label color; If you do need it see if you can implement it based on the code that’s already there.

1 Like

Thank you very much! It is unlikely that I will change the existing project now, as I am satisfied with the way it works. But in the future I will definitely use your module! Thanks again.