From 16a4367663f5ed2a909804de6d319c97c9854ecc Mon Sep 17 00:00:00 2001 From: syeyoung Date: Fri, 30 Jul 2021 11:43:38 +0900 Subject: Cursors in gui elements --- .../java/kr/syeyoung/dungeonsguide/gui/MGui.java | 47 +++++++++++++++++++++- .../java/kr/syeyoung/dungeonsguide/gui/MPanel.java | 16 ++++++++ .../dungeonsguide/gui/elements/MButton.java | 9 +++++ .../dungeonsguide/gui/elements/MCollapsable.java | 7 ++++ .../gui/elements/MPanelScaledGUI.java | 13 ++++++ .../dungeonsguide/gui/elements/MRootPanel.java | 9 +++++ .../dungeonsguide/gui/elements/MScrollBar.java | 13 ++++++ .../dungeonsguide/gui/elements/MTextField.java | 8 ++++ .../dungeonsguide/gui/elements/MToggleButton.java | 8 ++++ 9 files changed, 129 insertions(+), 1 deletion(-) (limited to 'src/main/java/kr/syeyoung/dungeonsguide/gui') diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java index c5428c14..d523c923 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/MGui.java @@ -19,11 +19,14 @@ package kr.syeyoung.dungeonsguide.gui; import kr.syeyoung.dungeonsguide.gui.elements.MRootPanel; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; +import kr.syeyoung.dungeonsguide.utils.cursor.GLCursors; import lombok.Getter; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.renderer.GlStateManager; +import org.lwjgl.LWJGLException; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; @@ -33,8 +36,15 @@ import java.io.IOException; public class MGui extends GuiScreen { @Getter - private final MPanel mainPanel = new MRootPanel(); + private final MRootPanel mainPanel = new MRootPanel(); + public MGui(){ + try { + Mouse.setNativeCursor(GLCursors.getCursor(EnumCursor.DEFAULT)); + } catch (Throwable e) { + e.printStackTrace(); + } + } @Override public void initGui() { @@ -98,6 +108,12 @@ public class MGui extends GuiScreen { @Override public void onGuiClosed() { super.onGuiClosed(); + + try { + Mouse.setNativeCursor(null); + } catch (LWJGLException e) { + e.printStackTrace(); + } } @Override @@ -122,10 +138,23 @@ public class MGui extends GuiScreen { } } + public void mouseMove(int mouseX, int mouseY) { + try { + mainPanel.mouseMoved0(mouseX, mouseY + ,mouseX ,mouseY); + } catch (Throwable e) { + if (e.getMessage() == null || !e.getMessage().contains("hack to stop")) + e.printStackTrace(); + } + } + + private int touchValue; private int eventButton; private long lastMouseEvent; + + private int lastX, lastY; @Override public void handleMouseInput() throws IOException { try { @@ -159,14 +188,30 @@ public class MGui extends GuiScreen { long l = Minecraft.getSystemTime() - this.lastMouseEvent; this.mouseClickMove(i, j, this.eventButton, l); } + if (lastX != i || lastY != j) { + try { + EnumCursor prevCursor = mainPanel.getCurrentCursor(); + mainPanel.setCursor(EnumCursor.DEFAULT); + this.mouseMove(i, j); + EnumCursor newCursor = mainPanel.getCurrentCursor(); + if (prevCursor != newCursor) Mouse.setNativeCursor(GLCursors.getCursor(newCursor)); + } catch (Throwable e) { + if (e.getMessage() == null || !e.getMessage().contains("hack to stop")) + e.printStackTrace(); + } + } int wheel = Mouse.getDWheel(); if (wheel != 0) { mainPanel.mouseScrolled0(i, j,i,j, wheel); } + lastX = i; + lastY = j; } catch (Throwable e) { e.printStackTrace(); } } + + } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java index 84cccdf5..126d69b6 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/MPanel.java @@ -19,6 +19,7 @@ package kr.syeyoung.dungeonsguide.gui; import kr.syeyoung.dungeonsguide.gui.elements.MTooltip; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; @@ -264,4 +265,19 @@ public class MPanel { mouseScrolled(absMouseX, absMouseY, relMousex, relMousey, scrollAmount); } public void mouseScrolled(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int scrollAmount) {} + + public void mouseMoved0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) { + int relMousex = relMouseX0 - getBounds().x; + int relMousey = relMouseY0 - getBounds().y; + + mouseMoved(absMouseX, absMouseY, relMousex, relMousey); + for (MPanel childComponent : getChildComponents()) { + childComponent.mouseMoved0(absMouseX, absMouseY, relMousex, relMousey); + } + } + public void mouseMoved(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) {} + + public void setCursor(EnumCursor enumCursor) { + parent.setCursor(enumCursor); + } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MButton.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MButton.java index da2ff78c..1a1f30d4 100755 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MButton.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MButton.java @@ -20,6 +20,7 @@ package kr.syeyoung.dungeonsguide.gui.elements; import kr.syeyoung.dungeonsguide.gui.MPanel; import kr.syeyoung.dungeonsguide.utils.RenderUtils; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; import lombok.Getter; import lombok.Setter; import net.minecraft.client.Minecraft; @@ -85,4 +86,12 @@ public class MButton extends MPanel { public void mouseReleased(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int state) { isclicked = false; } + + + @Override + public void mouseMoved(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) { + if (lastAbsClip.contains(absMouseX, absMouseY) && enabled) + setCursor(EnumCursor.POINTING_HAND); + } + } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MCollapsable.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MCollapsable.java index ef10377e..20fa2c93 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MCollapsable.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MCollapsable.java @@ -19,6 +19,7 @@ package kr.syeyoung.dungeonsguide.gui.elements; import kr.syeyoung.dungeonsguide.gui.MPanel; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; import lombok.Getter; import lombok.Setter; import net.minecraft.client.Minecraft; @@ -128,4 +129,10 @@ public class MCollapsable extends MPanel { if (onPreferedSizeChange != null) onPreferedSizeChange.run(); } } + + @Override + public void mouseMoved(int absMouseX, int absMouseY, int relMouseX, int relMouseY) { + if (relMouseX >= leftPad && relMouseY >= 0 && relMouseX <= leftPad + 10 && relMouseY <= representing.getPreferredSize().height) + setCursor(EnumCursor.POINTING_HAND); + } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MPanelScaledGUI.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MPanelScaledGUI.java index 64599058..4a1d30ee 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MPanelScaledGUI.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MPanelScaledGUI.java @@ -196,4 +196,17 @@ public class MPanelScaledGUI extends MPanel { } mouseScrolled(absMouseX, absMouseY, relMousex, relMousey, scrollAmount); } + + @Override + public void mouseMoved0(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) { + int relMousex = (int) ((relMouseX0 - getBounds().x) / scale); + int relMousey = (int) ((relMouseY0 - getBounds().y) / scale); + absMouseX = (int) (absMouseX / scale); + absMouseY = (int) (absMouseY / scale); + + mouseMoved(absMouseX, absMouseY, relMousex, relMousey); + for (MPanel childComponent : getChildComponents()) { + childComponent.mouseMoved0(absMouseX, absMouseY, relMousex, relMousey); + } + } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MRootPanel.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MRootPanel.java index a6987241..d1d96a35 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MRootPanel.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MRootPanel.java @@ -19,7 +19,9 @@ package kr.syeyoung.dungeonsguide.gui.elements; import kr.syeyoung.dungeonsguide.gui.MPanel; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; import lombok.Getter; +import lombok.Setter; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -47,4 +49,11 @@ public class MRootPanel extends MPanel { tooltips.remove(mTooltip); remove(mTooltip); } + + @Getter @Setter + private EnumCursor currentCursor = EnumCursor.DEFAULT; + @Override + public void setCursor(EnumCursor enumCursor) { + currentCursor = enumCursor; + } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MScrollBar.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MScrollBar.java index 215c7c6d..1797297a 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MScrollBar.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MScrollBar.java @@ -21,6 +21,7 @@ package kr.syeyoung.dungeonsguide.gui.elements; import kr.syeyoung.dungeonsguide.config.types.AColor; import kr.syeyoung.dungeonsguide.gui.MPanel; import kr.syeyoung.dungeonsguide.utils.RenderUtils; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; import lombok.Getter; import lombok.Setter; import net.minecraft.client.gui.Gui; @@ -71,6 +72,7 @@ public class MScrollBar extends MPanel { this.onUpdate = onUpdate; } + private Rectangle lastThumbRect = new Rectangle(); @Override public void render(int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks, Rectangle scissor) { // RENDER SUPER NICE SCROLL BAR @@ -93,12 +95,14 @@ public class MScrollBar extends MPanel { endX = Math.max(endX, startX + 20); Gui.drawRect(startX,0,endX,getBounds().height, color); + lastThumbRect.x = startX; lastThumbRect.y = 0; lastThumbRect.width = endX - startX; lastThumbRect.height = getBounds().height; } else if (axis == Axis.Y) { int startY = (int) (startPerc * getBounds().height); int endY = (int) (endPerc * getBounds().height); endY = Math.max(endY, startY + 20); Gui.drawRect(0,startY,getBounds().width,endY, color); + lastThumbRect.x = 0; lastThumbRect.y = startY; lastThumbRect.width = getBounds().width; lastThumbRect.height = endY - startY; } } @@ -146,4 +150,13 @@ public class MScrollBar extends MPanel { public static enum Axis { X, Y } + + + @Override + public void mouseMoved(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) { + if (grabbed) + setCursor(EnumCursor.CLOSED_HAND); + else if (lastThumbRect.contains(relMouseX0, relMouseY0)) + setCursor(EnumCursor.OPEN_HAND); + } } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java index 6d8b3f51..6054a68f 100755 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MTextField.java @@ -20,6 +20,7 @@ package kr.syeyoung.dungeonsguide.gui.elements; import kr.syeyoung.dungeonsguide.gui.MPanel; import kr.syeyoung.dungeonsguide.utils.RenderUtils; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; import lombok.Getter; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; @@ -371,4 +372,11 @@ public class MTextField extends MPanel { block != Character.UnicodeBlock.SPECIALS; } + + @Override + public void mouseMoved(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) { + if (lastAbsClip.contains(absMouseX, absMouseY)) + setCursor(EnumCursor.BEAM_CURSOR); + } + } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MToggleButton.java b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MToggleButton.java index 5b99959f..d3a3fe06 100755 --- a/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MToggleButton.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/gui/elements/MToggleButton.java @@ -20,6 +20,7 @@ package kr.syeyoung.dungeonsguide.gui.elements; import kr.syeyoung.dungeonsguide.gui.MPanel; import kr.syeyoung.dungeonsguide.utils.RenderUtils; +import kr.syeyoung.dungeonsguide.utils.cursor.EnumCursor; import lombok.Getter; import lombok.Setter; import net.minecraft.client.Minecraft; @@ -76,4 +77,11 @@ public class MToggleButton extends MPanel { onToggle.run(); } } + + + @Override + public void mouseMoved(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0) { + if (lastAbsClip.contains(absMouseX, absMouseY)) + setCursor(EnumCursor.POINTING_HAND); + } } -- cgit