Skip to content

Commit

Permalink
Merge some features from FiguraMC#41
Browse files Browse the repository at this point in the history
(Note, currently missing documentation)
  • Loading branch information
superpowers04 committed Sep 13, 2023
2 parents 795265b + 955bb83 commit f4731cb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.figuramc.figura.lua.docs.LuaTypeDoc;
import org.figuramc.figura.model.FiguraModelPart;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

import java.util.*;
Expand All @@ -18,6 +19,7 @@
name = "Animation",
value = "animation"
)
// Stole lua AnimationChannel and keyframe stuff from https://github.com/FiguraMC/Figura/pull/41
public class Animation {

private final Avatar owner;
Expand Down Expand Up @@ -606,6 +608,32 @@ public enum LoopMode {
ONCE,
HOLD
}
@LuaWhitelist
@LuaMethodDoc("animation.getChannels")
public LuaTable getChannels() {
LuaTable ret = new LuaTable();
for (Map.Entry<FiguraModelPart, List<Animation.AnimationChannel>> entry : animationParts) {
List<AnimationChannel> value = new ArrayList<>(entry.getValue());
ret.set((LuaValue) owner.luaRuntime.typeManager.javaToLua(entry.getKey()), (LuaValue) owner.luaRuntime.typeManager.javaToLua(value));
}
return ret;
}
@LuaWhitelist
@LuaTypeDoc(
name = "AnimationChannel",
value = "animation_channel"
)
public record AnimationChannel(TransformType type, Keyframe... keyframes) {

public record AnimationChannel(TransformType type, Keyframe... keyframes) {}
@LuaWhitelist
@LuaMethodDoc("animation.get_keyframes")
public List<Keyframe> getKeyframes() {
return List.of(keyframes);
}
@LuaWhitelist
@LuaMethodDoc("animation.get_type")
public String getType() {
return type.toString();
}
}
}
67 changes: 52 additions & 15 deletions common/src/main/java/org/figuramc/figura/animation/Keyframe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@
import com.mojang.datafixers.util.Pair;
import org.figuramc.figura.FiguraMod;
import org.figuramc.figura.avatar.Avatar;
import org.figuramc.figura.lua.LuaWhitelist;
import org.figuramc.figura.lua.docs.LuaMethodDoc;
import org.figuramc.figura.lua.docs.LuaTypeDoc;
import org.figuramc.figura.math.vector.FiguraVec3;
import org.figuramc.figura.utils.LuaUtils;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;

