turn scrollbars into a widget, improved blueprint library GUI

This commit is contained in:
asiekierka 2015-07-01 01:04:35 +02:00
parent 0a88284e24
commit 325cef4a58
10 changed files with 162 additions and 183 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 577 B

After

Width:  |  Height:  |  Size: 695 B

View file

@ -15,6 +15,8 @@ import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
@ -205,25 +207,7 @@ public class LibraryDatabase {
return null;
}
public ArrayList<LibraryId> getPage (int pageId) {
ArrayList<LibraryId> result = new ArrayList<LibraryId>();
if (pageId < 0) {
return result;
}
for (int i = pageId * PAGE_SIZE; i < pageId * PAGE_SIZE + PAGE_SIZE; ++i) {
if (i < pages.length) {
result.add(pages [i]);
} else {
break;
}
}
return result;
}
public int getPageNumber () {
return (int) Math.ceil((float) blueprintIds.size() / (float) PAGE_SIZE);
public List<LibraryId> getBlueprintIds() {
return Collections.unmodifiableList(new ArrayList<LibraryId>(blueprintIds));
}
}

View file

@ -10,6 +10,7 @@ package buildcraft.builders;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.netty.buffer.ByteBuf;
@ -50,15 +51,13 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
public int progressIn = 0;
public int progressOut = 0;
public ArrayList<LibraryId> currentPage;
public List<LibraryId> entries;
public int selected = -1;
public EntityPlayer uploadingPlayer = null;
public EntityPlayer downloadingPlayer = null;
public int pageId = 0;
private LibraryId blueprintDownloadId;
private byte[] blueprintDownload;
@ -69,7 +68,8 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
public void refresh() {
if (worldObj.isRemote) {
BuildCraftBuilders.clientDB.refresh();
setCurrentPage(BuildCraftBuilders.clientDB.getPage(pageId));
entries = BuildCraftBuilders.clientDB.getBlueprintIds();
selected = -1;
}
}
@ -80,38 +80,13 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
refresh();
}
public void setCurrentPage(ArrayList<LibraryId> newPage) {
currentPage = newPage;
selected = -1;
}
public void pageNext () {
if (pageId < BuildCraftBuilders.clientDB.getPageNumber() - 1) {
pageId++;
}
setCurrentPage(BuildCraftBuilders.clientDB.getPage(pageId));
}
public void pagePrev () {
if (pageId > 0) {
pageId--;
}
setCurrentPage(BuildCraftBuilders.clientDB.getPage (pageId));
}
public void deleteSelectedBpt() {
if (selected != -1) {
BuildCraftBuilders.clientDB.deleteBlueprint(currentPage
.get(selected));
if (pageId > BuildCraftBuilders.clientDB.getPageNumber() - 1
&& pageId > 0) {
pageId--;
BuildCraftBuilders.clientDB.deleteBlueprint(entries.get(selected));
entries = BuildCraftBuilders.clientDB.getBlueprintIds();
if (selected >= entries.size()) {
selected--;
}
setCurrentPage(BuildCraftBuilders.clientDB.getPage (pageId));
}
}
@ -307,18 +282,18 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
if (side.isClient()) {
if ("requestSelectedBlueprint".equals(command)) {
if (isOutputConsistent()) {
if (selected > -1 && selected < currentPage.size()) {
if (selected > -1 && selected < entries.size()) {
// Work around 32k max limit on client->server
final NBTTagCompound compound = BuildCraftBuilders.clientDB
.load(currentPage.get(selected));
compound.setString("__filename", currentPage.get(selected).name);
.load(entries.get(selected));
compound.setString("__filename", entries.get(selected).name);
final byte[] bptData = NBTUtils.save(compound);
final int chunks = (bptData.length + CHUNK_SIZE - 1) / CHUNK_SIZE;
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "uploadServerBegin",
new CommandWriter() {
public void write(ByteBuf data) {
currentPage.get(selected).writeData(data);
entries.get(selected).writeData(data);
data.writeShort(chunks);
}
}));
@ -355,7 +330,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
NBTTagCompound nbt = CompressedStreamTools.func_152457_a(data, NBTSizeTracker.field_152451_a);
BuildCraftBuilders.clientDB.add(id, nbt);
setCurrentPage(BuildCraftBuilders.clientDB.getPage(pageId));
entries = BuildCraftBuilders.clientDB.getBlueprintIds();
} catch (IOException e) {
e.printStackTrace();
}
@ -421,10 +396,10 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
}
private boolean isOutputConsistent() {
if (selected == -1 || getStackInSlot(2) == null) {
if (selected <= -1 || selected >= entries.size() || getStackInSlot(2) == null) {
return false;
}
return LibraryAPI.getHandlerFor(currentPage.get(selected).extension).isHandler(getStackInSlot(2), LibraryTypeHandler.HandlerType.LOAD);
return LibraryAPI.getHandlerFor(entries.get(selected).extension).isHandler(getStackInSlot(2), LibraryTypeHandler.HandlerType.LOAD);
}
}

View file

@ -16,9 +16,10 @@ import net.minecraft.inventory.Slot;
import buildcraft.builders.TileBlueprintLibrary;
import buildcraft.core.lib.gui.BuildCraftContainer;
import buildcraft.core.lib.gui.slots.SlotOutput;
import buildcraft.core.lib.gui.widgets.ScrollbarWidget;
public class ContainerBlueprintLibrary extends BuildCraftContainer {
protected ScrollbarWidget scrollbarWidget;
protected IInventory playerInventory;
protected TileBlueprintLibrary library;
@ -29,21 +30,25 @@ public class ContainerBlueprintLibrary extends BuildCraftContainer {
this.playerInventory = player.inventory;
this.library = library;
addSlotToContainer(new SlotBlueprintLibrary(library, player, 0, 211, 61));
addSlotToContainer(new SlotOutput(library, 1, 167, 61));
this.scrollbarWidget = new ScrollbarWidget(163, 21, 244, 0, 110);
this.scrollbarWidget.hidden = true;
this.addWidget(scrollbarWidget);
addSlotToContainer(new SlotBlueprintLibrary(library, player, 2, 167, 79));
addSlotToContainer(new SlotOutput(library, 3, 211, 79));
addSlotToContainer(new SlotBlueprintLibrary(library, player, 0, 219, 57));
addSlotToContainer(new SlotOutput(library, 1, 175, 57));
addSlotToContainer(new SlotBlueprintLibrary(library, player, 2, 175, 79));
addSlotToContainer(new SlotOutput(library, 3, 219, 79));
// Player inventory
for (int l = 0; l < 3; l++) {
for (int k1 = 0; k1 < 9; k1++) {
addSlotToContainer(new Slot(playerInventory, k1 + l * 9 + 9, 66 + k1 * 18, 140 + l * 18));
addSlotToContainer(new Slot(playerInventory, k1 + l * 9 + 9, 8 + k1 * 18, 138 + l * 18));
}
}
for (int i1 = 0; i1 < 9; i1++) {
addSlotToContainer(new Slot(playerInventory, i1, 66 + i1 * 18, 198));
addSlotToContainer(new Slot(playerInventory, i1, 8 + i1 * 18, 196));
}
}

View file

@ -15,17 +15,22 @@ import net.minecraft.inventory.Slot;
import buildcraft.builders.TileBuilder;
import buildcraft.core.lib.gui.BuildCraftContainer;
import buildcraft.core.lib.gui.widgets.ScrollbarWidget;
public class ContainerBuilder extends BuildCraftContainer {
IInventory playerIInventory;
TileBuilder builder;
protected ScrollbarWidget scrollbarWidget;
protected IInventory playerIInventory;
protected TileBuilder builder;
public ContainerBuilder(IInventory playerInventory, TileBuilder builder) {
super(builder.getSizeInventory());
this.playerIInventory = playerInventory;
this.builder = builder;
this.scrollbarWidget = new ScrollbarWidget(172, 17, 18, 0, 108);
this.scrollbarWidget.hidden = true;
this.addWidget(scrollbarWidget);
addSlotToContainer(new Slot(builder, 0, 80, 27));
for (int k = 0; k < 3; k++) {

View file

@ -23,17 +23,14 @@ import buildcraft.core.lib.gui.GuiBuildCraft;
import buildcraft.core.lib.utils.StringUtils;
public class GuiBlueprintLibrary extends GuiBuildCraft {
private static final ResourceLocation TEXTURE = new ResourceLocation("buildcraftbuilders:textures/gui/library_rw.png");
private GuiButton nextPageButton;
private GuiButton prevPageButton;
private GuiButton deleteButton;
private TileBlueprintLibrary library;
public GuiBlueprintLibrary(EntityPlayer player, TileBlueprintLibrary library) {
super(new ContainerBlueprintLibrary(player, library), library, TEXTURE);
xSize = 234;
ySize = 225;
xSize = 244;
ySize = 220;
this.library = library;
}
@ -43,19 +40,16 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
public void initGui() {
super.initGui();
prevPageButton = new GuiButton(0, guiLeft + 158, guiTop + 23, 20, 20, "<");
nextPageButton = new GuiButton(1, guiLeft + 180, guiTop + 23, 20, 20, ">");
buttonList.add(prevPageButton);
buttonList.add(nextPageButton);
deleteButton = new GuiButton(2, guiLeft + 158, guiTop + 114, 25, 20, StringUtils.localize("gui.del"));
deleteButton = new GuiButton(2, guiLeft + 174, guiTop + 109, 25, 20, StringUtils.localize("gui.del"));
buttonList.add(deleteButton);
library.refresh();
checkDelete();
checkPages();
}
private ContainerBlueprintLibrary getLibraryContainer() {
return (ContainerBlueprintLibrary) getContainer();
}
@Override
@ -63,29 +57,30 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
String title = StringUtils.localize("tile.libraryBlock.name");
fontRendererObj.drawString(title, getCenteredOffset(title), 6, 0x404040);
int c = 0;
for (LibraryId bpt : library.currentPage) {
int off = getLibraryContainer().scrollbarWidget.getPosition();
for (int i = off; i < (off + 12); i++) {
if (i >= library.entries.size()) {
break;
}
LibraryId bpt = library.entries.get(i);
String name = bpt.name;
if (name.length() > DefaultProps.MAX_NAME_SIZE) {
name = name.substring(0, DefaultProps.MAX_NAME_SIZE);
}
if (c == library.selected) {
if (i == library.selected) {
int l1 = 8;
int i2 = 24;
int i2 = 22;
// TODO
//if (bpt.kind == Kind.Blueprint) {
// drawGradientRect(l1, i2 + 9 * c, l1 + 146, i2 + 9 * (c + 1), 0xFFA0C0F0, 0xFFA0C0F0);
//} else {
drawGradientRect(l1, i2 + 9 * c, l1 + 146, i2 + 9 * (c + 1), 0x80ffffff, 0x80ffffff);
//}
drawGradientRect(l1, i2 + 9 * (i - off), l1 + 146, i2 + 9 * (i - off + 1), 0x80ffffff, 0x80ffffff);
}
fontRendererObj.drawString(name, 9, 25 + 9 * c, LibraryAPI.getHandlerFor(bpt.extension).getTextColor());
while (fontRendererObj.getStringWidth(name) > (160 - 9)) {
name = name.substring(0, name.length() - 1);
}
c++;
fontRendererObj.drawString(name, 9, 23 + 9 * (i - off), LibraryAPI.getHandlerFor(bpt.extension).getTextColor());
}
}
@ -96,20 +91,21 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
int inP = (int) (library.progressIn / 100.0 * 22.0);
int outP = (int) (library.progressOut / 100.0 * 22.0);
getLibraryContainer().scrollbarWidget.hidden = library.entries.size() <= 12;
getLibraryContainer().scrollbarWidget.setLength(Math.max(0, library.entries.size() - 12));
drawTexturedModalRect(guiLeft + 186 + 22 - inP, guiTop + 61, 234 + 22 - inP, 16, inP, 16);
drawTexturedModalRect(guiLeft + 186, guiTop + 78, 234, 0, outP, 16);
drawWidgets(x, y);
int inP = library.progressIn * 22 / 100;
int outP = library.progressOut * 22 / 100;
drawTexturedModalRect(guiLeft + 194 + 22 - inP, guiTop + 57, 234 + 22 - inP, 240, inP, 16);
drawTexturedModalRect(guiLeft + 194, guiTop + 79, 234, 224, outP, 16);
}
@Override
protected void actionPerformed(GuiButton button) {
if (button == nextPageButton) {
library.pageNext();
} else if (button == prevPageButton) {
library.pagePrev();
} else if (deleteButton != null && button == deleteButton) {
if (deleteButton != null && button == deleteButton) {
library.deleteSelectedBpt();
}
}
@ -121,18 +117,15 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
int x = i - guiLeft;
int y = j - guiTop;
if (x >= 8 && x <= 88) {
int ySlot = (y - 24) / 9;
if (x >= 8 && x <= 161) {
int ySlot = (y - 22) / 9 + getLibraryContainer().scrollbarWidget.getPosition();
if (ySlot >= 0 && ySlot <= 11) {
if (ySlot < library.currentPage.size()) {
library.selectBlueprint(ySlot);
}
if (ySlot > -1 && ySlot < library.entries.size()) {
library.selectBlueprint(ySlot);
}
}
checkDelete();
checkPages();
}
protected void checkDelete() {
@ -142,18 +135,4 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
deleteButton.enabled = false;
}
}
protected void checkPages() {
if (library.pageId != 0) {
prevPageButton.enabled = true;
} else {
prevPageButton.enabled = false;
}
if (library.pageId < BuildCraftBuilders.clientDB.getPageNumber() - 1) {
nextPageButton.enabled = true;
} else {
nextPageButton.enabled = false;
}
}
}

View file

@ -24,6 +24,7 @@ import buildcraft.core.blueprints.RequirementItemStack;
import buildcraft.core.lib.fluids.Tank;
import buildcraft.core.lib.gui.AdvancedSlot;
import buildcraft.core.lib.gui.GuiAdvancedInterface;
import buildcraft.core.lib.gui.widgets.ScrollbarWidget;
import buildcraft.core.lib.network.command.CommandWriter;
import buildcraft.core.lib.network.command.PacketCommand;
import buildcraft.core.lib.utils.StringUtils;
@ -34,8 +35,6 @@ public class GuiBuilder extends GuiAdvancedInterface {
private IInventory playerInventory;
private TileBuilder builder;
private GuiButton selectedButton;
private int sbPosition, sbLength;
private boolean sbInside;
public GuiBuilder(IInventory playerInventory, TileBuilder builder) {
super(new ContainerBuilder(playerInventory, builder), builder, BLUEPRINT_TEXTURE);
@ -53,6 +52,10 @@ public class GuiBuilder extends GuiAdvancedInterface {
}
}
private ContainerBuilder getContainerBuilder() {
return (ContainerBuilder) getContainer();
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
super.drawGuiContainerForegroundLayer(par1, par2);
@ -70,6 +73,8 @@ public class GuiBuilder extends GuiAdvancedInterface {
@Override
protected void drawGuiContainerBackgroundLayer(float f, int x, int y) {
// We cannot do super here due to some crazy shenanigans with a dynamically
// resized GUI.
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
boolean isBlueprint = builder.getStackInSlot(0) != null;
@ -84,21 +89,13 @@ public class GuiBuilder extends GuiAdvancedInterface {
if (needs != null) {
if (needs.size() > slots.size()) {
sbLength = (needs.size() - slots.size() + 3) / 4;
if (sbPosition >= sbLength) {
sbPosition = sbLength;
}
// render scrollbar
drawTexturedModalRect(guiLeft + 172, guiTop + 17, 18, 0, 6, 108);
int sbPixelPosition = sbPosition * 95 / sbLength;
drawTexturedModalRect(guiLeft + 172, guiTop + 17 + sbPixelPosition, 24, 0, 6, 14);
getContainerBuilder().scrollbarWidget.hidden = false;
getContainerBuilder().scrollbarWidget.setLength((needs.size() - slots.size() + 3) / 4);
} else {
sbPosition = 0;
sbLength = 0;
getContainerBuilder().scrollbarWidget.hidden = true;
}
int offset = sbPosition * 4;
int offset = getContainerBuilder().scrollbarWidget.getPosition() * 4;
for (int s = 0; s < slots.size(); s++) {
int ts = offset + s;
if (ts >= needs.size()) {
@ -112,8 +109,7 @@ public class GuiBuilder extends GuiAdvancedInterface {
b.visible = true;
}
} else {
sbPosition = 0;
sbLength = 0;
getContainerBuilder().scrollbarWidget.hidden = true;
for (AdvancedSlot slot : slots) {
((BuilderRequirementSlot) slot).stack = null;
}
@ -122,6 +118,8 @@ public class GuiBuilder extends GuiAdvancedInterface {
}
}
drawWidgets(x, y);
if (isBlueprint) {
drawBackgroundSlots(x, y);
@ -143,50 +141,9 @@ public class GuiBuilder extends GuiAdvancedInterface {
}
}
@Override
public void mouseClicked(int mouseX, int mouseY, int button) {
int guiX = mouseX - guiLeft;
int guiY = mouseY - guiTop;
if (sbLength > 0 && button == 0) {
if (guiX >= 172 && guiX < 178 && guiY >= 17 && guiY < 125) {
sbInside = true;
updateToSbHeight(guiY - 17);
}
}
super.mouseClicked(mouseX, mouseY, button);
}
private void updateToSbHeight(int h) {
int hFrac = (h * sbLength + 54) / 108;
sbPosition = hFrac;
}
@Override
protected void mouseClickMove(int x, int y, int button, long time) {
super.mouseClickMove(x, y, button, time);
if (sbInside && button == 0) {
int guiY = y - guiTop;
if (sbLength > 0) {
if (guiY >= 17 && guiY < 125) {
updateToSbHeight(guiY - 17);
}
}
}
}
@Override
protected void mouseMovedOrUp(int mouseX, int mouseY, int eventType) {
super.mouseMovedOrUp(mouseX, mouseY, eventType);
if (sbInside && eventType == 0) {
int guiY = mouseY - guiTop;
if (sbLength > 0) {
if (guiY >= 17 && guiY < 125) {
updateToSbHeight(guiY - 17);
sbInside = false;
}
}
}
if (this.selectedButton != null && eventType == 0) {
this.selectedButton.mouseReleased(mouseX, mouseY);
this.selectedButton = null;

View file

@ -8,6 +8,7 @@
*/
package buildcraft.core;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
@ -15,6 +16,7 @@ import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import buildcraft.core.lib.utils.ResourceUtils;
public class BlockPathMarker extends BlockMarker {
@ -43,6 +45,6 @@ public class BlockPathMarker extends BlockMarker {
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister par1IconRegister) {
super.registerBlockIcons(par1IconRegister);
activeMarker = par1IconRegister.registerIcon("buildcraftbuilders:pathMarkerBlock/active");
activeMarker = par1IconRegister.registerIcon(ResourceUtils.getObjectPrefix(Block.blockRegistry.getNameForObject(this)) + "/active");
}
}

View file

@ -174,6 +174,10 @@ public abstract class GuiBuildCraft extends GuiContainer {
int mX = mouseX - guiLeft;
int mY = mouseY - guiTop;
drawWidgets(mX, mY);
}
protected void drawWidgets(int mX, int mY) {
for (Widget widget : container.getWidgets()) {
if (widget.hidden) {
continue;

View file

@ -0,0 +1,68 @@
package buildcraft.core.lib.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import buildcraft.core.lib.gui.GuiBuildCraft;
public class ScrollbarWidget extends Widget {
private static final int HEIGHT = 14;
private int pos, len;
private boolean isClicking;
public ScrollbarWidget(int x, int y, int u, int v, int h) {
super(x, y, u, v, 6, h);
}
@Override
@SideOnly(Side.CLIENT)
public void draw(GuiBuildCraft gui, int guiX, int guiY, int mouseX, int mouseY) {
gui.drawTexturedModalRect(guiX + x, guiY + y, u, v, w, h);
int posPx = pos * (h - HEIGHT + 2) / len;
gui.drawTexturedModalRect(guiX + x, guiY + y + posPx, u + 6, v, w, HEIGHT);
}
private void updateLength(int mouseY) {
setPosition(((mouseY - y) * len + (h / 2)) / h);
}
@Override
@SideOnly(Side.CLIENT)
public boolean handleMouseClick(int mouseX, int mouseY, int mouseButton) {
if (mouseButton == 0) {
isClicking = true;
updateLength(mouseY);
return true;
}
return false;
}
@Override
@SideOnly(Side.CLIENT)
public void handleMouseMove(int mouseX, int mouseY, int mouseButton, long time) {
if (isClicking && mouseButton == 0) {
updateLength(mouseY);
}
}
@Override
@SideOnly(Side.CLIENT)
public void handleMouseRelease(int mouseX, int mouseY, int eventType) {
if (isClicking && eventType == 0) {
updateLength(mouseY);
isClicking = false;
}
}
public int getPosition() {
return pos;
}
public void setPosition(int pos) {
this.pos = pos > len ? len : pos;
}
public void setLength(int len) {
this.len = len;
setPosition(this.pos);
}
}