forked from Clones/Controlify
✏️ Remove jank from the controller submission screen
This commit is contained in:
@ -9,12 +9,10 @@ import dev.isxander.controlify.hid.HIDDevice;
|
||||
import dev.isxander.controlify.utils.Log;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.client.gui.components.Button;
|
||||
import net.minecraft.client.gui.components.EditBox;
|
||||
import net.minecraft.client.gui.components.*;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.client.gui.screens.multiplayer.WarningScreen;
|
||||
import net.minecraft.network.chat.CommonComponents;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
@ -24,12 +22,14 @@ import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SubmitUnknownControllerScreen extends WarningScreen implements CustomWarningTitlePadding {
|
||||
public class SubmitUnknownControllerScreen extends Screen {
|
||||
public static final String SUBMISSION_URL = "https://api-controlify.isxander.dev/api/v1/submit";
|
||||
public static final Pattern NAME_PATTERN = Pattern.compile("^[\\w\\- ]{3,32}$");
|
||||
|
||||
private final Controller<?, ?> controller;
|
||||
|
||||
private Checkbox dontShowAgain;
|
||||
|
||||
private final Screen lastScreen;
|
||||
|
||||
private int invalidNameTicks;
|
||||
@ -38,12 +38,7 @@ public class SubmitUnknownControllerScreen extends WarningScreen implements Cust
|
||||
private EditBox nameField;
|
||||
|
||||
public SubmitUnknownControllerScreen(Controller<?, ?> controller, Screen lastScreen) {
|
||||
super(
|
||||
Component.translatable("controlify.controller_submission.title").withStyle(ChatFormatting.BOLD),
|
||||
Component.translatable("controlify.controller_submission.message"),
|
||||
Component.translatable("controlify.controller_submission.dont_show_again"),
|
||||
Component.translatable("controlify.controller_submission.title")
|
||||
);
|
||||
super(Component.translatable("controlify.controller_submission.title").withStyle(ChatFormatting.BOLD));
|
||||
if (!canSubmit(controller))
|
||||
throw new IllegalArgumentException("Controller ineligible for submission!");
|
||||
this.controller = controller;
|
||||
@ -51,24 +46,65 @@ public class SubmitUnknownControllerScreen extends WarningScreen implements Cust
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initButtons(int textHeight) {
|
||||
protected void init() {
|
||||
var content = this.addRenderableWidget(
|
||||
new MultiLineTextWidget(
|
||||
Component.translatable("controlify.controller_submission.message"),
|
||||
font
|
||||
)
|
||||
);
|
||||
content.setMaxWidth(this.width - 100);
|
||||
content.setX(this.width / 2 - content.getWidth() / 2);
|
||||
|
||||
int titleBottomPadding = 11;
|
||||
int checkboxPadding = 6;
|
||||
int checkboxHeight = 20;
|
||||
int buttonHeight = 20;
|
||||
int nameFieldPaddingTop = 5;
|
||||
int nameFieldHeight = 20;
|
||||
int errorPadding = 4;
|
||||
|
||||
int allHeight = font.lineHeight + titleBottomPadding + content.getHeight() + checkboxPadding + checkboxHeight + checkboxPadding + buttonHeight + nameFieldPaddingTop + nameFieldHeight + errorPadding + font.lineHeight;
|
||||
|
||||
int y = this.height / 2 - allHeight / 2;
|
||||
this.addRenderableWidget(createStringWidget(this.getTitle(), font, 25, y));
|
||||
y += font.lineHeight + titleBottomPadding;
|
||||
|
||||
content.setY(y);
|
||||
y += content.getHeight() + checkboxPadding;
|
||||
|
||||
var dontShowAgainText = Component.translatable("controlify.controller_submission.dont_show_again");
|
||||
this.dontShowAgain = this.addRenderableWidget(
|
||||
new Checkbox(
|
||||
this.width / 2 - font.width(dontShowAgainText) / 2 - 8,
|
||||
y,
|
||||
150,
|
||||
checkboxHeight,
|
||||
Component.translatable("controlify.controller_submission.dont_show_again"),
|
||||
false
|
||||
)
|
||||
);
|
||||
y += checkboxHeight + checkboxPadding;
|
||||
|
||||
this.submitButton = this.addRenderableWidget(
|
||||
Button.builder(Component.translatable("controlify.controller_submission.submit"), this::onSubmitButton)
|
||||
.pos(this.width / 2 - 155, textHeight + 80)
|
||||
.pos(this.width / 2 - 155, y)
|
||||
.width(150)
|
||||
.build()
|
||||
);
|
||||
this.addRenderableWidget(
|
||||
Button.builder(CommonComponents.GUI_CANCEL, btn -> onClose())
|
||||
.pos(this.width / 2 + 5, textHeight + 80)
|
||||
Button.builder(Component.translatable("controlify.controller_submission.skip"), btn -> onClose())
|
||||
.pos(this.width / 2 + 5, y)
|
||||
.width(150)
|
||||
.build()
|
||||
);
|
||||
y += buttonHeight + nameFieldPaddingTop;
|
||||
|
||||
this.nameField = this.addRenderableWidget(
|
||||
new EditBox(
|
||||
font,
|
||||
this.width / 2 - 155,
|
||||
textHeight + 105,
|
||||
y,
|
||||
310, 20,
|
||||
Component.translatable("controlify.controller_submission.name_narration")
|
||||
)
|
||||
@ -109,6 +145,8 @@ public class SubmitUnknownControllerScreen extends WarningScreen implements Cust
|
||||
onClose();
|
||||
} else {
|
||||
// TODO: Show error message
|
||||
dontShowAgain();
|
||||
|
||||
onClose();
|
||||
}
|
||||
}
|
||||
@ -162,7 +200,7 @@ public class SubmitUnknownControllerScreen extends WarningScreen implements Cust
|
||||
|
||||
@Override
|
||||
public void onClose() {
|
||||
if (stopShowing.selected()) {
|
||||
if (dontShowAgain.selected()) {
|
||||
dontShowAgain();
|
||||
}
|
||||
|
||||
@ -175,14 +213,8 @@ public class SubmitUnknownControllerScreen extends WarningScreen implements Cust
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLineHeight() {
|
||||
return 9;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMessageY() {
|
||||
return 50;
|
||||
private static StringWidget createStringWidget(Component text, Font font, int x, int y) {
|
||||
return new StringWidget(x, y, font.width(text.getVisualOrderText()), font.lineHeight, text, font);
|
||||
}
|
||||
|
||||
public static boolean canSubmit(Controller<?, ?> controller) {
|
||||
|
Reference in New Issue
Block a user