2017-03-24 17:40:05 +01: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-03-24 17:40:05 +01: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-03-24 17:40:05 +01: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-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.NBTBase;
|
|
|
|
import net.minecraft.nbt.NBTTagByte;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagInt;
|
2017-03-24 17:40:05 +01:00
|
|
|
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-20 20:44:08 +02:00
|
|
|
public class LightedButton extends PanelComponent implements IConfigurableComponent {
|
2017-04-13 17:14:05 +02:00
|
|
|
public int color = 0xFF0000;
|
2017-03-24 17:40:05 +01:00
|
|
|
public boolean active;
|
|
|
|
public boolean latching;
|
2017-03-28 18:15:41 +02:00
|
|
|
public int rsOutputId;
|
|
|
|
public int rsOutputChannel;
|
2017-03-24 17:40:05 +01:00
|
|
|
private int ticksTillOff;
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-03-24 17:40:05 +01:00
|
|
|
public LightedButton() {
|
2017-04-13 17:14:05 +02:00
|
|
|
super("lighted_button");
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-03-28 18:15:41 +02:00
|
|
|
public LightedButton(int color, boolean active, boolean latching, int rsOutputId, int rsOutputChannel) {
|
2017-03-24 17:40:05 +01:00
|
|
|
this();
|
|
|
|
this.color = color;
|
|
|
|
this.active = active;
|
|
|
|
this.latching = latching;
|
2017-03-28 18:15:41 +02:00
|
|
|
this.rsOutputChannel = rsOutputChannel;
|
|
|
|
this.rsOutputId = rsOutputId;
|
2017-03-24 17:40:05 +01: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);
|
2017-04-09 16:59:57 +02:00
|
|
|
if (!toItem) {
|
2017-05-18 18:13:08 +02:00
|
|
|
nbt.setInteger("timeout", ticksTillOff);
|
2017-04-09 16:59:57 +02:00
|
|
|
nbt.setBoolean("active", active);
|
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
nbt.setBoolean(LATCHING, latching);
|
|
|
|
nbt.setInteger(RS_CHANNEL, rsOutputChannel);
|
|
|
|
nbt.setInteger(RS_ID, rsOutputId);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void readCustomNBT(NBTTagCompound nbt) {
|
2017-04-13 17:14:05 +02:00
|
|
|
color = nbt.getInteger(COLOR);
|
2017-03-24 17:40:05 +01:00
|
|
|
ticksTillOff = nbt.getInteger("timeout");
|
|
|
|
active = nbt.getBoolean("active");
|
2017-04-13 17:14:05 +02:00
|
|
|
latching = nbt.getBoolean(LATCHING);
|
|
|
|
rsOutputChannel = nbt.getInteger(RS_CHANNEL);
|
|
|
|
rsOutputId = nbt.getInteger(RS_ID);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-03-24 17:40:05 +01:00
|
|
|
private final static float size = .0625F;
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-03-24 17:40:05 +01:00
|
|
|
@Override
|
|
|
|
public List<RawQuad> getQuads() {
|
2017-04-22 18:14:49 +02:00
|
|
|
float[] color = PanelUtils.getFloatColor(active, this.color);
|
2017-03-24 17:40:05 +01:00
|
|
|
List<RawQuad> ret = new ArrayList<>(5);
|
2017-05-11 16:39:20 +02:00
|
|
|
PanelUtils.addColoredBox(color, GRAY, null, new Vector3f(0, 0, 0), new Vector3f(size, size / 2, size), ret, false);
|
|
|
|
if (active) {
|
|
|
|
ret.get(0).light = 0xff0ff;
|
|
|
|
}
|
|
|
|
|
2017-03-24 17:40:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nonnull
|
|
|
|
public PanelComponent copyOf() {
|
2017-03-28 18:15:41 +02:00
|
|
|
LightedButton ret = new LightedButton(color, active, latching, rsOutputId, rsOutputChannel);
|
2017-03-24 17:40:05 +01:00
|
|
|
ret.setX(x);
|
|
|
|
ret.setY(y);
|
2017-04-01 17:33:58 +02:00
|
|
|
ret.panelHeight = panelHeight;
|
2017-03-24 17:40:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:55:37 +02:00
|
|
|
@Nonnull
|
2017-03-24 17:40:05 +01:00
|
|
|
@Override
|
|
|
|
public AxisAlignedBB getBlockRelativeAABB() {
|
2017-05-11 16:39:20 +02:00
|
|
|
if (aabb == null) {
|
|
|
|
aabb = new AxisAlignedBB(x, 0, y, x + size, getHeight(), y + size);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-10 17:56:05 +02:00
|
|
|
public void interactWith(Vec3d hitRel, TileEntityPanel tile, EntityPlayerMP player) {
|
2017-05-11 16:39:20 +02:00
|
|
|
if (!latching && active) {
|
2017-03-24 17:40:05 +01:00
|
|
|
ticksTillOff = 10;
|
2017-07-13 17:14:37 +02:00
|
|
|
} else {
|
|
|
|
setOut(!active, tile);
|
|
|
|
if (!latching) {
|
|
|
|
ticksTillOff = 10;
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
tile.triggerRenderUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(TileEntityPanel tile) {
|
2017-05-11 16:39:20 +02:00
|
|
|
if (!latching && ticksTillOff > 0) {
|
2017-03-24 17:40:05 +01:00
|
|
|
ticksTillOff--;
|
|
|
|
tile.markDirty();
|
2017-05-11 16:39:20 +02:00
|
|
|
if (ticksTillOff == 0) {
|
2017-03-28 18:15:41 +02:00
|
|
|
setOut(false, tile);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 17:33:58 +02:00
|
|
|
@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(rsOutputId, id)) {
|
2017-06-17 17:22:44 +02:00
|
|
|
super.registerRSOutput(id, out);
|
|
|
|
out.accept(rsOutputChannel, (byte) (active ? 15 : 0), this);
|
2017-04-01 17:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:07:45 +02:00
|
|
|
@Override
|
|
|
|
public float getHeight() {
|
2017-05-11 16:39:20 +02:00
|
|
|
return size / 2;
|
2017-04-04 17:07:45 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
@Override
|
|
|
|
public void renderInGUI(GuiPanelCreator gui) {
|
2017-05-11 16:39:20 +02:00
|
|
|
renderInGUIDefault(gui, 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-08-10 21:58:05 +02:00
|
|
|
setOut(rsOutputChannel, 0);
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
|
2017-03-28 18:15:41 +02:00
|
|
|
private void setOut(boolean on, TileEntityPanel tile) {
|
|
|
|
active = on;
|
|
|
|
tile.triggerRenderUpdate();
|
2017-06-17 17:22:44 +02:00
|
|
|
setOut(rsOutputChannel, active ? 15 : 0);
|
2017-03-28 18:15:41 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 17:40:05 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
2017-03-28 18:15:41 +02:00
|
|
|
if (!super.equals(o)) return false;
|
2017-03-24 17:40:05 +01:00
|
|
|
|
|
|
|
LightedButton that = (LightedButton) o;
|
|
|
|
|
|
|
|
if (color != that.color) return false;
|
|
|
|
if (active != that.active) return false;
|
|
|
|
return latching == that.latching;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2017-03-28 18:15:41 +02:00
|
|
|
int result = super.hashCode();
|
|
|
|
result = 31 * result + color;
|
2017-03-24 17:40:05 +01:00
|
|
|
result = 31 * result + (active ? 1 : 0);
|
|
|
|
result = 31 * result + (latching ? 1 : 0);
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-20 20:44:08 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void applyConfigOption(ConfigType type, int id, NBTBase value) {
|
|
|
|
switch (type) {
|
2017-05-11 16:39:20 +02:00
|
|
|
case BOOL:
|
|
|
|
if (id == 0) {
|
|
|
|
latching = ((NBTTagByte) value).getByte() != 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RS_CHANNEL:
|
|
|
|
if (id == 0) {
|
|
|
|
rsOutputChannel = ((NBTTagByte) value).getByte();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INT:
|
|
|
|
if (id == 0) {
|
|
|
|
rsOutputId = ((NBTTagInt) value).getInt();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FLOAT:
|
|
|
|
color = PanelUtils.setColor(color, id, value);
|
|
|
|
break;
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fomatConfigName(ConfigType type, int id) {
|
|
|
|
switch (type) {
|
2017-05-11 16:39:20 +02:00
|
|
|
case BOOL:
|
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.latching");
|
|
|
|
case RS_CHANNEL:
|
|
|
|
case INT:
|
|
|
|
return null;
|
|
|
|
case FLOAT:
|
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc." + (id == 0 ? "red" : (id == 1 ? "green" : "blue")));
|
|
|
|
default:
|
|
|
|
return "INVALID";
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fomatConfigDescription(ConfigType type, int id) {
|
|
|
|
switch (type) {
|
2017-05-11 16:39:20 +02:00
|
|
|
case BOOL:
|
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.latching_info");
|
|
|
|
case RS_CHANNEL:
|
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.rschannel_info");
|
|
|
|
case INT:
|
|
|
|
return I18n.format(IndustrialWires.MODID + ".desc.rsid_info");
|
|
|
|
case FLOAT:
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
return "INVALID?";
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RSChannelConfig[] getRSChannelOptions() {
|
2017-05-11 16:39:20 +02:00
|
|
|
return new RSChannelConfig[]{new RSChannelConfig("channel", 0, 0, (byte) rsOutputChannel)};
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IntConfig[] getIntegerOptions() {
|
2017-05-01 18:40:29 +02:00
|
|
|
return new IntConfig[]{new IntConfig("rsId", 0, 50, rsOutputId, 2, false)};
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BoolConfig[] getBooleanOptions() {
|
2017-05-01 18:40:29 +02:00
|
|
|
return new BoolConfig[]{new BoolConfig("latching", 0, 70, latching)};
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FloatConfig[] getFloatOptions() {
|
2017-04-22 18:14:49 +02:00
|
|
|
float[] color = PanelUtils.getFloatColor(true, this.color);
|
2017-05-01 18:40:29 +02:00
|
|
|
int x = 70;
|
|
|
|
int yOffset = 10;
|
2017-04-20 20:44:08 +02:00
|
|
|
return new FloatConfig[]{
|
2017-05-01 18:40:29 +02:00
|
|
|
new FloatConfig("red", x, yOffset, color[0], 60),
|
2017-05-11 16:39:20 +02:00
|
|
|
new FloatConfig("green", x, yOffset + 20, color[1], 60),
|
|
|
|
new FloatConfig("blue", x, yOffset + 40, color[2], 60)
|
2017-04-20 20:44:08 +02:00
|
|
|
};
|
|
|
|
}
|
2017-04-27 18:15:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getColor() {
|
|
|
|
return color;
|
|
|
|
}
|
2017-04-20 20:44:08 +02:00
|
|
|
}
|