Created
November 6, 2014 01:48
-
-
Save willbailey/7800ee0205a2f094e588 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SpringChain = function(attachmentSpringConfig, mainSpringConfig) { | |
this._springSystem = new rebound.SpringSystem(); | |
this._listeners = []; | |
this._springs = []; | |
this._controlSpringIndex = -1; | |
this._attachmentSpringConfig = attachmentSpringConfig || | |
SpringChain.DEFAULT_ATTACHMENT_SPRING_CONFIG; | |
this._mainSpringConfig = mainSpringConfig || | |
SpringChain.DEFAULT_MAIN_SPRING_CONFIG; | |
}; | |
SpringChain.DEFAULT_MAIN_SPRING_CONFIG = | |
rebound.SpringConfig.fromOrigamiTensionAndFriction(40, 7); | |
SpringChain.DEFAULT_ATTACHMENT_SPRING_CONFIG = | |
rebound.SpringConfig.fromOrigamiTensionAndFriction(80, 9); | |
SpringChain.prototype.addSpring = function(listener) { | |
var spring = this._springSystem.createSpring() | |
.addListener(this) | |
.setSpringConfig(this._attachmentSpringConfig); | |
this._springs.push(spring); | |
this._listeners.push(listener); | |
return this; | |
}; | |
SpringChain.prototype.getControlSpring = function() { | |
return this._springs[this._controlSpringIndex]; | |
} | |
SpringChain.prototype.setControlSpringIndex = function(idx) { | |
this._controlSpringIndex = idx; | |
var controlSpring = this._springs[i]; | |
if (!controlSpring) { | |
return null; | |
} | |
for (var i = 0, len = this._springs.length; i < len; i++) { | |
spring.setSpringConfig(this._attachmentSpringConfig); | |
} | |
this.getControlSpring().setSpringConfig(this._mainSpringConfig); | |
return this; | |
}; | |
SpringChain.prototype.getControlSpring = function() { | |
return this._springs[this._controlSpringIndex]; | |
}; | |
SpringChain.prototype.getSpringSystem = function() { | |
return this._springSystem; | |
}; | |
SpringChain.prototype.getAllSprings = function() { | |
return this._springs; | |
}; | |
SpringChain.prototype._proxyEvent = function(evt, spring) { | |
var idx = this._springs.indexOf(spring); | |
var listener = this._listeners[idx]; | |
if (!listener) { | |
return; | |
} | |
var handler = listener[evt]; | |
if (!handler) { | |
return; | |
} | |
handler.call(listener, spring); | |
}; | |
SpringChain.prototype.onSpringActivate = function(spring) { | |
this._proxyEvent('onSpringActivate', spring); | |
}; | |
SpringChain.prototype.onSpringEndStateChange = function(spring) { | |
this._proxyEvent('onSpringEndStateChange', spring); | |
}; | |
SpringChain.prototype.onSpringUpdate = function(spring) { | |
var idx = this._springs.indexOf(spring); | |
var listener = this._listeners[idx]; | |
var above = -1, below = -1; | |
if (idx === this._controlSpringIndex) { | |
below = idx - 1; | |
above = idx + 1; | |
} else if (idx < this._controlSpringIndex) { | |
below = idx - 1; | |
} else if (idx > this._controlSpringIndex) { | |
above = idx + 1; | |
} | |
if (above > -1 && above < this._springs.length) { | |
this._springs[above].setEndValue(spring.getCurrentValue()); | |
} | |
if (below > -1 && below < this._springs.length) { | |
this._springs[below].setEndValue(spring.getCurrentValue()); | |
} | |
listener.onSpringUpdate(spring); | |
}; | |
SpringChain.prototype.onSpringAtRest = function(spring) { | |
this._proxyEvent('onSpringAtRest', spring); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment