Skip to content

Commit 7b74c57

Browse files
Add simple focus example
1 parent bca0790 commit 7b74c57

6 files changed

Lines changed: 152 additions & 0 deletions

File tree

simple-focus/.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "brightscript",
6+
"request": "launch",
7+
"name": "BrightScript Debug: Launch",
8+
"rootDir": "${workspaceFolder}",
9+
"host": "${promptForHost}",
10+
"injectRdbOnDeviceComponent": true,
11+
"enableDebugProtocol": true
12+
}
13+
]
14+
}

simple-focus/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"brightscript.bsdk": "1.0.0-alpha.39"
3+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
sub init()
2+
'get notified anytime the focus changes
3+
m.focusRing = m.top.findNode("focusRing")
4+
5+
m.nodesToFocus = [
6+
m.top.findNode("red"),
7+
m.top.findNode("green"),
8+
m.top.findNode("blue")
9+
m.top.findNode("black")
10+
]
11+
m.nodesToFocus[0].setFocus(true)
12+
m.top.observeField("focusedChild", "onFocusedChildChange")
13+
end sub
14+
15+
'draw a focus ring around the focused element
16+
sub onFocusedChildChange()
17+
print "focus has changed"
18+
focusedChild = m.top.focusedChild
19+
20+
'if we have a focusedChild and it's not the main scene (which has no ID)
21+
if focusedChild <> invalid and focusedChild.id <> "" then
22+
print "Currently focused element: " + focusedChild.id
23+
m.focusRing.translation = [
24+
focusedChild.translation[0] - 5,
25+
focusedChild.translation[1] - 5
26+
]
27+
end if
28+
end sub
29+
30+
'Set focus to the next sibling in the specified direction
31+
sub focusNextSibling()
32+
nodeToFocus = invalid
33+
34+
'focus the next child
35+
for i = 0 to m.nodesToFocus.count() - 1
36+
child = m.nodesToFocus[i]
37+
'skip the focusRing
38+
if child.id = "focusRing"
39+
continue for
40+
end if
41+
42+
if child.hasFocus()
43+
nodeToFocus = m.nodesToFocus[i + 1]
44+
exit for
45+
end if
46+
end for
47+
'if we have no node to focus, then focus the first node that's not the focusRing
48+
if nodeToFocus = invalid
49+
nodeToFocus = m.nodesToFocus[0]
50+
end if
51+
nodeToFocus.setFocus(true)
52+
end sub
53+
54+
function onKeyEvent(key as string, press as boolean) as boolean
55+
'only handle key up events
56+
if press <> true
57+
return false
58+
end if
59+
60+
'simple focus management for now, just move forwards or backwards
61+
if key = "up" or key = "right" or key = "down" or key = "left"
62+
focusNextSibling()
63+
return true
64+
end if
65+
66+
if key = "OK" then
67+
print "Item was selected", m.focusedChild.id
68+
return true
69+
end if
70+
return false
71+
end function
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<component name="MainScene" extends="Scene">
3+
<script type="text/brightscript" uri="MainScene.brs" />
4+
5+
<interface>
6+
<field id="appExit" type="bool" alwaysnotify="true" value="false" />
7+
</interface>
8+
9+
<children>
10+
<rectangle id="focusRing" color="0xFF69B4FF" width="310" height="310" translation="[95,95]" />
11+
12+
<Rectangle id="red" color="0xFF0000FF" width="300" height="300" translation="[100,100]" />
13+
<Rectangle id="green" color="0x00FF00FF" width="300" height="300" translation="[500,100]" />
14+
<Rectangle id="blue" color="0x000000FF" width="300" height="300" translation="[900,100]" />
15+
<Rectangle id="black" color="0x0000FFFF" width="300" height="300" translation="[1300, 100]" />
16+
</children>
17+
</component>

simple-focus/manifest

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Channel Details
2+
title=Sample Channel One
3+
subtitle=A simple sample channel
4+
major_version=1
5+
minor_version=0
6+
build_version=0
7+
8+
# Channel Assets
9+
mm_icon_focus_sd=pkg:/images/channel-poster_sd.png
10+
mm_icon_focus_hd=pkg:/images/channel-poster_hd.png
11+
mm_icon_focus_fhd=pkg:/images/channel-poster_fhd.png
12+
13+
# Splash Screen + Loading Screen Artwork
14+
splash_screen_sd=pkg:/images/splash-screen_sd.jpg
15+
splash_screen_hd=pkg:/images/splash-screen_hd.jpg
16+
splash_screen_fhd=pkg:/images/splash-screen_fhd.jpg
17+
splash_color=#808080
18+
splash_min_time=0
19+
20+
# Resolution
21+
ui_resolutions=fhd

simple-focus/source/main.brs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
sub Main(inputArguments as object)
2+
screen = createObject("roSGScreen")
3+
m.port = createObject("roMessagePort")
4+
screen.setMessagePort(m.port)
5+
scene = screen.CreateScene("MainScene")
6+
screen.show()
7+
' vscode_rdb_on_device_component_entry
8+
scene.observeField("appExit", m.port)
9+
scene.setFocus(true)
10+
11+
while true
12+
msg = wait(0, m.port)
13+
msgType = type(msg)
14+
15+
if msgType = "roSGScreenEvent" then
16+
if msg.isScreenClosed() then
17+
return
18+
end if
19+
else if msgType = "roSGNodeEvent" then
20+
field = msg.getField()
21+
if field = "appExit" then
22+
return
23+
end if
24+
end if
25+
end while
26+
end sub

0 commit comments

Comments
 (0)