Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
origami value mapper
Browse files Browse the repository at this point in the history
Adding a helper for constructing spring config values that match up with
the Origami design prototyping tool.

http://facebook.github.io/origami/
  • Loading branch information
willbailey committed Feb 3, 2014
1 parent a0c164e commit 416361f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.widget.TableLayout;
import android.widget.TextView;

import com.facebook.rebound.OrigamiValueConverter;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringConfigRegistry;
Expand All @@ -52,9 +53,9 @@ public class SpringConfiguratorView extends FrameLayout {

private static final int MAX_SEEKBAR_VAL = 100000;
private static final float MIN_TENSION = 0;
private static final float MAX_TENSION = 500;
private static final float MAX_TENSION = 200;
private static final float MIN_FRICTION = 0;
private static final float MAX_FRICTION = 100;
private static final float MAX_FRICTION = 50;
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.#");

private final SpinnerAdapter spinnerAdapter;
Expand Down Expand Up @@ -266,14 +267,16 @@ public void onProgressChanged(SeekBar seekBar, int val, boolean b) {

if (seekBar == mTensionSeekBar) {
float scaledTension = ((val) * tensionRange) / MAX_SEEKBAR_VAL + MIN_TENSION;
mSelectedSpringConfig.tension = scaledTension;
mSelectedSpringConfig.tension =
OrigamiValueConverter.tensionFromOrigamiValue(scaledTension);
String roundedTensionLabel = DECIMAL_FORMAT.format(scaledTension);
mTensionLabel.setText("T:" + roundedTensionLabel);
}

if (seekBar == mFrictionSeekBar) {
float scaledFriction = ((val) * frictionRange) / MAX_SEEKBAR_VAL + MIN_FRICTION;
mSelectedSpringConfig.friction = scaledFriction;
mSelectedSpringConfig.friction =
OrigamiValueConverter.frictionFromOrigamiValue(scaledFriction);
String roundedFrictionLabel = DECIMAL_FORMAT.format(scaledFriction);
mFrictionLabel.setText("F:" + roundedFrictionLabel);
}
Expand All @@ -293,11 +296,11 @@ public void onStopTrackingTouch(SeekBar seekBar) {
* @param springConfig current editing spring
*/
private void updateSeekBarsForSpringConfig(SpringConfig springConfig) {
float tension = (float) springConfig.tension;
float tension = (float) OrigamiValueConverter.origamiValueFromTension(springConfig.tension);
float tensionRange = MAX_TENSION - MIN_TENSION;
int scaledTension = Math.round(((tension - MIN_TENSION) * MAX_SEEKBAR_VAL) / tensionRange);

float friction = (float) springConfig.friction;
float friction = (float) OrigamiValueConverter.origamiValueFromFriction(springConfig.friction);
float frictionRange = MAX_FRICTION - MIN_FRICTION;
int scaledFriction = Math.round(((friction - MIN_FRICTION) * MAX_SEEKBAR_VAL) / frictionRange);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.facebook.rebound;

/**
* Helper math util to convert tension & friction values from the Origami design tool to values that
* the spring system needs.
*/
public class OrigamiValueConverter {

public static double tensionFromOrigamiValue(double oValue) {
return oValue == 0 ? 0 : (oValue - 30.0) * 3.62 + 194.0;
}

public static double origamiValueFromTension(double tension) {
return tension == 0 ? 0 : (tension - 194.0) / 3.62 + 30.0;
}

public static double frictionFromOrigamiValue(double oValue) {
return oValue == 0 ? 0 : (oValue - 8.0) * 3.0 + 25.0;
}

public static double origamiValueFromFriction(double friction) {
return friction == 0 ? 0 : (friction - 25.0) / 3.0 + 8.0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SpringConfig {
public double friction;
public double tension;

public static SpringConfig defaultConfig = new SpringConfig(230.2, 22);
public static SpringConfig defaultConfig = SpringConfig.fromOrigamiTensionAndFriction(40, 7);

/**
* constructor for the SpringConfig
Expand All @@ -28,4 +28,17 @@ public SpringConfig(double tension, double friction) {
this.tension = tension;
this.friction = friction;
}

/**
* A helper to make creating a SpringConfig easier with values mapping to the Origami values.
* @param qcTension tension as defined in the Quartz Composition
* @param qcFriction friction as defined in the Quartz Composition
* @return a SpringConfig that maps to these values
*/
public static SpringConfig fromOrigamiTensionAndFriction(double qcTension, double qcFriction) {
return new SpringConfig(
OrigamiValueConverter.tensionFromOrigamiValue(qcTension),
OrigamiValueConverter.frictionFromOrigamiValue(qcFriction)
);
}
}

0 comments on commit 416361f

Please sign in to comment.