Просмотр исходного кода

Start for 3d models loading + made UI easy modifiable

____tbvns____ 6 месяцев назад
Родитель
Сommit
c0ca889ae8

+ 44 - 0
PowerGDEditor/src/main/java/xyz/tbvns/Editor/ModelsManager.java

@@ -0,0 +1,44 @@
+package xyz.tbvns.Editor;
+
+import xyz.tbvns.Models.Animation;
+import xyz.tbvns.Models.GDModel;
+import xyz.tbvns.Models.OBJReader;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ModelsManager {
+    public static final List<GDModel> models = new ArrayList<>();
+    public static final List<Animation> animations = new ArrayList<>();
+
+    public void addAnimated(File folder) throws FileNotFoundException {
+        List<File> OBJs = new ArrayList<>();
+        List<File> MTLs = new ArrayList<>();
+
+        for (int i = 0; i < folder.listFiles().length; i++) {
+            File f = folder.listFiles()[i];
+            if (f.getAbsolutePath().endsWith(".obj")) {
+                OBJs.add(f);
+            }
+        }
+
+        for (int i = 0; i < folder.listFiles().length; i++) {
+            File f = folder.listFiles()[i];
+            if (f.getAbsolutePath().endsWith(".mtl")) {
+                MTLs.add(f);
+            }
+        }
+
+        Animation animation = new Animation();
+
+        for (int i = 0; i < OBJs.size(); i++) {
+            GDModel model = new GDModel(
+                    OBJReader.getFaceAnimation(OBJs.get(i), MTLs.get(i))
+            );
+            animation.getFrames().add(model);
+        }
+        animations.add(animation);
+    }
+}

+ 0 - 4
PowerGDEditor/src/main/java/xyz/tbvns/GeometryDash/LevelUtils.java

@@ -40,10 +40,6 @@ public class LevelUtils {
                         TexturesManager.idMaterialMap.get(Integer.parseInt(object.getProperties().get("1"))),
                         VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates
                 );
-//                Model model = new ModelBuilder().createBox(1f, 1f, 0,
-//                        TexturesManager.idMaterialMap.get(Integer.parseInt(object.getProperties().get("1"))),
-//                        VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates
-//                );
                 models.add(model);
                 modelCache.add(new ModelInstance(model, Float.parseFloat(object.getProperties().get("2")) / 30, Float.parseFloat(object.getProperties().get("3")) / 30, 0));
             } catch (Exception e){};

+ 0 - 45
PowerGDEditor/src/main/java/xyz/tbvns/GeometryDash/TextureExtractor.java

@@ -89,51 +89,6 @@ public class TextureExtractor {
                 } catch (Exception e) {e.printStackTrace();}
             }).start();
         }
