rokubrightscriptscenegraph

Create a dialog box that exits only when ok is clicked in roku


how to create a dialog box in roku that opens when back button on remote is clicked. when ok option is selected on dialog box app exits. this is the code in main.brs

sub Main()
    ShowChannelRSGScreen()
end sub

sub ShowChannelRSGScreen()
    ' The roSGScreen object is a SceneGraph canvas that displays the contents of a Scene node instance
    screen = CreateObject("roSGScreen")
    ' message port is the place where events are sent
    m.port = CreateObject("roMessagePort")
    ' sets the message port which will be used for events from the screen
    screen.SetMessagePort(m.port)
    ' every screen object must have a Scene node, or a node that derives from the Scene node
    scene = screen.CreateScene("MainScene")
    screen.Show() ' Init method in MainScene.brs is invoked

    ' event loop
    while(true)
        ' waiting for events from screen
        msg = wait(0, m.port)
        msgType = type(msg)
        if msgType = "roSGScreenEvent"
            if msg.IsScreenClosed() then return
        end if
    end while
end sub

i tried the above code but i dont know how to create an exit box dialog.


Solution

  • You can utilize roku's StandardMessageDialog for this. In summary:

    Here's your code, updated to show how this works:

    MainScene.brs

    sub init()
    end sub
    
    function onKeyEvent(key as string, press as boolean) as boolean
      if press and key = "back" then
        'create the dialog
        dialog = createObject("roSGNode", "StandardMessageDialog")
        '.message is an array of messages
        dialog.message = ["Do you really want to exit?"]
        dialog.buttons = ["Yes", "No"]
        'register a callback function for when a user clicks a button
        dialog.observeFieldScoped("buttonSelected", "onDialogButtonClick")
    
        'assigning the dialog to m.top.dialog will "show" the dialog
        m.top.dialog = dialog
        return true
      end if
    end function
    
    function onDialogButtonClick(evt)
      buttonIndex = evt.getData()
      'did the user click "Yes"
      if buttonIndex = 0 then
        'set appExit which will kill the channel in the main loop
        m.top.appExit = true
    
      'user clicked "no"
      else
          'close the dialog
          m.top.dialog.close = true
          return true
      end if
    end function
    

    MainScene.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <component name="MainScene" extends="Scene">
      <script type="text/brightscript" uri="MainScene.brs" />
      <interface>
        <field id="appExit" type="bool" alwaysnotify="true" value="false"/>
      </interface>
    </component>
    

    pkg:/source/main.brs

    sub Main()
      ShowChannelRSGScreen()
    end sub
    
    sub ShowChannelRSGScreen()
      ' The roSGScreen object is a SceneGraph canvas that displays the contents of a Scene node instance
      screen = CreateObject("roSGScreen")
      ' message port is the place where events are sent
      m.port = CreateObject("roMessagePort")
      ' sets the message port which will be used for events from the screen
      screen.SetMessagePort(m.port)
      ' every screen object must have a Scene node, or a node that derives from the Scene node
      scene = screen.CreateScene("MainScene")
      screen.Show() ' Init method in MainScene.brs is invoked
      'observe the appExit event to know when to kill the channel
      scene.observeField("appExit", m.port)
      'focus the scene so it can respond to key events
      scene.setFocus(true)
    
    
      ' event loop
      while true
        msg = wait(0, m.port)
        msgType = type(msg)
    
        if msgType = "roSGScreenEvent" then
          if msg.isScreenClosed() then
            return
          end if
        'respond to the appExit field on MainScene
        else if msgType = "roSGNodeEvent" then
          field = msg.getField()
          'if the scene's appExit field was changed in any way, exit the channel
          if field = "appExit" then
            return
          end if
        end if
      end while
    end sub