Actual working scrolling in grid...

This commit is contained in:
Raoul Van den Berge
2016-03-22 20:45:34 +01:00
parent fe7a45f84b
commit e23a17983b

View File

@@ -36,8 +36,6 @@ public class GuiGrid extends GuiBase
private int hoveringSlotId; private int hoveringSlotId;
private int hoveringId; private int hoveringId;
private int offset;
private float currentScroll; private float currentScroll;
private boolean wasClicking = false; private boolean wasClicking = false;
private boolean isScrolling = false; private boolean isScrolling = false;
@@ -69,19 +67,22 @@ public class GuiGrid extends GuiBase
@Override @Override
public void update(int x, int y) public void update(int x, int y)
{ {
// @TODO: Make this use currentScroll if (canScroll())
{
int wheel = Mouse.getDWheel(); int wheel = Mouse.getDWheel();
wheel = Math.max(Math.min(-wheel, 1), -1); wheel = Math.max(Math.min(-wheel, 1), -1);
if (canScroll(wheel)) float delta = 20;
{
offset += wheel;
}
if (offset > getMaxOffset()) if (wheel == -1)
{ {
offset = getMaxOffset(); setCurrentScroll(currentScroll - delta);
}
else if (wheel == 1)
{
setCurrentScroll(currentScroll + delta);
}
} }
} }
@@ -91,6 +92,14 @@ public class GuiGrid extends GuiBase
} }
public void handleScrolling(int mouseX, int mouseY) public void handleScrolling(int mouseX, int mouseY)
{
if (!canScroll())
{
isScrolling = false;
wasClicking = false;
currentScroll = 0;
}
else
{ {
boolean down = Mouse.isButtonDown(0); boolean down = Mouse.isButtonDown(0);
@@ -108,42 +117,53 @@ public class GuiGrid extends GuiBase
if (isScrolling) if (isScrolling)
{ {
currentScroll = mouseY - 20; setCurrentScroll(mouseY - 20);
}
}
}
if (currentScroll < 0) public void setCurrentScroll(float newCurrentScroll)
{ {
currentScroll = 0; if (newCurrentScroll < 0)
}
if (currentScroll > (89 - 20 - 12))
{ {
currentScroll = 89 - 20 - 12; newCurrentScroll = 0;
}
}
} }
private int getMaxOffset() if (newCurrentScroll > (89 - 20 - 12 - 2))
{
newCurrentScroll = 89 - 20 - 12 - 2;
}
currentScroll = newCurrentScroll;
}
public boolean canScroll()
{
return getRows() > getVisibleRows();
}
public int getOffset()
{
return (int) (currentScroll / 70f * (float) getRows());
}
public int getVisibleRows()
{
return 4;
}
public int getRows()
{ {
if (!grid.isConnected()) if (!grid.isConnected())
{ {
return 0; return 0;
} }
int max = ((int) Math.ceil((float) getItems().size() / (float) 9)) - 4; int max = (int) Math.ceil((float) getItems().size() / (float) 9);
return max < 0 ? 0 : max; return max < 0 ? 0 : max;
} }
private boolean canScroll(int delta)
{
if (offset + delta < 0)
{
return false;
}
return offset + delta <= getMaxOffset();
}
private boolean isHoveringOverValidSlot(List<StorageItem> items) private boolean isHoveringOverValidSlot(List<StorageItem> items)
{ {
return grid.isConnected() && isHoveringOverSlot() && hoveringSlotId < items.size(); return grid.isConnected() && isHoveringOverSlot() && hoveringSlotId < items.size();
@@ -178,7 +198,7 @@ public class GuiGrid extends GuiBase
drawTexture(x, y, 0, 0, width, height); drawTexture(x, y, 0, 0, width, height);
drawTexture(x + 174, y + 20 + (int) currentScroll, 232, 0, 12, 15); drawTexture(x + 174, y + 20 + (int) currentScroll, canScroll() ? 232 : 244, 0, 12, 15);
searchField.drawTextBox(); searchField.drawTextBox();
} }
@@ -204,11 +224,11 @@ public class GuiGrid extends GuiBase
hoveringSlotId = -1; hoveringSlotId = -1;
int slot = offset * 9; int slot = getOffset() * 9;
RenderHelper.enableGUIStandardItemLighting(); RenderHelper.enableGUIStandardItemLighting();
for (int i = 0; i < 9 * 4; ++i) for (int i = 0; i < 9 * getVisibleRows(); ++i)
{ {
if (slot < items.size()) if (slot < items.size())
{ {