Comparable and Iterable colors

C\hanges to be committed:
	modified:   src/main/java/org/dimdev/dimdoors/client/DetachedRiftBlockEntityRenderer.java
	modified:   src/main/java/org/dimdev/dimdoors/util/RGBA.java
This commit is contained in:
SD 2020-10-04 10:51:15 +05:30
parent 93cfa599cc
commit c9e3979917
No known key found for this signature in database
GPG key ID: E36B57EE08544BC5
2 changed files with 80 additions and 26 deletions

View file

@ -1,5 +1,7 @@
package org.dimdev.dimdoors.client;
import java.util.Objects;
import org.dimdev.dimdoors.ModConfig;
import org.dimdev.dimdoors.block.entity.DetachedRiftBlockEntity;
import org.dimdev.dimdoors.client.tesseract.Tesseract;
@ -20,7 +22,7 @@ import net.fabricmc.api.Environment;
@Environment(EnvType.CLIENT)
public class DetachedRiftBlockEntityRenderer extends BlockEntityRenderer<DetachedRiftBlockEntity> {
public static final Identifier TESSERACT_PATH = new Identifier("dimdoors:textures/other/tesseract.png");
private final RGBA COLOR = new RGBA(1, 0.5f, 1, 1);
private final RGBA color = new RGBA(1, 0.5f, 1, 1);
private static final Tesseract TESSERACT = new Tesseract();
private static final RiftCurves.PolygonInfo CURVE = RiftCurves.CURVES.get(0);
@ -56,7 +58,9 @@ public class DetachedRiftBlockEntityRenderer extends BlockEntityRenderer<Detache
private void renderTesseract( VertexConsumer vc, DetachedRiftBlockEntity rift, MatrixStack matrices, float tickDelta) {
double radian = this.nextAngle(rift, tickDelta) * TrigMath.DEG_TO_RAD;
RGBA color = rift.getColor();
if (color == RGBA.NONE) color = this.COLOR;
if (Objects.equals(color, RGBA.NONE)) {
color = this.color;
}
matrices.push();

View file

@ -1,25 +1,26 @@
package org.dimdev.dimdoors.util;
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Consumer;
import org.jetbrains.annotations.NotNull;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
public class RGBA implements Cloneable {
public final class RGBA implements Cloneable, Comparable<RGBA>, Iterable<Float> {
public static final RGBA NONE = new RGBA(-1, -1, -1, -1);
public static Codec<RGBA> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.FLOAT.fieldOf("red").forGetter(RGBA::getRed),
Codec.FLOAT.fieldOf("green").forGetter(RGBA::getGreen),
Codec.FLOAT.fieldOf("blue").forGetter(RGBA::getBlue),
Codec.FLOAT.fieldOf("alpha").forGetter(RGBA::getAlpha)
).apply(instance, RGBA::new);
});
public static Codec<RGBA> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.FLOAT.fieldOf("red").forGetter(RGBA::getRed),
Codec.FLOAT.fieldOf("green").forGetter(RGBA::getGreen),
Codec.FLOAT.fieldOf("blue").forGetter(RGBA::getBlue),
Codec.FLOAT.fieldOf("alpha").forGetter(RGBA::getAlpha)
).apply(instance, RGBA::new));
float red;
float green;
float blue;
float alpha;
private final float red;
private final float green;
private final float blue;
private final float alpha;
public RGBA(float red, float green, float blue, float alpha) {
this.red = red;
@ -29,39 +30,51 @@ public class RGBA implements Cloneable {
}
public float getRed() {
return red;
return this.red;
}
public float getGreen() {
return green;
return this.green;
}
public float getBlue() {
return blue;
return this.blue;
}
public float getAlpha() {
return alpha;
return this.alpha;
}
public static RGBA fromFloatArray(float[] f) {
return new RGBA(f[0], f[1], f[2], f[3]);
}
public static float[] toFloatArray(RGBA o) {
return new float[]{o.red, o.blue, o.green, o.alpha};
}
public float[] toFloatArray() {
return new float[]{this.red, this.blue, this.green, this.alpha};
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
RGBA rgba = (RGBA) o;
return Float.compare(rgba.red, red) == 0 &&
Float.compare(rgba.green, green) == 0 &&
Float.compare(rgba.blue, blue) == 0 &&
Float.compare(rgba.alpha, alpha) == 0;
return Float.compare(rgba.red, this.red) == 0 &&
Float.compare(rgba.green, this.green) == 0 &&
Float.compare(rgba.blue, this.blue) == 0 &&
Float.compare(rgba.alpha, this.alpha) == 0;
}
@Override
public int hashCode() {
return Objects.hash(red, green, blue, alpha);
return Objects.hash(this.red, this.green, this.blue, this.alpha);
}
public static RGBA[] fromFloatArrays(float[][] f) {
@ -80,4 +93,41 @@ public class RGBA implements Cloneable {
throw new RuntimeException(e);
}
}
@NotNull
@Override
public Iterator<Float> iterator() {
return new Iterator<Float>() {
private final float[] rgba = RGBA.this.toFloatArray();
private int index = 0;
@Override
public boolean hasNext() {
return this.index < 4;
}
@Override
public Float next() {
return this.rgba[this.index++];
}
};
}
@Override
public void forEach(Consumer<? super Float> action) {
for (Float e : this.toFloatArray()) {
action.accept(e);
}
}
@Override
public int compareTo(@NotNull RGBA o) {
if (this.equals(o)) {
return 0;
}
return Float.compare(this.alpha, o.alpha) +
Float.compare(this.red, o.red) +
Float.compare(this.green, o.green) +
Float.compare(this.blue, o.blue);
}
}