I need a button bar menu, but I can’t use the widget tabBar, because it uses only the event “onPress”. So I tried to buid my own bar menu.
local tabButtons = {
{
id="1",
width = 32,
height = 32,
defaultFile = normalIcon1,
overFile = overIcon1,
label = "Label1",
labelYOffset=-16,
labelColor = { default={ 0,0,0 }, over={ 0, 0, 0, 0.5 } },
font = native.systemfont,
size = 15,
onEvent = gotoSelection
},
{
id="2",
width = 32,
height = 32,
defaultFile = normalIcon2,
overFile = overIcon1,
label = "Label2",
labelYOffset=-16,
labelColor = { default={ 0,0,0 }, over={ 0, 0, 0, 0.5 } },
font = native.systemfont,
size = 15,
onEvent = gotoSelection
}
}
-- Create the widget
local tabBar = widget.newButtomMenu(
{
left = 0,
top = barraMenuY,
width = display.contentWidth,
height = 50,
backgroundFile = tabBarImg,
buttons = tabButtons
}
)
function widget.newButtomMenu( options )
local customOptions = options or {}
local opt = {}
opt.left = customOptions.left or nil
opt.top = customOptions.top or nil
opt.width = customOptions.width or display.contentWidth
opt.height = customOptions.height or 50
opt.id = customOptions.id
opt.backgroundImg = customOptions.backgroundImg
opt.backgroundColor = customOptions.backgroundColor
--
-- Coordinatex x,y
if ( opt.left ) then
opt.x = opt.left + opt.width * 0.5
end
if ( opt.top ) then
opt.y = opt.top + opt.height * 0.5
end
local barContainer = display.newGroup()
local background = display.newRect( barContainer, opt.x, opt.y, opt.width, opt.height )
if ( opt.backgroundImg ) then
background.fill = { type="image", filename=opt.backgroundImg }
elseif ( opt.backgroundColor ) then
background.fill = opt.backgroundColor
else
background.fill = { 0.96, 0.62, 0.34 }
end
opt.Btn=customOptions.buttons
-- Button constructor:
viewBtn={}
for i = 1, #opt.Btn do
-- construct an image button
if ( opt.Btn[i].defaultFile ) then
viewBtn[i] = widget.newButton(
{
id = opt.Btn[i].id,
width = opt.Btn[i].width,
height = opt.Btn[i].height,
baseDir = opt.Btn[i].baseDir,
**defaultFile = opt.Btn[i].defaultFile,**
**overFile = opt.Btn[i].overFile,**
onEvent = opt.Btn[i].onEvent,
**font = opt.Btn[i].font or opt.font,**
**size = opt.Btn[i].size or 14,**
**label = opt.Btn[i].label or "",**
labelXOffSet=opt.x+5,
**labelColor = opt.Btn[i].labelColor or { default={ 0, 0, 0}, over={ 0, 0, 0, 0.5 } },**
**top=opt.top**
}
)
else
-- else, construct a text button
viewBtn[i] = widget.newButton(
{
id = opt.Btn[i].id,
left = 8,
top=opt.height/2,
height = opt.Btn[i].height,
label = opt.Btn[i].label,
labelColor = opt.Btn[i].labelColor or { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } },
onEvent = opt.Btn[i].onEvent,
size = opt.Btn[i].size or 14,
font = opt.Btn[i].font or opt.font,
top=opt.top
}
)
end
end
for i = 1, #viewBtn do
-- Set the buttons position
viewBtn[i].x = ( ( opt.width / #viewBtn ) * i ) - ( opt.width / #viewBtn ) / 2
viewBtn[i].y = opt.top + ( viewBtn[i].height * 0.5 )
if nil ~= viewBtn[i].label then
if "" == viewBtn[i].label then
viewBtn[i].y = opt.height + ( viewBtn[i].height * 0.5 )
end
end
-- Labels position
local labelYOffset=opt.Btn[i].labelYOffset or 0
viewBtn[i].labelYOffset = opt.y +
viewBtn[i].contentHeight + ( viewBtn[i].size ) + labelYOffset
barContainer:insert(viewBtn[i])
end
return barContainer
end
The strange thing is that the parameters marked with asteriks are not passed from opt.Btn[i] to viewBtn[i]!
I’ve checked a lot of times the code but I fail to see what is wrong.
Please help me!