diff options
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java | 115 |
1 files changed, 27 insertions, 88 deletions
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 02805f1..27ee960 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 @@ -3,24 +3,16 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Axis; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; -public class WScrollBar extends WWidget { - protected Axis axis = Axis.HORIZONTAL; - protected int value; - protected int maxValue = 100; +public class WScrollBar extends WAbstractSlider { protected int window = 16; - - protected int anchor = -1; - protected int anchorValue = -1; - protected boolean sliding = false; /** * Constructs a horizontal scroll bar. */ public WScrollBar() { + super(0, 100, Axis.HORIZONTAL); } /** @@ -29,9 +21,21 @@ public class WScrollBar extends WWidget { * @param axis the axis */ public WScrollBar(Axis axis) { - this.axis = axis; + super(0, 100, axis); } - + + @Override + protected int getThumbWidth() { + return window; + } + + @Override + protected boolean isMouseInsideBounds(int x, int y) { + return axis == Axis.HORIZONTAL + ? (x >= getHandlePosition() + 1 && x <= getHandlePosition() + getHandleSize()) + : (y >= getHandlePosition() + 1 && y <= getHandlePosition() + getHandleSize()); + } + @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (LibGuiClient.config.darkMode) { @@ -39,12 +43,12 @@ public class WScrollBar extends WWidget { } else { ScreenDrawing.drawBeveledPanel(x, y, width, height, 0xFF_373737, 0xFF_8B8B8B, 0xFF_FFFFFF); } - if (maxValue<=0) return; + if (getMaxValue()<=0) return; // Handle colors int top, middle, bottom; - if (sliding) { + if (dragging) { if (LibGuiClient.config.darkMode) { top = 0xFF_6C6C6C; middle = 0xFF_2F2F2F; @@ -87,7 +91,7 @@ public class WScrollBar extends WWidget { * 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>=getMaxValue()) ? 1f : window / (float)getMaxValue(); int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; int result = (int)(percentage*bar); if (result<6) result = 6; @@ -102,15 +106,8 @@ public class WScrollBar extends WWidget { return bar-getHandleSize(); } - public int pixelsToValues(int pixels) { - int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; - //int bar = getMovableDistance(); - float percent = pixels / (float)bar; - return (int)(percent*(maxValue-window)); - } - public int getHandlePosition() { - float percent = value / (float)Math.max(maxValue-window, 1); + float percent = value / (float)Math.max(getMaxValue()-window, 1); return (int)(percent * getMovableDistance()); } @@ -119,75 +116,17 @@ public class WScrollBar extends WWidget { * window size */ public int getMaxScrollValue() { - return maxValue - window; - } - - protected void adjustSlider(int x, int y) { - - int delta = 0; - if (axis==Axis.HORIZONTAL) { - delta = x-anchor; - } else { - delta = y-anchor; - } - - int valueDelta = pixelsToValues(delta); - int valueNew = anchorValue + valueDelta; - - if (valueNew>getMaxScrollValue()) valueNew = getMaxScrollValue(); - if (valueNew<0) valueNew = 0; - this.value = valueNew; - } - - @Override - public WWidget onMouseDown(int x, int y, int button) { - //TODO: Clicking before or after the handle should jump instead of scrolling - - if (axis==Axis.HORIZONTAL) { - anchor = x; - anchorValue = value; - } else { - anchor = y; - anchorValue = value; - } - sliding = true; - return this; + return getMaxValue() - window; } - @Environment(EnvType.CLIENT) - @Override - public void onMouseDrag(int x, int y, int button) { - adjustSlider(x, y); - } - - @Environment(EnvType.CLIENT) - @Override - public WWidget onMouseUp(int x, int y, int button) { - //TODO: Clicking before or after the handle should jump instead of scrolling - anchor = -1; - anchorValue = -1; - sliding = false; - return this; - } - - public int getValue() { - return value; - } - - public WScrollBar setValue(int value) { - this.value = value; + public void setValue(int value) { + super.setValue(value); checkValue(); - return this; - } - - public int getMaxValue() { - return maxValue; } - public WScrollBar setMaxValue(int max) { - this.maxValue = max; + public void setMaxValue(int max) { + super.setMaxValue(max); checkValue(); - return this; } public int getWindow() { @@ -204,8 +143,8 @@ public class WScrollBar extends WWidget { * and adjusts it if needed. */ protected void checkValue() { - if (this.value>maxValue-window) { - this.value = maxValue-window; + if (this.value>getMaxValue()-window) { + this.value = getMaxValue()-window; } if (this.value<0) this.value = 0; } |