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-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;
|
2017-04-02 17:16:42 +02:00
|
|
|
import java.util.HashSet;
|
2017-03-24 17:40:05 +01:00
|
|
|
import java.util.List;
|
2017-04-02 17:16:42 +02:00
|
|
|
import java.util.Set;
|
2017-04-01 17:33:58 +02:00
|
|
|
import java.util.function.BiConsumer;
|
2017-03-24 17:40:05 +01:00
|
|
|
|
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-04-02 17:16:42 +02:00
|
|
|
private Set<BiConsumer<Integer, Byte>> rsOut = new HashSet<>();
|
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-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-03-24 17:40:05 +01:00
|
|
|
nbt.setInteger("timeout", ticksTillOff);
|
2017-04-09 16:59:57 +02:00
|
|
|
if (!toItem) {
|
|
|
|
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
|
|
|
}
|
|
|
|
private final static float size = .0625F;
|
|
|
|
@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-04-13 17:14:05 +02:00
|
|
|
PanelUtils.addColoredBox(color, GRAY, null, new Vector3f(0, 0, 0), new Vector3f(size, size/2, size), ret, false);
|
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() {
|
|
|
|
if (aabb==null) {
|
2017-04-04 17:07:45 +02:00
|
|
|
aabb = new AxisAlignedBB(x, 0, y, x+size, getHeight(), y+size);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-08 17:20:04 +02:00
|
|
|
public boolean interactWith(Vec3d hitRel, TileEntityPanel tile, EntityPlayerMP player) {
|
2017-03-24 17:40:05 +01:00
|
|
|
if (!latching&&active) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-28 18:15:41 +02:00
|
|
|
setOut(!active, tile);
|
2017-03-24 17:40:05 +01:00
|
|
|
if (!latching) {
|
|
|
|
ticksTillOff = 10;
|
|
|
|
}
|
|
|
|
tile.markDirty();
|
|
|
|
tile.triggerRenderUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(TileEntityPanel tile) {
|
|
|
|
if (!latching&&ticksTillOff>0) {
|
|
|
|
ticksTillOff--;
|
|
|
|
tile.markDirty();
|
|
|
|
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
|
|
|
|
public void registerRSOutput(int id, @Nonnull BiConsumer<Integer, Byte> out) {
|
|
|
|
if (id==rsOutputId) {
|
2017-04-02 17:16:42 +02:00
|
|
|
rsOut.add(out);
|
|
|
|
out.accept(rsOutputChannel, (byte) (active?15:0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unregisterRSOutput(int id, @Nonnull BiConsumer<Integer, Byte> out) {
|
|
|
|
if (id==rsOutputId) {
|
|
|
|
rsOut.remove(out);
|
2017-04-01 17:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:07:45 +02:00
|
|
|
@Override
|
|
|
|
public float getHeight() {
|
|
|
|
return size/2;
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
@Override
|
|
|
|
public void renderInGUI(GuiPanelCreator gui) {
|
|
|
|
renderInGUIDefault(gui, 0xff000000|color);
|
|
|
|
}
|
|
|
|
|
2017-04-20 20:44:08 +02:00
|
|
|
@Override
|
|
|
|
public void invalidate(TileEntityPanel te) {
|
|
|
|
setOut(false, te);
|
|
|
|
}
|
|
|
|
|
2017-03-28 18:15:41 +02:00
|
|
|
private void setOut(boolean on, TileEntityPanel tile) {
|
|
|
|
active = on;
|
|
|
|
tile.markDirty();
|
|
|
|
tile.triggerRenderUpdate();
|
2017-04-02 17:16:42 +02:00
|
|
|
for (BiConsumer<Integer, Byte> rs:rsOut) {
|
|
|
|
rs.accept(rsOutputChannel, (byte)(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) {
|
|
|
|
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:
|
2017-04-22 18:14:49 +02:00
|
|
|
color = PanelUtils.setColor(color, id, value);
|
2017-04-20 20:44:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fomatConfigName(ConfigType type, int id) {
|
|
|
|
switch (type) {
|
|
|
|
case BOOL:
|
2017-04-22 18:14:49 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID+".desc.latching");
|
2017-04-20 20:44:08 +02:00
|
|
|
case RS_CHANNEL:
|
|
|
|
case INT:
|
|
|
|
return null;
|
|
|
|
case FLOAT:
|
2017-04-22 18:14:49 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID+".desc."+(id==0?"red":(id==1?"green":"blue")));
|
2017-04-20 20:44:08 +02:00
|
|
|
default:
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fomatConfigDescription(ConfigType type, int id) {
|
|
|
|
switch (type) {
|
|
|
|
case BOOL:
|
2017-04-22 18:14:49 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID+".desc.latching_info");
|
2017-04-20 20:44:08 +02:00
|
|
|
case RS_CHANNEL:
|
2017-04-22 18:14:49 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID+".desc.rschannel_info");
|
2017-04-20 20:44:08 +02:00
|
|
|
case INT:
|
2017-04-22 18:14:49 +02:00
|
|
|
return I18n.format(IndustrialWires.MODID+".desc.rsid_info");
|
2017-04-20 20:44:08 +02:00
|
|
|
case FLOAT:
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
return "INVALID?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RSChannelConfig[] getRSChannelOptions() {
|
|
|
|
return new RSChannelConfig[]{new RSChannelConfig("channel", 0, 0, (byte)rsOutputChannel)};
|
|
|
|
}
|
|
|
|
|
|
|
|
@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),
|
|
|
|
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
|
|
|
}
|