Skip to content

Commit

Permalink
Fixes #69
Browse files Browse the repository at this point in the history
State Machine now defaults to Framer.Defaults.Animation if no animationOptions are set.
  • Loading branch information
koenbok committed May 15, 2014
1 parent bb15649 commit 30e688e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
19 changes: 9 additions & 10 deletions framer/LayerStates.coffee
Expand Up @@ -2,6 +2,7 @@


{Events} = require "./Events" {Events} = require "./Events"
{BaseClass} = require "./BaseClass" {BaseClass} = require "./BaseClass"
{Defaults} = require "./Defaults"


LayerStatesIgnoredKeys = ["ignoreEvents"] LayerStatesIgnoredKeys = ["ignoreEvents"]


Expand All @@ -16,8 +17,7 @@ class exports.LayerStates extends BaseClass
@_states = {} @_states = {}
@_orderedStates = [] @_orderedStates = []


@animationOptions = @animationOptions = {}
curve: "spring"


# Always add the default state as the current # Always add the default state as the current
@add "default", @layer.properties @add "default", @layer.properties
Expand Down Expand Up @@ -69,26 +69,25 @@ class exports.LayerStates extends BaseClass


animationOptions ?= @animationOptions animationOptions ?= @animationOptions
animationOptions.properties = {} animationOptions.properties = {}

animatingKeys = @animatingKeys() animatingKeys = @animatingKeys()


for k, v of @_states[stateName] for propertyName, value of @_states[stateName]


# Don't animate ignored properties # Don't animate ignored properties
if k in LayerStatesIgnoredKeys if propertyName in LayerStatesIgnoredKeys
continue continue


if k not in animatingKeys if propertyName not in animatingKeys
continue continue


# Allow dynamic properties as functions # Allow dynamic properties as functions
v = v.call(@layer, @layer, stateName) if _.isFunction(v) value = value.call(@layer, @layer, stateName) if _.isFunction(value)


animationOptions.properties[k] = v animationOptions.properties[propertyName] = value


animation = @layer.animate animationOptions @_animation = @layer.animate animationOptions


animation.on "stop", => @_animation.on "stop", =>
@emit Events.StateDidSwitch, _.last(@_previousStates), stateName, @ @emit Events.StateDidSwitch, _.last(@_previousStates), stateName, @


switchInstant: (stateName) -> switchInstant: (stateName) ->
Expand Down
28 changes: 28 additions & 0 deletions test/tests/LayerStatesTest.coffee
@@ -1,6 +1,7 @@
describe "LayerStates", -> describe "LayerStates", ->


describe "Events", -> describe "Events", ->

beforeEach -> beforeEach ->
@layer = new Layer() @layer = new Layer()
@layer.states.add 'a', x: 100, y: 100 @layer.states.add 'a', x: 100, y: 100
Expand All @@ -27,3 +28,30 @@ describe "LayerStates", ->


@layer.states.on 'didSwitch', test @layer.states.on 'didSwitch', test
@layer.states.switchInstant 'a' @layer.states.switchInstant 'a'

describe "Defaults", ->

it "should set defaults", ->

layer = new Layer
layer.states.add "test", {x:123}
layer.states.switch "test"

layer.states._animation.options.curve.should.equal Framer.Defaults.Animation.curve

Framer.Defaults.Animation =
curve: "spring(1, 2, 3)"

layer = new Layer
layer.states.add "test", {x:456}
layer.states.switch "test"

layer.states._animation.options.curve.should.equal "spring(1, 2, 3)"

Framer.resetDefaults()



# describe "Switch", ->

# it "should switch instant", (done) ->

0 comments on commit 30e688e

Please sign in to comment.