Skip to content

Commit

Permalink
Incrementing PartPicker! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DizzyThermal committed Oct 25, 2019
1 parent 35e5436 commit 28a40de
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/main/java/com/gamemode/tkviewer/gui/TKPartPickerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -65,35 +67,39 @@ public class TKPartPickerGUI extends JFrame implements ActionListener {
};
LinkedHashMap<String, PartInfo> characterPartInfo;

int partValue = 0;

public TKPartPickerGUI(String title) {
super(title);
this.setPreferredSize(new Dimension(640, 480));
this.clientIcon = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("client_icon.png"));
this.setIconImage(this.clientIcon);

characterPartInfo = new LinkedHashMap<String, PartInfo>();
characterPartInfo.put("Bodies", new PartInfo(0, 2, true, RenderUtils.createBodyRenderer()));
characterPartInfo.put("Bows", new PartInfo(0, 0,false, RenderUtils.createBowRenderer()));
characterPartInfo.put("Bodies", new PartInfo(46, 2, true, RenderUtils.createBodyRenderer()));
characterPartInfo.put("Coats", new PartInfo(0, 0,false, RenderUtils.createCoatRenderer()));
characterPartInfo.put("Faces", new PartInfo(0, 18,true, RenderUtils.createFaceRenderer()));
characterPartInfo.put("Face Decorations", new PartInfo(0, 0,false, RenderUtils.createFaceDecRenderer()));
characterPartInfo.put("Fans", new PartInfo(0, 0,false, RenderUtils.createFanRenderer()));
characterPartInfo.put("Hair", new PartInfo(0, 18,true, RenderUtils.createHairRenderer()));
characterPartInfo.put("Helmets", new PartInfo(0, 0,false, RenderUtils.createHelmetRenderer()));
characterPartInfo.put("Shoes", new PartInfo(0, 0,false, RenderUtils.createShoeRenderer()));
characterPartInfo.put("Mantles", new PartInfo(0, 0,false, RenderUtils.createMantleRenderer()));
characterPartInfo.put("Helmets", new PartInfo(0, 0,false, RenderUtils.createHelmetRenderer()));

characterPartInfo.put("Faces", new PartInfo(0, 2,true, RenderUtils.createFaceRenderer()));
characterPartInfo.put("Face Decorations", new PartInfo(3, 0,false, RenderUtils.createFaceDecRenderer()));
characterPartInfo.put("Hair", new PartInfo(0, 2,true, RenderUtils.createHairRenderer()));

characterPartInfo.put("Spears", new PartInfo(0, 0,false, RenderUtils.createSpearRenderer()));
characterPartInfo.put("Shoes", new PartInfo(0, 0,false, RenderUtils.createShoeRenderer()));
characterPartInfo.put("Shields", new PartInfo(0, 0,false, RenderUtils.createShieldRenderer()));
characterPartInfo.put("Swords", new PartInfo(0, 0,false, RenderUtils.createSwordRenderer()));
characterPartInfo.put("Fans", new PartInfo(0, 0,false, RenderUtils.createFanRenderer()));

initMenu();
initPanel();

