1
0
mirror of https://github.com/aNNiMON/HotaruFX.git synced 2024-09-19 14:14:21 +03:00

Small refactoring

This commit is contained in:
Victor 2017-09-16 11:49:36 +03:00
parent 7f33c77d22
commit 66ef5b01af
15 changed files with 45 additions and 87 deletions

View File

@ -38,7 +38,7 @@ public class CompositionBundle implements Bundle {
composition = new Composition();
break;
case 1:
double frameRate = args[0].asNumber();
double frameRate = args[0].asDouble();
composition = new Composition(frameRate);
break;
case 2:
@ -49,14 +49,14 @@ public class CompositionBundle implements Bundle {
case 3:
width = args[0].asInt();
height = args[1].asInt();
frameRate = args[2].asNumber();
frameRate = args[2].asDouble();
composition = new Composition(width, height, frameRate);
break;
case 4:
default:
width = args[0].asInt();
height = args[1].asInt();
frameRate = args[2].asNumber();
frameRate = args[2].asDouble();
val background = PropertyType.PAINT.<Paint>getFromHFX().apply(args[3]);
composition = new Composition(width, height, frameRate, background);
break;

View File

@ -41,7 +41,7 @@ public class NodeUtilsBundle implements Bundle {
if (args.length == 2) {
val array = validator.requireArrayAt(1);
val dashList = array.stream()
.map(Value::asNumber)
.map(Value::asDouble)
.collect(Collectors.toList());
shape.getStrokeDashArray().setAll(dashList);
return NumberValue.ZERO;

View File

@ -52,7 +52,7 @@ public class NodesBundle implements Bundle {
val validator = Validator.with(args);
val map = validator.requireMapAt(1);
val points = validator.requireArrayAt(0).stream()
.map(Value::asNumber)
.map(Value::asDouble)
.collect(Collectors.toList());
val node = new NodeValue(ctor.apply(points));
node.fill(map);

View File

@ -62,12 +62,7 @@ public class ArrayValue implements Value, Iterable<Value> {
}
@Override
public int asInt() {
throw new TypeException("Cannot cast array to integer");
}
@Override
public double asNumber() {
public Number asNumber() {
throw new TypeException("Cannot cast array to number");
}

View File

@ -26,12 +26,7 @@ public class FunctionValue implements Value {
}
@Override
public int asInt() {
throw new TypeException("Cannot cast function to integer");
}
@Override
public double asNumber() {
public Number asNumber() {
throw new TypeException("Cannot cast function to number");
}

View File

@ -24,12 +24,7 @@ public class InterpolatorValue implements Value {
}
@Override
public int asInt() {
throw new TypeException("Cannot cast interpolator to integer");
}
@Override
public double asNumber() {
public Number asNumber() {
throw new TypeException("Cannot cast interpolator to number");
}

View File

@ -46,12 +46,7 @@ public class MapValue implements Value, Iterable<Map.Entry<String, Value>> {
}
@Override
public int asInt() {
throw new TypeException("Cannot cast map to integer");
}
@Override
public double asNumber() {
public Number asNumber() {
throw new TypeException("Cannot cast map to number");
}

View File

@ -116,12 +116,7 @@ public class NodeValue implements Value {
}
@Override
public int asInt() {
throw new TypeException("Cannot cast node to integer");
}
@Override
public double asNumber() {
public Number asNumber() {
throw new TypeException("Cannot cast node to number");
}

View File

@ -55,34 +55,9 @@ public class NumberValue implements Value {
return value.intValue() != 0;
}
public byte asByte() {
return value.byteValue();
}
public short asShort() {
return value.shortValue();
}
@Override
public int asInt() {
return value.intValue();
}
public long asLong() {
return value.longValue();
}
public float asFloat() {
return value.floatValue();
}
public double asDouble() {
return value.doubleValue();
}
@Override
public double asNumber() {
return value.doubleValue();
public Number asNumber() {
return value;
}
@Override

View File

@ -108,12 +108,7 @@ public class PropertyValue implements Value {
}
@Override
public int asInt() {
throw new TypeException("Cannot cast property to integer");
}
@Override
public double asNumber() {
public Number asNumber() {
throw new TypeException("Cannot cast property to number");
}

View File

@ -4,8 +4,6 @@ import java.util.Objects;
public class StringValue implements Value {
public static final StringValue EMPTY = new StringValue("");
private final String value;
public StringValue(String value) {
@ -36,7 +34,7 @@ public class StringValue implements Value {
}
@Override
public double asNumber() {
public double asDouble() {
try {
return Double.parseDouble(value);
} catch (NumberFormatException e) {
@ -44,6 +42,11 @@ public class StringValue implements Value {
}
}
@Override
public Number asNumber() {
return asDouble();
}
@Override
public String asString() {
return value;

View File

@ -4,9 +4,19 @@ public interface Value extends Comparable<Value> {
Object raw();
int asInt();
default boolean asBoolean() {
return asInt() != 0;
}
double asNumber();
default int asInt() {
return asNumber().intValue();
}
default double asDouble() {
return asNumber().doubleValue();
}
Number asNumber();
String asString();

View File

@ -117,12 +117,12 @@ public class InterpreterVisitor implements ResultVisitor<Value, Context> {
final double frame;
switch (node.operator) {
case SECONDS:
frame = value.asNumber() * frameRate;
frame = value.asDouble() * frameRate;
break;
case MILLISECONDS:
default:
frame = (value.asNumber() * frameRate) / 1000d;
frame = (value.asDouble() * frameRate) / 1000d;
break;
}
return NumberValue.of(frame);

View File

@ -23,7 +23,7 @@ import lombok.val;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public enum PropertyType {
BOOLEAN(v -> v.asInt() != 0, o -> NumberValue.fromBoolean(Boolean.TRUE.equals(o))),
BOOLEAN(Value::asBoolean, o -> NumberValue.fromBoolean(Boolean.TRUE.equals(o))),
NUMBER(toNumber(), o -> NumberValue.of((Number) o)),
STRING(Value::asString, o -> new StringValue(String.valueOf(o))),
NODE(toNode(), fromNode()),
@ -50,7 +50,7 @@ public enum PropertyType {
if (value.type() == Types.NUMBER) {
return ((NumberValue) value).raw();
}
return value.asNumber();
return value.asDouble();
};
}
@ -82,12 +82,12 @@ public enum PropertyType {
val map = ((MapValue) value).getMap();
val family = map.getOrDefault("family", new StringValue(Font.getDefault().getFamily())).asString();
val weight = map.getOrDefault("weight", NumberValue.of(FontWeight.NORMAL.getWeight())).asInt();
val isItalic = map.getOrDefault("italic", NumberValue.ZERO).asInt() != 0;
val isItalic = map.getOrDefault("italic", NumberValue.ZERO).asBoolean();
val posture = isItalic ? FontPosture.ITALIC : FontPosture.REGULAR;
val size = map.getOrDefault("size", NumberValue.MINUS_ONE).asNumber();
val size = map.getOrDefault("size", NumberValue.MINUS_ONE).asDouble();
return Font.font(family, FontWeight.findByWeight(weight), posture, size);
}
return Font.font(value.asNumber());
return Font.font(value.asDouble());
};
}

View File

@ -126,25 +126,25 @@ class InterpreterVisitorTest {
Value value;
value = eval("rate(30) t = 1 sec", context);
assertThat(value.asNumber(), closeTo(30, 0.0001));
assertThat(value.asDouble(), closeTo(30, 0.0001));
value = eval("rate(30) t = 5 sec", context);
assertThat(value.asNumber(), closeTo(150, 0.0001));
assertThat(value.asDouble(), closeTo(150, 0.0001));
value = eval("rate(30) t = 0.5 sec", context);
assertThat(value.asNumber(), closeTo(15, 0.0001));
assertThat(value.asDouble(), closeTo(15, 0.0001));
value = eval("rate(30) t = 1000 ms", context);
assertThat(value.asNumber(), closeTo(30, 0.0001));
assertThat(value.asDouble(), closeTo(30, 0.0001));
value = eval("rate(30) t = 333 ms", context);
assertThat(value.asNumber(), closeTo(10, 0.01));
assertThat(value.asDouble(), closeTo(10, 0.01));
value = eval("rate(25) t = 1 sec", context);
assertThat(value.asNumber(), closeTo(25, 0.0001));
assertThat(value.asDouble(), closeTo(25, 0.0001));
value = eval("rate(25) t = 1000 ms", context);
assertThat(value.asNumber(), closeTo(25, 0.0001));
assertThat(value.asDouble(), closeTo(25, 0.0001));
}
@Test