2018-04-25 13:00:18 +02:00
|
|
|
package com.minelittlepony.pony.data;
|
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* Individual trigger pixels for a pony skin.
|
|
|
|
*
|
|
|
|
*/
|
2018-04-29 14:20:13 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2018-04-25 13:00:18 +02:00
|
|
|
public enum TriggerPixels {
|
2018-04-29 14:37:51 +02:00
|
|
|
RACE(PonyRace.HUMAN, Channel.ALL, 0, 0),
|
|
|
|
TAIL(TailLengths.FULL, Channel.ALL, 1, 0),
|
|
|
|
GENDER(PonyGender.MARE, Channel.ALL, 2, 0),
|
2018-07-11 20:51:04 +02:00
|
|
|
SIZE(PonySize.NORMAL, Channel.ALL, 3, 0),
|
2018-06-05 20:18:57 +02:00
|
|
|
GLOW(null, Channel.RAW, 0, 1),
|
|
|
|
WEARABLES(PonyWearable.NONE, Channel.RAW, 1, 1);
|
2018-04-25 13:00:18 +02:00
|
|
|
|
2018-04-26 23:53:03 +02:00
|
|
|
private int x;
|
|
|
|
private int y;
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-29 14:37:51 +02:00
|
|
|
private Channel channel;
|
|
|
|
|
2018-04-25 13:00:18 +02:00
|
|
|
ITriggerPixelMapped<?> def;
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-29 14:37:51 +02:00
|
|
|
TriggerPixels(ITriggerPixelMapped<?> def, Channel channel, int x, int y) {
|
2018-04-25 13:00:18 +02:00
|
|
|
this.def = def;
|
2018-04-29 14:37:51 +02:00
|
|
|
this.channel = channel;
|
2018-04-25 13:00:18 +02:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
}
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
2018-04-29 14:20:13 +02:00
|
|
|
* Reads this trigger pixel's value and returns the raw colour.
|
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* @param image Image to read
|
|
|
|
*/
|
2018-04-29 14:37:51 +02:00
|
|
|
public int readColor(BufferedImage image) {
|
|
|
|
return channel.readValue(x, y, image);
|
2018-04-25 13:00:18 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* Reads this trigger pixel's value and parses it to an Enum instance.
|
2018-04-27 13:49:33 +02:00
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* @param image Image to read
|
|
|
|
*/
|
2018-04-25 13:00:18 +02:00
|
|
|
public <T extends Enum<T> & ITriggerPixelMapped<T>> T readValue(BufferedImage image) {
|
2018-09-07 13:12:15 +02:00
|
|
|
if (Channel.ALPHA.readValue(x, y, image) < 255) {
|
|
|
|
return (T)def;
|
|
|
|
}
|
|
|
|
|
2018-04-29 14:37:51 +02:00
|
|
|
return ITriggerPixelMapped.getByTriggerPixel((T)def, readColor(image));
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:18:57 +02:00
|
|
|
public <T extends Enum<T> & ITriggerPixelMapped<T>> boolean[] readFlags(BufferedImage image) {
|
|
|
|
boolean[] out = new boolean[def.getClass().getEnumConstants().length];
|
|
|
|
readFlags(out, image);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T extends Enum<T> & ITriggerPixelMapped<T>> void readFlags(boolean[] out, BufferedImage image) {
|
|
|
|
readFlag(out, Channel.RED, image);
|
|
|
|
readFlag(out, Channel.GREEN, image);
|
|
|
|
readFlag(out, Channel.BLUE, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
private <T extends Enum<T> & ITriggerPixelMapped<T>> void readFlag(boolean[] out, Channel channel, BufferedImage image) {
|
2018-09-07 13:12:15 +02:00
|
|
|
|
|
|
|
if (Channel.ALPHA.readValue(x, y, image) < 255) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:18:57 +02:00
|
|
|
T value = ITriggerPixelMapped.getByTriggerPixel((T)def, channel.readValue(x, y, image));
|
2018-09-07 13:12:15 +02:00
|
|
|
|
|
|
|
out[value.ordinal()] |= value != def;
|
2018-06-05 20:18:57 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 14:37:51 +02:00
|
|
|
enum Channel {
|
|
|
|
RAW(-1, 0),
|
2018-09-07 13:12:15 +02:00
|
|
|
ALL (0xffffff, 0),
|
|
|
|
ALPHA(0xff, 24),
|
|
|
|
RED (0xff, 16),
|
|
|
|
GREEN(0xff, 8),
|
|
|
|
BLUE (0xff, 0);
|
2018-04-29 14:37:51 +02:00
|
|
|
|
|
|
|
private int mask;
|
|
|
|
private int offset;
|
|
|
|
|
|
|
|
Channel(int mask, int offset) {
|
|
|
|
this.mask = mask;
|
|
|
|
this.offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int readValue(int x, int y, BufferedImage image) {
|
2018-09-07 13:12:15 +02:00
|
|
|
return (image.getRGB(x, y) >> offset) & mask;
|
2018-04-29 14:37:51 +02:00
|
|
|
}
|
2018-04-25 13:00:18 +02:00
|
|
|
}
|
|
|
|
}
|