mirror of
https://github.com/aNNiMON/HotaruFX.git
synced 2024-09-19 14:14:21 +03:00
Add function types info to bundles
This commit is contained in:
parent
dddc678b23
commit
7f2f1422f1
@ -1,8 +1,23 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public interface Bundle {
|
||||
|
||||
void load(Context context);
|
||||
Map<String, FunctionInfo> functionsInfo();
|
||||
|
||||
default Map<String, FunctionType> functions() {
|
||||
return functionsInfo().entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
e -> e.getValue().getType()));
|
||||
}
|
||||
|
||||
default void load(Context context) {
|
||||
functionsInfo().forEach((name, info) -> {
|
||||
context.functions().put(name, info.extract(context));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,47 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import lombok.val;
|
||||
|
||||
public final class BundleLoader {
|
||||
|
||||
public static List<Class<? extends Bundle>> allBundles() {
|
||||
return Arrays.asList(
|
||||
CompositionBundle.class,
|
||||
NodesBundle.class
|
||||
);
|
||||
}
|
||||
|
||||
public static void loadSingle(Context context, Class<? extends Bundle> clazz) {
|
||||
load(context, Collections.singletonList(clazz));
|
||||
}
|
||||
|
||||
public static Map<String, FunctionType> functions() {
|
||||
val functions = new HashMap<String, FunctionType>();
|
||||
apply(allBundles(), functions, ((bundle, map) -> map.putAll(bundle.functions())));
|
||||
return functions;
|
||||
}
|
||||
|
||||
public static void load(Context context, List<Class<? extends Bundle>> bundles) {
|
||||
if (bundles == null || bundles.isEmpty()) {
|
||||
apply(bundles, context, Bundle::load);
|
||||
}
|
||||
|
||||
private static <T> void apply(List<Class<? extends Bundle>> bundles,
|
||||
T obj, BiConsumer<Bundle, T> action) {
|
||||
if (action == null || bundles == null || bundles.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Class<? extends Bundle> clazz : bundles) {
|
||||
try {
|
||||
val bundle = clazz.newInstance();
|
||||
bundle.load(context);
|
||||
action.accept(bundle, obj);
|
||||
} catch (IllegalAccessException | InstantiationException ignore) {}
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,35 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import com.annimon.hotarufx.lib.Function;
|
||||
import com.annimon.hotarufx.lib.NodeValue;
|
||||
import com.annimon.hotarufx.lib.NumberValue;
|
||||
import com.annimon.hotarufx.lib.Types;
|
||||
import com.annimon.hotarufx.visual.Composition;
|
||||
import com.annimon.hotarufx.visual.visitors.RenderVisitor;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import lombok.val;
|
||||
import static com.annimon.hotarufx.bundles.FunctionInfo.of;
|
||||
import static com.annimon.hotarufx.bundles.FunctionType.COMMON;
|
||||
|
||||
public class CompositionBundle implements Bundle {
|
||||
|
||||
private static final Map<String, FunctionInfo> FUNCTIONS;
|
||||
static {
|
||||
FUNCTIONS = new HashMap<>();
|
||||
FUNCTIONS.put("composition", of(COMMON, CompositionBundle::composition));
|
||||
FUNCTIONS.put("render", of(COMMON, CompositionBundle::render));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(Context context) {
|
||||
context.functions().put("composition", args -> {
|
||||
public Map<String, FunctionInfo> functionsInfo() {
|
||||
return FUNCTIONS;
|
||||
}
|
||||
|
||||
private static Function composition(Context context) {
|
||||
return args -> {
|
||||
final int width, height;
|
||||
final double frameRate;
|
||||
switch (args.length) {
|
||||
@ -47,9 +63,11 @@ public class CompositionBundle implements Bundle {
|
||||
context.variables().put("HalfWidth", NumberValue.of(scene.getVirtualWidth() / 2));
|
||||
context.variables().put("HalfHeight", NumberValue.of(scene.getVirtualHeight() / 2));
|
||||
return NumberValue.ZERO;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
context.functions().put("render", args -> {
|
||||
private static Function render(Context context) {
|
||||
return args -> {
|
||||
val renderVisitor = new RenderVisitor(context.composition().getTimeline());
|
||||
val scene = context.composition().getScene();
|
||||
Arrays.stream(args)
|
||||
@ -57,6 +75,6 @@ public class CompositionBundle implements Bundle {
|
||||
.map(v -> ((NodeValue) v).getNode())
|
||||
.forEach(node -> node.accept(renderVisitor, scene));
|
||||
return NumberValue.ZERO;
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import com.annimon.hotarufx.lib.Function;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class FunctionInfo {
|
||||
|
||||
public static FunctionInfo of(FunctionType type, Function function) {
|
||||
return of(type, (Context c) -> function);
|
||||
}
|
||||
|
||||
public static FunctionInfo of(FunctionType type, java.util.function.Function<Context, Function> extractor) {
|
||||
return new FunctionInfo(type, extractor);
|
||||
}
|
||||
|
||||
@Getter
|
||||
private final FunctionType type;
|
||||
private final java.util.function.Function<Context, Function> functionExtractor;
|
||||
|
||||
public Function extract(Context context) {
|
||||
return functionExtractor.apply(context);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
public enum FunctionType {
|
||||
COMMON, NODE
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import com.annimon.hotarufx.lib.Function;
|
||||
import com.annimon.hotarufx.lib.NodeValue;
|
||||
import com.annimon.hotarufx.lib.Types;
|
||||
@ -8,28 +7,38 @@ import com.annimon.hotarufx.lib.Validator;
|
||||
import com.annimon.hotarufx.lib.Value;
|
||||
import com.annimon.hotarufx.visual.objects.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.val;
|
||||
import static com.annimon.hotarufx.bundles.FunctionInfo.of;
|
||||
import static com.annimon.hotarufx.bundles.FunctionType.NODE;
|
||||
|
||||
public class NodesBundle implements Bundle {
|
||||
|
||||
@Override
|
||||
public void load(Context context) {
|
||||
context.functions().put("arc", node(ArcNode::new));
|
||||
context.functions().put("circle", node(CircleNode::new));
|
||||
context.functions().put("ellipse", node(EllipseNode::new));
|
||||
context.functions().put("group", group());
|
||||
context.functions().put("line", node(LineNode::new));
|
||||
context.functions().put("polygon", poly(PolygonNode::new));
|
||||
context.functions().put("polyline", poly(PolylineNode::new));
|
||||
context.functions().put("rectangle", node(RectangleNode::new));
|
||||
context.functions().put("svgPath", node(SVGPathNode::new));
|
||||
context.functions().put("text", node(TextNode::new));
|
||||
private static final Map<String, FunctionInfo> FUNCTIONS;
|
||||
static {
|
||||
FUNCTIONS = new HashMap<>();
|
||||
FUNCTIONS.put("arc", of(NODE, node(ArcNode::new)));
|
||||
FUNCTIONS.put("circle", of(NODE, node(CircleNode::new)));
|
||||
FUNCTIONS.put("ellipse", of(NODE, node(EllipseNode::new)));
|
||||
FUNCTIONS.put("group", of(NODE, group()));
|
||||
FUNCTIONS.put("line", of(NODE, node(LineNode::new)));
|
||||
FUNCTIONS.put("polygon", of(NODE, poly(PolygonNode::new)));
|
||||
FUNCTIONS.put("polyline", of(NODE, poly(PolylineNode::new)));
|
||||
FUNCTIONS.put("rectangle", of(NODE, node(RectangleNode::new)));
|
||||
FUNCTIONS.put("svgPath", of(NODE, node(SVGPathNode::new)));
|
||||
FUNCTIONS.put("text", of(NODE, node(TextNode::new)));
|
||||
}
|
||||
|
||||
private Function node(Supplier<? extends ObjectNode> supplier) {
|
||||
@Override
|
||||
public Map<String, FunctionInfo> functionsInfo() {
|
||||
return FUNCTIONS;
|
||||
}
|
||||
|
||||
private static Function node(Supplier<? extends ObjectNode> supplier) {
|
||||
return args -> {
|
||||
val map = Validator.with(args).requireMapAt(0);
|
||||
val node = new NodeValue(supplier.get());
|
||||
@ -38,7 +47,7 @@ public class NodesBundle implements Bundle {
|
||||
};
|
||||
}
|
||||
|
||||
private Function poly(java.util.function.Function<List<Double>, ObjectNode> ctor) {
|
||||
private static Function poly(java.util.function.Function<List<Double>, ObjectNode> ctor) {
|
||||
return args -> {
|
||||
val validator = Validator.with(args);
|
||||
val map = validator.requireMapAt(1);
|
||||
@ -51,7 +60,7 @@ public class NodesBundle implements Bundle {
|
||||
};
|
||||
}
|
||||
|
||||
private Function group() {
|
||||
private static Function group() {
|
||||
return args -> {
|
||||
val nodes = Arrays.stream(args)
|
||||
.filter(v -> v.type() == Types.NODE)
|
||||
|
Loading…
Reference in New Issue
Block a user