jf-roku/components/Buttons/JFButtons.bs

123 lines
3.5 KiB
Plaintext
Raw Normal View History

2020-08-08 20:43:37 +00:00
sub init()
m.top.focusable = true
m.menubg = m.top.findNode("menubg")
m.focusRing = m.top.findNode("focus")
2021-07-09 20:08:32 +00:00
m.buttonGroup = m.top.findNode("buttonGroup")
2020-08-08 20:43:37 +00:00
m.focusAnim = m.top.findNode("moveFocusAnimation")
m.focusAnimTranslation = m.top.findNode("focusLocation")
m.focusAnimWidth = m.top.findNode("focusWidth")
m.focusAnimHeight = m.top.findNode("focusHeight")
' Set button color to global
m.focusRing.color = m.global.constants.colors.button
2020-08-08 20:43:37 +00:00
m.buttonCount = 0
m.selectedFocusedIndex = 0
2020-08-08 20:43:37 +00:00
m.textSizeTask = createObject("roSGNode", "TextSizeTask")
m.top.observeField("focusedChild", "focusChanged")
m.top.enableRenderTracking = true
m.top.observeField("renderTracking", "renderChanged")
2021-07-09 20:08:32 +00:00
end sub
2020-08-08 20:43:37 +00:00
'
' When Selected Index set, ensure it is the one Focused
sub selectedIndexChanged()
m.selectedFocusedIndex = m.top.selectedIndex
end sub
2020-08-08 20:43:37 +00:00
'
' When options are fully displayed, set focus and selected option
sub renderChanged()
if m.top.renderTracking = "full"
2020-08-08 20:43:37 +00:00
highlightSelected(m.selectedFocusedIndex, false)
m.top.setfocus(true)
end if
end sub
sub updateButtons()
m.textSizeTask.fontsize = 40
2021-07-09 20:08:32 +00:00
m.textSizeTask.text = m.top.buttons
2020-08-08 20:43:37 +00:00
m.textSizeTask.name = m.buttonCount
m.textSizeTask.observeField("width", "showButtons")
m.textSizeTask.control = "RUN"
end sub
sub showButtons()
2021-07-09 20:08:32 +00:00
totalWidth = 110 ' track for menu background width - start with side padding
2020-08-08 20:43:37 +00:00
for i = 0 to m.top.buttons.count() - 1
m.buttonCount = m.buttonCount + 1
l = m.buttonGroup.createChild("Label")
l.text = m.top.buttons[i]
l.font.size = 40
2021-07-09 20:08:32 +00:00
l.translation = [0, 10]
2020-08-08 20:43:37 +00:00
l.height = m.textSizeTask.height
l.width = m.textSizeTask.width[i] + 50
l.horizAlign = "center"
2021-07-09 20:08:32 +00:00
l.vertAlign = "center"
2020-08-08 20:43:37 +00:00
totalWidth = totalWidth + l.width + 45
end for
m.menubg.width = totalWidth
m.menubg.height = m.textSizeTask.height + 40
end sub
' Highlight selected menu option
sub highlightSelected(index as integer, animate = true)
val = m.buttonGroup.getChild(index)
rect = val.ancestorBoundingRect(m.top)
if animate = true
2020-08-08 20:43:37 +00:00
m.focusAnimTranslation.keyValue = [m.focusRing.translation, [rect.x - 25, rect.y - 30]]
m.focusAnimWidth.keyValue = [m.focusRing.width, val.width + 50]
m.focusAnimHeight.keyValue = [m.focusRing.height, val.height + 60]
m.focusAnim.control = "start"
else
m.focusRing.translation = [rect.x - 25, rect.y - 30]
m.focusRing.width = val.width + 50
m.focusRing.height = val.height + 60
end if
end sub
' Change opacity of the highlighted menu item based on focus
sub focusChanged()
2021-07-09 20:08:32 +00:00
if m.top.isInFocusChain()
2020-08-08 20:43:37 +00:00
m.focusRing.opacity = 1
else
m.focusRing.opacity = 0.6
end if
end sub
function onKeyEvent(key as string, press as boolean) as boolean
2021-07-09 20:08:32 +00:00
if not press then return false
if key = "left"
if m.selectedFocusedIndex > 0 then m.selectedFocusedIndex = m.selectedFocusedIndex - 1
2020-08-08 20:43:37 +00:00
highlightSelected(m.selectedFocusedIndex)
m.top.focusedIndex = m.selectedFocusedIndex
return true
else if key = "right"
if m.selectedFocusedIndex < m.buttonCount - 1 then m.selectedFocusedIndex = m.selectedFocusedIndex + 1
2020-08-08 20:43:37 +00:00
highlightSelected(m.selectedFocusedIndex)
m.top.focusedIndex = m.selectedFocusedIndex
return true
2020-08-16 16:17:41 +00:00
else if key = "OK"
m.top.selectedIndex = m.selectedFocusedIndex
return true
2021-07-09 20:08:32 +00:00
end if
2020-08-08 20:43:37 +00:00
return false
end function