improve lists, implement GUI

This commit is contained in:
asiekierka 2015-06-29 09:14:50 +02:00
parent edbb37810d
commit ebcece1d72
8 changed files with 179 additions and 50 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View file

@ -25,41 +25,29 @@ import buildcraft.core.lib.gui.tooltips.ToolTip;
@SideOnly(Side.CLIENT)
public class GuiImageButton extends GuiButton implements IButtonClickEventTrigger, IToolTipProvider {
public enum ButtonImage {
BLANK(1, 19),
WHITE_LIST(19, 19),
BLACK_LIST(37, 19),
ROUND_ROBIN(55, 19);
private final int u, v;
ButtonImage(int u, int v) {
this.u = u;
this.v = v;
}
public int getU() {
return u;
}
public int getV() {
return v;
}
}
public static final ResourceLocation ICON_BUTTON_TEXTURES = new ResourceLocation("buildcraftcore:textures/gui/icon_button.png");
public static final int SIZE = 18;
private final int size, u, v, baseU, baseV;
private final ResourceLocation texture;
private ArrayList<IButtonClickEventListener> listeners = new ArrayList<IButtonClickEventListener>();
private ButtonImage image = ButtonImage.BLANK;
private boolean active = false;
private ToolTip toolTip;
public GuiImageButton(int id, int x, int y, ButtonImage image) {
super(id, x, y, SIZE, SIZE, "");
public GuiImageButton(int id, int x, int y, int size, ResourceLocation texture, int u, int v) {
this(id, x, y, size, texture, 0, 0, u, v);
}
this.image = image;
public GuiImageButton(int id, int x, int y, int size, ResourceLocation texture, int baseU, int baseV, int u, int v) {
super(id, x, y, size, size, "");
this.size = size;
this.u = u;
this.v = v;
this.baseU = baseU;
this.baseV = baseV;
this.texture = texture;
}
public int getSize() {
return size;
}
public boolean isActive() {
@ -76,20 +64,20 @@ public class GuiImageButton extends GuiButton implements IButtonClickEventTrigge
@Override
public void drawButton(Minecraft minecraft, int x, int y) {
if (!visible) {
return;
}
minecraft.renderEngine.bindTexture(ICON_BUTTON_TEXTURES);
minecraft.renderEngine.bindTexture(texture);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glDisable(GL11.GL_BLEND);
int buttonState = getButtonState(x, y);
drawTexturedModalRect(xPosition, yPosition, buttonState * SIZE, 0, SIZE, SIZE);
drawTexturedModalRect(xPosition + 1, yPosition + 1, image.getU(), image.getV(), SIZE - 2, SIZE - 2);
drawTexturedModalRect(xPosition, yPosition, baseU + buttonState * size, baseV, size, size);
drawTexturedModalRect(xPosition + 1, yPosition + 1, u, v, size - 2, size - 2);
mouseDragged(minecraft, x, y);
}
@ -144,7 +132,7 @@ public class GuiImageButton extends GuiButton implements IButtonClickEventTrigge
}
private boolean isMouseOverButton(int mouseX, int mouseY) {
return mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + SIZE && mouseY < yPosition + SIZE;
return mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + size && mouseY < yPosition + size;
}
@Override

View file

@ -64,7 +64,7 @@ public class ContainerListNew extends BuildCraftContainer implements ICommandRec
}
public void switchButton(final int lineIndex, final int button) {
lines[lineIndex].toggleOption(lineIndex);
lines[lineIndex].toggleOption(button);
ListHandlerNew.saveLines(player.getCurrentEquippedItem(), lines);
if (player.worldObj.isRemote) {

View file

@ -8,6 +8,11 @@
*/
package buildcraft.core.list;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
@ -16,12 +21,17 @@ import buildcraft.BuildCraftCore;
import buildcraft.core.ItemList;
import buildcraft.core.lib.gui.AdvancedSlot;
import buildcraft.core.lib.gui.GuiAdvancedInterface;
import buildcraft.core.lib.gui.buttons.GuiImageButton;
import buildcraft.core.lib.gui.buttons.IButtonClickEventListener;
import buildcraft.core.lib.gui.buttons.IButtonClickEventTrigger;
import buildcraft.core.lib.inventory.StackHelper;
public class GuiListNew extends GuiAdvancedInterface {
public class GuiListNew extends GuiAdvancedInterface implements IButtonClickEventListener {
private static final ResourceLocation TEXTURE_BASE = new ResourceLocation(
"buildcraftcore:textures/gui/list_new.png");
private static final int BUTTON_COUNT = 3;
private final Map<Integer, Map<ListMatchHandler.Type, List<ItemStack>>> exampleCache = new HashMap<Integer, Map<ListMatchHandler.Type, List<ItemStack>>>();
private GuiTextField textField;
private EntityPlayer player;
@ -39,8 +49,66 @@ public class GuiListNew extends GuiAdvancedInterface {
@Override
public ItemStack getItemStack() {
ContainerListNew container = (ContainerListNew) gui.getContainer();
return container.lines[lineIndex].getStack(slotIndex);
if (slotIndex == 0 || !container.lines[lineIndex].isOneStackMode()) {
return container.lines[lineIndex].getStack(slotIndex);
} else {
List<ItemStack> data = ((GuiListNew) gui).getExamplesList(lineIndex, container.lines[lineIndex].getSortingType());
if (data.size() >= slotIndex) {
return data.get(slotIndex - 1);
} else {
return null;
}
}
}
@Override
public void drawSprite(int cornerX, int cornerY) {
if (!shouldDrawHighlight()) {
Minecraft.getMinecraft().renderEngine.bindTexture(TEXTURE_BASE);
gui.drawTexturedModalRect(cornerX + x, cornerY + y, 176, 0, 16, 16);
}
super.drawSprite(cornerX, cornerY);
}
@Override
public boolean shouldDrawHighlight() {
ContainerListNew container = (ContainerListNew) gui.getContainer();
return slotIndex == 0 || !container.lines[lineIndex].isOneStackMode();
}
}
private void clearExamplesCache(int lineId) {
Map<ListMatchHandler.Type, List<ItemStack>> exampleList = exampleCache.get(lineId);
if (exampleList != null) {
exampleList.clear();
}
}
private List<ItemStack> getExamplesList(int lineId, ListMatchHandler.Type type) {
Map<ListMatchHandler.Type, List<ItemStack>> exampleList = exampleCache.get(lineId);
if (exampleList == null) {
exampleList = new HashMap<ListMatchHandler.Type, List<ItemStack>>();
exampleCache.put(lineId, exampleList);
}
ContainerListNew container = (ContainerListNew) getContainer();
if (!exampleList.containsKey(type)) {
List<ItemStack> examples = container.lines[lineId].getExamples();
ItemStack input = container.lines[lineId].stacks[0];
if (input != null) {
List<ItemStack> repetitions = new ArrayList<ItemStack>();
for (ItemStack is : examples) {
if (StackHelper.isMatchingItem(input, is, true, false)) {
repetitions.add(is);
}
}
examples.removeAll(repetitions);
}
exampleList.put(type, examples);
}
return exampleList.get(type);
}
public GuiListNew(EntityPlayer iPlayer) {
@ -49,12 +117,6 @@ public class GuiListNew extends GuiAdvancedInterface {
xSize = 176;
ySize = 192;
for (int sy = 0; sy < ListHandlerNew.HEIGHT; sy++) {
for (int sx = 0; sx < ListHandlerNew.WIDTH; sx++) {
slots.add(new ListSlot(this, 8 + sx * 18, 32 + sy * 33, sy, sx));
}
}
player = iPlayer;
}
@ -62,6 +124,34 @@ public class GuiListNew extends GuiAdvancedInterface {
public void initGui() {
super.initGui();
exampleCache.clear();
slots.clear();
buttonList.clear();
for (int sy = 0; sy < ListHandlerNew.HEIGHT; sy++) {
for (int sx = 0; sx < ListHandlerNew.WIDTH; sx++) {
slots.add(new ListSlot(this, 8 + sx * 18, 32 + sy * 33, sy, sx));
}
int bOff = sy * BUTTON_COUNT;
int bOffX = this.guiLeft + 8 + ListHandlerNew.WIDTH * 18 - BUTTON_COUNT * 11;
int bOffY = this.guiTop + 32 + sy * 33 + 18;
buttonList.add(new GuiImageButton(bOff + 0, bOffX, bOffY, 11, TEXTURE_BASE, 176, 16, 176, 28));
buttonList.add(new GuiImageButton(bOff + 1, bOffX + 11, bOffY, 11, TEXTURE_BASE, 176, 16, 185, 28));
buttonList.add(new GuiImageButton(bOff + 2, bOffX + 22, bOffY, 11, TEXTURE_BASE, 176, 16, 194, 28));
}
for (Object o : buttonList) {
GuiImageButton b = ((GuiImageButton) o);
int lineId = b.id / BUTTON_COUNT;
int buttonId = b.id % BUTTON_COUNT;
if (((ContainerListNew) getContainer()).lines[lineId].getOption(buttonId)) {
b.activate();
}
b.registerListener(this);
}
textField = new GuiTextField(this.fontRendererObj, 10, 10, 156, 12);
textField.setMaxStringLength(32);
textField.setText(BuildCraftCore.listItem.getLabel(player.getCurrentEquippedItem()));
@ -106,11 +196,22 @@ public class GuiListNew extends GuiAdvancedInterface {
if (slot instanceof ListSlot) {
container.setStack(((ListSlot) slot).lineIndex, ((ListSlot) slot).slotIndex, mc.thePlayer.inventory.getItemStack());
clearExamplesCache(((ListSlot) slot).lineIndex);
}
textField.mouseClicked(x - guiLeft, y - guiTop, b);
}
@Override
public void handleButtonClick(IButtonClickEventTrigger sender, int id) {
int buttonId = id % BUTTON_COUNT;
int lineId = id / BUTTON_COUNT;
ContainerListNew container = (ContainerListNew) getContainer();
container.switchButton(lineId, buttonId);
clearExamplesCache(lineId);
}
@Override
protected void keyTyped(char c, int i) {
if (textField.isFocused()) {

View file

@ -1,5 +1,7 @@
package buildcraft.core.list;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.item.ItemStack;
@ -55,7 +57,7 @@ public class ListHandlerNew {
}
List<ListMatchHandler> handlers = ListRegistry.getHandlers();
ListMatchHandler.Type type = byType ? (byMaterial ? ListMatchHandler.Type.CLASS : ListMatchHandler.Type.TYPE) : ListMatchHandler.Type.MATERIAL;
ListMatchHandler.Type type = getSortingType();
for (ListMatchHandler h : handlers) {
if (h.matches(type, stacks[0], target, precise)) {
return true;
@ -71,6 +73,10 @@ public class ListHandlerNew {
return false;
}
public ListMatchHandler.Type getSortingType() {
return byType ? (byMaterial ? ListMatchHandler.Type.CLASS : ListMatchHandler.Type.TYPE) : ListMatchHandler.Type.MATERIAL;
}
public static Line fromNBT(NBTTagCompound data) {
Line line = new Line();
@ -119,6 +125,22 @@ public class ListHandlerNew {
public ItemStack getStack(int i) {
return i >= 0 && i < stacks.length ? stacks[i] : null;
}
public List<ItemStack> getExamples() {
List<ItemStack> stackList = new ArrayList<ItemStack>();
if (stacks[0] != null) {
List<ListMatchHandler> handlers = ListRegistry.getHandlers();
ListMatchHandler.Type type = getSortingType();
for (ListMatchHandler h : handlers) {
List<ItemStack> examples = h.getClientExamples(type, stacks[0]);
if (examples != null) {
stackList.addAll(examples);
}
}
Collections.shuffle(stackList);
}
return stackList;
}
}
public static Line[] getLines(ItemStack item) {

View file

@ -1,9 +1,14 @@
package buildcraft.core.list;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@ -21,6 +26,19 @@ public class ListMatchHandlerClass extends ListMatchHandler {
@Override
public List<ItemStack> getClientExamples(Type type, ItemStack stack) {
if (type == Type.TYPE) {
Class kl = stack.getItem().getClass();
List<ItemStack> examples = new ArrayList<ItemStack>();
if (itemClasses.contains(kl)) {
for (Object key : Item.itemRegistry.getKeys()) {
Item i = (Item) Item.itemRegistry.getObject(key);
if (i != null && kl.equals(i.getClass())) {
i.getSubItems(i, CreativeTabs.tabMisc, examples);
}
}
}
return examples;
}
return null;
}
}

View file

@ -28,7 +28,7 @@ import buildcraft.transport.pipes.PipeItemsEmerald.FilterMode;
public class GuiEmeraldPipe extends GuiBuildCraft implements IButtonClickEventListener {
private static final ResourceLocation TEXTURE = new ResourceLocation("buildcrafttransport:textures/gui/pipe_emerald.png");
private static final ResourceLocation TEXTURE_BUTTON = new ResourceLocation("buildcrafttransport:textures/gui/pipe_emerald_button.png");
private static final int WHITE_LIST_BUTTON_ID = 1;
private static final int BLACK_LIST_BUTTON_ID = 2;
private static final int ROUND_ROBIN_BUTTON_ID = 3;
@ -54,17 +54,17 @@ public class GuiEmeraldPipe extends GuiBuildCraft implements IButtonClickEventLi
this.buttonList.clear();
this.whiteListButton = new GuiImageButton(WHITE_LIST_BUTTON_ID, this.guiLeft + 7, this.guiTop + 41, GuiImageButton.ButtonImage.WHITE_LIST);
this.whiteListButton = new GuiImageButton(WHITE_LIST_BUTTON_ID, this.guiLeft + 7, this.guiTop + 41, 18, TEXTURE_BUTTON, 19, 19);
this.whiteListButton.registerListener(this);
this.whiteListButton.setToolTip(new ToolTip(500, new ToolTipLine(StatCollector.translateToLocal("tip.PipeItemsEmerald.whitelist"))));
this.buttonList.add(this.whiteListButton);
this.blackListButton = new GuiImageButton(BLACK_LIST_BUTTON_ID, this.guiLeft + 7 + 18, this.guiTop + 41, GuiImageButton.ButtonImage.BLACK_LIST);
this.blackListButton = new GuiImageButton(BLACK_LIST_BUTTON_ID, this.guiLeft + 7 + 18, this.guiTop + 41, 18, TEXTURE_BUTTON, 37, 19);
this.blackListButton.registerListener(this);
this.blackListButton.setToolTip(new ToolTip(500, new ToolTipLine(StatCollector.translateToLocal("tip.PipeItemsEmerald.blacklist"))));
this.buttonList.add(this.blackListButton);
this.roundRobinButton = new GuiImageButton(ROUND_ROBIN_BUTTON_ID, this.guiLeft + 7 + 36, this.guiTop + 41, GuiImageButton.ButtonImage.ROUND_ROBIN);
this.roundRobinButton = new GuiImageButton(ROUND_ROBIN_BUTTON_ID, this.guiLeft + 7 + 36, this.guiTop + 41, 18, TEXTURE_BUTTON, 55, 19);
this.roundRobinButton.registerListener(this);
this.roundRobinButton.setToolTip(new ToolTip(500, new ToolTipLine(StatCollector.translateToLocal("tip.PipeItemsEmerald.roundrobin"))));
this.buttonList.add(this.roundRobinButton);