Skip to content

Commit

Permalink
Reworked tooltips and HUD fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonk9043 committed Aug 31, 2024
1 parent 2e32736 commit a1e8f77
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 64 deletions.
35 changes: 27 additions & 8 deletions src/main/java/net/aoba/gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,20 @@
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.resource.ResourceFactory;

import org.joml.Matrix4f;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;

import com.google.common.collect.Lists;
import com.mojang.blaze3d.systems.RenderSystem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class GuiManager implements KeyDownListener, TickListener, Render2DListener {
private static final MinecraftClient MC = MinecraftClient.getInstance();
private static CursorStyle currentCursor = CursorStyle.Default;

private static String tooltip = null;

public KeybindSetting clickGuiButton = new KeybindSetting("key.clickgui", "ClickGUI Key", InputUtil.fromKeyCode(GLFW.GLFW_KEY_GRAVE_ACCENT, 0));
private final KeyBinding esc = new KeyBinding("key.esc", GLFW.GLFW_KEY_ESCAPE, "key.categories.aoba");

Expand Down Expand Up @@ -134,8 +131,8 @@ public void Initialize() {
toolsPane.AddWindow(new GoToWindow("Go To Location", 1220, 550));

moduleSelector = new ModuleSelectorHud();
armorHud = new ArmorHud(0, 0, 200, 50);
radarHud = new RadarHud(0, 0, 180, 180);
armorHud = new ArmorHud(0, 0);
radarHud = new RadarHud(0, 0);
infoHud = new InfoHud(0, 0);
moduleArrayListHud = new ModuleArrayListHud(0, 0);
watermarkHud = new WatermarkHud(0, 0);
Expand Down Expand Up @@ -194,6 +191,15 @@ public static void setCursor(CursorStyle cursor) {
Input.setCursorStyle(currentCursor);
}

public static String getTooltip() {
return tooltip;
}

public static void setTooltip(String tt) {
if(tooltip != tt)
tooltip = tt;
}

public void AddWindow(Window hud, String pageName) {
for (Page page : clickGuiNavBar.getPanes()) {
if (page.getTitle().equals(pageName)) {
Expand All @@ -216,7 +222,7 @@ public void RemoveWindow(Window hud, String pageName) {
@Override
public void OnKeyDown(KeyDownEvent event) {
if (clickGuiButton.getValue().getCode() == event.GetKey() && MC.currentScreen == null) {
this.clickGuiOpen = !this.clickGuiOpen;
setClickGuiOpen(!this.clickGuiOpen);
this.toggleMouse();
}
}
Expand Down Expand Up @@ -291,6 +297,18 @@ public void OnRender(Render2DEvent event) {
}
}

// Draw Tooltip on top of all UI elements
if(tooltip != null) {
int mouseX = (int) MC.mouse.getX();
int mouseY = (int) MC.mouse.getY();
int tooltipWidth = Render2D.getStringWidth(tooltip) + 2;
int tooltipHeight = 10;

Render2D.drawRoundedBox(matrixStack.peek().getPositionMatrix(), mouseX + 12, mouseY + 12, (tooltipWidth + 4) * 2, (tooltipHeight + 4) * 2, GuiManager.roundingRadius.getValue(), GuiManager.backgroundColor.getValue().getAsSolid());
Render2D.drawString(drawContext, tooltip, mouseX + 18, mouseY + 18, GuiManager.foregroundColor.getValue());
}


matrixStack.pop();
RenderSystem.enableCull();
}
Expand All @@ -306,6 +324,7 @@ public boolean isClickGuiOpen() {

public void setClickGuiOpen(boolean state) {
this.clickGuiOpen = state;
setTooltip(null);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/aoba/gui/colors/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public Color(int r, int g, int b, int alpha) {
this.alpha = alpha;
}

public Color getAsSolid() {
return new Color(r, g, b, 255);
}

/**
* Interpolates between two colors.
*
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/aoba/gui/components/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.aoba.Aoba;
import net.aoba.event.events.MouseClickEvent;
import net.aoba.event.events.MouseMoveEvent;
import net.aoba.gui.GuiManager;
import net.aoba.gui.IGuiElement;
import net.aoba.gui.Margin;
import net.aoba.gui.Rectangle;
Expand Down Expand Up @@ -307,14 +308,27 @@ public void onMouseMove(MouseMoveEvent mouseMoveEvent) {
tabIterator.next().onMouseMove(mouseMoveEvent);
}

boolean wasHovered = hovered;
if (mouseMoveEvent.isCancelled() || !visible || !Aoba.getInstance().hudManager.isClickGuiOpen()) {
this.hovered = false;
if(wasHovered){
GuiManager.setTooltip(null);
}
} else {

float mouseX = (float) mouseMoveEvent.getX();
float mouseY = (float) mouseMoveEvent.getY();

this.hovered = actualSize.intersects(mouseX, mouseY);

String tooltip = getTooltip();
if(hovered && tooltip != null) {
GuiManager.setTooltip(tooltip);
mouseMoveEvent.cancel();
}
else if(wasHovered){
GuiManager.setTooltip(null);
}
}
}

Expand All @@ -335,4 +349,8 @@ public void onMouseClick(MouseClickEvent event) {
tabIterator.next().onMouseClick(event);
}
}

public String getTooltip() {
return null;
}
}
16 changes: 4 additions & 12 deletions src/main/java/net/aoba/gui/components/ModuleComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import net.aoba.utils.types.MouseButton;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import org.joml.Matrix4f;
import org.joml.Quaternionf;

Expand Down Expand Up @@ -95,17 +94,6 @@ public void draw(DrawContext drawContext, float partialTicks) {
Render2D.drawTexturedQuad(matrix4f, gear, (actualX + actualWidth - 16), (actualY + 6), 16, 16, hudColor);
}
}

if (this.hovered) {
int mouseX = (int) MC.mouse.getX();
int mouseY = (int) MC.mouse.getY();
String tooltip = module.getDescription();
int tooltipWidth = Render2D.getStringWidth(tooltip);
int tooltipHeight = 10;

Render2D.drawRoundedBox(matrix4f, mouseX, mouseY + 12, (tooltipWidth + 4) * 2, (tooltipHeight + 4) * 2, GuiManager.roundingRadius.getValue(), GuiManager.backgroundColor.getValue());
Render2D.drawString(drawContext, tooltip, mouseX + 4, mouseY + 16, GuiManager.foregroundColor.getValue());
}
}

@Override
Expand Down Expand Up @@ -176,4 +164,8 @@ public void onMouseClick(MouseClickEvent event) {
}
}
}

public String getTooltip() {
return module.getDescription();
}
}
6 changes: 5 additions & 1 deletion src/main/java/net/aoba/gui/navigation/huds/ArmorHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@

public class ArmorHud extends HudWindow {

public ArmorHud(int x, int y, int width, int height) {
public ArmorHud(int x, int y) {
super("ArmorHud", x, y, 16, 256);
this.minHeight = 256f;
this.maxHeight = 256f;
this.minWidth = 16f;
this.maxWidth = 16f;
}

@Override
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/net/aoba/gui/navigation/huds/PingHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,38 @@
import net.minecraft.client.network.ClientPlayNetworkHandler;

public class PingHud extends HudWindow {

private static final MinecraftClient MC = MinecraftClient.getInstance();

String pingText = null;

public PingHud(int x, int y) {
super("PingHud", x, y, 0, 0);
super("PingHud", x, y, 0, 32);
resizeable = false;
this.minHeight = 32f;
this.maxHeight = 32f;
}

@Override
public void update() {
ClientPlayNetworkHandler networkHandler = MC.getNetworkHandler();
if (networkHandler != null && MC.player != null) {
int ping = networkHandler.getPlayerListEntry(MC.player.getUuid()).getLatency();
pingText = "Ping: " + ping + " ms";

int textWidth = MC.textRenderer.getWidth(pingText);
setWidth(textWidth * 2);
}else
pingText = null;
}


@Override
public void draw(DrawContext drawContext, float partialTicks) {
super.draw(drawContext, partialTicks);

if (getVisible()) {
if (pingText != null && getVisible()) {
Rectangle pos = position.getValue();
if (pos.isDrawable()) {
ClientPlayNetworkHandler networkHandler = MC.getNetworkHandler();
if (networkHandler != null && MC.player != null) {
int ping = networkHandler.getPlayerListEntry(MC.player.getUuid()).getLatency();
String pingText = "Ping: " + ping + " ms";

int textWidth = MC.textRenderer.getWidth(pingText);
int textHeight = MC.textRenderer.fontHeight;

setWidth(textWidth * 2);
setHeight(textHeight * 2);

Render2D.drawString(drawContext, pingText, pos.getX(), pos.getY(), GuiManager.foregroundColor.getValue().getColorAsInt());
}
Render2D.drawString(drawContext, pingText, pos.getX(), pos.getY(), GuiManager.foregroundColor.getValue().getColorAsInt());
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/aoba/gui/navigation/huds/RadarHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public class RadarHud extends HudWindow {

float distance = 50;

public RadarHud(int x, int y, int width, int height) {
super("RadarHud", x, y, width, height);
public RadarHud(int x, int y) {
super("RadarHud", x, y, 180, 180);

this.minHeight = 180.0f;
this.minWidth = 180.0f;

this.inheritHeightFromChildren = false;
}

@Override
Expand Down
53 changes: 29 additions & 24 deletions src/main/java/net/aoba/gui/navigation/huds/SpeedHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,51 @@
import net.aoba.utils.render.Render2D;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;

public class SpeedHud extends HudWindow {

private static final MinecraftClient MC = MinecraftClient.getInstance();
private static final double BLOCKS_TO_KM = 0.001;
private String speedText = null;

public SpeedHud(int x, int y) {
super("SpeedHud", x, y, 0, 0);
super("SpeedHud", x, y, 0, 32);
resizeable = false;

this.minHeight = 32f;
this.maxHeight = 32f;
}

@Override
public void update() {
super.update();

PlayerEntity player = MC.player;
if (player != null) {
double dx = player.getX() - player.prevX;
double dz = player.getZ() - player.prevZ;
double dy = player.getY() - player.prevY;

double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);

double speed = distance * 20 * 3.6;

speedText = String.format("Speed: %.2f km/h", speed);

int textWidth = MC.textRenderer.getWidth(speedText);
setWidth(textWidth * 2);
}else
speedText = null;
}

@Override
public void draw(DrawContext drawContext, float partialTicks) {
super.draw(drawContext, partialTicks);

if (getVisible()) {
if (speedText != null && getVisible()) {
Rectangle pos = position.getValue();
if (pos.isDrawable()) {
PlayerEntity player = MC.player;
if (player != null) {
double dx = player.getX() - player.prevX;
double dz = player.getZ() - player.prevZ;
double dy = player.getY() - player.prevY;

double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);

double speed = distance * 20 * 3.6;

String speedText = String.format("Speed: %.2f km/h", speed);

int textWidth = MC.textRenderer.getWidth(speedText);
int textHeight = MC.textRenderer.fontHeight;

setWidth(textWidth * 2);
setHeight(textHeight * 2);

Render2D.drawString(drawContext, speedText, pos.getX(), pos.getY(), GuiManager.foregroundColor.getValue().getColorAsInt());
}
Render2D.drawString(drawContext, speedText, pos.getX(), pos.getY(), GuiManager.foregroundColor.getValue().getColorAsInt());
}
}
}
Expand Down

0 comments on commit a1e8f77

Please sign in to comment.