@LuaWhitelist
@LuaTypeDoc(name = "Keyframe",value = "keyframe")
public class Keyframe implements Comparable<Keyframe> {

private final Avatar owner;
private final Animation animation;
private final float time;
private final Interpolation interpolation;
private final FiguraVec3 targetA, targetB;
private final String[] aCode, bCode;
private float time;
private Interpolation interpolation;
private FiguraVec3 targetA, targetB;
private String[] aCode, bCode;
private final String chunkName;
private final FiguraVec3 bezierLeft, bezierRight;
private final FiguraVec3 bezierLeftTime, bezierRightTime;
private FiguraVec3 bezierLeft, bezierRight;
private FiguraVec3 bezierLeftTime, bezierRightTime;

public Keyframe(Avatar owner, Animation animation, float time, Interpolation interpolation, Pair<FiguraVec3, String[]> a, Pair<FiguraVec3, String[]> b, FiguraVec3 bezierLeft, FiguraVec3 bezierRight, FiguraVec3 bezierLeftTime, FiguraVec3 bezierRightTime) {
this.owner = owner;
Expand All @@ -35,15 +41,28 @@ public Keyframe(Avatar owner, Animation animation, float time, Interpolation int
this.bezierLeftTime = bezierLeftTime;
this.bezierRightTime = bezierRightTime;
}

@LuaWhitelist
@LuaMethodDoc("keyframe.get_target_a")
public FiguraVec3 getTargetA(float delta) {
return targetA != null ? targetA.copy() : FiguraVec3.of(parseStringData(aCode[0], delta), parseStringData(aCode[1], delta), parseStringData(aCode[2], delta));
}

@LuaWhitelist
@LuaMethodDoc("keyframe.get_target_b")
public FiguraVec3 getTargetB(float delta) {
return targetB != null ? targetB.copy() : FiguraVec3.of(parseStringData(bCode[0], delta), parseStringData(bCode[1], delta), parseStringData(bCode[2], delta));
}

@LuaWhitelist
@LuaMethodDoc("keyframe.set_target_a")
public Keyframe setTargetA(float delta,Object x, Double y, Double z) {
targetA = LuaUtils.parseVec3("keyframe.setTargetA",x,y,z);
return this;
}
@LuaWhitelist
@LuaMethodDoc("keyframe.set_target_b")
public Keyframe setTargetB(float delta,Object x, Double y, Double z) {
targetB = LuaUtils.parseVec3("keyframe.setTargetB",x,y,z);
return this;
}
private float parseStringData(String data, float delta) {
FiguraMod.pushProfiler(data);
try {
Expand Down Expand Up @@ -82,27 +101,45 @@ private float parseStringData(String data, float delta) {

return FiguraMod.popReturnProfiler(0f);
}

@LuaWhitelist
@LuaMethodDoc("keyframe.get_time")
public float getTime() {
return time;
}

@LuaWhitelist
@LuaMethodDoc("keyframe.set_time")
public Keyframe setTime(Float newTime) {
time = newTime;
return this;
}
@LuaWhitelist
@LuaMethodDoc("keyframe.get_interpolation")
public Interpolation getInterpolation() {
return interpolation;
}

@LuaWhitelist
@LuaMethodDoc("keyframe.set_interpolation")
public Keyframe setInterpolation(String interp) {
interpolation = Interpolation.valueOf(interp);
return this;
}
@LuaWhitelist
@LuaMethodDoc("keyframe.get_bezier_left")
public FiguraVec3 getBezierLeft() {
return bezierLeft.copy();
}

@LuaWhitelist
@LuaMethodDoc("keyframe.get_bezier_right")
public FiguraVec3 getBezierRight() {
return bezierRight.copy();
}

@LuaWhitelist
@LuaMethodDoc("keyframe.get_bezier_left_time")
public FiguraVec3 getBezierLeftTime() {
return bezierLeftTime.copy();
}

@LuaWhitelist
@LuaMethodDoc("keyframe.get_bezier_right_time")
public FiguraVec3 getBezierRightTime() {
return bezierRightTime.copy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public AvatarPath(Path path, Path folder, Path theActualPathForThis) {
else{
CompoundTag nbt = NbtIo.readCompressed(Files.newInputStream(path));
CompoundTag metadata = nbt.getCompound("metadata");
if(metadata.contains("name")) name = metadata.getString("name") + " (" + filename + ")";
if(metadata.contains("name")) name = metadata.getString("name") + " (" + Files.getLastModifiedTime(path) + ")";
cachedNames.put(path.toString().toLowerCase(),name);
}
}catch(Exception ignored){}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.figuramc.figura.animation.Keyframe;
import org.figuramc.figura.parsers.BlockbenchModel;

/**
* A set of Globals of which there is only one in the MC instance.
Expand Down Expand Up @@ -124,6 +126,8 @@ public class FiguraAPIManager {

add(AnimationAPI.class);
add(Animation.class);
add(Animation.AnimationChannel.class);
add(Keyframe.class);

add(HostAPI.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.network.chat.*;
import org.figuramc.figura.FiguraMod;
import org.figuramc.figura.animation.Animation;
import org.figuramc.figura.animation.Keyframe;
import org.figuramc.figura.entries.FiguraAPI;
import org.figuramc.figura.lua.api.*;
import org.figuramc.figura.lua.api.action_wheel.Action;
Expand Down Expand Up @@ -119,7 +120,9 @@ public class FiguraDocsManager {

put("animations", List.of(
AnimationAPI.class,
Animation.class
Animation.class,
Animation.AnimationChannel.class,
Keyframe.class
));

put("nameplate", List.of(
Expand Down

0 comments on commit f4731cb

Please sign in to comment.