فهرست منبع

changes with issue

____tbvns____ 5 ماه پیش
والد
کامیت
9a330f9fc6

+ 9 - 8
PowerGDEditor/src/main/java/xyz/tbvns/Editor/ModelsManager.java

@@ -24,7 +24,7 @@ public class ModelsManager {
     @Getter
     private static Model selectedModel;
     @Getter
-    private static ModelInstance selectedModelInstance;
+    private static GDInstance selectedInstance;
     @Getter
     private static List<ModelInstance> loadedModels = new ArrayList<>();
 
@@ -81,7 +81,7 @@ public class ModelsManager {
 
     public static void add(GDModel model) {
         selectedModel = model.toModel();
-        selectedModelInstance = new ModelInstance(selectedModel);
+        ModelInstance selectedModelInstance = new ModelInstance(selectedModel);
         selectedModelInstance.userData = new GDInstance(
                 selectedModelInstance,
                 model.getName(),
@@ -92,27 +92,28 @@ public class ModelsManager {
     }
 
     public static void addAnimation(Animation animation) {
-        selectedModel = animation.getFrames().get(0).toModel();
-        selectedModelInstance = new ModelInstance(selectedModel);
+        animation.generate();
+        selectedModel = animation.getModelFrames().get(0);
+        ModelInstance selectedModelInstance = new ModelInstance(selectedModel);
         selectedModelInstance.userData = new GDInstance(
                     selectedModelInstance,
                     24,
                     6,
                     0,
                     animation.getName(),
-                    animation.getName() + new Random().nextInt(0, 9999)
+                    animation.getName() + new Random().nextInt(0, 9999),
+                    animation
                 );
         BulletManager.addModel(selectedModelInstance);
         loadedModels.add(selectedModelInstance);
     }
 
     public static void select(ModelInstance model) {
-        selectedModelInstance = model;
-        selectedModel = model.model;
+        selectedInstance = (GDInstance) model.userData;
     }
 
     public static void deselect() {
-        selectedModelInstance = null;
+        selectedInstance = null;
         selectedModel = null;
     }
 }

+ 19 - 6
PowerGDEditor/src/main/java/xyz/tbvns/Main.java

@@ -8,7 +8,6 @@ import com.badlogic.gdx.graphics.GL20;
 import com.badlogic.gdx.graphics.PerspectiveCamera;
 import com.badlogic.gdx.graphics.g3d.*;
 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
-import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
 import com.badlogic.gdx.math.Vector3;
 import com.badlogic.gdx.physics.bullet.Bullet;
@@ -20,7 +19,7 @@ import xyz.tbvns.Inputs.Shortcut;
 import xyz.tbvns.Inputs.inputManager;
 import xyz.tbvns.LWJGL3.CustomViewport;
 import xyz.tbvns.Managers.TexturesManager;
-import xyz.tbvns.Models.GDModel;
+import xyz.tbvns.Models.GDInstance;
 import xyz.tbvns.Models.Shaders;
 import xyz.tbvns.Physics.BulletManager;
 import xyz.tbvns.ui.UIManager;
@@ -108,12 +107,26 @@ public class Main extends ApplicationAdapter {
         }
         batch.render(worldGridInstance);
         batch.render(levelGridInstance);
+//        for (ModelInstance model : ModelsManager.getLoadedModels()) {
+//            GDInstance instance = (GDInstance) model.userData;
+//            if (instance.isAnimation()) {
+//                instance.updateFrame(Utils.getCameraPercent());
+//                model = instance.getInstance();
+//            }
+//            if (ModelsManager.getSelectedModelInstance() == model) {
+//                Shaders.renderOutline(instance.getInstance());
+//            }
+//        }
         for (ModelInstance model : ModelsManager.getLoadedModels()) {
-            if (ModelsManager.getSelectedModelInstance() == model) {
-                Shaders.renderOutline(model);
+            GDInstance instance = (GDInstance) model.userData;
+            if (instance.isAnimation()) {
+                instance.updateFrame(Utils.getCameraPercent());
+                ModelsManager.getLoadedModels().set(ModelsManager.getLoadedModels().indexOf(model), instance.getInstance());
             }
-        }
-        for (ModelInstance model : ModelsManager.getLoadedModels()) {
+            if (ModelsManager.getSelectedInstance() == model.userData) {
+                Shaders.renderOutline(instance.getInstance());
+            }
+            model = instance.getInstance();
             batch.render(model);
         }
         batch.end();

+ 14 - 0
PowerGDEditor/src/main/java/xyz/tbvns/Models/Animation.java

@@ -1,5 +1,6 @@
 package xyz.tbvns.Models;
 
+import com.badlogic.gdx.graphics.g3d.Model;
 import imgui.ImGui;
 import lombok.Data;
 import xyz.tbvns.Editor.ModelsManager;
@@ -11,8 +12,21 @@ import java.util.List;
 @Data
 public class Animation implements Renderable {
     private List<GDModel> frames = new ArrayList<>();
+    private List<Model> modelFrames = new ArrayList<>();
     private String name;
 
+    public void generate() {
+        for (GDModel frame : frames) {
+            modelFrames.add(frame.toModel());
+        }
+    }
+
+    public void dispose() {
+        for (Model modelFrame : modelFrames) {
+            modelFrame.dispose();
+        }
+    }
+
     @Override
     public void render() {
         ImGui.separator();

+ 31 - 3
PowerGDEditor/src/main/java/xyz/tbvns/Models/GDInstance.java

@@ -1,9 +1,11 @@
 package xyz.tbvns.Models;
 
+import com.badlogic.gdx.graphics.g3d.Model;
 import com.badlogic.gdx.graphics.g3d.ModelInstance;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import xyz.tbvns.LevelInfo;
 
 @Data
 @NoArgsConstructor
@@ -15,15 +17,40 @@ public class GDInstance {
         this.animation = false;
     }
 
-    public GDInstance(ModelInstance instance, int fps, int frameSkip, double start, String name, String id) {
+    public GDInstance(ModelInstance instance, int fps, int frameSkip, double start, String name, String id, Animation animation) {
         this.instance = instance;
         this.fps = fps;
         this.frameSkip = frameSkip;
         this.start = start;
         this.name = name;
         this.id = id;
-        animation = true;
-        expanded = false;
+        this.animation = true;
+        this.animationObject = animation;
+        this.expanded = false;
+    }
+
+    public void updateFrame(double percent) {
+        instance = new ModelInstance(getFrameOnPercent(percent), instance.transform);
+        instance.userData = this;
+    }
+
+    //TODO: This doesn't work, repair later
+    public Model getFrameOnPercent(double percent) {
+        double usablePercent = percent - start;
+        if (usablePercent > 0) {
+            float max = LevelInfo.getLevel().getMaxPos();
+            double pos = max * percent;
+            double speed = LevelInfo.getLevel().getSpeedAt((float) percent);
+            double time = speed / fps;
+
+            int frame = (int) (pos / time);
+            if (!(frame > animationObject.getModelFrames().size() - 1)) {
+                System.out.println(frame);
+                return animationObject.getModelFrames().get(frame);
+            }
+            return animationObject.getModelFrames().getLast();
+        }
+        return animationObject.getModelFrames().getFirst();
     }
 
     private ModelInstance instance;
@@ -34,4 +61,5 @@ public class GDInstance {
     //TODO: add keyframes
     private String name, id;
     private boolean expanded;
+    private Animation animationObject;
 }

+ 8 - 0
PowerGDEditor/src/main/java/xyz/tbvns/Utils.java

@@ -15,4 +15,12 @@ public class Utils {
         out.setRGB(0, 0, w, h, pixels, 0, w);
         return out;
     }
+
+    /**
+     * @return The percent of the X axis of the camera position compared to the level size
+     * @apiNote This is not a percentage but goes from 0 to 1
+     */
+    public static float getCameraPercent() {
+        return Main.getCamera().position.x / (LevelInfo.getLevel().getMaxPos() / 30);
+    }
 }

+ 2 - 1
PowerGDEditor/src/main/java/xyz/tbvns/ui/Elements/Timeline.java

@@ -15,6 +15,7 @@ import xyz.tbvns.LevelInfo;
 import xyz.tbvns.Main;
 import xyz.tbvns.Models.GDInstance;
 import xyz.tbvns.Models.GDModel;
+import xyz.tbvns.Utils;
 import xyz.tbvns.ui.Element;
 
 import java.util.HashMap;
@@ -25,7 +26,7 @@ public class Timeline implements Element {
     @Override
     public void render() {
         ImGui.begin("TimeMenu", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoBringToFrontOnFocus);
-        float[] pos = new float[]{Main.getCamera().position.x / (LevelInfo.getLevel().getMaxPos() / 30) * 100};
+        float[] pos = new float[]{Utils.getCameraPercent() * 100};
         ImGui.pushItemWidth(ImGui.getWindowWidth() - 16);
         ImGui.sliderFloat("##Slider%", pos, 0, 100);
         Main.getCamera().position.set((pos[0] / 100) * (LevelInfo.getLevel().getMaxPos() / 30), Main.getCamera().position.y, Main.getCamera().position.z);

+ 4 - 4
PowerGDEditor/src/main/java/xyz/tbvns/ui/Windows/ObjectEdit.java

@@ -126,13 +126,13 @@ public class ObjectEdit implements Element {
     }
 
     public void move(axis axis, int d) {
-        Vector3 pos = ModelsManager.getSelectedModelInstance().transform.getTranslation(Vector3.Zero);
+        Vector3 pos = ModelsManager.getSelectedInstance().getInstance().transform.getTranslation(Vector3.Zero);
         if (axis == ObjectEdit.axis.X) {
-            ModelsManager.getSelectedModelInstance().transform.setToTranslation(pos.x + d, pos.y, pos.z);
+            ModelsManager.getSelectedInstance().getInstance().transform.setToTranslation(pos.x + d, pos.y, pos.z);
         } else if (axis == ObjectEdit.axis.Y) {
-            ModelsManager.getSelectedModelInstance().transform.setToTranslation(pos.x, pos.y + d, pos.z);
+            ModelsManager.getSelectedInstance().getInstance().transform.setToTranslation(pos.x, pos.y + d, pos.z);
         } else if (axis == ObjectEdit.axis.Z) {
-            ModelsManager.getSelectedModelInstance().transform.setToTranslation(pos.x, pos.y, pos.z + d);
+            ModelsManager.getSelectedInstance().getInstance().transform.setToTranslation(pos.x, pos.y, pos.z + d);
         }
     }