Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced jank GUI by ViaMCP's slider #69

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/de/florianmichael/viaforge/ViaForge.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.version.VersionProvider;
import com.viaversion.viaversion.protocols.base.BaseVersionProvider;

import de.florianmichael.viaforge.gui.AsyncVersionSlider;
import net.minecraft.client.Minecraft;
import net.raphimc.vialoader.ViaLoader;
import net.raphimc.vialoader.impl.platform.ViaBackwardsPlatformImpl;
Expand All @@ -38,6 +40,8 @@ public class ViaForge {
public static void start() {
VersionEnum.SORTED_VERSIONS.remove(VersionEnum.r1_7_6tor1_7_10);
VersionEnum.SORTED_VERSIONS.remove(VersionEnum.r1_7_2tor1_7_5);

initAsyncSlider();

ViaLoader.init(
null,
Expand Down Expand Up @@ -71,4 +75,18 @@ public String getEncoderName() {
ViaBackwardsPlatformImpl::new, ViaRewindPlatformImpl::new
);
}

private static AsyncVersionSlider asyncVersionSlider;

public static void initAsyncSlider() {
initAsyncSlider(5, 5, 110, 20);
}

public static void initAsyncSlider(int x, int y, int width, int height) {
asyncVersionSlider = new AsyncVersionSlider(-1, x, y, Math.max(width, 110), height);
}

public static AsyncVersionSlider getAsyncVersionSlider() {
return asyncVersionSlider;
}
}
122 changes: 122 additions & 0 deletions src/main/java/de/florianmichael/viaforge/gui/AsyncVersionSlider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* This file is part of ViaMCP - https://github.com/FlorianMichael/ViaMCP
* Copyright (C) 2020-2023 FlorianMichael/EnZaXD and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.florianmichael.viaforge.gui;

import de.florianmichael.viaforge.ViaForge;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.MathHelper;
import net.raphimc.vialoader.util.VersionEnum;

import java.util.Collections;
import java.util.List;

public class AsyncVersionSlider extends GuiButton {
private float dragValue = calculateDragValue(ViaForge.targetVersion);

private final List<VersionEnum> values;
private float sliderValue;
public boolean dragging;

public AsyncVersionSlider(int buttonId, int x, int y, int widthIn, int heightIn) {
super(buttonId, x, y, Math.max(widthIn, 110), heightIn, "");
this.values = VersionEnum.SORTED_VERSIONS;
Collections.reverse(values);
this.sliderValue = dragValue;
this.displayString = getSliderVersion().getName();
}

/**
* Calculates and returns the dragValue for the provided version.
*/
public float calculateDragValue(VersionEnum version) {
int size = VersionEnum.SORTED_VERSIONS.size();
return (size - VersionEnum.SORTED_VERSIONS.indexOf(version)) / size;
}

/**
* Finds and returns the currently selected version from the slider.
*/
public VersionEnum getSliderVersion() {
return values.get((int) (this.sliderValue * (values.size() - 1)));
}

public void drawButton(Minecraft mc, int mouseX, int mouseY) {
super.drawButton(mc, mouseX, mouseY);
}

/**
* Returns 0 if the button is disabled, 1 if the mouse is NOT hovering over this
* button and 2 if it IS hovering over
* this button.
*/
protected int getHoverState(boolean mouseOver) {
return 0;
}

/**
* Fired when the mouse button is dragged. Equivalent of
* MouseListener.mouseDragged(MouseEvent e).
*/
protected void mouseDragged(Minecraft mc, int mouseX, int mouseY) {
if (this.visible) {
if (this.dragging) {
this.sliderValue = (float) (mouseX - (this.xPosition + 4)) / (float) (this.width - 8);
this.sliderValue = MathHelper.clamp_float(this.sliderValue, 0.0F, 1.0F);
this.dragValue = sliderValue;
this.displayString = getSliderVersion().getName();
ViaForge.targetVersion = getSliderVersion();
}

mc.getTextureManager().bindTexture(buttonTextures);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.drawTexturedModalRect(this.xPosition + (int) (this.sliderValue * (float) (this.width - 8)),
this.yPosition, 0, 66, 4, 20);
this.drawTexturedModalRect(this.xPosition + (int) (this.sliderValue * (float) (this.width - 8)) + 4,
this.yPosition, 196, 66, 4, 20);
}
}

/**
* Returns true if the mouse has been pressed on this control. Equivalent of
* MouseListener.mousePressed(MouseEvent
* e).
*/
public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
if (super.mousePressed(mc, mouseX, mouseY)) {
this.sliderValue = (float) (mouseX - (this.xPosition + 4)) / (float) (this.width - 8);
this.sliderValue = MathHelper.clamp_float(this.sliderValue, 0.0F, 1.0F);
this.dragValue = sliderValue;
this.displayString = getSliderVersion().getName();
ViaForge.targetVersion = getSliderVersion();
this.dragging = true;
return true;
} else {
return false;
}
}

/**
* Fired when the mouse button is released. Equivalent of
* MouseListener.mouseReleased(MouseEvent e).
*/
public void mouseReleased(int mouseX, int mouseY) {
this.dragging = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ protected void actionPerformed(GuiButton p_actionPerformed_1_) throws IOExceptio
mc.displayGuiScreen(parent);
}

@Override
protected void keyTyped(char p_keyTyped_1_, int p_keyTyped_2_) throws IOException {
if (p_keyTyped_2_ == 1) //esc key
this.mc.displayGuiScreen(parent);
}

@Override
public void handleMouseInput() throws IOException {
list.handleMouseInput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,20 @@
*/
package de.florianmichael.viaforge.mixin.impl;

import de.florianmichael.viaforge.gui.GuiProtocolSelector;
import de.florianmichael.viaforge.ViaForge;
import net.minecraft.client.gui.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = {
GuiMainMenu.class, GuiMultiplayer.class, GuiScreenServerList.class
GuiMultiplayer.class, GuiScreenServerList.class
})
public class MixinGuiMainMenuGuiMultiplayerGuiServerList extends GuiScreen {

@Inject(method = "initGui", at = @At("RETURN"))
public void hookCustomButton(CallbackInfo ci) {
buttonList.add(new GuiButton(1337, 5, 6, 98, 20, "ViaForge"));
}

@Inject(method = "actionPerformed", at = @At("RETURN"))
public void handleCustomButtonAction(GuiButton p_actionPerformed_1_, CallbackInfo ci) {
if (p_actionPerformed_1_.id == 1337) {
mc.displayGuiScreen(new GuiProtocolSelector(this));
}
buttonList.add(ViaForge.getAsyncVersionSlider());
}
}
Loading