-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
710 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
src/main/java/gg/generations/rarecandy/tools/DualOutputStream.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/gg/generations/rarecandy/tools/ResourceCachedFileLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
150
src/main/java/gg/generations/rarecandy/tools/gui/GuiHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
} | ||
} |
Oops, something went wrong.