Bläddra i källkod

OMG so much time for nothing. I tried to use ImSequencer, but it has no port for java, spent 2h trying to understand the JNI and the ImGui-java codebase, huge waste of time. If anyone read this, please send help.

____tbvns____ 6 månader sedan
förälder
incheckning
cd32a2dc37

+ 19 - 3
PowerGDEditor/src/main/java/xyz/tbvns/Editor/ModelsManager.java

@@ -26,6 +26,21 @@ public class ModelsManager {
     @Getter
     private static List<ModelInstance> loadedModels = new ArrayList<>();
 
+    public static void addModel(File obj) throws FileNotFoundException {
+        File mtl = new File(obj.getParent() + "/" + obj.getName().replace(".obj", ".mtl"));
+        System.out.println(mtl.getPath());
+        if (!mtl.exists()) {
+            throw new FileNotFoundException("Missing mtl file");
+        }
+
+        GDModel model = new GDModel(
+                OBJReader.getFaces(obj, mtl),
+                obj.getName().replace(".obj", "")
+        );
+
+        models.add(model);
+    }
+
     public static void addAnimated(File folder) throws FileNotFoundException {
         List<File> OBJs = new ArrayList<>();
         List<File> MTLs = new ArrayList<>();
@@ -62,9 +77,10 @@ public class ModelsManager {
         frame.setVisible(false);
     }
 
-    public static void select(Model model) {
-        selectedModel = model;
-        selectedModelInstance = new ModelInstance(model);
+    public static void select(GDModel model) {
+        selectedModel = model.toModel();
+        selectedModelInstance = new ModelInstance(selectedModel);
+        selectedModelInstance.userData = model;
         BulletManager.addModel(selectedModelInstance);
         loadedModels.add(selectedModelInstance);
     }

+ 0 - 2
PowerGDEditor/src/main/java/xyz/tbvns/LWJGL3/CustomViewport.java

@@ -9,8 +9,6 @@ public class CustomViewport extends ScreenViewport {
         setCamera(camera);
     }
 
-
-
     @Override
     public void update(int screenWidth, int screenHeight, boolean centerCamera) {
         setScreenBounds(200, 200, screenWidth, screenHeight - 200);

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

@@ -19,7 +19,7 @@ public class Animation implements Renderable {
         ImGui.text(name);
         ImGui.text("Frames:" + frames.size());
         if (ImGui.button("Add " + name)) {
-            ModelsManager.select(frames.get(0).toModel());
+            ModelsManager.select(frames.get(0));
         }
     }
 }

+ 11 - 0
PowerGDEditor/src/main/java/xyz/tbvns/Models/GDModel.java

@@ -10,8 +10,10 @@ import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
 import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
 import com.badlogic.gdx.math.Vector3;
+import imgui.ImGui;
 import lombok.AllArgsConstructor;
 import lombok.Data;
+import xyz.tbvns.Editor.ModelsManager;
 import xyz.tbvns.ui.Renderable;
 
 import java.util.ArrayList;
@@ -64,4 +66,13 @@ public class GDModel implements Renderable {
         }
         return builder.end();
     }
+
+    @Override
+    public void render() {
+        ImGui.separator();
+        ImGui.text(name);
+        if (ImGui.button("Add " + name)) {
+            ModelsManager.select(this);
+        }
+    }
 }

+ 28 - 7
PowerGDEditor/src/main/java/xyz/tbvns/ui/Elements/ModelsList.java

@@ -3,18 +3,19 @@ package xyz.tbvns.ui.Elements;
 import com.badlogic.gdx.Gdx;
 import imgui.ImGui;
 import imgui.flag.ImGuiWindowFlags;
+import org.lwjgl.PointerBuffer;
 import org.lwjgl.util.tinyfd.TinyFileDialogs;
-import tv.wunderbox.nfd.awt.AwtFileDialog;
-import tv.wunderbox.nfd.nfd.NfdFileDialog;
 import xyz.tbvns.Editor.ModelsManager;
 import xyz.tbvns.Models.Animation;
 import xyz.tbvns.Models.GDModel;
 import xyz.tbvns.ui.Element;
 import xyz.tbvns.ui.Renderable;
 
+import javax.swing.filechooser.FileNameExtensionFilter;
 import java.awt.*;
 import java.io.File;
 import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -25,7 +26,7 @@ public class ModelsList implements Element {
         ImGui.setWindowSize(200, Gdx.graphics.getHeight() - 19 - 200);
         ImGui.setWindowPos(0, 19);
 
-        ImGui.beginTabBar("modelbar");
+        ImGui.beginTabBar("animationBar");
         modelsTab();
         ImGui.endTabBar();
 
@@ -34,14 +35,14 @@ public class ModelsList implements Element {
     }
 
     public void modelsTab() {
-        ImGui.beginTabItem("Models");
+        ImGui.beginTabItem("Assets");
         loadButton();
-        modelsButtons();
+        animationButtons();
         ImGui.endTabItem();
     }
 
     public void loadButton() {
-        if (ImGui.button("Load a model")) {
+        if (ImGui.button("Load an animation")) {
             try {
                 String path = TinyFileDialogs.tinyfd_selectFolderDialog("Select animation folder:", System.getProperty("user.home"));
                 if (path!=null) {
@@ -55,9 +56,29 @@ public class ModelsList implements Element {
                 throw new RuntimeException(e);
             }
         }
+        if (ImGui.button("Load a model")) {
+            try {
+                FileDialog fd = new FileDialog((Frame) null, "Save File", FileDialog.SAVE);
+                fd.setFile("*.obj");
+                fd.setFilenameFilter((dir, name) -> {
+                    if (name.endsWith(".obj")) {
+                        return true;
+                    }
+                    return false;
+                });
+                fd.setMode(FileDialog.LOAD);
+                fd.setVisible(true);
+
+                if (fd.getFile() != null) {
+                    ModelsManager.addModel(new File(fd.getDirectory() + "/" + fd.getFile()));
+                }
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
     }
 
-    public void modelsButtons() {
+    public void animationButtons() {
         List<Renderable> renderables = new ArrayList<>();
         for (GDModel model : ModelsManager.models) {
             renderables.add(model);

+ 29 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/Elements/Timeline.java

@@ -1,17 +1,46 @@
 package xyz.tbvns.ui.Elements;
 
 import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.graphics.g3d.Model;
+import com.badlogic.gdx.graphics.g3d.ModelInstance;
 import imgui.ImGui;
 import imgui.extension.imguizmo.ImGuizmo;
 import imgui.flag.ImGuiWindowFlags;
+import imgui.type.ImString;
+import xyz.tbvns.Editor.ModelsManager;
+import xyz.tbvns.Models.GDModel;
 import xyz.tbvns.ui.Element;
 
+import java.util.HashMap;
+import java.util.Random;
+
 public class Timeline implements Element {
+    private HashMap<ModelInstance, String> name = new HashMap<>();
+    private HashMap<ModelInstance, String> id = new HashMap<>();
+
     @Override
     public void render() {
         ImGui.begin("TimeMenu", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoBringToFrontOnFocus);
         ImGui.setWindowSize(Gdx.graphics.getWidth(), 201);
         ImGui.setWindowPos(0, Gdx.graphics.getHeight() - 201);
+        //TODO: replace every model with an object that contains the following information:
+        // - start and en frame
+        // - FPS for animation
+        // - keyframes for positions and scale
+        // - name
+        // - id (unique)
+        // - more thing if I have an idea later
+        for (ModelInstance model : ModelsManager.getLoadedModels()) {
+            ImString string;
+            if (name.containsKey(model)) {
+                string = new ImString(name.get(model));
+            } else {
+                string = new ImString(((GDModel) model.userData).getName() + "-" + new Random().nextInt(1000, 9999));
+                id.put(model, "##" + new Random().nextInt(1000, 9999999));
+            }
+            ImGui.inputText(id.get(model), string);
+            name.put(model, new String(string.getData()));
+        }
 
         ImGui.end();
     }

+ 7 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/GDXUi.java

@@ -0,0 +1,7 @@
+package xyz.tbvns.ui;
+
+public interface GDXUi {
+    void render();
+    void setup();
+    void dispose();
+}

+ 18 - 2
PowerGDEditor/src/main/java/xyz/tbvns/ui/UIManager.java

@@ -2,13 +2,13 @@ package xyz.tbvns.ui;
 
 import lombok.Getter;
 import org.reflections.Reflections;
-import xyz.tbvns.ui.Windows.ObjectEdit;
 
 import java.util.*;
 
 public class UIManager {
     private static List<Element> registeredElements = new ArrayList<>();
     private static List<MenuBarTab> registeredMenuBarTabs = new ArrayList<>();
+    private static List<GDXUi> registeredGDXUi = new ArrayList<>();
     @Getter
     private static final HashMap<Class, Boolean> activeUI = new HashMap<>();
 
@@ -28,6 +28,16 @@ public class UIManager {
                 activeUI.put(element.asSubclass(Element.class), true);
             }
 
+            reflections = new Reflections("xyz.tbvns.ui.Elements");
+            Set<Class<? extends GDXUi>> gdxUis = reflections.getSubTypesOf(GDXUi.class);
+            for (Class<? extends GDXUi> element : gdxUis) {
+                registeredGDXUi.add(element.getDeclaredConstructor().newInstance());
+                activeUI.put(element.asSubclass(GDXUi.class), true);
+            }
+            for (GDXUi ui : registeredGDXUi) {
+                ui.setup();
+            }
+
             reflections = new Reflections("xyz.tbvns.ui.MenuBarTabs");
             Set<Class<? extends MenuBarTab>> tabs = reflections.getSubTypesOf(MenuBarTab.class);
             for (Class<? extends MenuBarTab> element : tabs) {
@@ -39,7 +49,7 @@ public class UIManager {
         }
     }
 
-    public static void render() {
+    public static void renderImGUI() {
         for (Element element : registeredElements) {
             boolean isEnabled = activeUI.get(element.getClass());
             if (isEnabled) {
@@ -48,6 +58,12 @@ public class UIManager {
         }
     }
 
+    public static void renderLibgdx() {
+        for (GDXUi ui : registeredGDXUi) {
+            ui.render();
+        }
+    }
+
     public static void setup() {
         for (Element element : registeredElements) {
             element.setup();

+ 3 - 1
PowerGDEditor/src/main/java/xyz/tbvns/ui/Ui.java

@@ -18,8 +18,10 @@ public class Ui {
         ImGui.beginMainMenuBar();
         UIManager.renderMenuBarTabs();
         ImGui.endMainMenuBar();
-        UIManager.render();
+        UIManager.renderImGUI();
         endImGui();
+
+        UIManager.renderLibgdx();
     }
 
     public static void initImGui() {

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

@@ -14,7 +14,7 @@ public class ObjectEdit implements Element {
 
     @Override
     public void render() {
-        ImGui.begin("Edit model:", ImGuiWindowFlags.NoMove);
+        ImGui.begin("Edit model:", ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoResize);
         ImGui.setWindowPos(Gdx.graphics.getWidth() - ImGui.getWindowWidth(), 19);
 
         if (ImGui.button("-100")) {