From c365433e34bdd95e83740a5af6a3338444a3540a Mon Sep 17 00:00:00 2001 From: Falkreon Date: Sun, 25 Aug 2019 21:38:11 -0500 Subject: More gui test, fix slot offsets, tooltips, and button hovers --- .../cottonmc/cotton/gui/client/CottonScreen.java | 2 +- .../github/cottonmc/cotton/gui/widget/WButton.java | 6 ++ .../cottonmc/cotton/gui/widget/WItemSlot.java | 2 +- .../github/cottonmc/cotton/gui/widget/WLabel.java | 11 +-- .../cottonmc/cotton/gui/widget/WListPanel.java | 13 ++- .../cottonmc/cotton/gui/widget/WScrollBar.java | 99 +++++++++++++++++++--- .../cottonmc/cotton/gui/widget/WTextField.java | 5 ++ 7 files changed, 112 insertions(+), 26 deletions(-) (limited to 'src/main/java') 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 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 extends WPanel { protected int margin = 4; - protected int scrollOffset; + protected WScrollBar scrollBar = new WScrollBar(Axis.VERTICAL); public WListPanel(List data, Class listItemClass, Supplier supplier, BiConsumer 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 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 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; imaxValue) ? 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; -- cgit