Commit 9932e9f0 authored by Chinmay Garde's avatar Chinmay Garde

Test kinetic scrolling group simulation

parent c3aaf8aa
...@@ -15,6 +15,7 @@ class Scroll extends SimulationGroup { ...@@ -15,6 +15,7 @@ class Scroll extends SimulationGroup {
bool _isSpringing = false; bool _isSpringing = false;
Simulation _currentSimulation; Simulation _currentSimulation;
double _offset = 0.0;
Scroll(double position, double velocity, double leading, double trailing, Scroll(double position, double velocity, double leading, double trailing,
SpringDesc spring, double drag) SpringDesc spring, double drag)
...@@ -22,30 +23,43 @@ class Scroll extends SimulationGroup { ...@@ -22,30 +23,43 @@ class Scroll extends SimulationGroup {
_trailingExtent = trailing, _trailingExtent = trailing,
_springDesc = spring, _springDesc = spring,
_drag = drag { _drag = drag {
_chooseSimulation(position, velocity); _chooseSimulation(position, velocity, 0.0);
} }
@override @override
void step(double time) => _chooseSimulation( void step(double time) => _chooseSimulation(
_currentSimulation.x(time), _currentSimulation.dx(time)); _currentSimulation.x(time - _offset),
_currentSimulation.dx(time - _offset), time);
@override @override
Simulation get currentSimulation => _currentSimulation; Simulation get currentSimulation => _currentSimulation;
void _chooseSimulation(double position, double velocity) { @override
/// This simulation can only step forward double get currentIntervalOffset => _offset;
void _chooseSimulation(
double position, double velocity, double intervalOffset) {
/// This simulation can only step forward.
if (!_isSpringing) { if (!_isSpringing) {
if (position > _trailingExtent) { if (position > _trailingExtent) {
_isSpringing = true; _isSpringing = true;
_offset = intervalOffset;
_currentSimulation = _currentSimulation =
new Spring(_springDesc, position, _trailingExtent, velocity); new Spring(_springDesc, position, _trailingExtent, velocity);
return;
} else if (position < _leadingExtent) { } else if (position < _leadingExtent) {
_isSpringing = true; _isSpringing = true;
_offset = intervalOffset;
_currentSimulation = _currentSimulation =
new Spring(_springDesc, position, _leadingExtent, velocity); new Spring(_springDesc, position, _leadingExtent, velocity);
return;
} }
} else if (_currentSimulation == null) { }
if (_currentSimulation == null) {
_currentSimulation = new Friction(_drag, position, velocity); _currentSimulation = new Friction(_drag, position, velocity);
return;
} }
} }
} }
...@@ -4,24 +4,31 @@ ...@@ -4,24 +4,31 @@
part of newton; part of newton;
/// The abstract base class of all composite simulations. Concrete subclasses /// The abstract base class for all composite simulations. Concrete subclasses
/// must implement the appropriate methods to select the appropriate simulation /// must implement the appropriate methods to select the appropriate simulation
/// at a given time interval. The simulation group takes care to call the `step` /// at a given time interval. The simulation group takes care to call the `step`
/// method at appropriate intervals. If more fine grained control over the the /// method at appropriate intervals. If more fine grained control over the the
/// step is necessary, subclasses may override the `Simulatable` methods. /// step is necessary, subclasses may override `Simulatable` methods.
abstract class SimulationGroup extends Simulation { abstract class SimulationGroup extends Simulation {
/// The currently active simulation
Simulation get currentSimulation; Simulation get currentSimulation;
/// The time offset applied to the currently active simulation;
double get currentIntervalOffset;
/// Called when a significant change in the interval is detected. Subclasses
/// must decide if the the current simulation must be switched (or updated).
void step(double time); void step(double time);
double x(double time) { double x(double time) {
_stepIfNecessary(time); _stepIfNecessary(time);
return currentSimulation.x(time); return currentSimulation.x(time - currentIntervalOffset);
} }
double dx(double time) { double dx(double time) {
_stepIfNecessary(time); _stepIfNecessary(time);
return currentSimulation.dx(time); return currentSimulation.dx(time - currentIntervalOffset);
} }
@override @override
......
...@@ -34,7 +34,6 @@ class _CriticalSolution implements _SpringSolution { ...@@ -34,7 +34,6 @@ class _CriticalSolution implements _SpringSolution {
return new _CriticalSolution.withArgs(r, c1, c2); return new _CriticalSolution.withArgs(r, c1, c2);
} }
@override
SpringType get type => SpringType.criticallyDamped; SpringType get type => SpringType.criticallyDamped;
_CriticalSolution.withArgs(double r, double c1, double c2) _CriticalSolution.withArgs(double r, double c1, double c2)
...@@ -72,7 +71,6 @@ class _OverdampedSolution implements _SpringSolution { ...@@ -72,7 +71,6 @@ class _OverdampedSolution implements _SpringSolution {
_c1 = c1, _c1 = c1,
_c2 = c2; _c2 = c2;
@override
SpringType get type => SpringType.overDamped; SpringType get type => SpringType.overDamped;
double x(double time) => double x(double time) =>
...@@ -103,7 +101,6 @@ class _UnderdampedSolution implements _SpringSolution { ...@@ -103,7 +101,6 @@ class _UnderdampedSolution implements _SpringSolution {
_c1 = c1, _c1 = c1,
_c2 = c2; _c2 = c2;
@override
SpringType get type => SpringType.underDamped; SpringType get type => SpringType.underDamped;
double x(double time) => Math.pow(Math.E, _r * time) * double x(double time) => Math.pow(Math.E, _r * time) *
......
...@@ -132,4 +132,16 @@ void main() { ...@@ -132,4 +132,16 @@ void main() {
expect(under.isDone(6.0), true); expect(under.isDone(6.0), true);
}); });
test('test_kinetic_scroll', () {
var spring = new SpringDesc.withDampingRatio(1.0, 50.0, 0.5);
var scroll = new Scroll(100.0, 800.0, 0.0, 300.0, spring, 0.3);
expect(scroll.isDone(0.0), false);
expect(scroll.isDone(3.5), true);
var scroll2 = new Scroll(100.0, -800.0, 0.0, 300.0, spring, 0.3);
expect(scroll2.isDone(4.5), true);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment