components_ui_buttongroup_VertButtonGroup.bs
import "pkg:/source/utils/misc.bs"
sub init()
m.top.layoutDirection = "vert"
m.top.itemSpacings = 15
m.top.buttonFocused = 0
end sub
' center the button group horizontally
' NOTE: this centers using the entire screen width, not the parent bounding rect
sub center()
buttonsBoundingRect = m.top.boundingRect()
m.top.translation = [(1920 - buttonsBoundingRect.width) / 2, m.top.translation[1]]
end sub
' Use instead of setFocus(true)
sub focus()
m.top.setFocus(true)
if m.top.buttonFocused <> invalid
button = m.top.getChild(m.top.buttonFocused)
if button <> invalid
button.setFocus(true)
end if
end if
end sub
function onKeyEvent(key as string, press as boolean) as boolean
if not press then return false
currentFocus = m.top.buttonFocused
currentFocusButton = m.top.getChild(currentFocus)
' print "currentFocus =", currentFocus
' print "currentFocusButton =", currentFocusButton
' print "m.top.getChildCount() =", m.top.getChildCount()
if key = "down"
' Move down, skipping disabled buttons
for i = currentFocus + 1 to m.top.getChildCount() - 1
button = m.top.getChild(i)
if button.enabled
currentFocusButton.setFocus(false)
m.top.buttonFocused = i
button.setFocus(true)
return true
end if
end for
else if key = "up"
' Move up, skipping disabled buttons
for i = currentFocus - 1 to 0 step -1
button = m.top.getChild(i)
if button.enabled
currentFocusButton.setFocus(false)
m.top.buttonFocused = i
button.setFocus(true)
return true
end if
end for
end if
if key = "OK"
print "OK pressed from VertButtonGroup, buttonFocused =", m.top.buttonFocused
m.top.buttonSelected = m.top.buttonFocused
return true
end if
return false
end function