Advertisement
Barteks2x

Untitled

Dec 21st, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1.         final float baseValue = 2;
  2.         UISlider<Float> slider = new UISlider<>(this, this.width - 32, Converter.from(
  3.             x -> {
  4.                 // try to snap to number divisible by the highest power of base number that is within distance of 1 pixel
  5.                 // for non-integer values (negative exponent)... it will probably do something useful anyway
  6.                 final double prevVal = pow(baseValue, x);
  7.                 final double snapSize = (1.0f/this.width)*(max - min)*0.5;
  8.                 int startExponent = MathHelper.ceil(max);
  9.                 for (int trySnapDivExp = startExponent; ; trySnapDivExp--) {
  10.                     double divisor = pow(baseValue, trySnapDivExp);
  11.                     if (divisor == 0) {
  12.                         return (float) prevVal;
  13.                     }
  14.                     double snapVal = round(prevVal/divisor)*divisor;
  15.                     if (Double.isNaN(snapVal) || Double.isInfinite(snapVal)) {
  16.                         return (float) prevVal;
  17.                     }
  18.                     double snapExp = log(snapVal)/log(baseValue);
  19.                     double snapExpToExpDist = abs(x - snapExp);
  20.                     if (snapExpToExpDist < snapSize) {
  21.                         return (float) snapVal;
  22.                     }
  23.                 }
  24.             },
  25.             x -> {
  26.                 float exp = (float) (log(x)/log(baseValue));
  27.                 float offset = (exp - min)/(max - min);
  28.                 return offset;
  29.             }
  30.         ), name)
  31.             .setPosition(0, 16)
  32.             .setValue(defaultVal);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement