|
| 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 |
0 commit comments