如何在场景图组件中使用roImageCanvas

时间:2016-06-08 12:51:51

标签: roku brightscript

我正在使用 Scene Graph Component 来开发具有 MarkupList MarkupGrid 的应用程序。我想在用户退出应用程序时打开自定义对话框。我正在使用 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()代码放在Scene文件中,但它总是出错,所以请告诉我我做错了什么。

1 个答案:

答案 0 :(得分:2)

如果使用场景图组件,则应仅使用一个屏幕 - roSGScreen ,以及一个场景。要显示对话框,还应使用 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

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

关闭应用,场景"关闭"使用字段,以下是添加方法:

<?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