Skip to content

Commit

Permalink
Fixed scrollbar velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Apr 6, 2024
1 parent b91e80b commit a9b2836
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ public void mouseMoved(double mouseX, double mouseY) {
}

public int getMouseYOffset() {
return -margin.top - getScrollY() - getContentPadding().top;
return -getBounds().top - getScrollY() - getContentPadding().top;
}

public int getMouseXOffset() {
return -margin.left - getScrollX() - getContentPadding().left;
return -getBounds().left - getScrollX() - getContentPadding().left;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import com.minelittlepony.common.client.gui.scrollable.ScrollbarScrubber;

import net.minecraft.client.gui.Drawable;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.util.Window;
import net.minecraft.sound.SoundEvents;

/**
Expand Down Expand Up @@ -153,11 +155,13 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
}

private double calculateInternalYPosition(double mouseY) {
return mouseY + rootView.getScrollY() + rootView.getContentPadding().top;
double yOffset = -rootView.getScrollY() - rootView.getContentPadding().top;
return mouseY - yOffset;
}

private double calculateInternalXPosition(double mouseX) {
return mouseX + rootView.getScrollX() + rootView.getContentPadding().left;
double xOffset = -rootView.getScrollX() - rootView.getContentPadding().left;
return mouseX - xOffset;
}

@Override
Expand All @@ -167,11 +171,15 @@ public boolean mouseDragged(double mouseX, double mouseY, int button, double dif

double mousePosition = orientation.pick(mouseX, mouseY);

Window window = MinecraftClient.getInstance().getWindow();
double motionRatio = orientation.pick(window.getWidth(), window.getHeight()) / (double)orientation.pick(bounds.width, bounds.height);
double change = motionRatio * (prevMousePosition - mousePosition);

if (dragging) {
scrubber.scrollBy(-(int)(prevMousePosition - mousePosition), false);
scrubber.scrollBy(-(int)change, false);
} else if (touching) {
scrubber.scrollBy((int)(mousePosition - prevMousePosition) / 16, true);
scrubber.setMomentum((int)(mousePosition - prevMousePosition));
scrubber.scrollBy(-(int)change * 16, true);
scrubber.setMomentum(-(int)change);
}

prevMousePosition = mousePosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ public void scrollTo(int position, boolean animate) {
if (!animate) {
currentPosition = targetPosition;
}
setMomentum(0);
}

public void setMomentum(int momentum) {
this.momentum = momentum;
this.momentum += momentum;
}

public void reposition(Bounds containerBounds, Bounds contentBounds) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"kirin.mixin.json"
],
"license": "MIT",
"accessWidener": "kirin.aw",
"icon": "kirin_logo.png",
"environment": "*",
"depends": {
Expand Down

0 comments on commit a9b2836

Please sign in to comment.