Animation GUI Move to similar style menu. [Sub Gui not implemented yet]

This commit is contained in:
Kam committed 2023-11-01 21:39:08 -04:00
1 parent 7713970f77
commit f6e8370ba3
10 files changed
+790 -552

No files matched your search

@@ -4,15 +4,18 @@ import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.api.handler.IAnimationHandler;
import noppes.npcs.api.handler.data.IAnimation;
import noppes.npcs.api.handler.data.IFaction;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.controllers.data.Faction;
import noppes.npcs.util.NBTJsonUtil;
import java.io.File;
import java.util.*;
public class AnimationController implements IAnimationHandler {
public HashMap<String, Animation> animations;
public HashMap<Integer, Animation> animations;
public static AnimationController Instance;
private int lastUsedID = 0;
public AnimationController() {
Instance = this;
@@ -44,7 +47,7 @@ public class AnimationController implements IAnimationHandler {
Animation animation = new Animation();
animation.name = file.getName().substring(0, file.getName().length() - 5);
animation.readFromNBT(NBTJsonUtil.LoadFile(file));
animations.put(animation.name,animation);
animations.put(animation.id, animation);
} catch(Exception e) {
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
}
@@ -56,9 +59,35 @@ public class AnimationController implements IAnimationHandler {
return new File(CustomNpcs.getWorldSaveDirectory(), "animations");
}
public IAnimation saveAnimation(IAnimation animation){
animations.put(animation.getName(), (Animation) animation);
public int getUnusedId(){
if(lastUsedID == 0){
for(int catid : animations.keySet()){
if(catid > lastUsedID)
lastUsedID = catid;
}
}
lastUsedID++;
return lastUsedID;
}
public IAnimation saveAnimation(IAnimation animation){
if(animation.getID() < 0){
animation.setID(getUnusedId());
while(hasName(animation.getName()))
animation.setName(animation.getName() + "_");
}
else{
Animation existing = animations.get(animation.getID());
if(existing != null && !existing.name.equals(animation.getName()))
while(hasName(animation.getName()))
animation.setName(animation.getName() + "_");
}
animations.remove(animation.getID());
animations.put(animation.getID(), (Animation) animation);
// Save Animation File
File dir = this.getDir();
if(!dir.exists())
dir.mkdirs();
@@ -74,30 +103,64 @@ public class AnimationController implements IAnimationHandler {
} catch (Exception e) {
LogWriter.except(e);
}
return animations.get(animation.getName());
return animations.get(animation.getID());
}
public boolean hasName(String newName) {
if(newName.trim().isEmpty())
return true;
for(Animation animation : animations.values())
if(animation.name.equals(newName))
return true;
return false;
}
public void delete(String name) {
if(!this.animations.containsKey(name))
return;
File dir = this.getDir();
for (File file : dir.listFiles()) {
if (!file.isFile() || !file.getName().endsWith(".json"))
continue;
if (file.getName().equals(name+".json")) {
file.delete();
break;
Animation delete = getAnimationFromName(name);
if(delete != null){
Animation foundAnimation = this.animations.remove(delete.getID());
if(foundAnimation != null && foundAnimation.name != null){
File dir = this.getDir();
for (File file : dir.listFiles()) {
if (!file.isFile() || !file.getName().endsWith(".json"))
continue;
if (file.getName().equals(foundAnimation.name+".json")) {
file.delete();
break;
}
}
}
}
}
public void delete(int id) {
if(!this.animations.containsKey(id))
return;
Animation foundAnimation = this.animations.remove(id);
if(foundAnimation != null && foundAnimation.name != null){
File dir = this.getDir();
for (File file : dir.listFiles()) {
if (!file.isFile() || !file.getName().endsWith(".json"))
continue;
if (file.getName().equals(foundAnimation.name+".json")) {
file.delete();
break;
}
}
}
this.animations.remove(name);
}
public boolean has(String name) {
return this.animations.containsKey(name);
return getAnimationFromName(name) != null;
}
public IAnimation get(String name) {
return this.animations.get(name);
return getAnimationFromName(name);
}
public IAnimation get(int id) {
return this.animations.get(id);
}
public IAnimation[] getAnimations() {
@@ -106,7 +169,7 @@ public class AnimationController implements IAnimationHandler {
}
public Animation getAnimationFromName(String animation){
for (Map.Entry<String,Animation> entryAnimation : AnimationController.getInstance().animations.entrySet()){
for (Map.Entry<Integer,Animation> entryAnimation : AnimationController.getInstance().animations.entrySet()){
if (entryAnimation.getValue().name.equalsIgnoreCase(animation)){
return entryAnimation.getValue();
}
@@ -10,15 +10,18 @@ import noppes.npcs.constants.EnumAnimationPart;
import noppes.npcs.controllers.AnimationController;
import java.util.ArrayList;
import java.util.UUID;
public class Animation implements IAnimation {
public int id = -1; // Only for internal usage, does not save
public ArrayList<Frame> frames = new ArrayList<>();
public int currentFrame = 0;
public int currentFrameTime = 0;
public String name;
public String name = "";
public float speed = 1.0F;
public byte smooth = 0;
public int loop = -1; //If greater than 0 and less than the amount of frames, the animation will begin looping when it reaches this frame.
@@ -33,14 +36,17 @@ public class Animation implements IAnimation {
public Animation(){}
public Animation(String name){
public Animation(int id, String name){
this.name = name;
this.id = id;
}
public Animation(String name, float speed, byte smooth){
public Animation(int id, String name, float speed, byte smooth){
this.name = name;
this.speed = speed;
this.smooth = smooth;
this.id = id;
}
public IFrame currentFrame() {
@@ -155,7 +161,18 @@ public class Animation implements IAnimation {
return AnimationController.Instance.saveAnimation(this);
}
@Override
public int getID() {
return id;
}
@Override
public void setID(int newID) {
id = newID;
}
public void readFromNBT(NBTTagCompound compound){
id = compound.getInteger("ID");
name = compound.getString("Name");
speed = compound.getFloat("Speed");
smooth = compound.getByte("Smooth");
@@ -181,6 +198,7 @@ public class Animation implements IAnimation {
public NBTTagCompound writeToNBT(){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("ID", id);
compound.setString("Name", name);
compound.setFloat("Speed", speed);
compound.setByte("Smooth", smooth);