Use event to get mouse scroll wheel instead

This commit is contained in:
Raoul Van den Berge
2016-06-18 13:12:36 +02:00
parent cec9fdf566
commit 6e74d1d477
5 changed files with 81 additions and 72 deletions

View File

@@ -7,6 +7,7 @@ import net.minecraft.client.resources.I18n;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import refinedstorage.RefinedStorage; import refinedstorage.RefinedStorage;
import refinedstorage.gui.sidebutton.SideButton; import refinedstorage.gui.sidebutton.SideButton;
@@ -26,6 +27,8 @@ public abstract class GuiBase extends GuiContainer {
private int lastSideButtonY = 6; private int lastSideButtonY = 6;
private String sideButtonTooltip; private String sideButtonTooltip;
private Scrollbar scrollbar;
protected int width; protected int width;
protected int height; protected int height;
@@ -38,6 +41,14 @@ public abstract class GuiBase extends GuiContainer {
this.ySize = height; this.ySize = height;
} }
public void setScrollbar(Scrollbar scrollbar) {
this.scrollbar = scrollbar;
}
public Scrollbar getScrollbar() {
return scrollbar;
}
@Override @Override
public void initGui() { public void initGui() {
if (sideButtons.size() > 0) { if (sideButtons.size() > 0) {
@@ -65,11 +76,24 @@ public abstract class GuiBase extends GuiContainer {
update(guiLeft, guiTop); update(guiLeft, guiTop);
} }
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
if (scrollbar != null) {
scrollbar.update(this, mouseX - guiLeft, mouseY - guiTop);
}
}
@Override @Override
protected void drawGuiContainerBackgroundLayer(float renderPartialTicks, int mouseX, int mouseY) { protected void drawGuiContainerBackgroundLayer(float renderPartialTicks, int mouseX, int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
drawBackground(guiLeft, guiTop, mouseX, mouseY); drawBackground(guiLeft, guiTop, mouseX, mouseY);
if (scrollbar != null) {
scrollbar.draw(this);
}
} }
@Override @Override
@@ -96,6 +120,17 @@ public abstract class GuiBase extends GuiContainer {
} }
} }
@Override
public void handleMouseInput() throws IOException {
super.handleMouseInput();
int d = Mouse.getEventDWheel();
if (d != 0) {
scrollbar.wheel(d);
}
}
@Override @Override
protected void actionPerformed(GuiButton button) throws IOException { protected void actionPerformed(GuiButton button) throws IOException {
super.actionPerformed(button); super.actionPerformed(button);

View File

@@ -20,11 +20,11 @@ public class GuiController extends GuiBase {
private int barWidth = 16; private int barWidth = 16;
private int barHeight = 59; private int barHeight = 59;
private Scrollbar scrollbar = new Scrollbar(157, 20, 12, 59);
public GuiController(ContainerController container, TileController controller) { public GuiController(ContainerController container, TileController controller) {
super(container, 176, 181); super(container, 176, 181);
setScrollbar(new Scrollbar(157, 20, 12, 59));
this.controller = controller; this.controller = controller;
} }
@@ -33,17 +33,10 @@ public class GuiController extends GuiBase {
addSideButton(new SideButtonRedstoneMode(controller)); addSideButton(new SideButtonRedstoneMode(controller));
} }
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
scrollbar.update(this, mouseX - guiLeft, mouseY - guiTop);
}
@Override @Override
public void update(int x, int y) { public void update(int x, int y) {
scrollbar.setCanScroll(getRows() > VISIBLE_ROWS); getScrollbar().setCanScroll(getRows() > VISIBLE_ROWS);
scrollbar.setScrollDelta((float) scrollbar.getScrollbarHeight() / (float) getRows()); getScrollbar().setScrollDelta((float) getScrollbar().getScrollbarHeight() / (float) getRows());
} }
@Override @Override
@@ -55,8 +48,6 @@ public class GuiController extends GuiBase {
int barHeightNew = (int) ((float) controller.getEnergyStored(null) / (float) controller.getMaxEnergyStored(null) * (float) barHeight); int barHeightNew = (int) ((float) controller.getEnergyStored(null) / (float) controller.getMaxEnergyStored(null) * (float) barHeight);
drawTexture(x + barX, y + barY + barHeight - barHeightNew, 178, barHeight - barHeightNew, barWidth, barHeightNew); drawTexture(x + barX, y + barY + barHeight - barHeightNew, 178, barHeight - barHeightNew, barWidth, barHeightNew);
scrollbar.draw(this);
} }
@Override @Override
@@ -119,7 +110,7 @@ public class GuiController extends GuiBase {
} }
public int getOffset() { public int getOffset() {
return (int) (scrollbar.getCurrentScroll() / 59f * (float) getRows()); return (int) Math.ceil(getScrollbar().getCurrentScroll() / 59f * (float) getRows());
} }
private int getRows() { private int getRows() {

View File

@@ -31,11 +31,11 @@ public class GuiCraftingMonitor extends GuiBase {
private int renderItemSelectionX; private int renderItemSelectionX;
private int renderItemSelectionY; private int renderItemSelectionY;
private Scrollbar scrollbar = new Scrollbar(157, 20, 12, 89);
public GuiCraftingMonitor(ContainerCraftingMonitor container, TileCraftingMonitor craftingMonitor) { public GuiCraftingMonitor(ContainerCraftingMonitor container, TileCraftingMonitor craftingMonitor) {
super(container, 176, 230); super(container, 176, 230);
setScrollbar(new Scrollbar(157, 20, 12, 89));
this.craftingMonitor = craftingMonitor; this.craftingMonitor = craftingMonitor;
} }
@@ -53,17 +53,10 @@ public class GuiCraftingMonitor extends GuiBase {
cancelAllButton = addButton(x + 7 + cancelButtonWidth + 4, y + 113, cancelAllButtonWidth, 20, cancelAll, false); cancelAllButton = addButton(x + 7 + cancelButtonWidth + 4, y + 113, cancelAllButtonWidth, 20, cancelAll, false);
} }
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
scrollbar.update(this, mouseX - guiLeft, mouseY - guiTop);
}
@Override @Override
public void update(int x, int y) { public void update(int x, int y) {
scrollbar.setCanScroll(getRows() > VISIBLE_ROWS); getScrollbar().setCanScroll(getRows() > VISIBLE_ROWS);
scrollbar.setScrollDelta((float) scrollbar.getScrollbarHeight() / (float) getRows()); getScrollbar().setScrollDelta((float) getScrollbar().getScrollbarHeight() / (float) getRows());
if (itemSelected >= craftingMonitor.getTasks().size()) { if (itemSelected >= craftingMonitor.getTasks().size()) {
itemSelected = -1; itemSelected = -1;
@@ -82,8 +75,6 @@ public class GuiCraftingMonitor extends GuiBase {
if (renderItemSelection) { if (renderItemSelection) {
drawTexture(x + renderItemSelectionX, y + renderItemSelectionY, 178, 0, ITEM_WIDTH, ITEM_HEIGHT); drawTexture(x + renderItemSelectionX, y + renderItemSelectionY, 178, 0, ITEM_WIDTH, ITEM_HEIGHT);
} }
scrollbar.draw(this);
} }
@Override @Override
@@ -156,7 +147,7 @@ public class GuiCraftingMonitor extends GuiBase {
} }
public int getOffset() { public int getOffset() {
return (int) (scrollbar.getCurrentScroll() / 89f * (float) getRows()); return (int) Math.ceil(getScrollbar().getCurrentScroll() / 89f * (float) getRows());
} }
private int getRows() { private int getRows() {

View File

@@ -74,14 +74,13 @@ public class GuiGrid extends GuiBase {
private int slotNumber; private int slotNumber;
private int slotId; private int slotId;
private Scrollbar scrollbar;
public GuiGrid(ContainerGrid container, IGrid grid) { public GuiGrid(ContainerGrid container, IGrid grid) {
super(container, 193, (grid.getType() == EnumGridType.CRAFTING || grid.getType() == EnumGridType.PATTERN) ? 256 : 208); super(container, 193, (grid.getType() == EnumGridType.CRAFTING || grid.getType() == EnumGridType.PATTERN) ? 256 : 208);
setScrollbar(new Scrollbar(174, 20, 12, (grid.getType() == EnumGridType.CRAFTING || grid.getType() == EnumGridType.PATTERN) ? 70 : 88));
this.container = container; this.container = container;
this.grid = grid; this.grid = grid;
this.scrollbar = new Scrollbar(174, 20, 12, (grid.getType() == EnumGridType.CRAFTING || grid.getType() == EnumGridType.PATTERN) ? 70 : 88);
} }
@Override @Override
@@ -110,13 +109,6 @@ public class GuiGrid extends GuiBase {
addSideButton(new SideButtonGridSearchBoxMode(grid)); addSideButton(new SideButtonGridSearchBoxMode(grid));
} }
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
scrollbar.update(this, mouseX - guiLeft, mouseY - guiTop);
}
public IGrid getGrid() { public IGrid getGrid() {
return grid; return grid;
} }
@@ -179,33 +171,37 @@ public class GuiGrid extends GuiBase {
} }
} }
scrollbar.setCanScroll(getRows() > getVisibleRows()); getScrollbar().setCanScroll(getRows() > getVisibleRows());
scrollbar.setScrollDelta((float) scrollbar.getScrollbarHeight() / (float) getRows()); getScrollbar().setScrollDelta((float) getScrollbar().getScrollbarHeight() / (float) getRows());
} }
public int getOffset() { public int getOffset() {
return (int) (scrollbar.getCurrentScroll() / 70f * (float) getRows()); return (int) Math.ceil(getScrollbar().getCurrentScroll() / 70f * (float) getRows());
} }
public int getRows() { public int getRows() {
int max = (int) Math.ceil((float) items.size() / (float) 9); int max = (int) Math.ceil((float) items.size() / 9f);
return max < 0 ? 0 : max; return max < 0 ? 0 : max;
} }
private boolean isHoveringOverItemInSlot() { private boolean isOverSlotWithItem() {
return grid.isConnected() && isHoveringOverSlot() && slotNumber < items.size(); return grid.isConnected() && isOverSlot() && slotNumber < items.size();
} }
private boolean isHoveringOverSlot() { private boolean isOverSlot() {
return slotNumber >= 0; return slotNumber >= 0;
} }
private boolean isHoveringOverSlotArea(int mouseX, int mouseY) { private boolean isOverSlotArea(int mouseX, int mouseY) {
return inBounds(7, 19, 162, 18 * getVisibleRows(), mouseX, mouseY); return inBounds(7, 19, 162, 18 * getVisibleRows(), mouseX, mouseY);
} }
public boolean isHoveringOverClear(int mouseX, int mouseY) { public int getVisibleRows() {
return (grid.getType() == EnumGridType.CRAFTING || grid.getType() == EnumGridType.PATTERN) ? 4 : 5;
}
public boolean isOverClear(int mouseX, int mouseY) {
switch (grid.getType()) { switch (grid.getType()) {
case CRAFTING: case CRAFTING:
return inBounds(81, 105, 7, 7, mouseX, mouseY); return inBounds(81, 105, 7, 7, mouseX, mouseY);
@@ -216,7 +212,7 @@ public class GuiGrid extends GuiBase {
} }
} }
public boolean isHoveringOverCreatePattern(int mouseX, int mouseY) { public boolean isOverCreatePattern(int mouseX, int mouseY) {
return grid.getType() == EnumGridType.PATTERN && inBounds(152, 124, 16, 16, mouseX, mouseY) && ((TileGrid) grid).canCreatePattern(); return grid.getType() == EnumGridType.PATTERN && inBounds(152, 124, 16, 16, mouseX, mouseY) && ((TileGrid) grid).canCreatePattern();
} }
@@ -235,7 +231,7 @@ public class GuiGrid extends GuiBase {
if (grid.getType() == EnumGridType.PATTERN) { if (grid.getType() == EnumGridType.PATTERN) {
int ty = 0; int ty = 0;
if (isHoveringOverCreatePattern(mouseX - guiLeft, mouseY - guiTop)) { if (isOverCreatePattern(mouseX - guiLeft, mouseY - guiTop)) {
ty = 1; ty = 1;
} }
@@ -246,8 +242,6 @@ public class GuiGrid extends GuiBase {
drawTexture(x + 152, y + 124, 195, ty * 16, 16, 16); drawTexture(x + 152, y + 124, 195, ty * 16, 16, 16);
} }
scrollbar.draw(this);
searchField.drawTextBox(); searchField.drawTextBox();
} }
@@ -311,15 +305,15 @@ public class GuiGrid extends GuiBase {
} }
} }
if (isHoveringOverItemInSlot()) { if (isOverSlotWithItem()) {
drawTooltip(mouseX, mouseY, items.get(slotNumber).getStack()); drawTooltip(mouseX, mouseY, items.get(slotNumber).getStack());
} }
if (isHoveringOverClear(mouseX, mouseY)) { if (isOverClear(mouseX, mouseY)) {
drawTooltip(mouseX, mouseY, t("misc.refinedstorage:clear")); drawTooltip(mouseX, mouseY, t("misc.refinedstorage:clear"));
} }
if (isHoveringOverCreatePattern(mouseX, mouseY)) { if (isOverCreatePattern(mouseX, mouseY)) {
drawTooltip(mouseX, mouseY, t("gui.refinedstorage:grid.pattern_create")); drawTooltip(mouseX, mouseY, t("gui.refinedstorage:grid.pattern_create"));
} }
} }
@@ -355,8 +349,8 @@ public class GuiGrid extends GuiBase {
updateJEI(); updateJEI();
} }
boolean clickedClear = clickedButton == 0 && isHoveringOverClear(mouseX - guiLeft, mouseY - guiTop); boolean clickedClear = clickedButton == 0 && isOverClear(mouseX - guiLeft, mouseY - guiTop);
boolean clickedCreatePattern = clickedButton == 0 && isHoveringOverCreatePattern(mouseX - guiLeft, mouseY - guiTop); boolean clickedCreatePattern = clickedButton == 0 && isOverCreatePattern(mouseX - guiLeft, mouseY - guiTop);
if (clickedCreatePattern) { if (clickedCreatePattern) {
BlockPos gridPos = ((TileGrid) grid).getPos(); BlockPos gridPos = ((TileGrid) grid).getPos();
@@ -367,11 +361,11 @@ public class GuiGrid extends GuiBase {
RefinedStorage.NETWORK.sendToServer(new MessageGridCraftingClear((TileGrid) grid)); RefinedStorage.NETWORK.sendToServer(new MessageGridCraftingClear((TileGrid) grid));
} }
if (isHoveringOverSlotArea(mouseX - guiLeft, mouseY - guiTop) && container.getPlayer().inventory.getItemStack() != null && (clickedButton == 0 || clickedButton == 1)) { if (isOverSlotArea(mouseX - guiLeft, mouseY - guiTop) && container.getPlayer().inventory.getItemStack() != null && (clickedButton == 0 || clickedButton == 1)) {
grid.onHeldItemPush(clickedButton == 1); grid.onHeldItemPush(clickedButton == 1);
} }
if (isHoveringOverItemInSlot() && container.getPlayer().inventory.getItemStack() == null) { if (isOverSlotWithItem() && container.getPlayer().inventory.getItemStack() == null) {
if (items.get(slotNumber).getStack().stackSize == 0 || (GuiScreen.isShiftKeyDown() && GuiScreen.isCtrlKeyDown())) { if (items.get(slotNumber).getStack().stackSize == 0 || (GuiScreen.isShiftKeyDown() && GuiScreen.isCtrlKeyDown())) {
FMLCommonHandler.instance().showGuiScreen(new GuiCraftingSettings(this, slotId)); FMLCommonHandler.instance().showGuiScreen(new GuiCraftingSettings(this, slotId));
} else { } else {
@@ -413,8 +407,4 @@ public class GuiGrid extends GuiBase {
RefinedStorageJEIPlugin.INSTANCE.getRuntime().getItemListOverlay().setFilterText(searchField.getText()); RefinedStorageJEIPlugin.INSTANCE.getRuntime().getItemListOverlay().setFilterText(searchField.getText());
} }
} }
public int getVisibleRows() {
return (grid.getType() == EnumGridType.CRAFTING || grid.getType() == EnumGridType.PATTERN) ? 4 : 5;
}
} }

View File

@@ -74,16 +74,6 @@ public class Scrollbar {
wasClicking = false; wasClicking = false;
currentScroll = 0; currentScroll = 0;
} else { } else {
int wheel = Mouse.getDWheel();
wheel = Math.max(Math.min(-wheel, 1), -1);
if (wheel == -1) {
setCurrentScroll(currentScroll - scrollDelta);
} else if (wheel == 1) {
setCurrentScroll(currentScroll + scrollDelta);
}
boolean down = Mouse.isButtonDown(0); boolean down = Mouse.isButtonDown(0);
if (!wasClicking && down && gui.inBounds(x, y, scrollbarWidth, scrollbarHeight, mouseX, mouseY)) { if (!wasClicking && down && gui.inBounds(x, y, scrollbarWidth, scrollbarHeight, mouseX, mouseY)) {
@@ -101,4 +91,16 @@ public class Scrollbar {
} }
} }
} }
public void wheel(int delta) {
if (canScroll()) {
delta = Math.max(Math.min(-delta, 1), -1);
if (delta == -1) {
setCurrentScroll(currentScroll - scrollDelta);
} else if (delta == 1) {
setCurrentScroll(currentScroll + scrollDelta);
}
}
}
} }