Update tree camera and scrollbar dragging every frame (#1087)

This commit is contained in:
slprime authored and GitHub committed 2026-10-22 23:58:41 +03:00
1 parent e4e1964dec
commit 68bdb51c6b
4 files changed
+41 -12

No files matched your search

@@ -2,6 +2,8 @@ package codechicken.nei.bookmark.tree;
import java.awt.geom.Point2D;
import org.lwjgl.input.Mouse;
class Camera {
private static final float ZOOM_MIN = 0.1f;
@@ -39,23 +41,34 @@ class Camera {
&& y < this.widgetY + this.widgetH;
}
public void startDrag(int mouseX, int mouseY) {
public void startDrag(Point2D mouse) {
this.dragging = true;
this.lastDragX = mouseX;
this.lastDragY = mouseY;
this.lastDragX = (float) mouse.getX();
this.lastDragY = (float) mouse.getY();
}
public void drag(int mouseX, int mouseY) {
public void drag(Point2D mouse) {
if (!this.dragging) {
return;
}
final float mouseX = (float) mouse.getX();
final float mouseY = (float) mouse.getY();
this.scrollX -= (mouseX - this.lastDragX) / this.zoomLevel;
this.scrollY -= (mouseY - this.lastDragY) / this.zoomLevel;
this.lastDragX = mouseX;
this.lastDragY = mouseY;
}
public void update(Point2D mouse, int treeWidth, int treeHeight) {
if (this.dragging && Mouse.isButtonDown(0)) {
drag(mouse);
}
clampToBounds(treeWidth, treeHeight);
}
public void endDrag() {
this.dragging = false;
}
@@ -79,7 +92,6 @@ class Camera {
} else {
this.scrollY -= direction * this.widgetH / (SCROLL_STEP_DIVISOR * this.zoomLevel);
}
}
public void centerOn(float worldX, float worldY) {
@@ -169,12 +169,12 @@ class CraftingTreeCanvas {
: -FIT_MARGIN;
}
public void startDrag(int mouseX, int mouseY) {
this.camera.startDrag(mouseX, mouseY);
public void startDrag(Point2D mouse) {
this.camera.startDrag(mouse);
}
public void drag(int mouseX, int mouseY) {
this.camera.drag(mouseX, mouseY);
public void drag(Point2D mouse) {
this.camera.drag(mouse);
}
public void endDrag() {
@@ -259,7 +259,7 @@ class CraftingTreeCanvas {
public void draw(Point2D mouse, Set<BookmarkItem> collapsedItems, SearchResult searchResult,
boolean useInventorySnapshot, ItemTreeSlot hoveredNode, ItemTreeSlot deleteHoverNode) {
this.camera.clampToBounds(this.treeWidth, this.treeHeight);
this.camera.update(mouse, this.treeWidth, this.treeHeight);
final float zScrollX = this.camera.scrollX * this.camera.zoomLevel;
final float zScrollY = this.camera.scrollY * this.camera.zoomLevel;
@@ -757,7 +757,7 @@ public class GuiCraftingTree extends GuiContainer implements INEIGuiHandler, IGu
return true;
}
this.canvas.startDrag(mouseX, mouseY);
this.canvas.startDrag(getMousePosition());
}
return false;
@@ -778,7 +778,7 @@ public class GuiCraftingTree extends GuiContainer implements INEIGuiHandler, IGu
}
this.canvas.drag(mouseX, mouseY);
this.canvas.drag(getMousePosition());
}
@Override
@@ -7,6 +7,7 @@ import java.util.Map;
import java.util.function.Predicate;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import codechicken.lib.gui.GuiDraw;
@@ -249,6 +250,8 @@ public class ScrollContainer extends WidgetContainer {
final boolean canScrollHorizontal = canScrollHorizontal();
if (canScrollVertical || canScrollHorizontal) {
dragScrollBars(mousex, mousey, canScrollVertical, canScrollHorizontal);
GuiHelper.useScissor(
this.x + this.paddingInlineStart,
this.y + this.paddingBlockStart,
@@ -273,6 +276,20 @@ public class ScrollContainer extends WidgetContainer {
}
private void dragScrollBars(int mousex, int mousey, boolean canScrollVertical, boolean canScrollHorizontal) {
if (!Mouse.isButtonDown(0)) {
return;
}
if (canScrollVertical) {
this.verticalScrollBar.onMouseDragged(this, mousex, mousey, 0, 0);
}
if (canScrollHorizontal) {
this.horizontalScrollBar.onMouseDragged(this, mousex, mousey, 0, 0);
}
}
protected void drawContent(int mousex, int mousey) {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glPushMatrix();