From a0ee03a8a763ac4eee15190b6d68145acafc5154 Mon Sep 17 00:00:00 2001 From: Falkreon Date: Tue, 27 Aug 2019 15:39:49 -0500 Subject: Cleanup mouse events, add optional mouse hover info to paintBackground --- .../cotton/gui/CottonScreenController.java | 2 +- .../cotton/gui/client/ClientCottonScreen.java | 60 ++++++++++++++++++---- .../cottonmc/cotton/gui/client/CottonScreen.java | 30 ++++++++--- .../github/cottonmc/cotton/gui/widget/WButton.java | 4 +- .../cottonmc/cotton/gui/widget/WListPanel.java | 8 +-- .../github/cottonmc/cotton/gui/widget/WPanel.java | 8 +-- .../cottonmc/cotton/gui/widget/WScrollBar.java | 55 +++++++++++++++----- .../github/cottonmc/cotton/gui/widget/WWidget.java | 11 +++- 8 files changed, 136 insertions(+), 42 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java index 4710d5b..0e95295 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java @@ -306,7 +306,7 @@ public class CottonScreenController extends CraftingContainer impleme } } - if (rootPanel!=null) rootPanel.onClick(x, y, button); + //if (rootPanel!=null) rootPanel.onClick(x, y, button); } public void doCharType(char ch) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java index 16c0484..80ee311 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java @@ -57,7 +57,7 @@ public class ClientCottonScreen extends Screen { @Override public void render(int mouseX, int mouseY, float partialTicks) { - renderBackground(); + renderBackground(mouseX, mouseY); super.render(mouseX, mouseY, partialTicks); @@ -69,14 +69,13 @@ public class ClientCottonScreen extends Screen { } } - @Override - public void renderBackground() { + public void renderBackground(int mouseX, int mouseY) { super.renderBackground(); if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { - root.paintBackground(left, top); + root.paintBackground(left, top, mouseX-left, mouseY-top); } } @@ -93,6 +92,17 @@ public class ClientCottonScreen extends Screen { @Override public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { + if (description.getRootPanel()==null) return super.mouseClicked(mouseX, mouseY, mouseButton); + /* + boolean result = super.mouseClicked(mouseX, mouseY, mouseButton); + int containerX = (int)mouseX-left; + int containerY = (int)mouseY-top; + if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; + lastResponder = description.getRootPanel().onMouseDown(containerX, containerY, mouseButton); + + return result;*/ + + WWidget focus = description.getFocus(); if (focus!=null) { @@ -107,21 +117,23 @@ public class ClientCottonScreen extends Screen { } } - if (description.getRootPanel()==null) return super.mouseClicked(mouseX, mouseY, mouseButton); - boolean result = super.mouseClicked(mouseX, mouseY, mouseButton); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - lastResponder = description.getRootPanel().onMouseDown(containerX, containerY, mouseButton); - + if (lastResponder==null) { + lastResponder = description.getRootPanel().onMouseDown(containerX, containerY, mouseButton); + } else { + //This is a drag instead + } return result; + } @Override public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) { if (description.getRootPanel()==null) return super.mouseReleased(mouseX, mouseY, mouseButton); - + /* boolean result = super.mouseReleased(mouseX, mouseY, mouseButton); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; @@ -129,6 +141,21 @@ public class ClientCottonScreen extends Screen { WWidget responder = description.getRootPanel().onMouseUp(containerX, containerY, mouseButton); if (responder!=null && responder==lastResponder) description.getRootPanel().onClick(containerX, containerY, mouseButton); + lastResponder = null; + return result;*/ + boolean result = super.mouseReleased(mouseX, mouseY, mouseButton); + int containerX = (int)mouseX-left; + int containerY = (int)mouseY-top; + + if (lastResponder!=null) { + lastResponder.onMouseUp(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); + if (containerX>=0 && containerY>=0 && containerX=width || containerY>=height) return result; description.getRootPanel().onMouseDrag(containerX, containerY, mouseButton); + return result;*/ + boolean result = super.mouseDragged(mouseX, mouseY, mouseButton, unknown_1, unknown_2); + + int containerX = (int)mouseX-left; + int containerY = (int)mouseY-top; + + if (lastResponder!=null) { + lastResponder.onMouseDrag(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); + return result; + } else { + if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; + description.getRootPanel().onMouseDrag(containerX, containerY, mouseButton); + } return result; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java index 6935894..bb9f665 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonScreen.java @@ -100,7 +100,11 @@ public class CottonScreen extends AbstractCont int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - lastResponder = container.doMouseDown(containerX, containerY, mouseButton); + if (lastResponder==null) { + lastResponder = container.doMouseDown(containerX, containerY, mouseButton); + } else { + //This is a drag instead + } return result; } @@ -109,10 +113,16 @@ public class CottonScreen extends AbstractCont boolean result = super.mouseReleased(mouseX, mouseY, mouseButton); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - WWidget responder = container.doMouseUp(containerX, containerY, mouseButton); - if (responder!=null && responder==lastResponder) container.doClick(containerX, containerY, mouseButton); + if (lastResponder!=null) { + lastResponder.onMouseUp(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); + if (containerX>=0 && containerY>=0 && containerX extends AbstractCont int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - container.doMouseDrag(containerX, containerY, mouseButton); + + if (lastResponder!=null) { + lastResponder.onMouseDrag(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); + return result; + } else { + if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; + container.doMouseDrag(containerX, containerY, mouseButton); + } return result; } @@ -171,7 +187,7 @@ public class CottonScreen extends AbstractCont WPanel root = this.container.getRootPanel(); if (root==null) return; - root.paintBackground(left, top); + root.paintBackground(left, top, mouseX-left, mouseY-top); //TODO: Change this to a label that lives in the rootPanel instead? if (container instanceof Nameable) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index 0f5159c..32558a9 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -31,7 +31,7 @@ public class WButton extends WWidget { @Override public void paintForeground(int x, int y, int mouseX, int mouseY) { - System.out.println("Mouse: { "+mouseX+", "+mouseY+" }"); + //System.out.println("Mouse: { "+mouseX+", "+mouseY+" }"); boolean hovered = (mouseX>=x && mouseY>=y && mouseX extends WPanel { int cellsHigh = layoutHeight / cellHeight; - System.out.println("Adding children..."); + //System.out.println("Adding children..."); this.children.clear(); this.children.add(scrollBar); @@ -96,7 +96,7 @@ public class WListPanel extends WPanel { scrollBar.window = cellsHigh; scrollBar.setMaxValue(data.size()); int scrollOffset = scrollBar.value; - System.out.println(scrollOffset); + //System.out.println(scrollOffset); int presentCells = Math.min(data.size()-scrollOffset, cellsHigh); @@ -117,7 +117,7 @@ public class WListPanel extends WPanel { //At this point, w is nonnull and configured by d if (w.canResize()) { - w.setSize(this.width-(margin*2), cellHeight); + w.setSize(this.width-(margin*2) - scrollBar.getWidth(), cellHeight); } w.x = margin; w.y = margin + (cellHeight * i); @@ -125,7 +125,7 @@ public class WListPanel extends WPanel { } } - System.out.println("Children: "+children.size()); + //System.out.println("Children: "+children.size()); } public WListPanel setListItemHeight(int height) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 055e188..599df5d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -103,7 +103,7 @@ public abstract class WPanel extends WWidget { } super.onMouseDrag(x, y, button); } - + /* @Override public void onClick(int x, int y, int button) { if (children.isEmpty()) return; @@ -117,7 +117,7 @@ public abstract class WPanel extends WWidget { return; //Only send the message to the first valid recipient } } - } + }*/ @Override public void validate(GuiDescription c) { @@ -127,11 +127,11 @@ public abstract class WPanel extends WWidget { @Environment(EnvType.CLIENT) @Override - public void paintBackground(int x, int y) { + public void paintBackground(int x, int y, int mouseX, int mouseY) { if (backgroundPainter!=null) backgroundPainter.paintBackground(x, y, this); for(WWidget child : children) { - child.paintBackground(x + child.getX(), y + child.getY()); + child.paintBackground(x + child.getX(), y + child.getY(), mouseX-child.getX(), mouseY-child.getY()); } } 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 f84342c..bcab40a 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 @@ -1,6 +1,11 @@ package io.github.cottonmc.cotton.gui.widget; +import org.lwjgl.glfw.GLFW; + import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.util.InputUtil; +import net.minecraft.client.util.Window; public class WScrollBar extends WWidget { protected Axis axis = Axis.HORIZONTAL; @@ -10,6 +15,7 @@ public class WScrollBar extends WWidget { protected int anchor = -1; protected int anchorValue = -1; + protected boolean sliding = false; public WScrollBar() { } @@ -36,8 +42,8 @@ public class WScrollBar extends WWidget { super.paintForeground(x, y, mouseX, mouseY); //Sneakily update bar position - if (anchor!=-1) { - onMouseDown(mouseX+x, mouseY+y, 0); + if (sliding) { + adjustSlider(mouseX+x, mouseY+y); } } @@ -54,12 +60,12 @@ public class WScrollBar extends WWidget { * Gets the number of pixels the scrollbar handle is able to move along its track from one end to the other. */ public int getMovableDistance() { - int logicalDistance = maxValue-window; - if (logicalDistance<0) logicalDistance = 0; - 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); + //int logicalDistance = maxValue-window; + //if (logicalDistance<0) logicalDistance = 0; + //float percentage = logicalDistance / (float)maxValue; + int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; + return bar-getHandleSize(); + //return (int)(percentage*bar); } public int getHandlePosition() { @@ -75,20 +81,42 @@ public class WScrollBar extends WWidget { return maxValue - window; } + protected void adjustSlider(int x, int y) { + if (InputUtil.isKeyPressed(MinecraftClient.getInstance().window.getHandle(), GLFW.GLFW_MOUSE_BUTTON_LEFT)) { + System.out.println("LEFT BUTTON PRESS"); + } + + int delta = 0; + if (axis==Axis.HORIZONTAL) { + delta = x-anchor; + } else { + delta = y-anchor; + } + + float percentMoved = (delta / (float)getMovableDistance()); + int valueDelta = (int)(percentMoved * maxValue); + 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; + anchor = x-this.x; anchorValue = value; } else { - anchor = y; + anchor = y-this.y; anchorValue = value; } + sliding = true; + System.out.println("Start sliding"); return this; } - + /* @Override public void onMouseDrag(int x, int y, int button) { int delta = 0; @@ -108,14 +136,15 @@ public class WScrollBar extends WWidget { //System.out.println("Anchor: "+anchor+", Delta: "+delta+", PercentMoved: "+percentMoved+", ValueDelta: "+valueDelta); super.onMouseDrag(x, y, button); - } + }*/ @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; + System.out.println("Stop sliding"); return this; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index 9d0c80a..67c16d1 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -176,6 +176,11 @@ public class WWidget { host=c; } + @Environment(EnvType.CLIENT) + public void paintBackground(int x, int y, int mouseX, int mouseY) { + this.paintBackground(x, y); + } + @Environment(EnvType.CLIENT) public void paintBackground(int x, int y) { } @@ -187,6 +192,10 @@ public class WWidget { } } + public boolean isWithinBounds(int x, int y) { + return x>=0 && y>=0 && x information) { -} + } } -- cgit From 6907959d57777b2d3875d0e570d835f0ba9c1b57 Mon Sep 17 00:00:00 2001 From: Falkreon Date: Wed, 28 Aug 2019 02:59:58 -0500 Subject: Dramatically improved scrollbars, coordinate fixes --- .../github/cottonmc/test/client/TestClientGui.java | 3 +- .../cotton/gui/client/ClientCottonScreen.java | 30 +---- .../cottonmc/cotton/gui/widget/WListPanel.java | 132 +++++++++++---------- .../github/cottonmc/cotton/gui/widget/WPanel.java | 18 +++ .../cottonmc/cotton/gui/widget/WScrollBar.java | 67 +++-------- .../github/cottonmc/cotton/gui/widget/WWidget.java | 7 ++ 6 files changed, 116 insertions(+), 141 deletions(-) (limited to 'src/main/java') diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java index bf4ef07..9bbd1fc 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java @@ -33,9 +33,8 @@ public class TestClientGui extends LightweightGuiDescription { label.setText(new LiteralText(s)); }; WListPanel list = new WListPanel(data, WLabel.class, ()->new WLabel(""), configurator); - root.add(list, 0, 2, 7, 8); + root.add(list, 0, 2, 7, 5); root.validate(this); } - } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java index 80ee311..16921cf 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java @@ -93,16 +93,6 @@ public class ClientCottonScreen extends Screen { @Override public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { if (description.getRootPanel()==null) return super.mouseClicked(mouseX, mouseY, mouseButton); - /* - boolean result = super.mouseClicked(mouseX, mouseY, mouseButton); - int containerX = (int)mouseX-left; - int containerY = (int)mouseY-top; - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - lastResponder = description.getRootPanel().onMouseDown(containerX, containerY, mouseButton); - - return result;*/ - - WWidget focus = description.getFocus(); if (focus!=null) { @@ -122,7 +112,8 @@ public class ClientCottonScreen extends Screen { int containerY = (int)mouseY-top; if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; if (lastResponder==null) { - lastResponder = description.getRootPanel().onMouseDown(containerX, containerY, mouseButton); + lastResponder = description.getRootPanel().hit(containerX, containerY); + if (lastResponder!=null) lastResponder.onMouseDown(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); } else { //This is a drag instead } @@ -133,16 +124,6 @@ public class ClientCottonScreen extends Screen { @Override public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) { if (description.getRootPanel()==null) return super.mouseReleased(mouseX, mouseY, mouseButton); - /* - boolean result = super.mouseReleased(mouseX, mouseY, mouseButton); - int containerX = (int)mouseX-left; - int containerY = (int)mouseY-top; - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - - WWidget responder = description.getRootPanel().onMouseUp(containerX, containerY, mouseButton); - if (responder!=null && responder==lastResponder) description.getRootPanel().onClick(containerX, containerY, mouseButton); - lastResponder = null; - return result;*/ boolean result = super.mouseReleased(mouseX, mouseY, mouseButton); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; @@ -163,13 +144,6 @@ public class ClientCottonScreen extends Screen { @Override public boolean mouseDragged(double mouseX, double mouseY, int mouseButton, double unknown_1, double unknown_2) { if (description.getRootPanel()==null) return super.mouseDragged(mouseX, mouseY, mouseButton, unknown_1, unknown_2); - /* - boolean result = super.mouseDragged(mouseX, mouseY, mouseButton, unknown_1, unknown_2); - int containerX = (int)mouseX-left; - int containerY = (int)mouseY-top; - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - description.getRootPanel().onMouseDrag(containerX, containerY, mouseButton); - return result;*/ boolean result = super.mouseDragged(mouseX, mouseY, mouseButton, unknown_1, unknown_2); int containerX = (int)mouseX-left; 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 7d4c830..abb403e 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 @@ -40,7 +40,7 @@ public class WListPanel extends WPanel { } @Override - public void paintBackground(int x, int y) { + public void paintBackground(int x, int y, int mouseX, int mouseY) { if (getBackgroundPainter()!=null) { getBackgroundPainter().paintBackground(x, y, this); } else { @@ -58,75 +58,83 @@ public class WListPanel extends WPanel { } @Override - public void layout() { - //super.layout(); - - System.out.println("Validating"); - - //Recompute cellHeight if needed - if (!fixedHeight) { - if (unconfigured.isEmpty()) { - if (configured.isEmpty()) { - W exemplar = supplier.get(); - unconfigured.add(exemplar); - if (!exemplar.canResize()) cellHeight = exemplar.getHeight(); - } else { - W exemplar = configured.values().iterator().next(); - if (!exemplar.canResize()) cellHeight = exemplar.getHeight(); - } + public void layout() { + + this.children.clear(); + this.children.add(scrollBar); + scrollBar.setLocation(this.width-scrollBar.getWidth(), 0); + scrollBar.setSize(8, this.height); + //scrollBar.window = 6; + scrollBar.setMaxValue(data.size()); + + //super.layout(); + + //System.out.println("Validating"); + + //Recompute cellHeight if needed + if (!fixedHeight) { + if (unconfigured.isEmpty()) { + if (configured.isEmpty()) { + W exemplar = supplier.get(); + unconfigured.add(exemplar); + if (!exemplar.canResize()) cellHeight = exemplar.getHeight(); } else { - W exemplar = unconfigured.get(0); + W exemplar = configured.values().iterator().next(); if (!exemplar.canResize()) cellHeight = exemplar.getHeight(); } + } else { + W exemplar = unconfigured.get(0); + if (!exemplar.canResize()) cellHeight = exemplar.getHeight(); } - if (cellHeight<4) cellHeight=4; - - int layoutHeight = this.getHeight()-(margin*2); - int cellsHigh = layoutHeight / cellHeight; - - - //System.out.println("Adding children..."); - - this.children.clear(); - this.children.add(scrollBar); - 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; i0) { + for(int i=0; i setListItemHeight(int height) { cellHeight = height; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 599df5d..cda638d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -119,6 +119,24 @@ public abstract class WPanel extends WWidget { } }*/ + /** + * Finds the most specific child node at this location. + */ + @Override + public WWidget hit(int x, int y) { + if (children.isEmpty()) return this; + for(int i=children.size()-1; i>=0; i--) { //Backwards so topmost widgets get priority + WWidget child = children.get(i); + if ( x>=child.getX() && + y>=child.getY() && + x=maxValue) ? 1f : window / (float)maxValue; int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; - return (int)(percentage*bar); + int result = (int)(percentage*bar); + if (result<6) result = 6; + return result; } /** * Gets the number of pixels the scrollbar handle is able to move along its track from one end to the other. */ public int getMovableDistance() { - //int logicalDistance = maxValue-window; - //if (logicalDistance<0) logicalDistance = 0; - //float percentage = logicalDistance / (float)maxValue; int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; return bar-getHandleSize(); - //return (int)(percentage*bar); + } + + 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, 1); + float percent = value / (float)Math.max(maxValue-window, 1); return (int)(percent * getMovableDistance()); } @@ -82,9 +72,6 @@ public class WScrollBar extends WWidget { } protected void adjustSlider(int x, int y) { - if (InputUtil.isKeyPressed(MinecraftClient.getInstance().window.getHandle(), GLFW.GLFW_MOUSE_BUTTON_LEFT)) { - System.out.println("LEFT BUTTON PRESS"); - } int delta = 0; if (axis==Axis.HORIZONTAL) { @@ -93,9 +80,9 @@ public class WScrollBar extends WWidget { delta = y-anchor; } - float percentMoved = (delta / (float)getMovableDistance()); - int valueDelta = (int)(percentMoved * maxValue); + int valueDelta = pixelsToValues(delta); int valueNew = anchorValue + valueDelta; + if (valueNew>getMaxScrollValue()) valueNew = getMaxScrollValue(); if (valueNew<0) valueNew = 0; this.value = valueNew; @@ -106,37 +93,20 @@ public class WScrollBar extends WWidget { //TODO: Clicking before or after the handle should jump instead of scrolling if (axis==Axis.HORIZONTAL) { - anchor = x-this.x; + anchor = x; anchorValue = value; } else { - anchor = y-this.y; + anchor = y; anchorValue = value; } sliding = true; - System.out.println("Start sliding"); return this; } - /* + @Override public void onMouseDrag(int x, int y, int button) { - int delta = 0; - if (axis==Axis.HORIZONTAL) { - delta = x-anchor; - } else { - delta = y-anchor; - } - - float percentMoved = (delta / (float)getMovableDistance()); - int valueDelta = (int)(percentMoved * maxValue); - 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); - }*/ + adjustSlider(x, y); + } @Override public WWidget onMouseUp(int x, int y, int button) { @@ -144,7 +114,6 @@ public class WScrollBar extends WWidget { anchor = -1; anchorValue = -1; sliding = false; - System.out.println("Stop sliding"); return this; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index 67c16d1..1314f88 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -230,4 +230,11 @@ public class WWidget { */ public void addInformation(List information) { } + + /** + * Find the most specific child node at this location. For non-panel widgets, returns this widget. + */ + public WWidget hit(int x, int y) { + return this; + } } -- cgit