2017-05-03 18:08:52 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
2018-02-28 21:06:33 +01:00
|
|
|
* Copyright (C) 2016-2018 malte0811
|
2017-05-03 18:08:52 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package malte0811.industrialWires.controlpanel;
|
|
|
|
|
|
|
|
import blusunrize.immersiveengineering.common.util.chickenbones.Matrix4;
|
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
|
|
|
import malte0811.industrialWires.client.RawQuad;
|
|
|
|
import malte0811.industrialWires.client.gui.GuiPanelCreator;
|
2018-07-30 18:41:52 +02:00
|
|
|
import malte0811.industrialWires.controlpanel.ControlPanelNetwork.RSChannel;
|
2018-07-30 21:51:58 +02:00
|
|
|
import malte0811.industrialWires.controlpanel.ControlPanelNetwork.RSChannelState;
|
2017-05-03 18:08:52 +02:00
|
|
|
import net.minecraft.client.gui.Gui;
|
|
|
|
import net.minecraft.client.renderer.GlStateManager;
|
|
|
|
import net.minecraft.client.resources.I18n;
|
2017-05-08 17:20:04 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2017-05-03 18:08:52 +02:00
|
|
|
import net.minecraft.nbt.NBTBase;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
2017-10-31 16:29:38 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2017-05-03 18:08:52 +02:00
|
|
|
import org.lwjgl.util.vector.Vector3f;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-10-04 17:05:04 +02:00
|
|
|
import static malte0811.industrialWires.util.NBTKeys.*;
|
|
|
|
|
2017-05-03 18:08:52 +02:00
|
|
|
public class Variac extends PanelComponent implements IConfigurableComponent {
|
|
|
|
private static final float SIZE = 3 / 16F;
|
|
|
|
private static final float innerSize = (float) (Math.sqrt(2) / 2 * SIZE);
|
|
|
|
private static final float offset = (SIZE - innerSize) / 2;
|
|
|
|
private static final float[] darkGray = {.2F, .2F, .2F, 1};
|
|
|
|
private static final float[] white = {1, 1, 1, 1};
|
|
|
|
private static final float rodDia = .0625F / 2;
|
|
|
|
private static final float rodOffset = (SIZE - rodDia) / 2;
|
|
|
|
private static final float arrowSize = .0625F / 2;
|
|
|
|
|
2017-08-18 22:41:33 +02:00
|
|
|
private int out;
|
2018-07-30 18:41:52 +02:00
|
|
|
@Nonnull
|
2018-07-30 21:51:58 +02:00
|
|
|
private RSChannel primary = RSChannel.DEFAULT_CHANNEL;
|
2018-07-30 18:41:52 +02:00
|
|
|
@Nonnull
|
|
|
|
private RSChannel secondary = RSChannel.INVALID_CHANNEL;
|
2017-05-03 18:08:52 +02:00
|
|
|
|
2018-07-30 18:41:52 +02:00
|
|
|
public Variac(@Nonnull RSChannel primary, @Nonnull RSChannel secondary) {
|
2017-05-03 18:08:52 +02:00
|
|
|
this();
|
2018-07-30 18:41:52 +02:00
|
|
|
this.primary = primary;
|
|
|
|
this.secondary = secondary;
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Variac() {
|
|
|
|
super("variac");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void writeCustomNBT(NBTTagCompound nbt, boolean toItem) {
|
|
|
|
if (!toItem) {
|
2017-08-18 22:41:33 +02:00
|
|
|
nbt.setInteger("output", out);
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
2018-07-30 18:41:52 +02:00
|
|
|
nbt.setInteger(RS_ID, primary.getController());
|
|
|
|
nbt.setByte(RS_CHANNEL, primary.getColor());
|
|
|
|
nbt.setInteger(RS_ID2, secondary.getController());
|
|
|
|
nbt.setByte(RS_CHANNEL2, secondary.getColor());
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void readCustomNBT(NBTTagCompound nbt) {
|
2017-08-18 22:41:33 +02:00
|
|
|
out = nbt.getInteger("output");
|
2018-07-30 18:41:52 +02:00
|
|
|
int rsController = nbt.getInteger(RS_ID);
|
|
|
|
byte rsColor = nbt.getByte(RS_CHANNEL);
|
|
|
|
primary = new RSChannel(rsController, rsColor);
|
|
|
|
if (nbt.hasKey(RS_ID2)) {
|
|
|
|
rsController = nbt.getInteger(RS_ID2);
|
|
|
|
rsColor = nbt.getByte(RS_CHANNEL2);
|
|
|
|
secondary = new RSChannel(rsController, rsColor);
|
|
|
|
} else {
|
|
|
|
secondary = RSChannel.INVALID_CHANNEL;
|
2017-10-27 21:08:17 +02:00
|
|
|
}
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-10-31 16:29:38 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2017-05-03 18:08:52 +02:00
|
|
|
public List<RawQuad> getQuads() {
|
|
|
|
List<RawQuad> ret = new ArrayList<>();
|
2017-08-18 22:41:33 +02:00
|
|
|
float angle = -(float) (2 * Math.PI * (8.5 + out) / (17F*16F));
|
2017-05-03 18:08:52 +02:00
|
|
|
Matrix4 mat = new Matrix4();
|
|
|
|
mat.translate(SIZE / 2, 0, SIZE / 2);
|
|
|
|
mat.rotate(angle, 0, 1, 0);
|
|
|
|
mat.translate(-SIZE / 2, 0, -SIZE / 2);
|
|
|
|
PanelUtils.addColoredBox(darkGray, darkGray, null, new Vector3f(offset, getHeight() / 2, offset),
|
|
|
|
new Vector3f(innerSize, getHeight() / 2, innerSize), ret, false, mat);
|
|
|
|
PanelUtils.addColoredBox(GRAY, GRAY, null, new Vector3f(rodOffset, 0, rodOffset),
|
|
|
|
new Vector3f(rodDia, getHeight() / 2, rodDia), ret, false, mat);
|
|
|
|
mat.translate(SIZE / 2, 0, SIZE / 2);
|
|
|
|
mat.rotate(Math.PI / 4, 0, 1, 0);
|
|
|
|
mat.translate(-SIZE / 2, 0, -SIZE / 2);
|
|
|
|
PanelUtils.addColoredBox(darkGray, darkGray, null, new Vector3f(offset, getHeight() / 2, offset),
|
|
|
|
new Vector3f(innerSize, getHeight() / 2, innerSize), ret, false, mat);
|
|
|
|
mat.translate(SIZE / 2, 0, SIZE / 2);
|
|
|
|
mat.rotate(Math.PI / 2, 0, 1, 0);
|
2017-10-31 16:29:38 +01:00
|
|
|
mat.translate(-SIZE / 2, Y_DELTA, -SIZE / 2);
|
2017-06-17 17:50:16 +02:00
|
|
|
PanelUtils.addColoredQuad(ret, new Vector3f(offset, getHeight(), offset), new Vector3f(offset, getHeight(), offset),
|
|
|
|
new Vector3f(offset + arrowSize / 2, getHeight(), offset + arrowSize),
|
|
|
|
new Vector3f(offset + arrowSize, getHeight(), offset + arrowSize / 2), EnumFacing.UP, white, mat);
|
2017-05-03 18:08:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public PanelComponent copyOf() {
|
2018-07-30 18:41:52 +02:00
|
|
|
Variac ret = new Variac(primary, secondary);
|
2017-05-03 18:08:52 +02:00
|
|
|
ret.out = out;
|
|
|
|
ret.setX(x);
|
|
|
|
ret.setY(y);
|
|
|
|
ret.panelHeight = panelHeight;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public AxisAlignedBB getBlockRelativeAABB() {
|
|
|
|
if (aabb == null) {
|
|
|
|
aabb = new AxisAlignedBB(x, 0, y, x + SIZE, getHeight(), y + SIZE);
|
|
|
|
}
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-30 18:41:52 +02:00
|
|
|
public void interactWith(Vec3d hitRelative, EntityPlayerMP player) {
|
2017-06-25 18:34:10 +02:00
|
|
|
double xRel = hitRelative.x - SIZE / 2;
|
|
|
|
double yRel = -(hitRelative.z - SIZE / 2);
|
2017-05-03 18:08:52 +02:00
|
|
|
double angle = 1.5 * Math.PI - Math.atan2(yRel, xRel);
|
|
|
|
if (angle < 0) {
|
|
|
|
angle += 2 * Math.PI;
|
|
|
|
} else if (angle > 2 * Math.PI) {
|
|
|
|
angle -= 2 * Math.PI;
|
|
|
|
}
|
|
|
|
angle /= 2 * Math.PI;
|
2018-07-30 18:41:52 +02:00
|
|
|
int step = (secondary.isValid()&&player.isSneaking())?1:16;
|
2017-08-18 22:41:33 +02:00
|
|
|
int newLevel = (int) ((angle-1/34F) * 17 * 16);
|
|
|
|
int diff = Math.abs(newLevel-out);
|
|
|
|
if (diff>step) {
|
|
|
|
if (newLevel > out) {
|
|
|
|
newLevel = out + step;
|
|
|
|
} else if (newLevel < out) {
|
|
|
|
newLevel = out - step;
|
|
|
|
}
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
2017-08-18 22:41:33 +02:00
|
|
|
newLevel = Math.max(0, Math.min(newLevel, 255));
|
2017-05-03 18:08:52 +02:00
|
|
|
if (newLevel != out) {
|
2017-08-18 22:41:33 +02:00
|
|
|
setOut(newLevel);
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 22:41:33 +02:00
|
|
|
@Override
|
2018-07-30 18:41:52 +02:00
|
|
|
public void update() {
|
2017-05-03 18:08:52 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-30 21:51:58 +02:00
|
|
|
@Override
|
|
|
|
public void setNetwork(ControlPanelNetwork net) {
|
|
|
|
super.setNetwork(net);
|
|
|
|
network.setOutputs(this, new RSChannelState(primary, (byte) (out>>4)));
|
|
|
|
network.setOutputs(this, new RSChannelState(secondary, (byte) (out&0xf)));
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:08:52 +02:00
|
|
|
@Override
|
|
|
|
public float getHeight() {
|
|
|
|
return .0625F;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-10-31 16:29:38 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2017-05-03 18:08:52 +02:00
|
|
|
public void renderInGUI(GuiPanelCreator gui) {
|
2017-06-30 21:04:02 +02:00
|
|
|
AxisAlignedBB aabb = getBlockRelativeAABB();
|
2017-05-03 18:08:52 +02:00
|
|
|
int left = (int) Math.ceil(gui.getX0() + (offset + aabb.minX) * gui.panelSize);
|
|
|
|
int top = (int) Math.ceil(gui.getY0() + (offset + aabb.minZ) * gui.panelSize);
|
|
|
|
int right = (int) Math.floor(gui.getX0() + (aabb.maxX - offset) * gui.panelSize);
|
|
|
|
int bottom = (int) Math.floor(gui.getY0() + (aabb.maxZ - offset) * gui.panelSize);
|
|
|
|
|
|
|
|
GlStateManager.pushMatrix();
|
|
|
|
GlStateManager.translate((left + right) / 2, (top + bottom) / 2, 0);
|
|
|
|
GlStateManager.rotate(360 / 17F, 0, 0, 1);
|
|
|
|
GlStateManager.translate(-(left + right) / 2, -(top + bottom) / 2, 0);
|
|
|
|
Gui.drawRect(left, top, right, bottom, 0xff333333);
|
|
|
|
GlStateManager.translate((left + right) / 2, (top + bottom) / 2, 0);
|
|
|
|
GlStateManager.rotate(45, 0, 0, 1);
|
|
|
|
GlStateManager.translate(-(left + right) / 2, -(top + bottom) / 2, 0);
|
|
|
|
Gui.drawRect(left, top, right, bottom, 0xff333333);
|
|
|
|
GlStateManager.popMatrix();
|
|
|
|
}
|
|
|
|
|
2018-07-30 18:41:52 +02:00
|
|
|
public void setOut(int value) {
|
2018-07-30 21:51:58 +02:00
|
|
|
network.setOutputs(this, new RSChannelState(primary, (byte) (value>>4)));
|
|
|
|
network.setOutputs(this, new RSChannelState(secondary, (byte) (value&0xf)));
|
|
|
|
out = value;
|
|
|
|
panel.markDirty();
|
|
|
|
panel.triggerRenderUpdate();
|
2017-05-03 18:08:52 +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;
|
|
|
|
|
|
|
|
Variac variac = (Variac) o;
|
|
|
|
|
|
|
|
if (out != variac.out) return false;
|
2018-07-30 18:41:52 +02:00
|
|
|
if (!primary.equals(variac.primary)) return false;
|
|
|
|
return secondary.equals(variac.secondary);
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int result = super.hashCode();
|
2017-08-18 22:41:33 +02:00
|
|
|
result = 31 * result + out;
|
2018-07-30 18:41:52 +02:00
|
|
|
result = 31 * result + primary.hashCode();
|
|
|
|
result = 31 * result + secondary.hashCode();
|
2017-05-03 18:08:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void applyConfigOption(IConfigurableComponent.ConfigType type, int id, NBTBase value) {
|
|
|
|
switch (type) {
|
2018-07-30 18:41:52 +02:00
|
|
|
case RS_CHANNEL:
|
|
|
|
if (id == 0) {
|
|
|
|
primary = primary.withColor(value);
|
|
|
|
} else {
|
|
|
|
secondary = secondary.withColor(value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INT:
|
|
|
|
if (id == 0) {
|
|
|
|
primary = primary.withController(value);
|
|
|
|
} else {
|
|
|
|
secondary = secondary.withController(value);
|
|
|
|
}
|
|
|
|
break;
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fomatConfigName(IConfigurableComponent.ConfigType type, int id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-30 18:41:52 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
2017-05-03 18:08:52 +02:00
|
|
|
public String fomatConfigDescription(IConfigurableComponent.ConfigType type, int id) {
|
|
|
|
switch (type) {
|
2017-08-18 22:41:33 +02:00
|
|
|
case RS_CHANNEL:
|
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.rschannel_info" + (id == 1 ? "2" : ""));
|
|
|
|
case INT:
|
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.rsid_info" + (id == 1 ? "2" : ""));
|
|
|
|
default:
|
|
|
|
return "INVALID?";
|
2017-05-03 18:08:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-30 18:41:52 +02:00
|
|
|
public RSColorConfig[] getRSChannelOptions() {
|
|
|
|
return new RSColorConfig[]{
|
2018-07-30 21:51:58 +02:00
|
|
|
new RSColorConfig("channel", 0, 0, primary.getColor()),
|
|
|
|
new RSColorConfig("channel2", 90, 0, secondary.getColor())
|
2017-05-03 18:08:52 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-30 18:41:52 +02:00
|
|
|
public IntConfig[] getIntegerOptions() {
|
|
|
|
return new IntConfig[]{
|
2018-07-30 21:51:58 +02:00
|
|
|
new IntConfig("rsId", 0, 50, primary.getController(), 2, false),
|
|
|
|
new IntConfig("rsId2", 90, 50, secondary.getController(), 2, true)
|
2017-05-03 18:08:52 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getColor() {
|
|
|
|
return 0xffffff;
|
|
|
|
}
|
|
|
|
}
|