diff options
author | Falkreon <falkreon@gmail.com> | 2019-08-27 15:39:49 -0500 |
---|---|---|
committer | Falkreon <falkreon@gmail.com> | 2019-08-27 15:39:49 -0500 |
commit | a0ee03a8a763ac4eee15190b6d68145acafc5154 (patch) | |
tree | 60e4ebddb6e48c851f947bd7d7ac3a855d624426 /src | |
parent | c4fe40d1daee84662c6c269f53dd68a2c2a136f1 (diff) | |
download | LibGui-a0ee03a8a763ac4eee15190b6d68145acafc5154.tar.gz LibGui-a0ee03a8a763ac4eee15190b6d68145acafc5154.tar.bz2 LibGui-a0ee03a8a763ac4eee15190b6d68145acafc5154.zip |
Cleanup mouse events, add optional mouse hover info to paintBackground
Diffstat (limited to 'src')
8 files changed, 136 insertions, 42 deletions
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<Inventory> 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; @@ -130,18 +142,46 @@ 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) { + lastResponder.onClick(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); + } + } else { + description.getRootPanel().onMouseUp(containerX, containerY, mouseButton); + } + + lastResponder = null; return result; } @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; + 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<T extends CottonScreenController> 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<T extends CottonScreenController> 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<width && containerY<height) { + lastResponder.onClick(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); + } + } else { + container.doMouseUp(containerX, containerY, mouseButton); + } + lastResponder = null; return result; } @@ -123,8 +133,14 @@ public class CottonScreen<T extends CottonScreenController> 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<T extends CottonScreenController> 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<x+getWidth() && mouseY<y+getHeight()); int state = 1; //1=regular. 2=hovered. 0=disabled. @@ -75,7 +75,7 @@ public class WButton extends WWidget { public void onClick(int x, int y, int button) { super.onClick(x, y, button); - if (enabled) { + if (enabled && isWithinBounds(x, y)) { MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); if (onClick!=null) onClick.run(); 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 de3c7ed..7d4c830 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 @@ -85,7 +85,7 @@ public class WListPanel<D, W extends WWidget> 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<D, W extends WWidget> 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<D, W extends WWidget> 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<D, W extends WWidget> extends WPanel { } } - System.out.println("Children: "+children.size()); + //System.out.println("Children: "+children.size()); } public WListPanel<D, W> 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 @@ -177,6 +177,11 @@ public class WWidget { } @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<this.width && y<this.height; + } + /** * Internal method to conditionally render tooltip data. This requires an overriden {@link #addInformation(List) * addInformation} method to insert data into the tooltip - without this, the method returns early, because no work @@ -220,5 +229,5 @@ public class WWidget { * @param information List containing all previous tooltip data. */ public void addInformation(List<String> information) { -} + } } |