Skip to content

Commit

Permalink
1 hr 37 mins 59 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
hYdos committed Oct 10, 2023
1 parent dee6404 commit b569e3e
Show file tree
Hide file tree
Showing 10 changed files with 710 additions and 201 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
shadow(implementation("org.apache.commons", "commons-compress", "1.24.0"))
shadow(implementation("org.joml", "joml", "1.10.5"))

shadow(implementation(platform("org.lwjgl:lwjgl-bom:3.3.2"))!!)
shadow(implementation(platform("org.lwjgl:lwjgl-bom:3.3.3"))!!)
shadow(implementation("org.lwjgl", "lwjgl"))
shadow(implementation("org.lwjgl", "lwjgl-glfw"))
shadow(implementation("org.lwjgl", "lwjgl-opengl"))
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/gg/generations/rarecandy/tools/DualOutputStream.java

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/gg/generations/rarecandy/tools/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.generations.rarecandy.tools;

import gg.generations.rarecandy.tools.gui.PokeUtilsGui;
import gg.generations.rarecandy.tools.pkcreator.PixelConverter;
import gg.generations.rarecandy.tools.pkcreator.PixelmonArchiveBuilder;

Expand Down Expand Up @@ -32,7 +33,7 @@ private static void converter(String[] args) {
}

private static void archiveExplorer(String[] args) {
// PokeUtilsGui.main(args);
PokeUtilsGui.main(args);
}

public static void main(String[] args) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gg.generations.rarecandy.tools;

import com.thebombzen.jxlatte.JXLDecoder;
import com.thebombzen.jxlatte.JXLOptions;
import gg.generationsmod.rarecandy.FileLocator;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class ResourceCachedFileLocator implements FileLocator {

private final Map<String, byte[]> fileCache = new HashMap<>();
private final Path root;

public ResourceCachedFileLocator(Path root) {
this.root = root;
}

@Override
public byte[] getFile(String name) {
return fileCache.computeIfAbsent(name, s -> {
try {
return Files.readAllBytes(root.resolve(name));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

public BufferedImage read(byte[] imageBytes) throws IOException {
var options = new JXLOptions();
options.hdr = JXLOptions.HDR_OFF;
options.threads = 2;
var reader = new JXLDecoder(new ByteArrayInputStream(imageBytes), options);
var image = reader.decode();
return image.asBufferedImage();
}

@Override
public BufferedImage readImage(String name) {
try {
var is = Files.newInputStream(root.resolve(name));
var image = name.endsWith(".jxl") ? read(is.readAllBytes()) : ImageIO.read(is);
int height = image.getHeight();
int width = image.getWidth();

// Mirror image if not square. TODO: maybe do this in the shader to save gpu memory and upload time in general
if (height / width == 2) {
var mirror = new BufferedImage(width * 2, height, BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < height; y++) {
for (int lx = 0, rx = width * 2 - 1; lx < width; lx++, rx--) {
int p = image.getRGB(lx, y);
mirror.setRGB(lx, y, p);
mirror.setRGB(rx, y, p);
}
}

image = mirror;
}

return image;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
150 changes: 150 additions & 0 deletions src/main/java/gg/generations/rarecandy/tools/gui/GuiHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package gg.generations.rarecandy.tools.gui;

import gg.generations.rarecandy.tools.ResourceCachedFileLocator;
import gg.generationsmod.rarecandy.assimp.AssimpModelLoader;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class GuiHandler implements KeyListener {
private static final String BASE_TITLE = "Rare Candy Model Explorer (RKS Version)";
private final PokeUtilsGui gui;
private final JFrame frame;
private final List<Integer> pressedKeys = new ArrayList<>();
public Path assetPath;
private String currentTitle = BASE_TITLE;
private boolean dirty = false;

public GuiHandler(JFrame frame, PokeUtilsGui gui) {
this.frame = frame;
this.gui = gui;

gui.setHandler(this);
frame.setTitle(currentTitle);
frame.setVisible(true);
frame.pack();
frame.transferFocus();
getCanvas().attachArcBall();
}

public RareCandyCanvas getCanvas() {
return (RareCandyCanvas) gui.canvasPanel.getComponents()[0];
}

public void initializeAsset(Path path) {
try {
this.assetPath = path;
((PixelAssetTree) gui.fileViewer).initializeAsset(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void save() {
save(assetPath);
}

public void save(Path savePath) {
if (dirty) {
throw new RuntimeException("Nuh uh");
// try {
// if (!Files.exists(savePath)) {
// Files.deleteIfExists(savePath);
// Files.createDirectories(savePath.getParent());
// Files.createFile(savePath);
// }
//
// var saveBox = new JDialog(frame, "Saving File", true);
// var progressBar = (JProgressBar) saveBox.add(BorderLayout.CENTER, new JProgressBar(0, 100));
// var fileChunk = 100 / asset.files.size();
// saveBox.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
// saveBox.setSize(300, 50);
// saveBox.setLocationRelativeTo(frame);
// SwingUtilities.invokeLater(() -> saveBox.setVisible(true));
//
// new Thread(() -> {
// try (var xzWriter = new XZOutputStream(Files.newOutputStream(assetPath), OPTIONS)) {
// try (var tarWriter = new TarArchiveOutputStream(xzWriter)) {
// print(tarWriter.getBytesWritten());
// for (var file : asset.files.entrySet()) {
// var entry = new TarArchiveEntry(file.getKey());
// entry.setSize(file.getValue().length);
// tarWriter.putArchiveEntry(entry);
//// tarWriter.write(file.getValue());
// IOUtils.copy(new BufferedInputStream(new ByteArrayInputStream(file.getValue())), tarWriter);
// tarWriter.closeArchiveEntry();
// SwingUtilities.invokeLater(() -> progressBar.setValue(progressBar.getValue() + fileChunk));
// }
//
// print(tarWriter.getBytesWritten());
// }
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
//
//
// SwingUtilities.invokeLater(() -> saveBox.setVisible(false));
// frame.setTitle(currentTitle.substring(0, currentTitle.length() - 1));
// }).start();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
//
// dirty = false;
}
}

public void markDirty() {
if (!dirty) {
this.dirty = true;
currentTitle = frame.getTitle() + "*";
frame.setTitle(frame.getTitle() + "*");
}
}

public void openAsset(Path filePath) {
initializeAsset(filePath);
var title = BASE_TITLE + " - " + filePath.getFileName().toString();
frame.setTitle(title);
this.currentTitle = title;
getCanvas().openFile(AssimpModelLoader.load("model.gltf", new ResourceCachedFileLocator(filePath), 0));
}

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
pressedKeys.add(e.getKeyCode());

var isCtrlPressed = pressedKeys.contains(KeyEvent.VK_CONTROL);
if (e.getKeyCode() == KeyEvent.VK_S && isCtrlPressed) save();
}

@Override
public void keyReleased(KeyEvent e) {
pressedKeys.remove((Integer) e.getKeyCode());
}

public void convertGlb(Path chosenFile) {
throw new RuntimeException("Nuh uh");
// try {
// var is = Files.newInputStream(chosenFile);
// var filePath = Path.of(chosenFile.toString().replace(".glb", ".pk"));
// initializeAsset(new PixelAsset(chosenFile.getFileName().toString(), is.readAllBytes()), filePath);
// var title = BASE_TITLE + " - " + filePath.getFileName().toString();
// frame.setTitle(title);
// this.currentTitle = title;
// getCanvas().openFile(asset);
//
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
}
}
Loading

0 comments on commit b569e3e

Please sign in to comment.