From 5da88c85511c76d5a4ba48b2bcd7d9135fea98b8 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 25 Apr 2018 13:00:18 +0200 Subject: [PATCH] Wave 3, simplify trigger pixel handling --- .../java/com/minelittlepony/PonyData.java | 85 +++---------------- .../java/com/minelittlepony/PonyGender.java | 19 ++++- .../java/com/minelittlepony/PonyRace.java | 34 +++++--- .../java/com/minelittlepony/PonySize.java | 22 +++-- .../java/com/minelittlepony/TailLengths.java | 25 ++++-- .../pony/data/ITriggerPixelMapped.java | 14 +++ .../pony/data/TriggerPixels.java | 35 ++++++++ 7 files changed, 136 insertions(+), 98 deletions(-) create mode 100644 src/main/java/com/minelittlepony/pony/data/ITriggerPixelMapped.java create mode 100644 src/main/java/com/minelittlepony/pony/data/TriggerPixels.java diff --git a/src/main/java/com/minelittlepony/PonyData.java b/src/main/java/com/minelittlepony/PonyData.java index 459fbe83..af59130c 100644 --- a/src/main/java/com/minelittlepony/PonyData.java +++ b/src/main/java/com/minelittlepony/PonyData.java @@ -1,37 +1,13 @@ package com.minelittlepony; import com.google.common.base.MoreObjects; -import com.google.common.collect.ImmutableBiMap; +import com.minelittlepony.pony.data.TriggerPixels; import java.awt.image.BufferedImage; -import java.util.Map; import javax.annotation.concurrent.Immutable; @Immutable public class PonyData implements IPonyData { - - private static final Map RACE_COLORS = ImmutableBiMap.builder() - .put(0xf9b131, PonyRace.EARTH) - .put(0xd19fe4, PonyRace.UNICORN) - .put(0x88caf0, PonyRace.PEGASUS) - .put(0xfef9fc, PonyRace.ALICORN) - .put(0xd0cccf, PonyRace.ZEBRA) - .put(0x282b29, PonyRace.CHANGELING) - .put(0xcaed5a, PonyRace.REFORMED_CHANGELING) - .put(0xae9145, PonyRace.GRIFFIN) - .put(0xd6ddac, PonyRace.HIPPOGRIFF) - .build(); - private static final Map TAIL_COLORS = ImmutableBiMap.builder() - .put(0x425844, TailLengths.STUB) - .put(0xd19fe4, TailLengths.QUARTER) - .put(0x534b76, TailLengths.HALF) - .put(0x8a6b7f, TailLengths.THREE_QUARTERS).build(); - private static final Map SIZE_COLORS = ImmutableBiMap.builder() - .put(0xffbe53, PonySize.FOAL) - .put(0xce3254, PonySize.LARGE) - .put(0x534b76, PonySize.TALL) - .build(); - private final PonyRace race; private final TailLengths tailSize; private final PonyGender gender; @@ -39,15 +15,19 @@ public class PonyData implements IPonyData { private final int glowColor; public PonyData() { - this(PonyRace.HUMAN, TailLengths.FULL, PonyGender.MARE, PonySize.NORMAL, 0x4444aa); + this.race = PonyRace.HUMAN; + this.tailSize = TailLengths.FULL; + this.gender = PonyGender.MARE; + this.size = PonySize.NORMAL; + this.glowColor = 0x4444aa; } - - private PonyData(PonyRace race, TailLengths tailSize, PonyGender gender, PonySize size, int glowColor) { - this.race = race; - this.tailSize = tailSize; - this.gender = gender; - this.size = size; - this.glowColor = glowColor; + + private PonyData(BufferedImage image) { + this.race = TriggerPixels.RACE.readValue(image); + this.tailSize = TriggerPixels.TAIL.readValue(image); + this.size = TriggerPixels.SIZE.readValue(image); + this.gender = TriggerPixels.GENDER.readValue(image); + this.glowColor = TriggerPixels.GLOW.readColor(image, -1); } @Override @@ -92,43 +72,6 @@ public class PonyData implements IPonyData { } static IPonyData parse(BufferedImage image) { - int racePx = TriggerPixels.RACE.readColor(image); - PonyRace race = RACE_COLORS.getOrDefault(racePx, PonyRace.HUMAN); - - int tailPx = TriggerPixels.TAIL.readColor(image); - TailLengths tail = TAIL_COLORS.getOrDefault(tailPx, TailLengths.FULL); - - int sizePx = TriggerPixels.SIZE.readColor(image); - PonySize size = SIZE_COLORS.getOrDefault(sizePx, PonySize.NORMAL); - - int genderPx = TriggerPixels.GENDER.readColor(image); - PonyGender gender = genderPx == 0xffffff ? PonyGender.STALLION : PonyGender.MARE; - - int glowColor = TriggerPixels.GLOW.readColor(image, -1); - - return new PonyData(race, tail, gender, size, glowColor); - } - - private enum TriggerPixels { - RACE(0, 0), - TAIL(1, 0), - GENDER(2, 0), - SIZE(3, 0), - GLOW(0, 1); - - private int x, y; - - TriggerPixels(int x, int y) { - this.x = x; - this.y = y; - } - - private int readColor(BufferedImage image) { - return readColor(image, 0xffffff); - } - - private int readColor(BufferedImage image, int mask) { - return image.getRGB(x, y) & mask; - } + return new PonyData(image); } } diff --git a/src/main/java/com/minelittlepony/PonyGender.java b/src/main/java/com/minelittlepony/PonyGender.java index 060295f3..1da3900d 100644 --- a/src/main/java/com/minelittlepony/PonyGender.java +++ b/src/main/java/com/minelittlepony/PonyGender.java @@ -1,6 +1,19 @@ package com.minelittlepony; -public enum PonyGender { - MARE, - STALLION +import com.minelittlepony.pony.data.ITriggerPixelMapped; + +public enum PonyGender implements ITriggerPixelMapped { + MARE(0), + STALLION(0xffffff); + + int triggerValue; + + PonyGender(int pixel) { + triggerValue = pixel; + } + + @Override + public int getTriggerPixel() { + return triggerValue; + } } diff --git a/src/main/java/com/minelittlepony/PonyRace.java b/src/main/java/com/minelittlepony/PonyRace.java index fa0f82b5..35846b7e 100644 --- a/src/main/java/com/minelittlepony/PonyRace.java +++ b/src/main/java/com/minelittlepony/PonyRace.java @@ -1,25 +1,30 @@ package com.minelittlepony; import com.minelittlepony.model.PlayerModels; +import com.minelittlepony.pony.data.ITriggerPixelMapped; -public enum PonyRace { +public enum PonyRace implements ITriggerPixelMapped { - HUMAN(PlayerModels.HUMAN, false, false), - EARTH(PlayerModels.PONY,false, false), - PEGASUS(PlayerModels.PONY, true, false), - UNICORN(PlayerModels.PONY, false, true), - ALICORN(PlayerModels.PONY, true, true), - CHANGELING(PlayerModels.PONY, true, true), - ZEBRA(PlayerModels.PONY, false, false), - REFORMED_CHANGELING(PlayerModels.PONY, true, true), - GRIFFIN(PlayerModels.PONY, true, false), - HIPPOGRIFF(PlayerModels.PONY, true, false); + HUMAN(0, PlayerModels.HUMAN, false, false), + EARTH(0xf9b131, PlayerModels.PONY,false, false), + PEGASUS(0x88caf0, PlayerModels.PONY, true, false), + UNICORN(0xd19fe4, PlayerModels.PONY, false, true), + ALICORN(0xfef9fc, PlayerModels.PONY, true, true), + CHANGELING(0x282b29, PlayerModels.PONY, true, true), + ZEBRA(0xd0cccf, PlayerModels.PONY, false, false), + REFORMED_CHANGELING(0xcaed5a, PlayerModels.PONY, true, true), + GRIFFIN(0xae9145, PlayerModels.PONY, true, false), + HIPPOGRIFF(0xd6ddac, PlayerModels.PONY, true, false); private boolean wings, horn; + private int triggerPixel; + private PlayerModels model; - PonyRace(PlayerModels model, boolean wings, boolean horn) { + PonyRace(int triggerPixel, PlayerModels model, boolean wings, boolean horn) { + this.triggerPixel = triggerPixel; + this.wings = wings; this.horn = horn; this.model = model; @@ -45,4 +50,9 @@ public enum PonyRace { if (level == PonyLevel.HUMANS) return HUMAN; return this; } + + @Override + public int getTriggerPixel() { + return triggerPixel; + } } diff --git a/src/main/java/com/minelittlepony/PonySize.java b/src/main/java/com/minelittlepony/PonySize.java index 400bf5f9..1c230455 100644 --- a/src/main/java/com/minelittlepony/PonySize.java +++ b/src/main/java/com/minelittlepony/PonySize.java @@ -1,14 +1,19 @@ package com.minelittlepony; -public enum PonySize { - NORMAL(0.4f, 1f), - LARGE(0.5f, 0.8f), - FOAL(0.25f, 0.8f), - TALL(0.45f, 1f); +import com.minelittlepony.pony.data.ITriggerPixelMapped; +public enum PonySize implements ITriggerPixelMapped { + NORMAL(0, 0.4f, 1f), + LARGE(0xce3254, 0.5f, 0.8f), + FOAL(0xffbe53, 0.25f, 0.8f), + TALL(0x534b76, 0.45f, 1f); + + private int triggerValue; + private float shadowSize, scale; - PonySize(float shadowSz, float scaleF) { + PonySize(int pixel, float shadowSz, float scaleF) { + triggerValue = pixel; shadowSize = shadowSz; scale = scaleF; } @@ -20,4 +25,9 @@ public enum PonySize { public float getScaleFactor() { return scale; } + + @Override + public int getTriggerPixel() { + return triggerValue; + } } diff --git a/src/main/java/com/minelittlepony/TailLengths.java b/src/main/java/com/minelittlepony/TailLengths.java index 5c1cc534..483cb977 100644 --- a/src/main/java/com/minelittlepony/TailLengths.java +++ b/src/main/java/com/minelittlepony/TailLengths.java @@ -1,10 +1,23 @@ package com.minelittlepony; -public enum TailLengths { +import com.minelittlepony.pony.data.ITriggerPixelMapped; - STUB, - QUARTER, - HALF, - THREE_QUARTERS, - FULL; +public enum TailLengths implements ITriggerPixelMapped { + + STUB(0x425844), + QUARTER(0xd19fe4), + HALF(0x534b76), + THREE_QUARTERS(0x8a6b7f), + FULL(0); + + private int triggerValue; + + TailLengths(int pixel) { + triggerValue = pixel; + } + + @Override + public int getTriggerPixel() { + return triggerValue; + } } diff --git a/src/main/java/com/minelittlepony/pony/data/ITriggerPixelMapped.java b/src/main/java/com/minelittlepony/pony/data/ITriggerPixelMapped.java new file mode 100644 index 00000000..9ba53aa0 --- /dev/null +++ b/src/main/java/com/minelittlepony/pony/data/ITriggerPixelMapped.java @@ -0,0 +1,14 @@ +package com.minelittlepony.pony.data; + +public interface ITriggerPixelMapped & ITriggerPixelMapped> { + + int getTriggerPixel(); + + @SuppressWarnings("unchecked") + public static & ITriggerPixelMapped> T getByTriggerPixel(T type, int pixelValue) { + for (T i : (T[])type.getClass().getEnumConstants()) { + if (i.getTriggerPixel() == pixelValue) return i; + } + return type; + } +} diff --git a/src/main/java/com/minelittlepony/pony/data/TriggerPixels.java b/src/main/java/com/minelittlepony/pony/data/TriggerPixels.java new file mode 100644 index 00000000..83ecd9a2 --- /dev/null +++ b/src/main/java/com/minelittlepony/pony/data/TriggerPixels.java @@ -0,0 +1,35 @@ +package com.minelittlepony.pony.data; + +import java.awt.image.BufferedImage; + +import com.minelittlepony.PonyGender; +import com.minelittlepony.PonyRace; +import com.minelittlepony.PonySize; +import com.minelittlepony.TailLengths; + +public enum TriggerPixels { + RACE(PonyRace.HUMAN, 0, 0), + TAIL(TailLengths.FULL, 1, 0), + GENDER(PonyGender.MARE, 2, 0), + SIZE(PonySize.NORMAL, 3, 0), + GLOW(null, 0, 1); + + private int x, y; + + ITriggerPixelMapped def; + + TriggerPixels(ITriggerPixelMapped def, int x, int y) { + this.def = def; + this.x = x; + this.y = y; + } + + public int readColor(BufferedImage image, int mask) { + return image.getRGB(x, y) & mask; + } + + @SuppressWarnings("unchecked") + public & ITriggerPixelMapped> T readValue(BufferedImage image) { + return ITriggerPixelMapped.getByTriggerPixel((T)def, readColor(image, 0xffffff)); + } +} \ No newline at end of file