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-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;
|
|
|
|
import net.minecraft.client.gui.Gui;
|
2017-04-04 17:07:45 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
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.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.function.BiConsumer;
|
|
|
|
|
|
|
|
public class Slider extends PanelComponent {
|
|
|
|
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;
|
|
|
|
private Set<BiConsumer<Integer, Byte>> outputs = new HashSet<>();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
public Slider() {
|
|
|
|
super("slider");
|
|
|
|
}
|
|
|
|
@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;
|
|
|
|
float xSize = horizontal?length:WIDTH;
|
|
|
|
float ySize = horizontal?WIDTH:length;
|
|
|
|
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;
|
|
|
|
for (int i = 0;i<3;i++) {
|
|
|
|
color[i] = ((this.color>>(8*(2-i)))&255)/255F*(.5F+out/30F);
|
|
|
|
}
|
|
|
|
float val;
|
|
|
|
if (horizontal) {
|
|
|
|
val = (out/15F)*(length-.0625F);
|
|
|
|
} else {
|
|
|
|
val = (1-out/15F)*(length-.0625F);
|
|
|
|
}
|
2017-04-13 17:14:05 +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);
|
|
|
|
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() {
|
|
|
|
if (aabb==null) {
|
|
|
|
aabb = new AxisAlignedBB(x, 0, y, x+(horizontal?length:WIDTH), getHeight(), y+(horizontal?WIDTH:length));
|
|
|
|
}
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean interactWith(Vec3d hitRelative, TileEntityPanel tile) {
|
|
|
|
double pos = horizontal?hitRelative.xCoord:(length-hitRelative.zCoord);
|
|
|
|
byte newLevel = (byte)(Math.min(pos*16/length, 15));
|
|
|
|
if (newLevel!=out) {
|
|
|
|
for (BiConsumer<Integer, Byte> output:outputs) {
|
|
|
|
output.accept((int)rsChannel, newLevel);
|
|
|
|
}
|
|
|
|
out = newLevel;
|
|
|
|
tile.markDirty();
|
|
|
|
tile.triggerRenderUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void registerRSOutput(int id, @Nonnull BiConsumer<Integer, Byte> out) {
|
|
|
|
if (id==rsId) {
|
|
|
|
outputs.add(out);
|
|
|
|
out.accept((int)rsChannel, this.out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unregisterRSOutput(int id, @Nonnull BiConsumer<Integer, Byte> out) {
|
|
|
|
if (id==rsId) {
|
|
|
|
outputs.remove(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(TileEntityPanel tile) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public float getHeight() {
|
|
|
|
return .0625F/2;
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
@Override
|
|
|
|
public void renderInGUI(GuiPanelCreator gui) {
|
|
|
|
renderInGUIDefault(gui, GRAY_INT);
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
}
|