io.github.spair.byond.dmi.StateExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of byond-dmi-util Show documentation
Show all versions of byond-dmi-util Show documentation
Small set of util classes to work with BYOND dmi files.
package io.github.spair.byond.dmi;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.Comparator;
final class StateExtractor {
static Map extractStates(final BufferedImage dmiImage, final DmiMeta dmiMeta) {
final int dmiWidth = dmiImage.getWidth();
final int spriteWidth = dmiMeta.getSpritesWidth();
final int spriteHeight = dmiMeta.getSpritesHeight();
final int spritesInARow = dmiWidth / spriteWidth;
int xPos = 0;
int yPos = 0;
int spriteIndex = 1;
Map dmiStates = new HashMap<>();
for (Meta metaEntry : dmiMeta.getMetas()) {
List allSprites = new ArrayList<>();
for (int frameNumber = 1; frameNumber <= metaEntry.getFrames(); frameNumber++) {
for (int dirCount = 1; dirCount <= metaEntry.getDirs(); dirCount++) {
final BufferedImage spriteImage = cropSpriteImage(dmiImage, spriteWidth, spriteHeight, xPos, yPos);
allSprites.add(new DmiSprite(spriteImage, SpriteDir.valueOf(dirCount), frameNumber));
if (spriteIndex < spritesInARow) {
spriteIndex++;
xPos += spriteWidth;
} else {
spriteIndex = 1;
xPos = 0;
yPos += spriteHeight;
}
}
}
DmiState dmiState = new DmiState();
dmiState.setMeta(metaEntry);
dmiState.setSprites(distributeAllSpritesInMap(allSprites));
dmiState.setDuplicate(dmiStates.containsKey(metaEntry.getName()));
dmiStates.put(metaEntry.getName(), dmiState);
}
return dmiStates;
}
private static BufferedImage cropSpriteImage(
final BufferedImage dmiImage, final int width, final int height, final int xPos, final int yPos) {
BufferedImage dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = dst.getGraphics();
graphics.drawImage(dmiImage, 0, 0, width, height, xPos, yPos, xPos + width, yPos + height, null);
graphics.dispose();
return dst;
}
private static Map> distributeAllSpritesInMap(final List allSprites) {
Map> spriteMap = new TreeMap<>(Comparator.comparingInt(dir -> dir.compareWeight));
for (DmiSprite sprite : allSprites) {
List spritesInDir = spriteMap.getOrDefault(sprite.getDir(), new ArrayList<>());
spritesInDir.add(sprite);
spriteMap.putIfAbsent(sprite.getDir(), spritesInDir);
}
return spriteMap;
}
private StateExtractor() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy