diff --git a/src/main/java/org/saturnclient/ui/PanelScreen.java b/src/main/java/org/saturnclient/ui/PanelScreen.java index 65d8fbb..2bf84a2 100644 --- a/src/main/java/org/saturnclient/ui/PanelScreen.java +++ b/src/main/java/org/saturnclient/ui/PanelScreen.java @@ -3,6 +3,7 @@ package org.saturnclient.ui; import org.saturnclient.ui.components.Sidebar; import org.saturnclient.ui.elements.ImageTexture; import org.saturnclient.ui.elements.Panel; +import org.saturnclient.ui.elements.Scroll; import org.saturnclient.ui.resources.Textures; /** @@ -18,6 +19,9 @@ public abstract class PanelScreen extends SaturnScreen { protected static final int PADDING = 20; + /** How far a scroll reaches past its content area, into the padding, for its bar */ + private static final int SCROLL_OVERHANG = 14; + private final int sidebarItem; protected int panelX, panelY, panelWidth, panelHeight; @@ -61,4 +65,18 @@ public abstract class PanelScreen extends SaturnScreen { * Draws the panel's content. */ protected abstract void panel(); + + /** + * The width of a scroll filling {@code width} of the content area. + */ + protected static int scrollWidth(int width) { + return width + SCROLL_OVERHANG; + } + + /** + * The width left for a scroll's children, clear of its scrollbar. + */ + protected static int scrollContentWidth(int width) { + return scrollWidth(width) - Scroll.SCROLLBAR_SPACE; + } } diff --git a/src/main/java/org/saturnclient/ui/components/inputs/FloatInput.java b/src/main/java/org/saturnclient/ui/components/inputs/FloatInput.java index 4e10b34..bc2908e 100644 --- a/src/main/java/org/saturnclient/ui/components/inputs/FloatInput.java +++ b/src/main/java/org/saturnclient/ui/components/inputs/FloatInput.java @@ -1,5 +1,8 @@ package org.saturnclient.ui.components.inputs; +import java.math.BigDecimal; +import java.math.RoundingMode; + import org.saturnclient.config.ConfigManager; import org.saturnclient.config.property.FloatProperty; import org.saturnclient.ui.elements.Field; @@ -11,7 +14,16 @@ public class FloatInput extends Input { this.width = 96; this.height = Field.HEIGHT; this.prop = prop; - this.text = String.valueOf(prop.value); + this.text = format(prop.value); + } + + /** + * The value with at most three decimals and no trailing zeros, since floats + * like 1.2000000476837158 can't be stored exactly. + */ + private static String format(float value) { + return new BigDecimal(Float.toString(value)).setScale(3, RoundingMode.HALF_UP).stripTrailingZeros() + .toPlainString(); } @Override @@ -51,7 +63,7 @@ public class FloatInput extends Input { @Override public void checkReset() { if (prop.isReset) { - this.text = String.valueOf(prop.value); + this.text = format(prop.value); this.cursorPosition = 0; prop.isReset = false; } diff --git a/src/main/java/org/saturnclient/ui/components/inputs/Slider.java b/src/main/java/org/saturnclient/ui/components/inputs/Slider.java index b9f24ab..13f1965 100644 --- a/src/main/java/org/saturnclient/ui/components/inputs/Slider.java +++ b/src/main/java/org/saturnclient/ui/components/inputs/Slider.java @@ -23,6 +23,7 @@ public class Slider extends Element { private static final int KNOB = 16; private static final int VALUE_WIDTH = 72; private static final int VALUE_GAP = 14; + private static final int MAX_DECIMALS = 3; private final double min, max, step; private final int decimals; @@ -32,25 +33,28 @@ public class Slider extends Element { private boolean dragging = false; - private Slider(double min, double max, double step, String unit, DoubleSupplier value, DoubleConsumer onChange) { + /** + * @param step as text, so a float step like 0.1 isn't read as 0.10000000149 + */ + private Slider(double min, double max, String step, String unit, DoubleSupplier value, DoubleConsumer onChange) { this.min = min; this.max = max; - this.step = step; + this.step = Double.parseDouble(step); this.unit = unit; this.value = value; this.onChange = onChange; - this.decimals = Math.max(0, BigDecimal.valueOf(step).stripTrailingZeros().scale()); + this.decimals = Math.min(MAX_DECIMALS, Math.max(0, new BigDecimal(step).stripTrailingZeros().scale())); this.width = 280; this.height = Field.HEIGHT; } public static Slider of(IntProperty prop) { - return new Slider(prop.min, prop.max, prop.step, prop.unit, () -> prop.value, + return new Slider(prop.min, prop.max, Integer.toString(prop.step), prop.unit, () -> prop.value, v -> prop.value = (int) Math.round(v)); } public static Slider of(FloatProperty prop) { - return new Slider(prop.min, prop.max, prop.step, prop.unit, () -> prop.value, + return new Slider(prop.min, prop.max, Float.toString(prop.step), prop.unit, () -> prop.value, v -> prop.value = (float) v); } diff --git a/src/main/java/org/saturnclient/ui/elements/Scroll.java b/src/main/java/org/saturnclient/ui/elements/Scroll.java index fe6fcf3..a9ddef1 100644 --- a/src/main/java/org/saturnclient/ui/elements/Scroll.java +++ b/src/main/java/org/saturnclient/ui/elements/Scroll.java @@ -4,24 +4,40 @@ import java.util.ArrayList; import java.util.List; import org.saturnclient.config.Theme; -import org.saturnclient.config.property.IntProperty; -import org.saturnclient.config.property.Property; import org.saturnclient.ui.Element; import org.saturnclient.ui.ElementContext; import org.saturnclient.ui.ElementRenderer; import org.saturnclient.ui.RenderScope; +/** + * A vertically scrolling container. It scrolls with the mouse wheel or by + * dragging its scrollbar, which shows along its right edge when the children + * don't fit. + */ public class Scroll extends Element { - private static IntProperty scrollBarRadius = Property.integer(10); - private static IntProperty scrollBarWidth = Property.integer(5); - private static IntProperty scrollBarPadding = Property.integer(5); + /** Units scrolled per mouse wheel notch */ + private static final double SCROLL_SPEED = 24; + /** Space left below the last child when scrolled to the end */ + private static final int END_SPACE = 10; + + private static final int BAR_WIDTH = 5; + private static final int BAR_INSET = 5; + private static final int MIN_THUMB = 20; + + /** Width to keep clear at the right of the content for the scrollbar and its click area */ + public static final int SCROLLBAR_SPACE = BAR_WIDTH + BAR_INSET * 2 + 4; int padding = 0; boolean background = true; protected List children = new ArrayList<>(); - int scroll = 0; - int maxScroll = 0; + + /** Kept fractional so small trackpad deltas add up instead of being dropped */ + private double scroll = 0; + private int maxScroll = 0; + + /** Where in the thumb it was grabbed, while it's being dragged */ + private Integer thumbGrab = null; public Scroll(int padding) { this.padding = padding; @@ -50,7 +66,8 @@ public class Scroll extends Element { @Override public void render(RenderScope renderScope, ElementContext ctx) { - calculateMaxScroll(); + updateMaxScroll(); + int offset = getOffset(); if (background) { renderScope.drawRoundedRectangle(0, 0, width, height, @@ -59,75 +76,27 @@ public class Scroll extends Element { renderScope.enableScissor(padding, padding, width - padding, height - padding); renderScope.getMatrixStack().push(); - renderScope.getMatrixStack().translate(padding, -scroll + padding); + renderScope.getMatrixStack().translate(padding, -offset + padding); ElementRenderer.render(children, ctx.elapsed, renderScope, ctx.mouseX - padding, - ctx.mouseY - padding + scroll); + ctx.mouseY - padding + offset); renderScope.getMatrixStack().pop(); renderScope.disableScissor(); if (maxScroll > 0) { - renderScope.drawRoundedRectangle( - width - scrollBarWidth.value - scrollBarPadding.value, - calculateScrollBarY(), - scrollBarWidth.value, - calculateScrollBarHeight(), - scrollBarRadius.value, - Theme.SCROLL.value); + boolean active = thumbGrab != null || ctx.isHovering(getBarX() - BAR_INSET, 0, BAR_WIDTH + BAR_INSET * 2, + height); + + renderScope.drawRoundedRectangle(getBarX(), getThumbY(), BAR_WIDTH, getThumbHeight(), BAR_WIDTH / 2, + active ? Theme.SCROLL.value : Theme.withAlpha(0.7f, Theme.SCROLL.value)); } } @Override public void scroll(int mouseX, int mouseY, double horizontalAmount, double verticalAmount) { - scroll -= verticalAmount; - if (scroll < 0) { - scroll = 0; - } else if (scroll > maxScroll) { - scroll = maxScroll; - } - } - - @Override - public Element dimensions(int width, int height) { - super.dimensions(width, height); - - maxScroll = 0; - for (Element element : children) { - maxScroll = Math.max(maxScroll, (element.y + element.height + padding) - (height - padding * 2)); - } - - return this; - } - - int calculateScrollBarHeight() { - if (maxScroll <= 0) - return height - (scrollBarPadding.value * 2); - return Math.max(20, (height * height) / (height + maxScroll + 10)) - (scrollBarPadding.value * 2); - } - - int calculateScrollBarY() { - int scrollBarHeight = calculateScrollBarHeight(); - if (maxScroll <= 0) - return scrollBarPadding.value; - int y = (scroll * (height - scrollBarHeight - scrollBarPadding.value * 2)) / maxScroll + scrollBarPadding.value; - return Math.min(y, height - scrollBarHeight - scrollBarPadding.value); - } - - private void calculateMaxScroll() { - maxScroll = 0; - - for (Element element : children) { - int bottom = element.y + element.height; - maxScroll = Math.max(maxScroll, bottom - height + padding); - } - - if (maxScroll > 0) { - maxScroll = maxScroll + 10; - } else { - maxScroll = 0; - } + setScroll(scroll - verticalAmount * SCROLL_SPEED); } @Override @@ -146,17 +115,80 @@ public class Scroll extends Element { return; } - ElementRenderer.mouseClicked(children, mouseX - padding, mouseY - padding + scroll, button); + if (maxScroll > 0 && mouseX >= getBarX() - BAR_INSET) { + int thumbY = getThumbY(); + int thumbHeight = getThumbHeight(); + + // Grab the thumb where it was clicked, or center it on a click in the track. + boolean onThumb = mouseY >= thumbY && mouseY <= thumbY + thumbHeight; + thumbGrab = onThumb ? (int) mouseY - thumbY : thumbHeight / 2; + dragThumb(mouseY); + return; + } + + ElementRenderer.mouseClicked(children, mouseX - padding, mouseY - padding + getOffset(), button); } @Override public void mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - ElementRenderer.mouseDragged(children, mouseX - padding, mouseY - padding + scroll, button, deltaX, + if (thumbGrab != null) { + dragThumb(mouseY); + return; + } + + ElementRenderer.mouseDragged(children, mouseX - padding, mouseY - padding + getOffset(), button, deltaX, deltaY); } @Override public void mouseReleased(double mouseX, double mouseY, int button) { - ElementRenderer.mouseReleased(children, mouseX - padding, mouseY - padding + scroll, button); + thumbGrab = null; + ElementRenderer.mouseReleased(children, mouseX - padding, mouseY - padding + getOffset(), button); + } + + private void dragThumb(double mouseY) { + int travel = getTrackHeight() - getThumbHeight(); + if (travel > 0) { + setScroll((mouseY - thumbGrab - BAR_INSET) / travel * maxScroll); + } + } + + private void setScroll(double value) { + scroll = Math.max(0, Math.min(maxScroll, value)); + } + + private int getOffset() { + return (int) Math.round(scroll); + } + + private void updateMaxScroll() { + int bottom = 0; + for (Element element : children) { + bottom = Math.max(bottom, element.y + element.height); + } + + int overflow = bottom + padding * 2 - height; + maxScroll = overflow > 0 ? overflow + END_SPACE : 0; + + // The content may have shrunk, such as after filtering. + setScroll(scroll); + } + + private int getBarX() { + return width - BAR_WIDTH - BAR_INSET; + } + + private int getTrackHeight() { + return height - BAR_INSET * 2; + } + + private int getThumbHeight() { + int track = getTrackHeight(); + return Math.max(MIN_THUMB, (int) ((long) track * height / (height + maxScroll))); + } + + private int getThumbY() { + int travel = getTrackHeight() - getThumbHeight(); + return BAR_INSET + (int) (travel * (scroll / maxScroll)); } } diff --git a/src/main/java/org/saturnclient/ui/screens/ClientSettings.java b/src/main/java/org/saturnclient/ui/screens/ClientSettings.java index d002207..db3fd4e 100644 --- a/src/main/java/org/saturnclient/ui/screens/ClientSettings.java +++ b/src/main/java/org/saturnclient/ui/screens/ClientSettings.java @@ -52,7 +52,6 @@ public class ClientSettings extends PanelScreen { private static final int LIST_Y = 58; private static final int SECTION_GAP = 22; private static final int PRESET_GAP = 10; - private static final int SCROLLBAR_SPACE = 14; private Section section = Section.GENERAL; @@ -88,14 +87,15 @@ public class ClientSettings extends PanelScreen { // The section's settings Scroll scroll = new Scroll(0).noBackground(); + int listWidth = scrollContentWidth(width); switch (section) { - case GENERAL -> PropertyList.draw(scroll, getGeneralProperties(), Set.of(), width, 0); - case UI_STYLE -> drawUiStyle(scroll, width); - case ANIMATIONS -> PropertyList.draw(scroll, AnimationConfig.config.getProperties(), Set.of(), width, 0); + case GENERAL -> PropertyList.draw(scroll, getGeneralProperties(), Set.of(), listWidth, 0); + case UI_STYLE -> drawUiStyle(scroll, listWidth); + case ANIMATIONS -> PropertyList.draw(scroll, AnimationConfig.config.getProperties(), Set.of(), listWidth, 0); } - draw(scroll.dimensions(width + SCROLLBAR_SPACE, contentHeight - LIST_Y).position(x, contentY + LIST_Y)); + draw(scroll.dimensions(scrollWidth(width), contentHeight - LIST_Y).position(x, contentY + LIST_Y)); } private void drawUiStyle(Scroll scroll, int width) { diff --git a/src/main/java/org/saturnclient/ui/screens/CosmeticsMenu.java b/src/main/java/org/saturnclient/ui/screens/CosmeticsMenu.java index 738a3d3..8fa0556 100644 --- a/src/main/java/org/saturnclient/ui/screens/CosmeticsMenu.java +++ b/src/main/java/org/saturnclient/ui/screens/CosmeticsMenu.java @@ -101,7 +101,6 @@ public class CosmeticsMenu extends PanelScreen { private static final int COLUMNS = 4; private static final int TILE_GAP = 10; private static final int GRID_Y = 58; - private static final int SCROLLBAR_SPACE = 14; private static final float SMALL_SCALE = 0.6f; private Kind kind = Kind.CLOAKS; @@ -139,10 +138,10 @@ public class CosmeticsMenu extends PanelScreen { scroll.draw(new Label(() -> message, 0.7f, Theme::getBodyFont, Theme::getMutedFg) .position(messageX, 60)); } else { - scroll.draw(createGrid(width)); + scroll.draw(createGrid(scrollContentWidth(width))); } - draw(scroll.dimensions(width + SCROLLBAR_SPACE, gridHeight).position(x, contentY + GRID_Y)); + draw(scroll.dimensions(scrollWidth(width), gridHeight).position(x, contentY + GRID_Y)); } /** diff --git a/src/main/java/org/saturnclient/ui/screens/ModMenu.java b/src/main/java/org/saturnclient/ui/screens/ModMenu.java index eb2e742..05f433a 100644 --- a/src/main/java/org/saturnclient/ui/screens/ModMenu.java +++ b/src/main/java/org/saturnclient/ui/screens/ModMenu.java @@ -24,7 +24,6 @@ public class ModMenu extends PanelScreen { private static final int COLUMNS = 3; private static final int CARD_GAP = 10; - private static final int SCROLLBAR_SPACE = 14; private String category = ALL; private String query = ""; @@ -40,7 +39,7 @@ public class ModMenu extends PanelScreen { protected void panel() { // The search box starts empty after a resize, so the query does too. query = ""; - gridWidth = contentWidth; + gridWidth = scrollContentWidth(contentWidth); // Header draw(new Label(() -> "Mods", 1.15f, () -> Theme.FONT.value, () -> Theme.FOREGROUND.value) @@ -66,7 +65,7 @@ public class ModMenu extends PanelScreen { // Mods int scrollY = chipsY + categories.height + 14; scroll = new Scroll(0).noBackground(); - scroll.dimensions(gridWidth + SCROLLBAR_SPACE, panelY + panelHeight - 12 - scrollY); + scroll.dimensions(scrollWidth(contentWidth), panelY + panelHeight - 12 - scrollY); refresh(true); draw(scroll.position(contentX, scrollY)); } diff --git a/src/main/java/org/saturnclient/ui/screens/ModSettings.java b/src/main/java/org/saturnclient/ui/screens/ModSettings.java index 9d924b9..f6b096a 100644 --- a/src/main/java/org/saturnclient/ui/screens/ModSettings.java +++ b/src/main/java/org/saturnclient/ui/screens/ModSettings.java @@ -33,7 +33,6 @@ public class ModSettings extends PanelScreen { private static final int PREVIEW_WIDTH = 280; private static final int PREVIEW_MAX_HEIGHT = 220; private static final int COLUMN_GAP = 22; - private static final int SCROLLBAR_SPACE = 14; private final Mod mod; @@ -77,8 +76,9 @@ public class ModSettings extends PanelScreen { } Scroll scroll = new Scroll(0).noBackground(); - PropertyList.draw(scroll, mod.getConfig().getProperties(), Set.of(ENABLED), listWidth, 0); - draw(scroll.dimensions(listWidth + SCROLLBAR_SPACE, bodyHeight).position(contentX, bodyY)); + PropertyList.draw(scroll, mod.getConfig().getProperties(), Set.of(ENABLED), scrollContentWidth(listWidth), + 0); + draw(scroll.dimensions(scrollWidth(listWidth), bodyHeight).position(contentX, bodyY)); } /**