Skip to content

Commit

Permalink
Tracked layers can now be queried directly for their focus boolean va…
Browse files Browse the repository at this point in the history
…lue.

The engine now relies on this value rather than on the name of the layer's current state.
Debug statement will try to look for the layer's __framerInstanceInfo.name if no name is set.
Running the focus engine with no initial focus set now fails gracefully.
  • Loading branch information
John Marstall committed Jul 19, 2017
1 parent c5a150c commit 73fac6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
19 changes: 16 additions & 3 deletions FocusEngine.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
layerA.on "unfocus", ->
layerA.on "selected", ->
# Check the currently focused layer
print fe.focus
# Check whether a layer has focus
print layerA.focus
# Integration with RemoteLayer (https://github.com/bpxl-labs/RemoteLayer)
RemoteLayer = require "RemoteLayer"
myRemote = new RemoteLayer
Expand Down Expand Up @@ -94,9 +100,9 @@ exports.unfocusStyle =
exports.initialize = (focusableArray) ->
exports.focusable = focusableArray
for layer in exports.focusable
layer.focus = false
styleLayer(layer)


# layer visibility
checkVisible = (layer) ->
isVisible = true
Expand Down Expand Up @@ -125,13 +131,15 @@ exports.placeFocus = Utils.throttle 0.1, (layer = null) ->
exports.focus = layer
unfocusAll()
layer.emit "focus"
layer.focus = true
if layer != null and layer in exports.focusable
layer?.animate("focus")

unfocusAll = () ->
for layer in exports.focusable
if layer.states.current.name == "focus"
if layer.focus == true
layer.emit "unfocus"
layer.focus = false
layer.animate("unfocus")

exports.focusPrevious = () ->
Expand All @@ -140,6 +148,7 @@ exports.focusPrevious = () ->

exports.addLayer = (layer) ->
exports.focusable.push(layer)
layer.focus = false
styleLayer(layer)

styleLayer = (layer) ->
Expand All @@ -162,8 +171,12 @@ styleLayer = (layer) ->
layer.animate("unfocus", instant: true)

exports.changeFocus = Utils.throttle 0.1, (direction) ->
# if focus was never placed, give up
if exports.focus == null and exports.initialFocus == null
print "No initial focus set. Use placeFocus(layer) to set."
return
if exports.debug == true
print "current: " + exports.focus?.name + "; direction: " + direction
print "focus was: " + (exports.focus?.name or exports.focus?.__framerInstanceInfo?.name or "unnamed layer") + "; direction: " + direction
tempArray = []
# if we've lost all focus, reset
if exports.focus == null or exports.focus == undefined
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ myRemote = new RemoteLayer
fe.debug = true
```

#### Check the currently focused layer
```coffeescript
print fe.focus
```

#### Check whether a layer has focus
```coffeescript
print layerA.focus
```

#### Known issues

Attempting to perform a `placeFocus()` call as FocusEngine is changing its own focus will fail. (The call is discarded.) If you need to override FocusEngine's logic, Use a slight delay to ensure the call is respected.
Expand Down

0 comments on commit 73fac6c

Please sign in to comment.