Skip to content

Commit

Permalink
Added Left Mouse Button Up Listener.
Browse files Browse the repository at this point in the history
Added a listener that will fire when the left mouse button is raised after being pressed. With this, you can now move HUD elements again. Performance when moving these HUD elements has likely improved.
  • Loading branch information
coltonk9043 committed Sep 30, 2023
1 parent 8e5b3cc commit 9ebc902
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import java.util.ArrayList;
import net.aoba.event.listeners.AbstractListener;
import net.aoba.event.listeners.MouseLeftClickListener;
import net.aoba.event.listeners.LeftMouseDownListener;

public class MouseLeftClickEvent extends AbstractEvent{
public class LeftMouseDownEvent extends AbstractEvent{

double mouseX;
double mouseY;

public MouseLeftClickEvent(double mouseX2, double mouseY2) {
public LeftMouseDownEvent(double mouseX2, double mouseY2) {
super();
this.mouseX = mouseX2;
this.mouseY = mouseY2;
Expand All @@ -26,14 +26,14 @@ public double GetMouseY() {
@Override
public void Fire(ArrayList<? extends AbstractListener> listeners) {
for(AbstractListener listener : listeners) {
MouseLeftClickListener mouseLeftClickListener = (MouseLeftClickListener) listener;
mouseLeftClickListener.OnMouseLeftClick(this);
LeftMouseDownListener mouseLeftClickListener = (LeftMouseDownListener) listener;
mouseLeftClickListener.OnLeftMouseDown(this);
}
}

@SuppressWarnings("unchecked")
@Override
public Class<MouseLeftClickListener> GetListenerClassType() {
return MouseLeftClickListener.class;
public Class<LeftMouseDownListener> GetListenerClassType() {
return LeftMouseDownListener.class;
}
}
39 changes: 39 additions & 0 deletions src/main/java/net/aoba/event/events/LeftMouseUpEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.aoba.event.events;

import java.util.ArrayList;
import net.aoba.event.listeners.AbstractListener;
import net.aoba.event.listeners.LeftMouseUpListener;

public class LeftMouseUpEvent extends AbstractEvent{

double mouseX;
double mouseY;

public LeftMouseUpEvent(double mouseX2, double mouseY2) {
super();
this.mouseX = mouseX2;
this.mouseY = mouseY2;
}

public double GetMouseX() {
return mouseX;
}

public double GetMouseY() {
return mouseY;
}

@Override
public void Fire(ArrayList<? extends AbstractListener> listeners) {
for(AbstractListener listener : listeners) {
LeftMouseUpListener mouseLeftClickListener = (LeftMouseUpListener) listener;
mouseLeftClickListener.OnLeftMouseUp(this);
}
}

@SuppressWarnings("unchecked")
@Override
public Class<LeftMouseUpListener> GetListenerClassType() {
return LeftMouseUpListener.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.aoba.event.listeners;

import net.aoba.event.events.LeftMouseDownEvent;

public interface LeftMouseDownListener extends AbstractListener {
public abstract void OnLeftMouseDown(LeftMouseDownEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.aoba.event.listeners;

import net.aoba.event.events.LeftMouseUpEvent;

public interface LeftMouseUpListener extends AbstractListener {
public abstract void OnLeftMouseUp(LeftMouseUpEvent event);
}

This file was deleted.

20 changes: 15 additions & 5 deletions src/main/java/net/aoba/gui/HudManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import net.aoba.core.settings.SettingManager;
import net.aoba.core.settings.types.BooleanSetting;
import net.aoba.core.settings.types.FloatSetting;
import net.aoba.event.events.MouseLeftClickEvent;
import net.aoba.event.events.LeftMouseDownEvent;
import net.aoba.event.events.LeftMouseUpEvent;
import net.aoba.event.events.MouseScrollEvent;
import net.aoba.event.listeners.MouseLeftClickListener;
import net.aoba.event.listeners.LeftMouseDownListener;
import net.aoba.event.listeners.LeftMouseUpListener;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
Expand All @@ -30,7 +32,7 @@
import net.minecraft.client.util.Window;
import net.minecraft.client.util.math.MatrixStack;

public class HudManager implements MouseLeftClickListener {
public class HudManager implements LeftMouseDownListener, LeftMouseUpListener {

protected MinecraftClient mc = MinecraftClient.getInstance();
protected RenderUtils renderUtils = new RenderUtils();
Expand Down Expand Up @@ -120,7 +122,8 @@ public HudManager() {
SettingManager.register_setting(rainbow, Aoba.getInstance().settingManager.config_category);
SettingManager.register_setting(ah, Aoba.getInstance().settingManager.config_category);

Aoba.getInstance().eventManager.AddListener(MouseLeftClickListener.class, this);
Aoba.getInstance().eventManager.AddListener(LeftMouseDownListener.class, this);
Aoba.getInstance().eventManager.AddListener(LeftMouseUpListener.class, this);
}

/**
Expand Down Expand Up @@ -235,9 +238,16 @@ public void toggleMouse() {
}

@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
public void OnLeftMouseDown(LeftMouseDownEvent event) {
if (this.clickGuiOpen) {
event.SetCancelled(true);
}
}

@Override
public void OnLeftMouseUp(LeftMouseUpEvent event) {
if(this.clickGuiOpen) {
currentGrabbed = null;
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/net/aoba/gui/NavigationBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import java.util.List;
import net.aoba.Aoba;
import net.aoba.AobaClient;
import net.aoba.event.events.MouseLeftClickEvent;
import net.aoba.event.listeners.MouseLeftClickListener;
import net.aoba.event.events.LeftMouseDownEvent;
import net.aoba.event.listeners.LeftMouseDownListener;
import net.aoba.misc.RenderUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.Window;
import net.minecraft.client.util.math.MatrixStack;

public class NavigationBar implements MouseLeftClickListener {
public class NavigationBar implements LeftMouseDownListener {
MinecraftClient mc = MinecraftClient.getInstance();

private List<NavigationPane> options;
Expand Down Expand Up @@ -65,7 +65,7 @@ public void draw(DrawContext drawContext, float partialTicks, Color color) {
}

@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
public void OnLeftMouseDown(LeftMouseDownEvent event) {
AobaClient aoba = Aoba.getInstance();
Window window = mc.getWindow();

Expand Down
95 changes: 53 additions & 42 deletions src/main/java/net/aoba/gui/hud/AbstractHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,124 @@
import net.aoba.core.settings.SettingManager;
import net.aoba.core.settings.types.Vector2Setting;
import net.aoba.core.utils.types.Vector2;
import net.aoba.event.events.MouseLeftClickEvent;
import net.aoba.event.events.LeftMouseDownEvent;
import net.aoba.event.events.MouseMoveEvent;
import net.aoba.event.listeners.MouseLeftClickListener;
import net.aoba.event.listeners.LeftMouseDownListener;
import net.aoba.event.listeners.MouseMoveListener;
import net.aoba.gui.Color;
import net.aoba.gui.HudManager;
import net.aoba.misc.RenderUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;

public abstract class AbstractHud implements MouseLeftClickListener, MouseMoveListener {
public abstract class AbstractHud implements LeftMouseDownListener, MouseMoveListener {
protected static RenderUtils renderUtils = new RenderUtils();
protected static MinecraftClient mc = MinecraftClient.getInstance();
protected String ID;

protected String ID;

protected Vector2Setting position;
protected float width;
protected float height;

protected boolean isMouseOver = false;
protected boolean moveable = true;

// Mouse Variables
protected double lastMouseX;
protected double lastMouseY;
protected double lastClickOffsetX;
protected double lastClickOffsetY;

public AbstractHud(String ID, int x, int y, int width, int height) {
this.ID = ID;

this.position = new Vector2Setting(ID + "_position", "GUI POS", new Vector2(x, y));
this.width = width;
this.height = height;

SettingManager.register_setting(position, Aoba.getInstance().settingManager.hidden_category);
Aoba.getInstance().eventManager.AddListener(MouseLeftClickListener.class, this);

Aoba.getInstance().eventManager.AddListener(LeftMouseDownListener.class, this);
Aoba.getInstance().eventManager.AddListener(MouseMoveListener.class, this);
}

public float getHeight() {
return this.height;
}
public float getX(){

public float getX() {
return position.getValue().x;
}
public float getY(){

public float getY() {
return position.getValue().y;
}

public float getWidth() {
return this.width;
}

public void setX(float x) {
position.setX(x);
}

public void setY(float y) {
position.setY(y);
}

public void setWidth(float width) {
this.width = width;
}

public void setHeight(float height) {
this.height = height;
}

public abstract void update();

public abstract void draw(DrawContext drawContext, float partialTicks, Color color);

@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
public void OnLeftMouseDown(LeftMouseDownEvent event) {
// TODO: Before the logic would store the clicked mouse pos, which lead to
// the choppy, unpredictable nature of moving GUI. Not sure if it is needed
// anymore as we have raw mouse position.

double mouseX = event.GetMouseX();
double mouseY = event.GetMouseY();

Vector2 pos = position.getValue();

if (Aoba.getInstance().hudManager.isClickGuiOpen()) {
if (HudManager.currentGrabbed == null) {
if (mouseX >= pos.x && mouseX <= (pos.x + width)) {
if (mouseY >= pos.y && mouseY <= (pos.y + height)) {
lastMouseX = mouseX;
lastMouseY = mouseY;
this.lastClickOffsetX = mouseX - pos.x;
this.lastClickOffsetY = mouseY - pos.y;
}
}
}
}
}
@Override

@Override
public void OnMouseMove(MouseMoveEvent event) {
/*
* if(Aoba.getInstance().hudManager.isClickGuiOpen()) { float mouseX =
* (float)event.GetHorizontal(); float mouseY = (float)event.GetVertical();
*
* if(mouseX >= x && mouseX <= x + width) { if(mouseY >= y && mouseY <= y +
* height) { isMouseOver = true; if(HudManager.currentGrabbed == this &&
* this.moveable) { float deltaX = mouseX - lastMouseX; float deltaY = mouseY -
* lastMouseY;
*
* this.x = this.x + deltaX; this.y = this.y + deltaY; } } } }
*/

if (Aoba.getInstance().hudManager.isClickGuiOpen()) {
double mouseX = event.GetHorizontal();
double mouseY = event.GetVertical();

Vector2 pos = position.getValue();

if (HudManager.currentGrabbed == this && this.moveable) {
pos.x = (float) (mouseX - this.lastClickOffsetX);
pos.y = (float) (mouseY - this.lastClickOffsetY);
}

if (mouseX >= pos.x && mouseX <= pos.x + width) {
if (mouseY >= pos.y && mouseY <= pos.y + height) {
isMouseOver = true;
}
}
}
}
}
16 changes: 11 additions & 5 deletions src/main/java/net/aoba/gui/tabs/ClickGuiTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import net.aoba.core.settings.types.BooleanSetting;
import net.aoba.core.settings.types.Vector2Setting;
import net.aoba.core.utils.types.Vector2;
import net.aoba.event.events.MouseLeftClickEvent;
import net.aoba.event.listeners.MouseLeftClickListener;
import net.aoba.event.events.LeftMouseDownEvent;
import net.aoba.event.listeners.LeftMouseDownListener;
import net.aoba.event.listeners.MouseMoveListener;
import net.aoba.gui.Color;
import net.aoba.gui.HudManager;
Expand All @@ -39,7 +39,7 @@
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;

public class ClickGuiTab extends AbstractHud implements MouseLeftClickListener, MouseMoveListener {
public class ClickGuiTab extends AbstractHud implements LeftMouseDownListener, MouseMoveListener {
protected String title;

protected boolean pinnable = true;
Expand Down Expand Up @@ -163,14 +163,20 @@ public void draw(DrawContext drawContext, float partialTicks, Color color) {
}

@Override
public void OnMouseLeftClick(MouseLeftClickEvent event) {
super.OnMouseLeftClick(event);
public void OnLeftMouseDown(LeftMouseDownEvent event) {
super.OnLeftMouseDown(event);

double mouseX = mc.mouse.getX();
double mouseY = mc.mouse.getY();
Vector2 pos = position.getValue();

if (Aoba.getInstance().hudManager.isClickGuiOpen()) {
if(mouseX >= pos.x && mouseX <= pos.x + width) {
if(mouseY >= pos.y && mouseY <= pos.y + 24) {
HudManager.currentGrabbed = this;
}
}

if (this.pinnable) {
if (mouseX >= (pos.x + width - 24) && mouseX <= (pos.x + width - 2)) {
if (mouseY >= (pos.y + 4) && mouseY <= (pos.y + 20)) {
Expand Down
Loading

0 comments on commit 9ebc902

Please sign in to comment.