Skip to content

Commit

Permalink
Adding Export Frames button to ViewFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
DizzyThermal committed Oct 27, 2020
1 parent b76962f commit f63069f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 341 deletions.
36 changes: 36 additions & 0 deletions common/src/main/java/com/gamemode/tkviewer/TKItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.gamemode.tkviewer;

import com.gamemode.tkviewer.file_handlers.DatFileHandler;
import com.gamemode.tkviewer.file_handlers.EpfFileHandler;
import com.gamemode.tkviewer.file_handlers.PalFileHandler;
import com.gamemode.tkviewer.render.TileRenderer;
import com.gamemode.tkviewer.resources.Resources;
import com.gamemode.tkviewer.utilities.FileUtils;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;

public class TKItems {

public static void main(String[] args) {
File outputDirectory = new File("C:\\NTK_Items");

if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}

DatFileHandler charDat = new DatFileHandler(Resources.NTK_DATA_DIRECTORY + File.separator + "char.dat");
DatFileHandler miscDat = new DatFileHandler(Resources.NTK_DATA_DIRECTORY + File.separator + "misc.dat");

EpfFileHandler itemEpf = new EpfFileHandler(miscDat.getFile("ITEM.EPF"));
PalFileHandler itemPal = new PalFileHandler(charDat.getFile("ITEM.PAL"));

TileRenderer tileRenderer = new TileRenderer(new ArrayList<EpfFileHandler>(Arrays.asList(itemEpf)), itemPal, 0);

for (int i = 0; i < itemEpf.frameCount; i++) {
FileUtils.writeBufferedImageToFile(tileRenderer.renderTile(i), Paths.get(outputDirectory.toString(), "item-" + i + ".png").toString());
}
}
}
27 changes: 27 additions & 0 deletions common/src/main/java/com/gamemode/tkviewer/gui/ViewFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.gamemode.tkviewer.render.Renderer;
import com.gamemode.tkviewer.resources.Resources;
import com.gamemode.tkviewer.utilities.FileUtils;
import org.apache.commons.io.FilenameUtils;

import javax.swing.*;
import javax.swing.border.LineBorder;
Expand All @@ -17,7 +18,9 @@
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -33,6 +36,7 @@ public class ViewFrame extends JFrame implements ActionListener {
JPanel imagePanel;
JList list;

JButton exportButton;
JRadioButton framesButton;
JRadioButton animationsButton;

Expand Down Expand Up @@ -118,6 +122,9 @@ public void valueChanged(ListSelectionEvent e) {
statusPanel.setBorder(new LineBorder(Color.BLACK));
statusPanel.setPreferredSize(new Dimension(this.getWidth(), 36));

exportButton = new JButton("Export Frames");
exportButton.addActionListener(this);

framesButton = new JRadioButton("Frames");
animationsButton = new JRadioButton("Animations");
animationsButton.setSelected(true);
Expand All @@ -129,6 +136,8 @@ public void valueChanged(ListSelectionEvent e) {
framesButton.addActionListener(this);
animationsButton.addActionListener(this);

statusPanel.add(exportButton);

statusPanel.add(framesButton);
statusPanel.add(animationsButton);

Expand Down Expand Up @@ -298,6 +307,22 @@ public void mouseClicked(MouseEvent e) {
revalidate();
}

public void exportFrames(int index) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("Choose export directory");
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
Image[] images = renderer.getFrames(index);
for (int i = 0; i < images.length; i++) {
final int frameIndex = renderer.getFrameIndex(index, i);
FileUtils.writeBufferedImageToFile(((BufferedImage) images[i]), Paths.get(fileChooser.getSelectedFile().toString(), singular + "-" + index + "-" + frameIndex + ".png").toString());
}

JOptionPane.showMessageDialog(this, "Frames exported successfully!", "TKViewer", JOptionPane.INFORMATION_MESSAGE);
}
}

@Override
public void actionPerformed(ActionEvent ae) {
int listIndex = list.getSelectedIndex();
Expand All @@ -315,6 +340,8 @@ public void actionPerformed(ActionEvent ae) {
} else if (renderer instanceof PartRenderer) {
this.renderPartAnimations(listIndex);
}
} else if (ae.getSource() == this.exportButton) {
this.exportFrames(listIndex);
}
}
}
66 changes: 41 additions & 25 deletions common/src/main/java/com/gamemode/tkviewer/render/MobRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,42 @@ public BufferedImage renderMob(int tileIndex, int paletteIndex) {
return image;
}

public int[] getGlobalCanvasSize(int mobIndex) {
// Returns [l, t, r, b] for all chunks in mob
Mob mob = this.mobDna.mobs.get(mobIndex);

// Determine Canvas Size
int l, t, r, b;
l = t = r = b = 0;
for (int i = 0; i < mob.getChunkCount(); i++) {
MobChunk chunk = mob.getChunks().get(i);
int frameCount = chunk.getBlockCount();
for (int j = 0; j < frameCount; j++) {
MobBlock block = chunk.getBlocks().get(j);
int frameIndex = (int) (mob.getFrameIndex() + block.getFrameOffset());

Frame frame = FileUtils.getFrameFromEpfs(frameIndex, this.mobEpfs);
if (frame == null) {
continue;
}
if (frame.getLeft() < l) {
l = frame.getLeft();
}
if (frame.getTop() < t) {
t = frame.getTop();
}
if (frame.getRight() > r) {
r = frame.getRight();
}
if (frame.getBottom() > b) {
b = frame.getBottom();
}
}
}

return new int[]{l, t, r, b};
}

public List<EffectImage> renderAnimation(int mobIndex, ANIMATIONS animation) {
return renderAnimation(mobIndex, animation.ordinal());
}
Expand All @@ -141,31 +177,11 @@ public List<EffectImage> renderAnimation(int mobIndex, int chunkIndex) {

int frameCount = chunk.getBlockCount();

// Determine Canvas Size
int l, t, r, b;
l = t = r = b = 0;
for (int i = 0; i < frameCount; i++) {
MobBlock block = chunk.getBlocks().get(i);
int frameIndex = (int)(mob.getFrameIndex() + block.getFrameOffset());

Frame frame = FileUtils.getFrameFromEpfs(frameIndex, this.mobEpfs);
if (frame == null) {
continue;
}
// if (frame)
if (frame.getLeft() < l) {
l = frame.getLeft();
}
if (frame.getTop() < t) {
t = frame.getTop();
}
if (frame.getRight() > r) {
r = frame.getRight();
}
if (frame.getBottom() > b) {
b = frame.getBottom();
}
}
int[] dims = getGlobalCanvasSize(mobIndex);
int l = dims[0];
int t = dims[1];
int r = dims[2];
int b = dims[3];

int effectWidth = r-l;
int effectHeight = b-t;
Expand Down
Loading

0 comments on commit f63069f

Please sign in to comment.