Permalink
Browse files

Add LayerStateMachine

  • Loading branch information...
nvh committed Jul 26, 2016
1 parent 6c25ab8 commit 5b8ca53ce214b1f9e4faae85f81eb6c87c142028
Showing with 52 additions and 4 deletions.
  1. +5 −4 framer/Layer.coffee
  2. +47 −0 framer/LayerStateMachine.coffee
View
@@ -11,7 +11,7 @@ Utils = require "./Utils"
{Matrix} = require "./Matrix"
{Animation} = require "./Animation"
{LayerStyle} = require "./LayerStyle"
-{LayerStates} = require "./LayerStates"
+{LayerStateMachine} = require "./LayerStateMachine"
{LayerDraggable} = require "./LayerDraggable"
{LayerPinchable} = require "./LayerPinchable"
{Gestures} = require "./Gestures"
@@ -132,7 +132,7 @@ class exports.Layer extends BaseClass
if options.hasOwnProperty(p)
@[p] = options[p]
- @_states = new LayerStates(@)
+ @_stateMachine = new LayerStateMachine(@)
@_context.emit("layer:create", @)
##############################################################
@@ -990,9 +990,10 @@ class exports.Layer extends BaseClass
enumerable: false
exportable: false
importable: false
- get: -> @_states
+ get: -> @_stateMachine.states
set: (states) ->
- @_states = new LayerStates(@, @_states.initial, states)
+ for name,state of states
+ @_stateMachine.states[name] = state
#############################################################################
## Draggable, Pinchable
@@ -0,0 +1,47 @@
+# {_} = require "./Underscore"
+#
+# {Events} = require "./Events"
+{BaseClass} = require "./BaseClass"
+# {Defaults} = require "./Defaults"
+
+{LayerStates} = require "./LayerStates"
+
+class exports.LayerStateMachine extends BaseClass
+ constructor: (layer) ->
+ super
+ @_previousNames = []
+ @_layer = layer
+ @currentName = 'initial'
+ @initial = LayerStates.filterStateProperties(layer.props)
+ @states = new LayerStates(@)
+
+ @define "current",
+ get: -> @states[@currentName]
+
+ @define "previous",
+ get: -> @states[@previousName]
+
+ @define "previousName",
+ get: -> _.last(@_previousNames)
+
+ @define "stateNames",
+ get: -> _.keys(@states)
+
+ switchTo: (stateName) ->
+ properties = @states[stateName]
+ if not properties?
+ throw Error "No such state: '#{stateName}'"
+ @emit(Events.StateWillSwitch, @currentName, stateName, @)
+ @_previousNames.push(@currentName)
+ @currentName = stateName
+ properties
+
+ next: (states) ->
+ if not states.length
+ states = @stateNames
+ Utils.arrayNext(states, @currentName)
+
+ emit: (args...) ->
+ super
+ # Also emit this to the layer with self as argument
+ @_layer.emit args...

0 comments on commit 5b8ca53

Please sign in to comment.