mirror of
https://github.com/aNNiMON/HotaruFX.git
synced 2024-09-19 14:14:21 +03:00
Add composition bundle
This commit is contained in:
parent
cf21aef0ac
commit
1e5ca3c1b3
@ -0,0 +1,8 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
|
||||
public interface Bundle {
|
||||
|
||||
void load(Context context);
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import lombok.val;
|
||||
|
||||
public final class BundleLoader {
|
||||
|
||||
public static void loadSingle(Context context, Class<? extends Bundle> clazz) {
|
||||
load(context, Collections.singletonList(clazz));
|
||||
}
|
||||
|
||||
public static void load(Context context, List<Class<? extends Bundle>> bundles) {
|
||||
if (bundles == null || bundles.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Class<? extends Bundle> clazz : bundles) {
|
||||
try {
|
||||
val bundle = clazz.newInstance();
|
||||
bundle.load(context);
|
||||
} catch (IllegalAccessException | InstantiationException ignore) {}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import com.annimon.hotarufx.lib.NumberValue;
|
||||
import com.annimon.hotarufx.visual.Composition;
|
||||
import lombok.val;
|
||||
|
||||
public class CompositionBundle implements Bundle {
|
||||
|
||||
@Override
|
||||
public void load(Context context) {
|
||||
context.functions().put("composition", args -> {
|
||||
final int width, height;
|
||||
final double frameRate;
|
||||
switch (args.length) {
|
||||
case 0:
|
||||
width = 1280;
|
||||
height = 720;
|
||||
frameRate = 30d;
|
||||
break;
|
||||
case 1:
|
||||
width = 1280;
|
||||
height = 720;
|
||||
frameRate = args[0].asNumber();
|
||||
break;
|
||||
case 2:
|
||||
width = args[0].asInt();
|
||||
height = args[1].asInt();
|
||||
frameRate = 30d;
|
||||
break;
|
||||
case 3:
|
||||
default:
|
||||
width = args[0].asInt();
|
||||
height = args[1].asInt();
|
||||
frameRate = args[2].asNumber();
|
||||
break;
|
||||
}
|
||||
val composition = new Composition(width, height, frameRate);
|
||||
val scene = composition.getScene();
|
||||
context.composition(composition);
|
||||
context.variables().put("Width", NumberValue.of(scene.getVirtualWidth()));
|
||||
context.variables().put("Height", NumberValue.of(scene.getVirtualHeight()));
|
||||
context.variables().put("HalfWidth", NumberValue.of(scene.getVirtualWidth() / 2));
|
||||
context.variables().put("HalfHeight", NumberValue.of(scene.getVirtualHeight() / 2));
|
||||
return NumberValue.ZERO;
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.annimon.hotarufx.lib;
|
||||
|
||||
import com.annimon.hotarufx.visual.Composition;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -7,6 +8,7 @@ public final class Context {
|
||||
|
||||
private final Map<String, Value> variables;
|
||||
private final Map<String, Function> functions;
|
||||
private Composition composition;
|
||||
|
||||
public Context() {
|
||||
variables = new ConcurrentHashMap<>();
|
||||
@ -20,4 +22,12 @@ public final class Context {
|
||||
public Map<String, Function> functions() {
|
||||
return functions;
|
||||
}
|
||||
|
||||
public Composition composition() {
|
||||
return composition;
|
||||
}
|
||||
|
||||
public void composition(Composition composition) {
|
||||
this.composition = composition;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package com.annimon.hotarufx.bundles;
|
||||
|
||||
import com.annimon.hotarufx.lib.Context;
|
||||
import com.annimon.hotarufx.lib.NumberValue;
|
||||
import lombok.val;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
class CompositionBundleTest {
|
||||
|
||||
@Test
|
||||
void testBundle() {
|
||||
val context = new Context();
|
||||
BundleLoader.loadSingle(context, CompositionBundle.class);
|
||||
|
||||
assertThat(context.functions(), hasKey("composition"));
|
||||
assertThat(context.composition(), nullValue());
|
||||
assertThat(context.variables(), allOf(
|
||||
not(hasKey("Width")),
|
||||
not(hasKey("Height")),
|
||||
not(hasKey("HalfWidth")),
|
||||
not(hasKey("HalfHeight"))
|
||||
));
|
||||
|
||||
context.functions().get("composition").execute();
|
||||
|
||||
assertThat(context.composition(), notNullValue());
|
||||
assertThat(context.variables(), allOf(
|
||||
hasEntry("Width", NumberValue.of(1920)),
|
||||
hasEntry("Height", NumberValue.of(1080)),
|
||||
hasEntry("HalfWidth", NumberValue.of(960)),
|
||||
hasEntry("HalfHeight", NumberValue.of(540))
|
||||
));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user