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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package malte0811.industrialWires.blocks.controlpanel;
|
|
|
|
|
|
|
|
import blusunrize.immersiveengineering.common.util.IELogger;
|
|
|
|
import malte0811.industrialWires.client.RawQuad;
|
2017-04-12 15:55:37 +02:00
|
|
|
import malte0811.industrialWires.client.gui.GuiPanelCreator;
|
|
|
|
import net.minecraft.client.gui.Gui;
|
2017-03-24 17:40:05 +01:00
|
|
|
import net.minecraft.client.renderer.GlStateManager;
|
|
|
|
import net.minecraft.client.renderer.RenderGlobal;
|
|
|
|
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
2017-04-01 17:33:58 +02:00
|
|
|
import javax.annotation.Nullable;
|
2017-03-24 17:40:05 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-04-01 17:33:58 +02:00
|
|
|
import java.util.function.BiConsumer;
|
|
|
|
import java.util.function.Consumer;
|
2017-03-24 17:40:05 +01:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
public abstract class PanelComponent {
|
|
|
|
protected float panelHeight;
|
2017-04-12 15:55:37 +02:00
|
|
|
protected AxisAlignedBB aabb = null;
|
2017-03-24 17:40:05 +01:00
|
|
|
protected float x, y;
|
|
|
|
private final String type;
|
2017-04-04 17:07:45 +02:00
|
|
|
protected final static float[] gray = {.8F, .8F, .8F};
|
2017-03-24 17:40:05 +01:00
|
|
|
protected PanelComponent(String type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
private static final Map<String, Supplier<PanelComponent>> baseCreaters = new HashMap<>();
|
|
|
|
static {
|
|
|
|
baseCreaters.put("lightedButton", LightedButton::new);
|
2017-03-28 18:15:41 +02:00
|
|
|
baseCreaters.put("label", Label::new);
|
2017-04-01 17:33:58 +02:00
|
|
|
baseCreaters.put("indicator_light", IndicatorLight::new);
|
2017-04-04 17:07:45 +02:00
|
|
|
baseCreaters.put("slider", Slider::new);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
2017-04-09 16:59:57 +02:00
|
|
|
protected abstract void writeCustomNBT(NBTTagCompound nbt, boolean toItem);
|
2017-03-24 17:40:05 +01:00
|
|
|
protected abstract void readCustomNBT(NBTTagCompound nbt);
|
|
|
|
// DON'T OFFSET BY x, y IN THIS METHOD!
|
|
|
|
public abstract List<RawQuad> getQuads();
|
|
|
|
@Nonnull
|
|
|
|
public abstract PanelComponent copyOf();
|
|
|
|
|
2017-03-28 18:15:41 +02:00
|
|
|
//well, only relative in the x/z directions
|
2017-04-12 15:55:37 +02:00
|
|
|
@Nonnull
|
2017-03-24 17:40:05 +01:00
|
|
|
public abstract AxisAlignedBB getBlockRelativeAABB();
|
|
|
|
|
|
|
|
public abstract boolean interactWith(Vec3d hitRelative, TileEntityPanel tile);
|
|
|
|
|
|
|
|
public abstract void update(TileEntityPanel tile);
|
|
|
|
|
2017-04-01 17:33:58 +02:00
|
|
|
@Nullable
|
|
|
|
public Consumer<byte[]> getRSInputHandler(int id, TileEntityPanel panel) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerRSOutput(int id, @Nonnull BiConsumer<Integer, Byte> out) {}
|
|
|
|
|
|
|
|
public void unregisterRSOutput(int id, @Nonnull BiConsumer<Integer, Byte> out) {}
|
|
|
|
|
2017-03-24 17:40:05 +01:00
|
|
|
public float getX() {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getY() {
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:07:45 +02:00
|
|
|
public abstract float getHeight();
|
|
|
|
|
2017-03-24 17:40:05 +01:00
|
|
|
public void setX(float x) {
|
|
|
|
this.x = x;
|
2017-04-12 15:55:37 +02:00
|
|
|
aabb = null;
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setY(float y) {
|
|
|
|
this.y = y;
|
2017-04-12 15:55:37 +02:00
|
|
|
aabb = null;
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setPanelHeight(float panelHeight) {
|
|
|
|
this.panelHeight = panelHeight;
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:59:57 +02:00
|
|
|
public void writeToNBT(NBTTagCompound nbt, boolean toItem) {
|
|
|
|
writeCustomNBT(nbt, toItem);
|
2017-03-24 17:40:05 +01:00
|
|
|
nbt.setFloat("x", getX());
|
|
|
|
nbt.setFloat("y", getY());
|
|
|
|
nbt.setFloat("panelHeight", panelHeight);
|
|
|
|
nbt.setString("type", type);
|
|
|
|
}
|
|
|
|
public static PanelComponent read(NBTTagCompound nbt) {
|
|
|
|
String type = nbt.getString("type");
|
|
|
|
if (baseCreaters.containsKey(type)) {
|
|
|
|
PanelComponent ret = baseCreaters.get(type).get();
|
|
|
|
ret.readCustomNBT(nbt);
|
|
|
|
ret.setX(nbt.getFloat("x"));
|
|
|
|
ret.setY(nbt.getFloat("y"));
|
|
|
|
ret.setPanelHeight(nbt.getFloat("panelHeight"));
|
|
|
|
return ret;
|
|
|
|
} else {
|
2017-04-04 17:07:45 +02:00
|
|
|
IELogger.info("(IndustrialWires) Unknown panel component: "+type);//TODO own logger?
|
2017-03-24 17:40:05 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void renderBox(TileEntityPanel te) {
|
|
|
|
GlStateManager.enableBlend();
|
|
|
|
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
|
|
|
|
GlStateManager.glLineWidth(2.0F);
|
|
|
|
GlStateManager.disableTexture2D();
|
|
|
|
GlStateManager.depthMask(false);
|
|
|
|
double px = te.getPos().getX()-TileEntityRendererDispatcher.staticPlayerX;
|
|
|
|
double py = te.getPos().getY()-TileEntityRendererDispatcher.staticPlayerY;
|
|
|
|
double pz = te.getPos().getZ()-TileEntityRendererDispatcher.staticPlayerZ;
|
2017-04-01 17:33:58 +02:00
|
|
|
RenderGlobal.func_189697_a(te.apply(te.getComponents().getPanelTopTransform(), getBlockRelativeAABB()).expandXyz(0.002).offset(px, py, pz), 0.0F, 0.0F, 0.0F, 0.4F);
|
2017-03-24 17:40:05 +01:00
|
|
|
GlStateManager.depthMask(true);
|
|
|
|
GlStateManager.enableTexture2D();
|
|
|
|
GlStateManager.disableBlend();
|
|
|
|
}
|
2017-03-28 18:15:41 +02:00
|
|
|
|
2017-04-12 15:55:37 +02:00
|
|
|
public void renderInGUI(GuiPanelCreator gui) {
|
|
|
|
//TODO override in special components
|
|
|
|
AxisAlignedBB aabb = getBlockRelativeAABB();
|
|
|
|
int left = (int) (gui.getX0()+aabb.minX*gui.panelSize);
|
|
|
|
int top = (int) (gui.getY0()+aabb.minZ*gui.panelSize);
|
|
|
|
int right = (int) (gui.getX0()+aabb.maxX*gui.panelSize);
|
|
|
|
int bottom = (int) (gui.getY0()+aabb.maxZ*gui.panelSize);
|
|
|
|
Gui.drawRect(left, top, right, bottom, 0xffffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isValidPos() {
|
|
|
|
AxisAlignedBB aabb = getBlockRelativeAABB().offset(0, panelHeight, 0);
|
|
|
|
if (aabb.minX<0||aabb.maxX>1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (aabb.minY<0||aabb.maxY>1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (aabb.minZ<0||aabb.maxZ>1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-28 18:15:41 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
PanelComponent that = (PanelComponent) o;
|
|
|
|
|
|
|
|
if (Float.compare(that.panelHeight, panelHeight) != 0) return false;
|
|
|
|
if (Float.compare(that.x, x) != 0) return false;
|
|
|
|
if (Float.compare(that.y, y) != 0) return false;
|
|
|
|
return type.equals(that.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int result = (panelHeight != +0.0f ? Float.floatToIntBits(panelHeight) : 0);
|
|
|
|
result = 31 * result + (x != +0.0f ? Float.floatToIntBits(x) : 0);
|
|
|
|
result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0);
|
|
|
|
result = 31 * result + type.hashCode();
|
|
|
|
return result;
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|