Remove duplicated code in screens, improve docs

This commit is contained in:
FlorianMichael 2024-10-11 16:11:07 +02:00
parent dccc75e045
commit b076971686
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
14 changed files with 72 additions and 76 deletions

View File

@ -19,12 +19,25 @@
package de.florianmichael.viafabricplus.screen; package de.florianmichael.viafabricplus.screen;
import de.florianmichael.viafabricplus.screen.base.PerServerVersionScreen;
import de.florianmichael.viafabricplus.screen.base.ProtocolSelectionScreen;
import de.florianmichael.viafabricplus.settings.impl.GeneralSettings; import de.florianmichael.viafabricplus.settings.impl.GeneralSettings;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget; import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget;
public class VFPList<T extends AlwaysSelectedEntryListWidget.Entry<T>> extends AlwaysSelectedEntryListWidget<T> { /**
* Wrapper class for {@link AlwaysSelectedEntryListWidget} including the following features:
* <ul>
* <li>Changing the constructor arguments to be more readable and customizable</li>
* <li>Adds {@link #initScrollAmount(double)} to save the scroll state after closing the screen, requires static tracking by the implementation</li>
* <li>Removes the selection box</li>
* </ul>
*
* @see ProtocolSelectionScreen
* @see PerServerVersionScreen
*/
public class VFPList extends AlwaysSelectedEntryListWidget<VFPListEntry> {
public VFPList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) { public VFPList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {
super(minecraftClient, width, height - top - bottom, top, entryHeight); super(minecraftClient, width, height - top - bottom, top, entryHeight);

View File

@ -30,8 +30,19 @@ import net.minecraft.util.math.MathHelper;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
* This class is a wrapper for the {@link net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget.Entry} class which provides some global * This class is a wrapper for the {@link net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget.Entry} class.
* functions and features used in all screens which are added by ViaFabricPlus * Features included:
* <ul>
* <li>Add wrapper function {@link #mappedRender(DrawContext, int, int, int, int, int, int, int, boolean, float)} for:
* <ul>
* <li>cross-sharing entry position/dimension between other helper functions</li>
* <li>Setting the entry position as start inside the {@link MatrixStack}</li>
* <li>rendering a default background</li>
* </ul>
* <li>Adds {@link #mappedMouseClicked(double, double, int)} to automatically play a click sound</li>
* <li>Adds some more utility functions, see {@link #renderScrollableText(Text, int, int)} and {@link #renderTooltip(Text, int, int)}</li>
* </li>
* </ul>
*/ */
public abstract class VFPListEntry extends AlwaysSelectedEntryListWidget.Entry<VFPListEntry> { public abstract class VFPListEntry extends AlwaysSelectedEntryListWidget.Entry<VFPListEntry> {

View File

@ -37,7 +37,32 @@ import java.awt.*;
/** /**
* This class is a wrapper for the {@link net.minecraft.client.gui.screen.Screen} class which provides some global * This class is a wrapper for the {@link net.minecraft.client.gui.screen.Screen} class which provides some global
* functions and features used in all screens which are added by ViaFabricPlus * functions and features used in all screens which are added by ViaFabricPlus.
* <p>
* Features:
* <ul>
* <li>Title and subtitle system, see:
* <ul>
* <li>{@link #setupDefaultSubtitle()}</li>
* <li>{@link #setupUrlSubtitle(String)}</li>
* <li>{@link #setupSubtitle(Text)}</li>
* <li>{@link #setupSubtitle(Text, ButtonWidget.PressAction)}</li>
* </ul>
* </li>
* <li>Automatically adds a button when set inside the constructor</li>
* <li>Helper functions:
* <ul>
* <li>{@link #playClickSound()}</li>
* <li>{@link #showErrorScreen(Text, Throwable, Screen)}</li>
* </ul>
* </li>
* </ul>
*
* Terminology:
* <p>
* Instead of creating the screen every time it needs to be opened, the screen is created once and hold by a static
* field and later opened by calling the {@link #open(Screen)} method.
* </p>
*/ */
public class VFPScreen extends Screen { public class VFPScreen extends Screen {
@ -131,6 +156,13 @@ public class VFPScreen extends Screen {
} }
} }
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
@Override @Override
public void close() { public void close() {
if (prevScreen instanceof VFPScreen vfpScreen) { if (prevScreen instanceof VFPScreen vfpScreen) {
@ -173,6 +205,10 @@ public class VFPScreen extends Screen {
return false; return false;
} }
public void renderScreenTitle(final DrawContext context) {
context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 70, 16777215);
}
/** /**
* Plays Minecraft's button click sound * Plays Minecraft's button click sound
*/ */

View File

@ -54,14 +54,7 @@ public class PerServerVersionScreen extends VFPScreen {
this.addDrawableChild(new SlotList(this.client, width, height, 3 + 3 /* start offset */ + (textRenderer.fontHeight + 2) * 3 /* title is 2 */, -5, textRenderer.fontHeight + 4)); this.addDrawableChild(new SlotList(this.client, width, height, 3 + 3 /* start offset */ + (textRenderer.fontHeight + 2) * 3 /* title is 2 */, -5, textRenderer.fontHeight + 4));
} }
@Override public class SlotList extends VFPList {
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
public class SlotList extends VFPList<VFPListEntry> {
public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) { public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {
super(minecraftClient, width, height, top, bottom, entryHeight); super(minecraftClient, width, height, top, bottom, entryHeight);

View File

@ -58,14 +58,7 @@ public class ProtocolSelectionScreen extends VFPScreen {
super.init(); super.init();
} }
@Override public static class SlotList extends VFPList {
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
public static class SlotList extends VFPList<VFPListEntry> {
private static double scrollAmount; private static double scrollAmount;
public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) { public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {

View File

@ -22,7 +22,6 @@ package de.florianmichael.viafabricplus.screen.base;
import com.viaversion.viaversion.util.DumpUtil; import com.viaversion.viaversion.util.DumpUtil;
import de.florianmichael.viafabricplus.ViaFabricPlus; import de.florianmichael.viafabricplus.ViaFabricPlus;
import de.florianmichael.viafabricplus.screen.VFPScreen; import de.florianmichael.viafabricplus.screen.VFPScreen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Util; import net.minecraft.util.Util;
@ -99,9 +98,4 @@ public class ReportIssuesScreen extends VFPScreen {
} }
} }
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
} }

View File

@ -19,14 +19,12 @@
package de.florianmichael.viafabricplus.screen.base; package de.florianmichael.viafabricplus.screen.base;
import de.florianmichael.classic4j.BetaCraftHandler;
import de.florianmichael.viafabricplus.ViaFabricPlus; import de.florianmichael.viafabricplus.ViaFabricPlus;
import de.florianmichael.viafabricplus.screen.VFPScreen; import de.florianmichael.viafabricplus.screen.VFPScreen;
import de.florianmichael.viafabricplus.screen.classic4j.BetaCraftScreen; import de.florianmichael.viafabricplus.screen.classic4j.BetaCraftScreen;
import de.florianmichael.viafabricplus.screen.classic4j.ClassiCubeLoginScreen; import de.florianmichael.viafabricplus.screen.classic4j.ClassiCubeLoginScreen;
import de.florianmichael.viafabricplus.screen.classic4j.ClassiCubeServerListScreen; import de.florianmichael.viafabricplus.screen.classic4j.ClassiCubeServerListScreen;
import de.florianmichael.viafabricplus.screen.realms.BedrockRealmsScreen; import de.florianmichael.viafabricplus.screen.realms.BedrockRealmsScreen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text; import net.minecraft.text.Text;
@ -82,10 +80,4 @@ public class ServerListScreen extends VFPScreen {
} }
} }
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
} }

View File

@ -73,19 +73,12 @@ public class BetaCraftScreen extends VFPScreen {
}).position(width - 60 - 5, 5).size(60, 20).build()); }).position(width - 60 - 5, 5).size(60, 20).build());
} }
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
@Override @Override
protected boolean subtitleCentered() { protected boolean subtitleCentered() {
return SERVER_LIST == null; return SERVER_LIST == null;
} }
public static class SlotList extends VFPList<VFPListEntry> { public static class SlotList extends VFPList {
private static double scrollAmount; private static double scrollAmount;
public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) { public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {

View File

@ -103,9 +103,7 @@ public class ClassiCubeLoginScreen extends VFPScreen {
@Override @Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) { public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta); super.render(context, mouseX, mouseY, delta);
this.renderTitle(context); this.renderScreenTitle(context);
context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 70, 16777215);
} }
} }

