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

Add strokeDashArray function

This commit is contained in:
Victor 2017-09-08 14:45:43 +03:00
parent d879ece3f9
commit 076825a814
5 changed files with 75 additions and 17 deletions

View File

@ -11,10 +11,11 @@ import lombok.val;
public final class BundleLoader {
public static List<Class<? extends Bundle>> allBundles() {
public static List<Class<? extends Bundle>> runtimeBundles() {
return Arrays.asList(
CompositionBundle.class,
NodesBundle.class
NodesBundle.class,
NodeUtilsBundle.class
);
}
@ -24,7 +25,7 @@ public final class BundleLoader {
public static Map<String, FunctionType> functions() {
val functions = new HashMap<String, FunctionType>();
apply(allBundles(), functions, ((bundle, map) -> map.putAll(bundle.functions())));
apply(runtimeBundles(), functions, ((bundle, map) -> map.putAll(bundle.functions())));
return functions;
}

View File

@ -0,0 +1,53 @@
package com.annimon.hotarufx.bundles;
import com.annimon.hotarufx.exceptions.TypeException;
import com.annimon.hotarufx.lib.ArrayValue;
import com.annimon.hotarufx.lib.Context;
import com.annimon.hotarufx.lib.Function;
import com.annimon.hotarufx.lib.NumberValue;
import com.annimon.hotarufx.lib.Types;
import com.annimon.hotarufx.lib.Validator;
import com.annimon.hotarufx.lib.Value;
import com.annimon.hotarufx.visual.objects.ShapeNode;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import javafx.scene.shape.Shape;
import lombok.val;
import static com.annimon.hotarufx.bundles.FunctionInfo.of;
import static com.annimon.hotarufx.bundles.FunctionType.COMMON;
public class NodeUtilsBundle implements Bundle {
private static final Map<String, FunctionInfo> FUNCTIONS;
static {
FUNCTIONS = new HashMap<>();
FUNCTIONS.put("strokeDashArray", of(COMMON, NodeUtilsBundle::strokePattern));
}
@Override
public Map<String, FunctionInfo> functionsInfo() {
return FUNCTIONS;
}
private static Function strokePattern(Context context) {
return args -> {
val validator = Validator.with(args);
validator.checkOrOr(1, 2);
if (args[0].type() != Types.NODE || !(args[0].raw() instanceof ShapeNode)) {
throw new TypeException("Shape required at first argument");
}
val shape = (Shape) ((ShapeNode) args[0].raw()).getFxNode();
if (args.length == 2) {
val array = validator.requireArrayAt(1);
val dashList = array.stream()
.map(Value::asNumber)
.collect(Collectors.toList());
shape.getStrokeDashArray().setAll(dashList);
return NumberValue.ZERO;
} else {
return ArrayValue.from(shape.getStrokeDashArray(), NumberValue::of);
}
};
}
}

View File

@ -2,13 +2,21 @@ package com.annimon.hotarufx.lib;
import com.annimon.hotarufx.exceptions.TypeException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import lombok.Getter;
public class ArrayValue implements Value, Iterable<Value> {
public static <T> ArrayValue from(Collection<? extends T> collection,
Function<? super T, Value> converter) {
return new ArrayValue(collection.stream()
.map(converter)
.toArray(Value[]::new));
}
@Getter
private final Value[] elements;

View File

@ -2,10 +2,7 @@ package com.annimon.hotarufx.ui.controller;
import com.annimon.hotarufx.Main;
import com.annimon.hotarufx.bundles.BundleLoader;
import com.annimon.hotarufx.bundles.CompositionBundle;
import com.annimon.hotarufx.bundles.NodesBundle;
import com.annimon.hotarufx.exceptions.Exceptions;
import com.annimon.hotarufx.exceptions.HotaruRuntimeException;
import com.annimon.hotarufx.io.DocumentListener;
import com.annimon.hotarufx.io.DocumentManager;
import com.annimon.hotarufx.io.FileManager;
@ -19,7 +16,6 @@ import com.annimon.hotarufx.ui.control.LibraryItem;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import javafx.application.Platform;
@ -134,10 +130,7 @@ public class EditorController implements Initializable, DocumentListener {
}
val context = new Context();
BundleLoader.load(context, Arrays.asList(
CompositionBundle.class,
NodesBundle.class
));
BundleLoader.load(context, BundleLoader.runtimeBundles());
val parser = new HotaruParser(HotaruLexer.tokenize(input));
val program = parser.parse();

View File

@ -270,17 +270,20 @@
<LibraryItem text="Line">
<graphic>
<Line startX="-20" startY="-20"
strokeWidth="3" stroke="#aaa" />
strokeWidth="3" stroke="#aaa"
strokeDashArray="6, 8, 2, 8" />
</graphic>
<code>
line({\n
__startX: -600\n
__startY: -300\n
LINE = line({\n
__startX: -600,\n
__startY: -300,\n
__endX: 600\n
__endY: 300,\n
__stroke: "#AAA",\n
__strokeWidth: 5\n
})
})\n
strokeDashArray(LINE, [25, 20, 5, 20])\n
LINE@strokeDashOffset.add(2 sec, -200)\n
</code>
</LibraryItem>
<LibraryItem text="Arc">