diff options
Diffstat (limited to 'src/main')
7 files changed, 112 insertions, 26 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java index 390b715..6935894 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java @@ -190,7 +190,7 @@ public class CottonScreen<T extends CottonScreenController> extends AbstractCont } if (this.container.getRootPanel()!=null) { - this.container.getRootPanel().paintForeground(0, 0, mouseX+left, mouseY+top); + this.container.getRootPanel().paintForeground(0, 0, mouseX-left, mouseY-top); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index 3b6c299..0f5159c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -31,6 +31,8 @@ public class WButton extends WWidget { @Override public void paintForeground(int x, int y, int mouseX, int mouseY) { + System.out.println("Mouse: { "+mouseX+", "+mouseY+" }"); + boolean hovered = (mouseX>=x && mouseY>=y && mouseX<x+getWidth() && mouseY<y+getHeight()); int state = 1; //1=regular. 2=hovered. 0=disabled. if (!enabled) state = 0; @@ -64,6 +66,10 @@ public class WButton extends WWidget { super.paintForeground(x, y, mouseX, mouseY); } + @Override + public void setSize(int x, int y) { + super.setSize(x, 20); + } @Override public void onClick(int x, int y, int button) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java index 64d4171..f9918b0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java @@ -93,7 +93,7 @@ public class WItemSlot extends WWidget { for (int y = 0; y < slotsHigh; y++) { for (int x = 0; x < slotsWide; x++) { - ValidatedSlot slot = new ValidatedSlot(inventory, index, this.getAbsoluteX() + (x * 18) + 1, this.getAbsoluteY() + (y * 18) + 1); + ValidatedSlot slot = new ValidatedSlot(inventory, index, this.getAbsoluteX() + (x * 18), this.getAbsoluteY() + (y * 18)); peers.add(slot); c.addSlotPeer(slot); index++; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index 6c6f82a..d17729f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -34,17 +34,12 @@ public class WLabel extends WWidget { @Override public boolean canResize() { - return false; + return true; } @Override - public int getWidth() { - return 8; //We don't actually clip to our boundaries so return a dummy value. - } - - @Override - public int getHeight() { - return 8; + public void setSize(int x, int y) { + super.setSize(x, 20); } public WLabel setDarkmodeColor(int color) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java index a98857e..21bff58 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java @@ -6,8 +6,6 @@ import java.util.List; import java.util.function.BiConsumer; import java.util.function.Supplier; -import io.github.cottonmc.cotton.gui.GuiDescription; -import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; /** @@ -30,13 +28,14 @@ public class WListPanel<D, W extends WWidget> extends WPanel { protected int margin = 4; - protected int scrollOffset; + protected WScrollBar scrollBar = new WScrollBar(Axis.VERTICAL); public WListPanel(List<D> data, Class<W> listItemClass, Supplier<W> supplier, BiConsumer<D, W> configurator) { this.data = data; this.listItemClass = listItemClass; this.supplier = supplier; this.configurator = configurator; + scrollBar.setMaxValue(data.size()); } @Override @@ -56,6 +55,8 @@ public class WListPanel<D, W extends WWidget> extends WPanel { public void layout() { super.layout(); + int scrollOffset = scrollBar.value; + System.out.println("Validating"); //Recompute cellHeight if needed @@ -83,6 +84,12 @@ public class WListPanel<D, W extends WWidget> extends WPanel { System.out.println("Adding children..."); this.children.clear(); + this.children.add(scrollBar); + scrollBar.setLocation(this.width-4, 0); + scrollBar.setSize(4, this.height); + scrollBar.window = cellsHigh; + scrollBar.setMaxValue(data.size()); + if (presentCells>0) { for(int i=0; i<presentCells; i++) { int index = i+scrollOffset; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index 289a2cd..6eb6359 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -8,6 +8,8 @@ public class WScrollBar extends WWidget { protected int maxValue = 100; protected int window = 16; + protected int anchor = -1; + public WScrollBar() { } @@ -18,25 +20,96 @@ public class WScrollBar extends WWidget { @Override public void paintBackground(int x, int y) { ScreenDrawing.drawBeveledPanel(x, y, width, height, 0xFFFFFFFF, 0xFF8b8b8b, 0xFF373737); + if (maxValue<=0) return; - float barHeight = (window>maxValue) ? 1f : window / maxValue; - int scrollDistance = maxValue - window; + int color = 0xFF_FFFFFF; + if (axis==Axis.HORIZONTAL) { + ScreenDrawing.rect(x+1+getHandlePosition(), y+1, getHandleSize(), height-2, color); + } else { + ScreenDrawing.rect(x+1, y+1+getHandlePosition(), width-2, getHandleSize(), color); + } + } + + /** + * Gets the on-axis size of the scrollbar handle in gui pixels + */ + public int getHandleSize() { + float percentage = (window>maxValue) ? 1f : window / (float)maxValue; + int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; + return (int)(percentage*bar); + } + + /** + * Gets the number of pixels the scrollbar handle is able to move along its track from one end to the other. + */ + public int getMovableDistance() { + int logicalDistance = maxValue-window; + if (logicalDistance<0) logicalDistance = 0; + float percentage = logicalDistance / maxValue; + return (int) ( (axis==Axis.HORIZONTAL) ? (width-2)*percentage : (height-2)*percentage); + } + + public int getHandlePosition() { + float percent = value / (float)Math.max(maxValue, 1); + return (int)(percent * getMovableDistance()); + } + + /** + * Gets the maximum scroll value achievable; this will typically be the maximum value minus the + * window size + */ + public int getMaxScrollValue() { + return maxValue - window; + } + + @Override + public WWidget onMouseDown(int x, int y, int button) { + //TODO: Clicking before or after the handle should jump instead of scrolling - super.paintBackground(x, y); + if (axis==Axis.HORIZONTAL) { + anchor = x; + } else { + anchor = y; + } + System.out.println("Anchor set to "+anchor); + return this; + } + + @Override + public void onMouseDrag(int x, int y, int button) { + int delta = 0; + if (axis==Axis.HORIZONTAL) { + delta = x-anchor; + } else { + delta = y-anchor; + } + + float percentMoved = (delta / (float)getMovableDistance()); + int valueDelta = (int)(percentMoved * maxValue); + //System.out.println("Anchor: "+anchor+", Delta: "+delta+", ValueDelta: "+valueDelta); + + super.onMouseDrag(x, y, button); } @Override - public void setSize(int x, int y) { - switch(axis) { - case HORIZONTAL: - this.width = x; - this.height = 8; - break; - case VERTICAL: - this.width = 8; - this.height = y; - break; + public WWidget onMouseUp(int x, int y, int button) { + //TODO: Clicking before or after the handle should jump instead of scrolling + System.out.println("Anchor released."); + anchor = -1; + + return this; + } + + public int getValue() { + return value; + } + + public WScrollBar setMaxValue(int max) { + this.maxValue = max; + if (this.value>maxValue-window) { + this.value = maxValue-window; } + return this; } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java index 217e5d3..99ea221 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java @@ -81,6 +81,11 @@ public class WTextField extends WWidget { return true; } + @Override + public void setSize(int x, int y) { + super.setSize(x, 20); + } + /* public String getSelectedText() { int start = this.cursorMax < this.cursorMin ? this.cursorMax : this.cursorMin; |