-//
-//        String name = "";
-//        float x = 0, y = 0, w = 0, h = 0;
-//        boolean rotated = false, ready = false;
-//        for (int i = 0; i < readableConfig.length; i++) {
-//            String line = readableConfig[i];
-//            if (line.contains("<key>") && line.contains(".png</key>")) {
-//                name = line.replace("<key>", "").replace("</key>", "");
-//            }
-//            else if (line.contains("<key>textureRect</key>")) {
-//                String data = readableConfig[i+1];
-//                String[] readableData = data
-//                        .replace("<string>", "")
-//                        .replace("</string>", "")
-//                        .replace("}", "")
-//                        .replace("{", "")
-//                        .split(",");
-//                x = Float.parseFloat(readableData[0]);
-//                y = Float.parseFloat(readableData[1]);
-//                w = Float.parseFloat(readableData[2]);
-//                h = Float.parseFloat(readableData[3]);
-//            } else if (line.contains("<key>textureRotated</key>")) {
-//                if (readableConfig[i+1].contains("<true/>")) {
-//                    rotated = true;
-//                } else {
-//                    rotated = false;
-//                }
-//            }
-//            else if (line.contains("</dict>")) {
-//                ready = true;
-//            }
-//            if (ready && !name.isBlank()) {
-//                BufferedImage crop;
-//                try {
-//                    if (!rotated) {
-//                        crop = image.getSubimage(round(x), round(y), round(w), round(h));
-//                    } else {
-//                        crop = rotate(image.getSubimage(round(x), round(y), round(h), round(w)));
-//                    }
-//                    File file = new File(output.getPath() + "/" + name);
-//                    ImageIO.write(crop, "png", file);
-//                } catch (Exception e) {}
-//                x=0; y=0; w=0; h=0; name = "";
-//            }
-//        }
     }
 
     public static BufferedImage rotate(BufferedImage src) {

+ 0 - 38
PowerGDEditor/src/main/java/xyz/tbvns/GeometryDash/TexturePojo.java

@@ -1,38 +0,0 @@
-package xyz.tbvns.GeometryDash;
-
-import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.annotation.JsonIgnoreType;
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
-
-import javax.annotation.Nullable;
-import java.util.List;
-
-public class TexturePojo {
-    public static class dict {
-        public String name;
-        public String spriteOffset;
-        public String spriteSize;
-        public String spriteSourceSize;
-        public String textureRect;
-        public List<dict> dict;
-        public int format;
-        public String pixelFormat;
-        public String realTextureFileName;
-        public String size;
-        public String smartupdate;
-        public String textureFileName;
-    }
-
-    public static class plist {
-        public dict dict;
-        public double version;
-        public String text;
-    }
-
-    public static class Root {
-
-        public List<plist> plist;
-    }
-
-
-}

+ 4 - 4
PowerGDEditor/src/main/java/xyz/tbvns/Main.java

@@ -16,6 +16,8 @@ import xyz.tbvns.Inputs.Shortcut;
 import xyz.tbvns.Inputs.inputManager;
 import xyz.tbvns.LWJGL3.CustomViewport;
 import xyz.tbvns.Managers.TexturesManager;
+import xyz.tbvns.ui.Elements.LevelSelector;
+import xyz.tbvns.ui.UIManager;
 import xyz.tbvns.ui.Ui;
 
 import static org.lwjgl.opengl.GL11.GL_CULL_FACE;
@@ -36,6 +38,8 @@ public class Main extends ApplicationAdapter {
     public void create() {
         Setup.run();
         Ui.initImGui();
+        UIManager.loadUi();
+        UIManager.getActiveUI().put(LevelSelector.class, true);
         TexturesManager.idFileMap.size();
         batch = new ModelBatch();
         camera = new PerspectiveCamera(80, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
@@ -57,10 +61,6 @@ public class Main extends ApplicationAdapter {
         levelGridInstance.transform.setTranslation(new Vector3(0, 50, 0));
 
         cameraInputController = new inputManager(camera);
-//        cameraInputController.forwardKey = Input.Keys.W;
-//        cameraInputController.backwardKey = Input.Keys.S;
-//        cameraInputController.rotateRightKey = -1;
-//        cameraInputController.rotateLeftKey = -1;
         Gdx.input.setInputProcessor(cameraInputController);
     }
 

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

@@ -0,0 +1,11 @@
+package xyz.tbvns.Models;
+
+import lombok.Data;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Data
+public class Animation {
+    private List<GDModel> frames = new ArrayList<>();
+}

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

@@ -0,0 +1,13 @@
+package xyz.tbvns.Models;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import org.joml.Vector4f;
+
+import java.util.ArrayList;
+
+@Data
+@AllArgsConstructor
+public class GDModel {
+    private ArrayList<Face> faces;
+}

+ 1 - 1
PowerGDEditor/src/main/java/xyz/tbvns/Models/ReadMTL.java → PowerGDEditor/src/main/java/xyz/tbvns/Models/MTLReader.java

@@ -8,7 +8,7 @@ import java.io.FileReader;
 import java.util.HashMap;
 import java.util.concurrent.atomic.AtomicReference;
 
-public class ReadMTL {
+public class MTLReader {
     public static HashMap<String, Color> getColors(File file) throws FileNotFoundException {
         BufferedReader reader = new BufferedReader(new FileReader(file));
         HashMap<String, Color> materials = new HashMap<>();

+ 6 - 6
PowerGDEditor/src/main/java/xyz/tbvns/Models/ReadOBJ.java → PowerGDEditor/src/main/java/xyz/tbvns/Models/OBJReader.java

@@ -13,8 +13,8 @@ import java.util.HashMap;
 import java.util.concurrent.atomic.AtomicReference;
 
 @Slf4j
-public class ReadOBJ {
-    public ArrayList<Vector4f> getPoints(File OBJFile) throws FileNotFoundException {
+public class OBJReader {
+    public static ArrayList<Vector4f> getPoints(File OBJFile) throws FileNotFoundException {
         BufferedReader reader = new BufferedReader(new FileReader(OBJFile));
         ArrayList<Vector4f> Points = new ArrayList<>();
         reader.lines().forEach(l -> {
@@ -31,14 +31,14 @@ public class ReadOBJ {
         return Points;
     }
 
-    public ArrayList<Face> getFace(File OBJFile, File MTLFile) throws FileNotFoundException {
+    public static ArrayList<Face> getFace(File OBJFile, File MTLFile) throws FileNotFoundException {
         BufferedReader reader = new BufferedReader(new FileReader(OBJFile));
         ArrayList<Face> Faces = new ArrayList<>();
         AtomicReference<HashMap<String, Color>> materials = new AtomicReference<>(new HashMap<>());
         AtomicReference<Color> color = new AtomicReference<>(new Color(0, 0, 0));
         reader.lines().forEach(l -> {
                 try {
-                    materials.set(ReadMTL.getColors(MTLFile));
+                    materials.set(MTLReader.getColors(MTLFile));
                 } catch (FileNotFoundException e) {
                     throw new RuntimeException(e);
                 }
@@ -78,14 +78,14 @@ public class ReadOBJ {
         return Faces;
     }
 
-    public ArrayList<Face> getFaceAnimation(File OBJFile, File MTLFile) throws FileNotFoundException {
+    public static ArrayList<Face> getFaceAnimation(File OBJFile, File MTLFile) throws FileNotFoundException {
         BufferedReader reader = new BufferedReader(new FileReader(OBJFile));
         ArrayList<Face> Faces = new ArrayList<>();
         AtomicReference<HashMap<String, Color>> materials = new AtomicReference<>(new HashMap<>());
         AtomicReference<Color> color = new AtomicReference<>(new Color(0, 0, 0));
         reader.lines().forEach(l -> {
             try {
-                materials.set(ReadMTL.getColors(MTLFile));
+                materials.set(MTLReader.getColors(MTLFile));
             } catch (FileNotFoundException e) {
                 throw new RuntimeException(e);
             }

+ 7 - 4
PowerGDEditor/src/main/java/xyz/tbvns/ui/LevelSelector.java → PowerGDEditor/src/main/java/xyz/tbvns/ui/Elements/LevelSelector.java

@@ -1,4 +1,4 @@
-package xyz.tbvns.ui;
+package xyz.tbvns.ui.Elements;
 
 import imgui.ImGui;
 import imgui.flag.ImGuiInputTextFlags;
@@ -10,11 +10,14 @@ import xyz.tbvns.GeometryDash.LevelUtils;
 import xyz.tbvns.LevelInfo;
 import xyz.tbvns.Main;
 import xyz.tbvns.Objects.Level;
+import xyz.tbvns.ui.UIElement;
+import xyz.tbvns.ui.UIManager;
+import xyz.tbvns.ui.Ui;
 
 import java.io.IOException;
 import java.util.List;
 
-public class LevelSelector {
+public class LevelSelector implements UIElement {
     private static List<Level> levelList;
     static {
         try {
@@ -32,7 +35,7 @@ public class LevelSelector {
         }
     }
 
-    public static void render() {
+    public void render() {
         ImGui.begin("Load a level:", ImGuiWindowFlags.NoCollapse);
         ImGui.setWindowFocus();
         ImString string = new ImString();
@@ -43,7 +46,7 @@ public class LevelSelector {
                     Main.modelCache = LevelUtils.generateModelCache(level);
                     LevelInfo.setLevel(level);
                     LevelInfo.setLevelString(Decoder.decodeLevel(level.getEncodedLevelString()));
-                    Ui.setShowLevelSelector(false);
+                    UIManager.getActiveUI().put(getClass(), false);
                 } catch (Exception e) {
                     throw new RuntimeException(e);
                 }

+ 13 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/MenuBarTab.java

@@ -0,0 +1,13 @@
+package xyz.tbvns.ui;
+
+import imgui.ImGui;
+
+public interface MenuBarTab {
+    default void render() {
+        ImGui.beginMenu("Default menu");
+    }
+
+    default int index() {
+        return 0;
+    }
+}

+ 23 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/MenuBarTabs/AboutTabs.java

@@ -0,0 +1,23 @@
+package xyz.tbvns.ui.MenuBarTabs;
+
+import imgui.ImGui;
+import xyz.tbvns.ui.MenuBarTab;
+
+public class AboutTabs implements MenuBarTab {
+    @Override
+    public void render() {
+        if (ImGui.beginMenu("About")) {
+            ImGui.menuItem("Help", "H");
+            ImGui.menuItem("Wiki", "CTRL - H");
+            ImGui.menuItem("Info", "I");
+            ImGui.menuItem("Discord");
+            ImGui.menuItem("Git");
+            ImGui.endMenu();
+        }
+    }
+
+    @Override
+    public int index() {
+        return 3;
+    }
+}

+ 29 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/MenuBarTabs/SaveMenu.java

@@ -0,0 +1,29 @@
+package xyz.tbvns.ui.MenuBarTabs;
+
+import imgui.ImGui;
+import xyz.tbvns.Main;
+import xyz.tbvns.ui.Elements.LevelSelector;
+import xyz.tbvns.ui.MenuBarTab;
+import xyz.tbvns.ui.UIElement;
+import xyz.tbvns.ui.UIManager;
+
+public class SaveMenu implements MenuBarTab {
+    @Override
+    public void render() {
+        if (ImGui.beginMenu("Files")) {
+            ImGui.menuItem("Save", "CTRL S");
+            ImGui.menuItem("Export", "SHIFT S");
+            if (ImGui.menuItem("Open", "O")) {
+                UIManager.getActiveUI().put(LevelSelector.class, true);
+                Main.modelCache = null;
+            }
+            ImGui.menuItem("Close", "C");
+            ImGui.endMenu();
+        }
+    }
+
+    @Override
+    public int index() {
+        return 0;
+    }
+}

+ 20 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/MenuBarTabs/ToolsMenu.java

@@ -0,0 +1,20 @@
+package xyz.tbvns.ui.MenuBarTabs;
+
+import imgui.ImGui;
+import xyz.tbvns.ui.MenuBarTab;
+
+public class ToolsMenu implements MenuBarTab {
+    @Override
+    public void render() {
+        if (ImGui.beginMenu("Tools")) {
+            ImGui.menuItem("Edit level string", "L");
+            ImGui.menuItem("BPM to FPS", "B");
+            ImGui.endMenu();
+        }
+    }
+
+    @Override
+    public int index() {
+        return 1;
+    }
+}

+ 20 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/MenuBarTabs/ViewMenu.java

@@ -0,0 +1,20 @@
+package xyz.tbvns.ui.MenuBarTabs;
+
+import imgui.ImGui;
+import xyz.tbvns.ui.MenuBarTab;
+
+public class ViewMenu implements MenuBarTab {
+    @Override
+    public void render() {
+        if (ImGui.beginMenu("View")) {
+            ImGui.checkbox("Level grid", true);
+            ImGui.checkbox("World grid", true);
+            ImGui.endMenu();
+        }
+    }
+
+    @Override
+    public int index() {
+        return 2;
+    }
+}

+ 11 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/UIElement.java

@@ -0,0 +1,11 @@
+package xyz.tbvns.ui;
+
+import imgui.ImGui;
+
+public interface UIElement {
+    default void render() {
+        ImGui.begin("Default UIElement");
+        ImGui.text("This is the default UI !");
+        ImGui.end();
+    }
+}

+ 49 - 0
PowerGDEditor/src/main/java/xyz/tbvns/ui/UIManager.java

@@ -0,0 +1,49 @@
+package xyz.tbvns.ui;
+
+import lombok.Getter;
+import org.reflections.Reflections;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.*;
+
+public class UIManager {
+    private static List<UIElement> registeredUiElements = new ArrayList<>();
+    private static List<MenuBarTab> registeredMenuBarTabs = new ArrayList<>();
+    @Getter
+    private static final HashMap<Class, Boolean> activeUI = new HashMap<>();
+
+    public static void loadUi() {
+        try {
+            Reflections reflections = new Reflections("xyz.tbvns.ui.Elements");
+            Set<Class<? extends UIElement>> elements = reflections.getSubTypesOf(UIElement.class);
+            for (Class<? extends UIElement> element : elements) {
+                registeredUiElements.add(element.getDeclaredConstructor().newInstance());
+                activeUI.put(element.getClass(), false);
+            }
+
+            reflections = new Reflections("xyz.tbvns.ui.MenuBarTabs");
+            Set<Class<? extends MenuBarTab>> tabs = reflections.getSubTypesOf(MenuBarTab.class);
+            for (Class<? extends MenuBarTab> element : tabs) {
+                registeredMenuBarTabs.add(element.getDeclaredConstructor().newInstance());
+            }
+            registeredMenuBarTabs.sort(Comparator.comparingInt(MenuBarTab::index));
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static void render() {
+        for (UIElement element : registeredUiElements) {
+            boolean isEnabled = activeUI.get(element.getClass());
+            if (isEnabled) {
+                element.render();
+            }
+        }
+    }
+
+    public static void renderMenuBarTabs() {
+        for (MenuBarTab tab : registeredMenuBarTabs) {
+            tab.render();
+        }
+    }
+}

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

@@ -8,13 +8,10 @@ import imgui.ImGuiIO;
 import imgui.flag.ImGuiWindowFlags;
 import imgui.gl3.ImGuiImplGl3;
 import imgui.glfw.ImGuiImplGlfw;
-import lombok.Setter;
 import xyz.tbvns.Main;
+import xyz.tbvns.ui.Elements.LevelSelector;
 
 public class Ui {
-    @Setter
-    private static boolean showLevelSelector = true;
-
     static private ImGuiImplGlfw imGuiGlfw;
     static private ImGuiImplGl3 imGuiGl3;
     private static InputProcessor tmpProcessor;
@@ -24,41 +21,7 @@ public class Ui {
 
         //TODO: Add functionality
         ImGui.beginMainMenuBar();
-
-        if (ImGui.beginMenu("Files")) {
-            ImGui.menuItem("Save", "CTRL S");
-            ImGui.menuItem("Export", "SHIFT S");
-            if (ImGui.menuItem("Open", "O")) {
-                //TODO: Add save dialog if the level was modified
-                showLevelSelector = true;
-                Main.modelCache = null;
-            }
-            ImGui.menuItem("Close", "C");
-            ImGui.endMenu();
-        }
-
-        if (ImGui.beginMenu("View")) {
-            ImGui.checkbox("Level grid", true);
-            ImGui.checkbox("World grid", true);
-            ImGui.endMenu();
-        }
-
-        if (ImGui.beginMenu("Tools")) {
-            ImGui.menuItem("Edit level string", "L");
-            ImGui.menuItem("BPM to FPS", "B");
-            ImGui.endMenu();
-        }
-
-        if (ImGui.beginMenu("About")) {
-            ImGui.menuItem("Help", "H");
-            ImGui.menuItem("Wiki", "CTRL - H");
-            ImGui.menuItem("Info", "I");
-            ImGui.menuItem("Discord");
-            ImGui.menuItem("Git");
-            ImGui.endMenu();
-        }
-
-
+        UIManager.renderMenuBarTabs();
         ImGui.endMainMenuBar();
 
         ImGui.begin("Menu", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove);
@@ -71,9 +34,7 @@ public class Ui {
         ImGui.setWindowPos(0, Gdx.graphics.getHeight() - 201);
         ImGui.end();
 
-        if (showLevelSelector) {
-            LevelSelector.render();
-        }
+        UIManager.render();
 
         endImGui();
     }