场景图组件中roImageCanvas的使用方法

How to use roImageCanvas in scene graph component

我正在使用 场景图组件 开发一个带有 MarkupListMarkupGrid 的应用程序。当用户退出应用程序时,我希望打开一个自定义对话框。我正在使用 roImageCanvas 创建自定义对话框,但不知道如何实现它。 Main.brs 文件代码是...

sub main()
screen = CreateObject("roSGScreen")
m.port = CreateObject("roMessagePort")
screen.setMessagePor`enter code here`t(m.port)
scene = screen.CreateScene("HomeScene")
screen.show()

while(true)
msg = wait(1000, m.port)
msgType = type(msg)
if msgType = "roSGScreenEvent"

if msg.isScreenClosed() then showImageCanvas()
end if
end while
end sub

roImageCanvas 代码是...

Sub showImageCanvas()
print "sdfsdf"
canvasItems = [
{
url:"http://192.168.1.23/boardwalk.jpg"
TargetRect:{x:100,y:100,w:400,h:300}
},
{
url:"http://192.168.1.23/walking.jpg"
TargetRect:{x:500,y:400,w:400,h:300}
},
{
Text:"Hello ImageCanvas"
TextAttrs:{Color:"#FFCCCCCC", Font:"Medium",
HAlign:"HCenter", VAlign:"VCenter",
Direction:"LeftToRight"}
TargetRect:{x:390,y:357,w:500,h:60}
}
]

canvas = CreateObject("roImageCanvas")
port = CreateObject("roMessagePort")
canvas.SetMessagePort(port)
'Set opaque background
canvas.SetLayer(0, {Color:"#FF000000", CompositionMode:"Source"})
canvas.SetRequireAllImagesToDraw(true)
canvas.SetLayer(1, canvasItems)
canvas.Show()
while(true)
msg = wait(0,port)
if type(msg) = "roImageCanvasEvent" then
if (msg.isRemoteKeyPressed()) then
i = msg.GetIndex()
print "Key Pressed - " ; msg.GetIndex()
if (i = 2) then
' Up - Close the screen.
canvas.close()
end if
else if (msg.isScreenClosed()) then
print "Closed"
return
end if
end if
end while
End Sub

通常我希望用户退出应用程序时调用 showImageCanvas()。我也试过将 showImageCanvas() 代码放在场景文件中,但它总是出错所以请告诉我我做错了什么。

如果您使用场景图形组件,您应该只使用一个屏幕 - roSGScreen,和一个 Scene。要显示对话,您还应该使用 Scene Graph 工具集。为此,有 Dialog 组件。要在频道关闭前显示确认对话框,您可以将 onKeyEvent 观察器添加到您的 HomeScene 并像这样显示对话框:

function onKeyEvent(key as String, press as Boolean) as Boolean
    handled = false

    if press
        if key = "back"
            showExitConfirmationDialog()
            handled = true
        end if
    end if

    return handled
end function


sub showExitConfirmationDialog()
    dialog = createObject("roSGNode", "Dialog")
    dialog.message = "Are you sure you want to exit?"
    dialog.buttons = ["Cancel", "Exit"]
    dialog.observeField("buttonSelected", "onDialogButtonSelected")
    m.top.dialog = dialog
end sub


sub onDialogButtonSelected()
    if m.top.dialog.buttonSelected = 1
        m.top.close = true
    else
        m.top.dialog.close = true
    end if
end sub

注意无法拦截Home按钮事件,Home按钮无论如何都会关闭频道。

要关闭应用程序,使用场景的 "close" 字段,添加方法如下:

<?xml version="1.0" encoding="UTF-8"?>
<component name="HomeScene" extends="Scene" xsi:noNamespaceSchemaLocation="http://rokudev.roku.com/rokudev/schema/RokuSceneGraph.xsd">

<script type="text/brightscript" uri="pkg:/components/HomeScene.brs"/>

<interface>
    <field id="close" type="bool"/>
</interface>

</component>

也在你的主函数中观察它:

sub main(params as Object)
    screen = createObject("roSGScreen")
    scene = screen.createScene("HomeScene")
    port = createObject("roMessagePort")
    screen.setMessagePort(port)
    screen.show()

    scene.observeField("close", port)

    while true
        msg = wait(0, port)

        if type(msg) = "roSGNodeEvent"
            if msg.getField() = "close" and msg.getData()
                return
            end if
        end if
    end while
end sub