View File

@ -83,9 +83,7 @@ public class ClassiCubeMFAScreen extends VFPScreen {
@Override @Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) { public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta); super.render(context, mouseX, mouseY, delta);
this.renderTitle(context); this.renderScreenTitle(context);
context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 70, 16777215);
} }
} }

View File

@ -81,9 +81,7 @@ public class ClassiCubeServerListScreen extends VFPScreen {
@Override @Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) { public void render(DrawContext context, int mouseX, int mouseY, float delta) {
this.renderBackground(context, mouseX, mouseY, delta);
super.render(context, mouseX, mouseY, delta); super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
final var account = ViaFabricPlus.global().getSaveManager().getAccountsSave().getClassicubeAccount(); final var account = ViaFabricPlus.global().getSaveManager().getAccountsSave().getClassicubeAccount();
if (account != null) { if (account != null) {
@ -92,7 +90,7 @@ public class ClassiCubeServerListScreen extends VFPScreen {
} }
} }
public static class SlotList extends VFPList<VFPListEntry> { public static class SlotList extends VFPList {
private static double scrollAmount; private static double scrollAmount;
public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) { public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {
@ -113,7 +111,6 @@ public class ClassiCubeServerListScreen extends VFPScreen {
} }
} }
public static class ServerSlot extends VFPListEntry { public static class ServerSlot extends VFPListEntry {
private final CCServerInfo classiCubeServerInfo; private final CCServerInfo classiCubeServerInfo;

View File

@ -20,7 +20,6 @@
package de.florianmichael.viafabricplus.screen.realms; package de.florianmichael.viafabricplus.screen.realms;
import de.florianmichael.viafabricplus.screen.VFPScreen; import de.florianmichael.viafabricplus.screen.VFPScreen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text; import net.minecraft.text.Text;
@ -53,11 +52,4 @@ public class AcceptInvitationCodeScreen extends VFPScreen {
}).position(this.width / 2 - ButtonWidget.DEFAULT_WIDTH / 2, this.height / 2 + 20).build()); }).position(this.width / 2 - ButtonWidget.DEFAULT_WIDTH / 2, this.height / 2 + 20).build());
} }
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
} }

View File

@ -145,13 +145,6 @@ public class BedrockRealmsScreen extends VFPScreen {
}).position(xPos, height - 20 - 5).size(115, 20).build()); }).position(xPos, height - 20 - 5).size(115, 20).build());
} }
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();
@ -167,7 +160,7 @@ public class BedrockRealmsScreen extends VFPScreen {
return slotList == null; return slotList == null;
} }
public class SlotList extends VFPList<VFPListEntry> { public class SlotList extends VFPList {
private static double scrollAmount; private static double scrollAmount;
public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) { public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {

View File

@ -45,14 +45,7 @@ public class SettingsScreen extends VFPScreen {
super.init(); super.init();
} }
@Override public static class SlotList extends VFPList {
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
this.renderTitle(context);
}
public static class SlotList extends VFPList<VFPListEntry> {
private static double scrollAmount; private static double scrollAmount;
public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) { public SlotList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {