2017-04-10 16:20:39 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
|
|
|
* Copyright (C) 2016-2017 malte0811
|
|
|
|
*
|
|
|
|
* Industrial Wires is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Industrial Wires is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
package malte0811.industrialWires.controlpanel;
|
2017-04-04 17:07:45 +02:00
|
|
|
|
2017-04-22 18:14:49 +02:00
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
2017-04-13 17:14:05 +02:00
|
|
|
import malte0811.industrialWires.blocks.controlpanel.TileEntityPanel;
|
2017-04-04 17:07:45 +02:00
|
|
|
import malte0811.industrialWires.client.RawQuad;
|
2017-04-13 17:14:05 +02:00
|
|
|
import malte0811.industrialWires.client.gui.GuiPanelCreator;
|
2017-06-17 17:22:44 +02:00
|
|
|
import malte0811.industrialWires.util.TriConsumer;
|
2017-04-13 17:14:05 +02:00
|
|
|
import net.minecraft.client.gui.Gui;
|
2017-04-22 18:14:49 +02:00
|
|
|
import net.minecraft.client.resources.I18n;
|
2017-05-08 17:20:04 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2017-04-22 18:14:49 +02:00
|
|
|
import net.minecraft.nbt.*;
|
2017-04-04 17:07:45 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
|
|
|
import org.lwjgl.util.vector.Vector3f;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-04-22 18:14:49 +02:00
|
|
|
public class Slider extends PanelComponent implements IConfigurableComponent {
|
2017-04-04 17:07:45 +02:00
|
|
|
private static final float WIDTH = .0625F;
|
2017-04-13 17:14:05 +02:00
|
|
|
private float length = .5F;
|
|
|
|
private int color = 0xffff00;
|
2017-04-04 17:07:45 +02:00
|
|
|
private boolean horizontal;
|
|
|
|
private byte out;
|
|
|
|
private byte rsChannel;
|
|
|
|
private int rsId;
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-04-04 17:07:45 +02:00
|
|
|
public Slider(float length, int color, boolean horizontal, int rsId, byte rsChannel) {
|
|
|
|
this();
|
|
|
|
this.color = color;
|
|
|
|
this.length = length;
|
|
|
|
this.horizontal = horizontal;
|
|
|
|
this.rsChannel = rsChannel;
|
|
|
|
this.rsId = rsId;
|
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-04-04 17:07:45 +02:00
|
|
|
public Slider() {
|
|
|
|
super("slider");
|
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-04-04 17:07:45 +02:00
|
|
|
@Override
|
2017-04-09 16:59:57 +02:00
|
|
|
protected void writeCustomNBT(NBTTagCompound nbt, boolean toItem) {
|
2017-04-13 17:14:05 +02:00
|
|
|
nbt.setInteger(COLOR, color);
|
|
|
|
nbt.setFloat(LENGTH, length);
|
2017-04-09 16:59:57 +02:00
|
|
|
if (!toItem) {
|
|
|
|
nbt.setByte("output", out);
|
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
nbt.setByte(RS_CHANNEL, rsChannel);
|
|
|
|
nbt.setInteger(RS_ID, rsId);
|
|
|
|
nbt.setBoolean(HORIZONTAL, horizontal);
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void readCustomNBT(NBTTagCompound nbt) {
|
2017-04-13 17:14:05 +02:00
|
|
|
color = nbt.getInteger(COLOR);
|
|
|
|
length = nbt.getFloat(LENGTH);
|
2017-04-04 17:07:45 +02:00
|
|
|
out = nbt.getByte("output");
|
2017-04-13 17:14:05 +02:00
|
|
|
rsChannel = nbt.getByte(RS_CHANNEL);
|
|
|
|
rsId = nbt.getInteger(RS_ID);
|
|
|
|
horizontal = nbt.getBoolean(HORIZONTAL);
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<RawQuad> getQuads() {
|
|
|
|
List<RawQuad> ret = new ArrayList<>();
|
|
|
|
final float yOff = .001F;
|
2017-05-11 16:39:20 +02:00
|
|
|
float xSize = horizontal ? length : WIDTH;
|
|
|
|
float ySize = horizontal ? WIDTH : length;
|
2017-04-04 17:07:45 +02:00
|
|
|
PanelUtils.addColoredQuad(ret, new Vector3f(0, yOff, 0), new Vector3f(0, yOff, ySize), new Vector3f(xSize, yOff, ySize), new Vector3f(xSize, yOff, 0),
|
2017-04-13 17:14:05 +02:00
|
|
|
EnumFacing.UP, GRAY);
|
2017-04-04 17:07:45 +02:00
|
|
|
float[] color = new float[4];
|
|
|
|
color[3] = 1;
|
2017-05-11 16:39:20 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
color[i] = ((this.color >> (8 * (2 - i))) & 255) / 255F * (.5F + out / 30F);
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
|
|
|
float val;
|
|
|
|
if (horizontal) {
|
2017-05-11 16:39:20 +02:00
|
|
|
val = (out / 15F) * (length - .0625F);
|
2017-04-04 17:07:45 +02:00
|
|
|
} else {
|
2017-05-11 16:39:20 +02:00
|
|
|
val = (1 - out / 15F) * (length - .0625F);
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
PanelUtils.addColoredBox(color, GRAY, null, new Vector3f(horizontal ? val : 0, 0, horizontal ? 0 : val),
|
2017-04-04 17:07:45 +02:00
|
|
|
new Vector3f(.0625F, getHeight(), .0625F), ret, false);
|
2017-05-11 16:39:20 +02:00
|
|
|
if (out>0) {
|
|
|
|
ret.get(1).light = 0xff0ff;
|
|
|
|
}
|
2017-04-04 17:07:45 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public PanelComponent copyOf() {
|
|
|
|
Slider ret = new Slider(length, color, horizontal, rsId, rsChannel);
|
|
|
|
ret.out = out;
|
|
|
|
ret.setX(x);
|
|
|
|
ret.setY(y);
|
|
|
|
ret.panelHeight = panelHeight;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:55:37 +02:00
|
|
|
@Nonnull
|
2017-04-04 17:07:45 +02:00
|
|
|
@Override
|
|
|
|
public AxisAlignedBB getBlockRelativeAABB() {
|
2017-05-11 16:39:20 +02:00
|
|
|
if (aabb == null) {
|
|
|
|
aabb = new AxisAlignedBB(x, 0, y, x + (horizontal ? length : WIDTH), getHeight(), y + (horizontal ? WIDTH : length));
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-10 17:56:05 +02:00
|
|
|
public void interactWith(Vec3d hitRelative, TileEntityPanel tile, EntityPlayerMP player) {
|
2017-06-25 18:34:10 +02:00
|
|
|
double pos = horizontal ? hitRelative.x : (length - hitRelative.z);
|
2017-05-11 16:39:20 +02:00
|
|
|
byte newLevel = (byte) (Math.min(pos * 16 / length, 15));
|
|
|
|
if (newLevel != out) {
|
2017-06-17 17:22:44 +02:00
|
|
|
setOut(rsChannel, newLevel);
|
2017-04-04 17:07:45 +02:00
|
|
|
out = newLevel;
|
|
|
|
tile.markDirty();
|
|
|
|
tile.triggerRenderUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-06-17 17:22:44 +02:00
|
|
|
public void registerRSOutput(int id, @Nonnull TriConsumer<Integer, Byte, PanelComponent> out) {
|
2017-07-13 09:22:20 +02:00
|
|
|
if (matchesId(rsId, id)) {
|
2017-06-17 17:22:44 +02:00
|
|
|
super.registerRSOutput(id, out);
|
|
|
|
out.accept((int) rsChannel, this.out, this);
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(TileEntityPanel tile) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public float getHeight() {
|
2017-05-11 16:39:20 +02:00
|
|
|
return .0625F / 2;
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
@Override
|
|
|
|
public void renderInGUI(GuiPanelCreator gui) {
|
|
|
|
renderInGUIDefault(gui, GRAY_INT);
|
2017-05-11 16:39:20 +02:00
|
|
|
double middleX = (getX() + (horizontal ? length : .0625) / 2);
|
|
|
|
double middleY = (getY() + (horizontal ? .0625 : length) / 2);
|
|
|
|
int left = gui.getX0() + (int) ((middleX - .0625 / 2) * gui.panelSize);
|
|
|
|
int right = gui.getX0() + (int) ((middleX + .0625 / 2) * gui.panelSize);
|
|
|
|
int top = gui.getY0() + (int) ((middleY - .0625 / 2) * gui.panelSize);
|
|
|
|
int bottom = gui.getY0() + (int) ((middleY + .0625 / 2) * gui.panelSize);
|
|
|
|
Gui.drawRect(left, top, right, bottom, 0xff000000 | color);
|
2017-04-13 17:14:05 +02:00
|
|
|
}
|
|
|
|
|
2017-04-20 20:44:08 +02:00
|
|
|
@Override
|
|
|
|
public void invalidate(TileEntityPanel te) {
|
2017-06-17 17:22:44 +02:00
|
|
|
setOut(rsChannel, 0);
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 17:07:45 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
if (!super.equals(o)) return false;
|
|
|
|
|
|
|
|
Slider slider = (Slider) o;
|
|
|
|
|
|
|
|
if (Float.compare(slider.length, length) != 0) return false;
|
|
|
|
if (color != slider.color) return false;
|
|
|
|
if (horizontal != slider.horizontal) return false;
|
|
|
|
if (out != slider.out) return false;
|
|
|
|
if (rsChannel != slider.rsChannel) return false;
|
|
|
|
return rsId == slider.rsId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int result = super.hashCode();
|
|
|
|
result = 31 * result + (length != +0.0f ? Float.floatToIntBits(length) : 0);
|
|
|
|
result = 31 * result + color;
|
|
|
|
result = 31 * result + (horizontal ? 1 : 0);
|
|
|
|
result = 31 * result + (int) out;
|
|
|
|
result = 31 * result + (int) rsChannel;
|
|
|
|
result = 31 * result + rsId;
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-22 18:14:49 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void applyConfigOption(ConfigType type, int id, NBTBase value) {
|
|
|
|
switch (type) {
|
|
|
|
case BOOL:
|
2017-05-11 16:39:20 +02:00
|
|
|
horizontal = ((NBTTagByte) value).getByte() != 0;
|
2017-04-22 18:14:49 +02:00
|
|
|
break;
|
|
|
|
case RS_CHANNEL:
|
2017-05-11 16:39:20 +02:00
|
|
|
rsChannel = ((NBTTagByte) value).getByte();
|
2017-04-22 18:14:49 +02:00
|
|
|
break;
|
|
|
|
case INT:
|
2017-05-11 16:39:20 +02:00
|
|
|
rsId = ((NBTTagInt) value).getInt();
|
2017-04-22 18:14:49 +02:00
|
|
|
break;
|
|
|
|
case FLOAT:
|
2017-05-11 16:39:20 +02:00
|
|
|
if (id < 3) {
|
2017-04-22 18:14:49 +02:00
|
|
|
color = PanelUtils.setColor(color, id, value);
|
|
|
|
} else {
|
2017-05-11 16:39:20 +02:00
|
|
|
length = scaleToRangePercent(((NBTTagFloat) value).getFloat(), .125F, 1);
|
2017-04-22 18:14:49 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:23:10 +02:00
|
|
|
private float scaleToRangePercent(float x, float min, float max) {
|
|
|
|
return min + x / 100F * (max - min);
|
|
|
|
}
|
|
|
|
|
2017-04-22 18:14:49 +02:00
|
|
|
@Override
|
|
|
|
public String fomatConfigName(ConfigType type, int id) {
|
|
|
|
switch (type) {
|
|
|
|
case BOOL:
|
2017-05-11 16:39:20 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID + ".tooltip.horizontal");
|
2017-04-22 18:14:49 +02:00
|
|
|
case RS_CHANNEL:
|
|
|
|
case INT:
|
|
|
|
return null;
|
|
|
|
case FLOAT:
|
2017-05-11 16:39:20 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc." + (id == 0 ? "red" : (id == 1 ? "green" : id == 2 ? "blue" : "length")));
|
2017-04-22 18:14:49 +02:00
|
|
|
default:
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fomatConfigDescription(ConfigType type, int id) {
|
|
|
|
switch (type) {
|
|
|
|
case BOOL:
|
|
|
|
return null;
|
|
|
|
case RS_CHANNEL:
|
2017-05-11 16:39:20 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.rschannel_info");
|
2017-04-22 18:14:49 +02:00
|
|
|
case INT:
|
2017-05-11 16:39:20 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.rsid_info");
|
2017-04-22 18:14:49 +02:00
|
|
|
case FLOAT:
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
return "INVALID?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RSChannelConfig[] getRSChannelOptions() {
|
2017-05-11 16:39:20 +02:00
|
|
|
return new RSChannelConfig[]{
|
2017-04-22 18:14:49 +02:00
|
|
|
new RSChannelConfig("channel", 0, 0, rsChannel)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IntConfig[] getIntegerOptions() {
|
2017-05-11 16:39:20 +02:00
|
|
|
return new IntConfig[]{
|
2017-05-01 18:40:29 +02:00
|
|
|
new IntConfig("rsId", 0, 50, rsId, 2, false)
|
2017-04-22 18:14:49 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BoolConfig[] getBooleanOptions() {
|
2017-05-11 16:39:20 +02:00
|
|
|
return new BoolConfig[]{
|
2017-04-22 18:14:49 +02:00
|
|
|
new BoolConfig("horizontal", 0, 70, horizontal)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FloatConfig[] getFloatOptions() {
|
|
|
|
float[] color = PanelUtils.getFloatColor(true, this.color);
|
|
|
|
int x = 70;
|
|
|
|
int yOffset = 10;
|
|
|
|
return new FloatConfig[]{
|
2017-05-11 16:39:20 +02:00
|
|
|
new FloatConfig("red", x, yOffset + 20, color[0], 60),
|
|
|
|
new FloatConfig("green", x, yOffset + 40, color[1], 60),
|
|
|
|
new FloatConfig("blue", x, yOffset + 60, color[2], 60),
|
|
|
|
new FloatConfig("length", x, yOffset, (length - .125F) / (1 - .125F), 60)
|
2017-04-22 18:14:49 +02:00
|
|
|
};
|
|
|
|
}
|
2017-04-27 18:15:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getColor() {
|
|
|
|
return color;
|
|
|
|
}
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|