diff --git a/gradle.properties b/gradle.properties index 7460226..2e21a5d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.7.2+build.174 # Mod Properties - mod_version = 1.0.0-SNAPSHOT1 + mod_version = 1.0.0-SNAPSHOT2 maven_group = me.lambdaurora archives_base_name = lambdacontrols diff --git a/src/main/java/me/lambdaurora/lambdacontrols/ControllerInput.java b/src/main/java/me/lambdaurora/lambdacontrols/ControllerInput.java index 54298d6..0bf8392 100644 --- a/src/main/java/me/lambdaurora/lambdacontrols/ControllerInput.java +++ b/src/main/java/me/lambdaurora/lambdacontrols/ControllerInput.java @@ -9,6 +9,7 @@ package me.lambdaurora.lambdacontrols; +import me.lambdaurora.lambdacontrols.gui.LabelWidget; import me.lambdaurora.lambdacontrols.gui.LambdaControlsControlsScreen; import me.lambdaurora.lambdacontrols.mixin.EntryListWidgetAccessor; import me.lambdaurora.lambdacontrols.util.AbstractContainerScreenAccessor; @@ -82,15 +83,18 @@ public class ControllerInput public void on_tick(@NotNull MinecraftClient client) { + this.prev_target_yaw = this.target_yaw; + this.prev_target_pitch = this.target_pitch; + + // Handles the key bindings. if (LambdaControls.BINDING_LOOK_UP.isPressed()) { this.handle_look(client, GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 2); } else if (LambdaControls.BINDING_LOOK_DOWN.isPressed()) { this.handle_look(client, GLFW_GAMEPAD_AXIS_RIGHT_Y, 0.8F, 1); } - - if (LambdaControls.BINDING_LOOK_RIGHT.isPressed()) { + if (LambdaControls.BINDING_LOOK_LEFT.isPressed()) { this.handle_look(client, GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 2); - } else if (LambdaControls.BINDING_LOOK_LEFT.isPressed()) { + } else if (LambdaControls.BINDING_LOOK_RIGHT.isPressed()) { this.handle_look(client, GLFW_GAMEPAD_AXIS_RIGHT_X, 0.8F, 1); } } @@ -110,8 +114,6 @@ public class ControllerInput // Decreases the cooldown for GUI actions. if (this.action_gui_cooldown > 0) --this.action_gui_cooldown; - this.prev_target_yaw = this.target_yaw; - this.prev_target_pitch = this.target_pitch; this.prev_target_mouse_x = this.target_mouse_x; this.prev_target_mouse_y = this.target_mouse_y; @@ -464,6 +466,9 @@ public class ControllerInput button_widget.playDownSound(MinecraftClient.getInstance().getSoundManager()); button_widget.onPress(); return true; + } else if (focused instanceof LabelWidget) { + ((LabelWidget) focused).on_press(); + return true; } else if (focused instanceof WorldListWidget) { WorldListWidget list = (WorldListWidget) focused; list.method_20159().ifPresent(WorldListWidget.Entry::play); diff --git a/src/main/java/me/lambdaurora/lambdacontrols/gui/LabelWidget.java b/src/main/java/me/lambdaurora/lambdacontrols/gui/LabelWidget.java index 4c13699..cb5b101 100644 --- a/src/main/java/me/lambdaurora/lambdacontrols/gui/LabelWidget.java +++ b/src/main/java/me/lambdaurora/lambdacontrols/gui/LabelWidget.java @@ -9,13 +9,19 @@ package me.lambdaurora.lambdacontrols.gui; +import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.Drawable; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.Element; +import net.minecraft.client.render.Tessellator; +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.util.math.Matrix4f; +import net.minecraft.client.util.math.MatrixStack; import org.jetbrains.annotations.NotNull; import org.lwjgl.glfw.GLFW; +import java.util.List; import java.util.function.Consumer; /** @@ -28,31 +34,34 @@ public class LabelWidget extends DrawableHelper implements Element, Drawable }; private final MinecraftClient client = MinecraftClient.getInstance(); - private final Consumer click_action; + private final Consumer press_action; private final int x; private final int y; private final int max_width; //private final int max_height; private String text; + private String tooltip_text; public boolean visible; private int width; private int height; private boolean centered; + protected boolean hovered; + protected boolean focused; - public LabelWidget(int x, int y, @NotNull String text, int max_width, @NotNull Consumer click_action, boolean centered) + public LabelWidget(int x, int y, @NotNull String text, int max_width, @NotNull Consumer press_action, boolean centered) { this.visible = true; this.x = x; this.y = y; this.max_width = max_width; - this.click_action = click_action; + this.press_action = press_action; this.centered = centered; this.set_text(text); } - public LabelWidget(int x, int y, @NotNull String text, int max_width, @NotNull Consumer click_action) + public LabelWidget(int x, int y, @NotNull String text, int max_width, @NotNull Consumer press_action) { - this(x, y, text, max_width, click_action, false); + this(x, y, text, max_width, press_action, false); } public LabelWidget(int x, int y, @NotNull String text, int max_width, boolean centered) @@ -73,11 +82,9 @@ public class LabelWidget extends DrawableHelper implements Element, Drawable public void set_text(@NotNull String text) { int width = this.client.textRenderer.getStringWidth(text); - if (width > this.max_width) { - while (width > this.max_width) { - text = text.substring(0, text.length() - 1); - width = this.client.textRenderer.getStringWidth(text); - } + while (width > this.max_width) { + text = text.substring(0, text.length() - 1); + width = this.client.textRenderer.getStringWidth(text); } this.text = text; @@ -85,6 +92,16 @@ public class LabelWidget extends DrawableHelper implements Element, Drawable this.height = this.client.textRenderer.fontHeight; } + /** + * Sets the tooltip text of this label. + * + * @param text The tooltip text. + */ + public void set_tooltip_text(String text) + { + this.tooltip_text = text; + } + /** * Gets the width of this label widget. * @@ -105,24 +122,118 @@ public class LabelWidget extends DrawableHelper implements Element, Drawable return this.height; } + /** + * Fires the press event on this label widget. + */ + public void on_press() + { + this.press_action.accept(this); + } + @Override public void render(int mouse_x, int mouse_y, float delta) { - if (this.centered) - this.drawCenteredString(this.client.textRenderer, this.text, this.x, this.y, 10526880); - else - this.drawString(this.client.textRenderer, this.text, this.x, this.y, 10526880); + if (this.visible) { + int x = this.centered ? this.x - this.client.textRenderer.getStringWidth(this.text) / 2 : this.x; + this.hovered = mouse_x >= x && mouse_y >= this.y && mouse_x < x + this.width && mouse_y < this.y + this.height; + this.drawString(this.client.textRenderer, this.text, x, this.y, 10526880); + + if (this.tooltip_text != null && !this.tooltip_text.isEmpty()) { + List wrapped_tooltip_text = this.client.textRenderer.wrapStringToWidthAsList(this.tooltip_text, Math.max(this.width / 2, 200)); + if (this.hovered) + this.render_tooltip(wrapped_tooltip_text, mouse_x, mouse_y); + else if (this.focused) + this.render_tooltip(wrapped_tooltip_text, this.x - 12, this.y); + } + } } @Override public boolean mouseClicked(double mouse_x, double mouse_y, int button) { if (this.visible && button == GLFW.GLFW_MOUSE_BUTTON_1) { - if (mouse_x >= (double) this.x && mouse_y >= (double) this.y && mouse_x < (double) (this.x + this.width) && mouse_y < (double) (this.y + this.height)) { - this.click_action.accept(this); + if (this.hovered) { + this.on_press(); return true; } } return false; } + + @Override + public boolean changeFocus(boolean down) + { + if (this.visible) { + this.focused = !this.focused; + return this.focused; + } else { + return false; + } + } + + public void render_tooltip(List text, int x, int y) + { + if (!text.isEmpty()) { + RenderSystem.disableRescaleNormal(); + RenderSystem.disableDepthTest(); + int i = 0; + + for (String string : text) { + int j = this.client.textRenderer.getStringWidth(string); + if (j > i) { + i = j; + } + } + + int k = x + 12; + int l = y - 12; + int n = 8; + if (text.size() > 1) { + n += 2 + (text.size() - 1) * 10; + } + + if (k + i > this.client.getWindow().getScaledWidth()) { + k -= 28 + i; + } + + if (l + n + 6 > this.client.getWindow().getScaledHeight()) { + l = this.client.getWindow().getScaledHeight() - n - 6; + } + + this.setBlitOffset(300); + this.client.getItemRenderer().zOffset = 300.0F; + this.fillGradient(k - 3, l - 4, k + i + 3, l - 3, -267386864, -267386864); + this.fillGradient(k - 3, l + n + 3, k + i + 3, l + n + 4, -267386864, -267386864); + this.fillGradient(k - 3, l - 3, k + i + 3, l + n + 3, -267386864, -267386864); + this.fillGradient(k - 4, l - 3, k - 3, l + n + 3, -267386864, -267386864); + this.fillGradient(k + i + 3, l - 3, k + i + 4, l + n + 3, -267386864, -267386864); + this.fillGradient(k - 3, l - 3 + 1, k - 3 + 1, l + n + 3 - 1, 1347420415, 1344798847); + this.fillGradient(k + i + 2, l - 3 + 1, k + i + 3, l + n + 3 - 1, 1347420415, 1344798847); + this.fillGradient(k - 3, l - 3, k + i + 3, l - 3 + 1, 1347420415, 1347420415); + this.fillGradient(k - 3, l + n + 2, k + i + 3, l + n + 3, 1344798847, 1344798847); + MatrixStack matrixStack = new MatrixStack(); + VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer()); + matrixStack.translate(0.0D, 0.0D, this.client.getItemRenderer().zOffset); + Matrix4f matrix4f = matrixStack.peek().getModel(); + + for (int r = 0; r < text.size(); ++r) { + String string2 = text.get(r); + if (string2 != null) { + this.client.textRenderer.draw(string2, (float) k, (float) l, -1, true, matrix4f, immediate, false, 0, 15728880); + } + + if (r == 0) { + l += 2; + } + + l += 10; + } + + immediate.draw(); + this.setBlitOffset(0); + this.client.getItemRenderer().zOffset = 0.0F; + RenderSystem.enableDepthTest(); + RenderSystem.enableRescaleNormal(); + } + } } diff --git a/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsHud.java b/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsHud.java index 30c0d09..a41722c 100644 --- a/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsHud.java +++ b/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsHud.java @@ -36,7 +36,7 @@ public class LambdaControlsHud extends DrawableHelper */ public void render() { - if (this.mod.config.get_controls_mode() == ControlsMode.CONTROLLER && this.mod.config.is_hud_enabled() && this.client.currentScreen == null) { + if (this.mod.config.get_controls_mode() == ControlsMode.CONTROLLER && this.mod.config.is_hud_enabled() && this.client.currentScreen == null && !this.client.options.hudHidden) { int x = 10, y = bottom(10); x += this.draw_button_tip(x, y, ButtonBinding.INVENTORY, true) + 10; this.draw_button_tip(x, y, ButtonBinding.SWAP_HANDS, true); diff --git a/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsSettingsScreen.java b/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsSettingsScreen.java index de1cf26..0619c47 100644 --- a/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsSettingsScreen.java +++ b/src/main/java/me/lambdaurora/lambdacontrols/gui/LambdaControlsSettingsScreen.java @@ -147,6 +147,7 @@ public class LambdaControlsSettingsScreen extends Screen this.gamepad_tool_url_label = new LabelWidget(this.width / 2, this.height - 29 - (5 + this.font.fontHeight) * 2, this.controller_mappings_url_text, this.width, label -> Util.getOperatingSystem().open(GAMEPAD_TOOL_URL), true); + this.gamepad_tool_url_label.set_tooltip_text(I18n.translate("chat.link.open")); this.children.add(this.gamepad_tool_url_label); this.addButton(new ButtonWidget(this.width / 2 - 155, this.height - 29, 300, button_height, I18n.translate("gui.done"), diff --git a/src/main/java/me/lambdaurora/lambdacontrols/gui/TouchscreenOverlay.java b/src/main/java/me/lambdaurora/lambdacontrols/gui/TouchscreenOverlay.java index f1e6f45..d957c3e 100644 --- a/src/main/java/me/lambdaurora/lambdacontrols/gui/TouchscreenOverlay.java +++ b/src/main/java/me/lambdaurora/lambdacontrols/gui/TouchscreenOverlay.java @@ -253,4 +253,10 @@ public class TouchscreenOverlay extends Screen { super.render(mouseX, mouseY, delta); } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) + { + return false; + } }