aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorFalkreon <falkreon@gmail.com>2019-08-25 23:04:12 -0500
committerFalkreon <falkreon@gmail.com>2019-08-25 23:04:12 -0500
commitc4fe40d1daee84662c6c269f53dd68a2c2a136f1 (patch)
tree3a00fa41b1ed4fe5cfddfc8646f9daaf42e1d79d /src/main/java
parentc365433e34bdd95e83740a5af6a3338444a3540a (diff)
downloadLibGui-c4fe40d1daee84662c6c269f53dd68a2c2a136f1.tar.gz
LibGui-c4fe40d1daee84662c6c269f53dd68a2c2a136f1.tar.bz2
LibGui-c4fe40d1daee84662c6c269f53dd68a2c2a136f1.zip
More responsive scrollbar. Still glitchy though.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java23
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java30
2 files changed, 41 insertions, 12 deletions
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 21bff58..de3c7ed 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
@@ -29,6 +29,7 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
protected int margin = 4;
protected WScrollBar scrollBar = new WScrollBar(Axis.VERTICAL);
+ int lastScroll = -1;
public WListPanel(List<D> data, Class<W> listItemClass, Supplier<W> supplier, BiConsumer<D, W> configurator) {
this.data = data;
@@ -46,6 +47,11 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
ScreenDrawing.drawBeveledPanel(x, y, width, height);
}
+ if (scrollBar.getValue()!=lastScroll) {
+ layout();
+ lastScroll = scrollBar.getValue();
+ }
+
for(WWidget child : children) {
child.paintBackground(x + child.getX(), y + child.getY());
}
@@ -53,9 +59,7 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
@Override
public void layout() {
- super.layout();
-
- int scrollOffset = scrollBar.value;
+ //super.layout();
System.out.println("Validating");
@@ -79,16 +83,22 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
int layoutHeight = this.getHeight()-(margin*2);
int cellsHigh = layoutHeight / cellHeight;
- int presentCells = Math.min(data.size()-scrollOffset, cellsHigh);
+
System.out.println("Adding children...");
this.children.clear();
this.children.add(scrollBar);
- scrollBar.setLocation(this.width-4, 0);
- scrollBar.setSize(4, this.height);
+ scrollBar.setLocation(this.width-scrollBar.getWidth(), 0);
+ scrollBar.setSize(8, this.height);
+
+ //Fix up the scrollbar handle and track metrics
scrollBar.window = cellsHigh;
scrollBar.setMaxValue(data.size());
+ int scrollOffset = scrollBar.value;
+ System.out.println(scrollOffset);
+
+ int presentCells = Math.min(data.size()-scrollOffset, cellsHigh);
if (presentCells>0) {
for(int i=0; i<presentCells; i++) {
@@ -102,6 +112,7 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
w = unconfigured.remove(0);
}
configurator.accept(d, w);
+ configured.put(d, w);
}
//At this point, w is nonnull and configured by d
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 6eb6359..f84342c 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
@@ -9,6 +9,7 @@ public class WScrollBar extends WWidget {
protected int window = 16;
protected int anchor = -1;
+ protected int anchorValue = -1;
public WScrollBar() {
}
@@ -19,7 +20,7 @@ public class WScrollBar extends WWidget {
@Override
public void paintBackground(int x, int y) {
- ScreenDrawing.drawBeveledPanel(x, y, width, height, 0xFFFFFFFF, 0xFF8b8b8b, 0xFF373737);
+ ScreenDrawing.drawBeveledPanel(x, y, width, height, 0xFF373737, 0xFF_000000, 0xFFFFFFFF);
if (maxValue<=0) return;
int color = 0xFF_FFFFFF;
@@ -30,11 +31,21 @@ public class WScrollBar extends WWidget {
}
}
+ @Override
+ public void paintForeground(int x, int y, int mouseX, int mouseY) {
+ super.paintForeground(x, y, mouseX, mouseY);
+
+ //Sneakily update bar position
+ if (anchor!=-1) {
+ onMouseDown(mouseX+x, mouseY+y, 0);
+ }
+ }
+
/**
* Gets the on-axis size of the scrollbar handle in gui pixels
*/
public int getHandleSize() {
- float percentage = (window>maxValue) ? 1f : window / (float)maxValue;
+ float percentage = (window>=maxValue) ? 1f : window / (float)maxValue;
int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2;
return (int)(percentage*bar);
}
@@ -45,8 +56,9 @@ public class WScrollBar extends WWidget {
public int getMovableDistance() {
int logicalDistance = maxValue-window;
if (logicalDistance<0) logicalDistance = 0;
- float percentage = logicalDistance / maxValue;
+ float percentage = logicalDistance / (float)maxValue;
+ //System.out.println("MovableDistance! maxValue: "+maxValue+", window: "+window+", logicalDistance: "+logicalDistance+", percentage: "+percentage);
return (int) ( (axis==Axis.HORIZONTAL) ? (width-2)*percentage : (height-2)*percentage);
}
@@ -69,10 +81,11 @@ public class WScrollBar extends WWidget {
if (axis==Axis.HORIZONTAL) {
anchor = x;
+ anchorValue = value;
} else {
anchor = y;
+ anchorValue = value;
}
- System.out.println("Anchor set to "+anchor);
return this;
}
@@ -87,7 +100,12 @@ public class WScrollBar extends WWidget {
float percentMoved = (delta / (float)getMovableDistance());
int valueDelta = (int)(percentMoved * maxValue);
- //System.out.println("Anchor: "+anchor+", Delta: "+delta+", ValueDelta: "+valueDelta);
+ int valueNew = anchorValue + valueDelta;
+ if (valueNew>getMaxScrollValue()) valueNew = getMaxScrollValue();
+ if (valueNew<0) valueNew = 0;
+ this.value = valueNew;
+
+ //System.out.println("Anchor: "+anchor+", Delta: "+delta+", PercentMoved: "+percentMoved+", ValueDelta: "+valueDelta);
super.onMouseDrag(x, y, button);
}
@@ -95,8 +113,8 @@ public class WScrollBar extends WWidget {
@Override
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;
+ anchorValue = -1;
return this;
}