SwingWorker loadingWorker = new SwingWorker<Boolean, Integer>() {
@Override
protected Boolean doInBackground() throws Exception {
while (!this.isCancelled()) {
while (true) {
Thread.sleep(250);
tickValue++;

viewerPanel.removeAll();

Expand All @@ -102,8 +108,6 @@ protected Boolean doInBackground() throws Exception {
viewerPanel.revalidate();
viewerPanel.repaint();
}

return true;
}

@Override
Expand Down Expand Up @@ -172,27 +176,34 @@ public BufferedImage renderCharacter() {
BufferedImage characterImage = createGrassBackground();
Graphics2D graphicsObject = characterImage.createGraphics();

int number = 0;
List<List<EffectImage>> effImages = new ArrayList<List<EffectImage>>();
for (Map.Entry<String, PartInfo> characterPartInfo : this.characterPartInfo.entrySet()) {
String partKey = characterPartInfo.getKey();
PartInfo partInfo = characterPartInfo.getValue();

if (partInfo.getShouldRender()) {

int partIndex = partInfo.getPartIndex();
int animationIndex = partInfo.getAnimationIndex();

List<EffectImage> effectImages = partInfo.getPartRenderer().renderAnimation(partIndex, animationIndex);

graphicsObject.drawImage(effectImages.get(tickValue).getImage(), null, 50, 50);
System.out.println(tickValue);
if (tickValue >= 1) {
tickValue = 0;
} else {
tickValue++;
}
effImages.add(effectImages);
}
}
tickValue++;

List<EffectImage> effectImages = RenderUtils.aggregateAnimations(effImages);

BufferedImage drawing = effectImages.get(tickValue % effectImages.size()).getImage();
int backgroundWidth = characterImage.getWidth();
int backgroundHeight = characterImage.getHeight();
int width = drawing.getWidth();
int height = drawing.getHeight();
graphicsObject.drawImage(drawing, null, (backgroundWidth / 2) - (width / 2), (backgroundHeight / 2) - (height / 2));

characterPartInfo.get("Bodies").setPartIndex(partValue++);
characterPartInfo.get("Hair").setPartIndex(partValue++);
//characterPartInfo.get("Face").setPartIndex(partValue++);

return characterImage;
}
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/gamemode/tkviewer/utilities/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
import com.gamemode.tkviewer.render.PartRenderer;
import com.gamemode.tkviewer.render.SObjRenderer;
import com.gamemode.tkviewer.render.TileRenderer;
import com.gamemode.tkviewer.resources.EffectImage;
import com.gamemode.tkviewer.resources.Frame;
import com.gamemode.tkviewer.resources.PivotData;
import com.gamemode.tkviewer.resources.Resources;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RenderUtils {
public static DatFileHandler CHAR_DAT = new DatFileHandler(Resources.NTK_DATA_DIRECTORY + File.separator + "char.dat");
Expand All @@ -28,6 +34,16 @@ public class RenderUtils {
*/
private RenderUtils() {}

public static PivotData getPivotData(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();

int left = width / 2;
int top = height / 2;

return new PivotData(left, top, width, height);
}

public static PivotData getPivotData(List<Frame> frames) {
// Determine Canvas Size
int left, top, right, bottom;
Expand Down Expand Up @@ -58,6 +74,66 @@ public static PivotData getPivotData(List<Frame> frames) {
return new PivotData(Math.abs(left), Math.abs(top), width, height);
}

public static List<EffectImage> aggregateAnimations (List < List <EffectImage>> effImages){
List<Frame> allFrames = new ArrayList<>();
for (List<EffectImage> subListImages : effImages) {
allFrames.addAll(subListImages.stream().map(EffectImage::getFrame).collect(Collectors.toList()));
}
PivotData pivotData = RenderUtils.getPivotData(allFrames);
int maxWidth = pivotData.getCanvasWidth();
int maxHeight = pivotData.getCanvasHeight();

// Correct Images according to maxWidth and maxHeight
for (int i = 0; i < effImages.size(); i++) {
for (int j = 0; j < effImages.get(i).size(); j++) {
EffectImage effImage = effImages.get(i).get(j);
effImage.setImage(resizeImage(effImage.getImage(), maxWidth, maxHeight, pivotData,
effImage.getFrame(), effImage.getPivotData()));
}
}

List<EffectImage> mergedImages = effImages.get(0);
for (int i = 1; i < effImages.size(); i++) {
mergedImages = mergeEffectImages(mergedImages, effImages.get(i));
}

return mergedImages;
}

/**
* Draws images2 on top of images1 - images all must be equal size!
*/
public static List<EffectImage> mergeEffectImages(List<EffectImage> images1, List<EffectImage> images2) {
List<EffectImage> returnEffectImages = new ArrayList<EffectImage>();

int count = Math.max(images1.size(), images2.size());
int width = Math.max(images1.get(0).getImage().getWidth(), images2.get(0).getImage().getWidth());
int height = Math.max(images1.get(0).getImage().getHeight(), images2.get(0).getImage().getHeight());
for (int i = 0; i < count; i++) {
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Graphics2D graphicsObject = newImage.createGraphics();
graphicsObject.drawImage(images1.get(i % images1.size()).getImage(),null,0,0);
graphicsObject.drawImage(images2.get(i % images2.size()).getImage(),null,0,0);

returnEffectImages.add(new EffectImage(newImage, images1.get(i % images1.size()).getDelay(), null, null));
}

return returnEffectImages;
}

public static BufferedImage resizeImage(BufferedImage image, int newWidth, int newHeight, PivotData pivotData,
Frame frame, PivotData framePivotData) {
BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);

Graphics2D graphicsObject = newImage.createGraphics();
int frameLeft = (pivotData.getPivotX() - framePivotData.getPivotX());
int frameTop = (pivotData.getPivotY() - framePivotData.getPivotY());
graphicsObject.drawImage(image, null, frameLeft, frameTop);

return newImage;
}

public static PartRenderer createBodyRenderer () { return new PartRenderer("Body"); }
public static PartRenderer createBowRenderer () {
return new PartRenderer("Bow");
Expand Down

0 comments on commit 28a40de

Please sign in to comment.