I am 100% positive that I am only setting the Observer once. That's the only Observer I set in the whole program. The only other event listener is in my GuideItem interface for the content onChange event, but all of those events trigger before the itemFocused Observer is even set.
The only other clue I have is that when the grid first gains focus, onFocusChanged() is only called once. After that, focusing on a new element generates two calls to onFocusChanged().
I'm including all of my code below because I'm all out of ideas. If you get a chance to copy it over and run it I would be very interested in your results.
And thank you so much for your help so far.
main.brs
sub Main()
showChannelSGScreen()
end sub
sub showChannelSGScreen()
screen = CreateObject("roSGScreen")
m.port = CreateObject("roMessagePort")
screen.setMessagePort(m.port)
scene = screen.CreateScene("GridGuideScene")
screen.show()
while(true)
msg = wait(0, m.port)
msgType = type(msg)
if msgType = "roSGScreenEvent"
if msg.isScreenClosed() then return
end if
end while
end sub
GridGuideScene.brs
sub init()
m.guideGrid = m.top.findNode("GuideGrid")
m.guideGrid.horizFocusAnimationStyle = "floatingFocus"
m.guideGrid.vertFocusAnimationStyle = "floatingFocus"
m.guideGrid.content = getGuideGridData()
m.guideGrid.SetFocus(true)
m.guideGrid.ObserveField("itemFocused", "onFocusChanged")
End sub
function getGuideGridData() as object
data = CreateObject("roSGNode", "ContentNode")
count=0
for i = 0 to 1 'Rows'
for j = 0 to 4 'Cols'
count++
if (count<>10) then
dataItem = data.CreateChild("GuideItemData")
dataItem.labelText = "Guide item" + stri(count)
dataItem.X = j
dataItem.Y = i
dataItem.W = 1
dataItem.H = 1
if (count=9) then
dataItem.W = 2
endif
endif
end for
end for
return data
end function
function onFocusChanged() as void
print "Focus on item: " + stri(m.guideGrid.itemFocused)
'stop'
end function
function onKeyEvent(key as String, press as Boolean) as Boolean
print "onKeyEvent: " + key
result = false
return result
end function
GridGuideScene.xml
<?xml version="1.0" encoding="UTF-8"?>
<component name="GridGuideScene" extends="Scene" xsi:noNamespaceSchemaLocation="https://devtools.web.roku.com/schema/RokuSceneGraph.xsd">
<script type="text/brightscript" uri="pkg:/components/GridGuideScene.brs" />
<children>
<MarkupGrid
id="GuideGrid"
itemSize="[390,80]"
itemComponentName="GuideItem"
fixedLayout="true"
translation="[300,310]"
numRows="6"
numColumns="5"
rowHeights="80"
columnWidths="390"
itemSpacing="[3,3]" >
</MarkupGrid>
</children>
</component>
GuideItem.xml
<?xml version="1.0" encoding="utf-8" ?>
<component name="GuideItem" extends="Group">
<interface>
<field id="itemContent" type="node" onChange="itemContentChanged"/>
</interface>
<script type="text/brightscript" >
<![CDATA[
function itemContentChanged()
itemData = m.top.itemContent
m.itemText.text = itemData.labelText
m.boundRect = m.top.findNode("boundingRect")
m.boundRect.width *= itemData.W
end function
function init()
m.itemText = m.top.findNode("itemText")
end function
]]>
</script>
<children>
<Rectangle
id="boundingRect"
color="#000000"
height="80"
width="390" >
<Label id="itemText" font="font:MediumSystemFont" height="80" width="390" vertAlign="center" horizAlign="center"/>
</Rectangle>
</children>
</component>
GuideItemData.xml
<?xml version="1.0" encoding="utf-8" ?>
<component name="GuideItemData" extends="ContentNode">
<interface>
<field id="labelText" type="string" />
</interface>
</component>