implement ignoring fully transparent sprites

This commit is contained in:
OMGeeky
2023-09-10 17:15:28 +02:00
parent fc0d97aa2f
commit ada352a02c
2 changed files with 41 additions and 5 deletions

View File

@@ -4,7 +4,6 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -15,6 +14,7 @@ import com.gpl.rpg.atcontentstudio.ATContentStudio;
import com.gpl.rpg.atcontentstudio.model.Project;
import com.gpl.rpg.atcontentstudio.model.sprites.Spritesheet;
import com.gpl.rpg.atcontentstudio.model.sprites.Spritesheet.Category;
import com.gpl.rpg.atcontentstudio.utils.SpriteUtils;
public class SpriteChooser extends JDialog {
@@ -97,11 +97,14 @@ public class SpriteChooser extends JDialog {
Point nextFreeSlot = new Point(0, 0);
int i;
Image img;
BufferedImage img;
group = new ButtonGroup();
for (Spritesheet sheet : spritesheets) {
i = 0;
while ((img = sheet.getImage(i)) != null) {
i = -1;
while ((img = sheet.getImage(++i)) != null) {
if (SpriteUtils.checkIsImageEmpty(img)) {
continue;
}
IconButton button = new IconButton(img, sheet.id, i);
group.add(button);
if (sheet.spriteWidth == STD_WIDTH && sheet.spriteHeight == STD_HEIGHT) {
@@ -150,7 +153,6 @@ public class SpriteChooser extends JDialog {
}
nextFreeSlot.setLocation(c.gridx, c.gridy);
}
i++;
}
}

View File

@@ -0,0 +1,34 @@
package com.gpl.rpg.atcontentstudio.utils;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
public final class SpriteUtils {
/**
* Check if the image is empty (transparent )
*
* @param img The image to check
* @return true if the image is empty
*/
public static boolean checkIsImageEmpty(BufferedImage img) {
int width = img.getWidth(null);
int height = img.getHeight(null);
WritableRaster raster = img.getAlphaRaster();
if (raster == null) {
return false;
}
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
//get pixel alpha value
int alpha = raster.getSample(x, y, 0);
//if alpha is not 0 then the pixel is not transparent
if (alpha != 0) {
return false;
}
}
}
//no non-transparent pixel found
return true;
}
}