package mcjty.rftools.blocks.screens.modulesclient;
import mcjty.lib.api.MachineInformation;
import mcjty.lib.gui.events.ChoiceEvent;
import mcjty.lib.gui.events.ColorChoiceEvent;
import mcjty.lib.gui.events.TextEvent;
import mcjty.lib.gui.layout.HorizontalAlignment;
import mcjty.lib.gui.layout.HorizontalLayout;
import mcjty.lib.gui.layout.VerticalLayout;
import mcjty.lib.gui.widgets.*;
import mcjty.lib.varia.Coordinate;
import mcjty.rftools.blocks.screens.ModuleGuiChanged;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;
import java.util.HashMap;
import java.util.Map;
public class MachineInformationClientScreenModule implements ClientScreenModule {
private String line = "";
private int labcolor = 0xffffff;
private int txtcolor = 0xffffff;
protected int dim = 0;
protected Coordinate coordinate = Coordinate.INVALID;
@Override
public TransformMode getTransformMode() {
return TransformMode.TEXT;
}
@Override
public int getHeight() {
return 10;
}
@Override
public void render(FontRenderer fontRenderer, int currenty, Object[] screenData, float factor) {
GL11.glDisable(GL11.GL_LIGHTING);
int xoffset;
if (!line.isEmpty()) {
fontRenderer.drawString(line, 7, currenty, labcolor);
xoffset = 7 + 40;
} else {
xoffset = 7;
}
if (coordinate.isValid() && screenData != null && screenData.length >= 1 && screenData[0] instanceof String) {
fontRenderer.drawString((String)screenData[0], xoffset, currenty, txtcolor);
} else {
fontRenderer.drawString("<invalid>", xoffset, currenty, 0xff0000);
}
}
@Override
public void mouseClick(World world, int x, int y, boolean clicked) {
}
@Override
public Panel createGui(Minecraft mc, Gui gui, final NBTTagCompound currentData, final ModuleGuiChanged moduleGuiChanged) {
Panel panel = new Panel(mc, gui).setLayout(new VerticalLayout());
TextField textField = new TextField(mc, gui).setDesiredHeight(16).setTooltips("Text to use as label").addTextEvent(new TextEvent() {
@Override
public void textChanged(Widget parent, String newText) {
currentData.setString("text", newText);
moduleGuiChanged.updateData();
}
});
panel.addChild(textField);
addColorPanel(mc, gui, currentData, moduleGuiChanged, panel);
addOptionPanel(mc, gui, currentData, moduleGuiChanged, panel);
addMonitorPanel(mc, gui, currentData, panel);
if (currentData != null) {
textField.setText(currentData.getString("text"));
}
return panel;
}
private void addOptionPanel(Minecraft mc, Gui gui, final NBTTagCompound currentData, final ModuleGuiChanged moduleGuiChanged, Panel panel) {
Panel optionPanel = new Panel(mc, gui).setLayout(new HorizontalLayout()).setDesiredHeight(16);
final Map<String,Integer> choiceToIndex = new HashMap<String, Integer>();
final ChoiceLabel tagButton = new ChoiceLabel(mc, gui).setDesiredHeight(16).setDesiredWidth(80);
optionPanel.addChild(tagButton);
// int dim = currentData.getInteger("dim");
int x = currentData.getInteger("monitorx");
int y = currentData.getInteger("monitory");
int z = currentData.getInteger("monitorz");
TileEntity tileEntity = mc.theWorld.getTileEntity(x, y, z);
if (tileEntity instanceof MachineInformation) {
int current = currentData.getInteger("monitorTag");
MachineInformation information = (MachineInformation) tileEntity;
String currentTag = null;
for (int i = 0 ; i < information.getTagCount() ; i++) {
String tag = information.getTagName(i);
choiceToIndex.put(tag, i);
tagButton.addChoices(tag);
tagButton.setChoiceTooltip(tag, information.getTagDescription(i));
if (current == i) {
currentTag = tag;
}
}
if (currentTag != null) {
tagButton.setChoice(currentTag);
}
}
tagButton.addChoiceEvent(new ChoiceEvent() {
@Override
public void choiceChanged(Widget parent, String newChoice) {
String choice = tagButton.getCurrentChoice();
Integer index = choiceToIndex.get(choice);
if (index != null) {
currentData.setInteger("monitorTag", index);
}
moduleGuiChanged.updateData();
}
});
panel.addChild(optionPanel);
}
private void addMonitorPanel(Minecraft mc, Gui gui, final NBTTagCompound currentData, Panel panel) {
Panel monitorPanel = new Panel(mc, gui).setLayout(new HorizontalLayout()).
setDesiredHeight(16);
String monitoring;
if (currentData.hasKey("monitorx")) {
int dim = currentData.getInteger("dim");
World world = mc.thePlayer.worldObj;
if (dim == world.provider.dimensionId) {
int x = currentData.getInteger("monitorx");
int y = currentData.getInteger("monitory");
int z = currentData.getInteger("monitorz");
monitoring = currentData.getString("monitorname");
Block block = world.getBlock(x, y, z);
monitorPanel.addChild(new BlockRender(mc, gui).setRenderItem(block)).setDesiredWidth(20);
monitorPanel.addChild(new Label(mc, gui).setText(x + "," + y + "," + z).setHorizontalAlignment(HorizontalAlignment.ALIGH_LEFT).setDesiredWidth(150));
} else {
monitoring = "<unreachable>";
}
} else {
monitoring = "<not set>";
}
panel.addChild(monitorPanel);
panel.addChild(new Label(mc, gui).setText(monitoring));
}
private void addColorPanel(Minecraft mc, Gui gui, final NBTTagCompound currentData, final ModuleGuiChanged moduleGuiChanged, Panel panel) {
ColorChoiceLabel labelColorSelector = addColorSelector(mc, gui, currentData, moduleGuiChanged, "color").setTooltips("Color for the label");
ColorChoiceLabel txtColorSelector = addColorSelector(mc, gui, currentData, moduleGuiChanged, "txtcolor").setTooltips("Color for the text");
Panel colorPanel = new Panel(mc, gui).setLayout(new HorizontalLayout()).
addChild(new Label(mc, gui).setText("L:")).addChild(labelColorSelector).
addChild(new Label(mc, gui).setText("Txt:")).addChild(txtColorSelector).
setDesiredHeight(12);
panel.addChild(colorPanel);
}
private ColorChoiceLabel addColorSelector(Minecraft mc, Gui gui, final NBTTagCompound currentData, final ModuleGuiChanged moduleGuiChanged, final String tagName) {
ColorChoiceLabel colorChoiceLabel = new ColorChoiceLabel(mc, gui).addColors(0xffffff, 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff).setDesiredWidth(26).setDesiredHeight(14).addChoiceEvent(new ColorChoiceEvent() {
@Override
public void choiceChanged(Widget parent, Integer newColor) {
currentData.setInteger(tagName, newColor);
moduleGuiChanged.updateData();
}
});
if (currentData != null) {
int currentColor = currentData.getInteger(tagName);
if (currentColor != 0) {
colorChoiceLabel.setCurrentColor(currentColor);
}
}
return colorChoiceLabel;
}
@Override
public void setupFromNBT(NBTTagCompound tagCompound, int dim, int x, int y, int z) {
if (tagCompound != null) {
line = tagCompound.getString("text");
if (tagCompound.hasKey("color")) {
labcolor = tagCompound.getInteger("color");
} else {
labcolor = 0xffffff;
}
if (tagCompound.hasKey("txtcolor")) {
txtcolor = tagCompound.getInteger("txtcolor");
} else {
txtcolor = 0xffffff;
}
setupCoordinateFromNBT(tagCompound, dim, x, y, z);
}
}
protected void setupCoordinateFromNBT(NBTTagCompound tagCompound, int dim, int x, int y, int z) {
coordinate = Coordinate.INVALID;
if (tagCompound.hasKey("monitorx")) {
this.dim = tagCompound.getInteger("dim");
if (dim == this.dim) {
Coordinate c = new Coordinate(tagCompound.getInteger("monitorx"), tagCompound.getInteger("monitory"), tagCompound.getInteger("monitorz"));
int dx = Math.abs(c.getX() - x);
int dy = Math.abs(c.getY() - y);
int dz = Math.abs(c.getZ() - z);
if (dx <= 64 && dy <= 64 && dz <= 64) {
coordinate = c;
}
}
}
}
@Override
public boolean needsServerData() {
return true;
}
}