From 7cdd7cdc73f9e7bda1ff86f0628a47ac26e4ebc0 Mon Sep 17 00:00:00 2001 From: maloryware Date: Tue, 31 Dec 2024 18:56:47 +0000 Subject: [PATCH 1/6] Entry Title rendered as MultilineTextWidget Added tooltip functionality for Entry Title --- .../lib/config/MidnightConfig.java | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java index 131176e..73f4f8e 100755 --- a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java +++ b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java @@ -33,6 +33,12 @@ import static net.minecraft.client.MinecraftClient.IS_SYSTEM_MAC; * Based on ... * Credits to Minenash */ +// hi mots! + // TODO: + // define identifier for fetching the title tooltip text from lang + // fetch title tooltip from entry info? + // refactor at will :p + @SuppressWarnings("unchecked") public abstract class MidnightConfig { private static final Pattern INTEGER_ONLY = Pattern.compile("(-?[0-9]*)"); @@ -79,6 +85,7 @@ public abstract class MidnightConfig { .registerTypeAdapter(Identifier.class, new Identifier.Serializer()) .setPrettyPrinting().create(); + @SuppressWarnings("unused") // shhhhhh... public static @Nullable Object getDefaultValue(String modid, String entry) { for (EntryInfo e : entries) { if (modid.equals(e.modid) && entry.equals(e.field.getName())) return e.defaultValue; @@ -107,6 +114,7 @@ public abstract class MidnightConfig { } catch (IllegalAccessException ignored) {} } } + @SuppressWarnings("ConstantValue") //pertains to requiredModLoaded @Environment(EnvType.CLIENT) private static void initClient(String modid, Field field, EntryInfo info) { info.dataType = getUnderlyingType(field); @@ -118,6 +126,7 @@ public abstract class MidnightConfig { if (e != null) { if (!e.requiredMod().isEmpty()) requiredModLoaded = PlatformFunctions.isModLoaded(e.requiredMod()); + if (!requiredModLoaded) return; if (!e.name().isEmpty()) info.name = Text.translatable(e.name()); if (info.dataType == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, (int) e.min(), (int) e.max(), true); @@ -155,6 +164,7 @@ public abstract class MidnightConfig { } // TODO: Maybe move this into the screen class itself to free up some RAM? + private static void textField(EntryInfo info, Function f, Pattern pattern, double min, double max, boolean cast) { boolean isNumber = pattern != null; info.function = (BiFunction>) (t, b) -> s -> { @@ -412,7 +422,7 @@ public abstract class MidnightConfig { @Environment(EnvType.CLIENT) public static class MidnightConfigListWidget extends ElementListWidget { public boolean renderHeaderSeparator = true; - public MidnightConfigListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) { super(client, width, height, y, itemHeight); } + public MidnightConfigListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) { super(client, width, height, y, itemHeight); } @Override public int getScrollbarX() { return this.width -7; } @Override @@ -432,18 +442,56 @@ public abstract class MidnightConfig { public final List buttons; public final EntryInfo info; public boolean centered = false; + public final Text tooltipText; + public final TextWidget entryText; + public MultilineTextWidget title; + private final List tooltipLines; public ButtonEntry(List buttons, Text text, EntryInfo info) { this.buttons = buttons; this.text = text; this.info = info; + this.entryText = text != null ? new TextWidget(text, textRenderer) : null; if (info != null) this.centered = info.centered; + + this.tooltipText = Text.of("A very neat, original, thought out, yet badly worded tooltip, leading to uncomfortably long text."); + this.tooltipLines = textRenderer.wrapLines(tooltipText, MinecraftClient.getInstance().getWindow().getScaledWidth() / 2); + + // moved text declaration to constructor + if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) { + //int wrappedY = y; + title = new MultilineTextWidget( + (centered) // x + ? (MinecraftClient.getInstance().getWindow().getScaledWidth() / 2 - (textRenderer.getWidth(text) / 2)) + : 12, + 0, // will be set on render + Text.of(text), + textRenderer + ); + + title.setTooltip(Tooltip.of(Text.of("Test!"))); + title.setMaxWidth(buttons.size() > 1 + ? buttons.get(1).getX() - 24 + : MinecraftClient.getInstance().getWindow().getScaledWidth() - 24); + } } public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { - buttons.forEach(b -> { b.setY(y); b.render(context, mouseX, mouseY, tickDelta); }); - if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) { int wrappedY = y; - for (Iterator textIterator = textRenderer.wrapLines(text, (buttons.size() > 1 ? buttons.get(1).getX()-24 : MinecraftClient.getInstance().getWindow().getScaledWidth() - 24)).iterator(); textIterator.hasNext(); wrappedY += 9) { - context.drawTextWithShadow(textRenderer, textIterator.next(), (centered) ? (MinecraftClient.getInstance().getWindow().getScaledWidth() / 2 - (textRenderer.getWidth(text) / 2)) : 12, wrappedY + 5, 0xFFFFFF); + buttons.forEach(b -> { b.setY(y); b.render(context, mouseX, mouseY, tickDelta);}); + + if(title != null) { + + title.setY(y + 9); + + title.render(context, mouseX, mouseY, tickDelta); + + + // isHovered and isMouseOver didn't work! sad! + if(mouseX >= title.getX() && mouseX < title.getWidth() + title.getX() + && mouseY >= title.getY() && mouseY < title.getHeight() + title.getY()) + context.drawOrderedTooltip(textRenderer, tooltipLines, mouseX, mouseY); + + // testing purposes + // else context.drawTooltip(textRenderer, Text.of(String.format("%s, %s", mouseX, mouseY)), mouseX, mouseY); } - } + } public List children() {return Lists.newArrayList(buttons);} public List selectableChildren() {return Lists.newArrayList(buttons);} From 00e080e7155e910d8e48845f7b5b1df43cbdf9ad Mon Sep 17 00:00:00 2001 From: maloryware Date: Tue, 31 Dec 2024 18:57:42 +0000 Subject: [PATCH 2/6] bunp. --- .../main/java/eu/midnightdust/lib/config/MidnightConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java index 73f4f8e..f65bebf 100755 --- a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java +++ b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java @@ -489,7 +489,7 @@ public abstract class MidnightConfig { context.drawOrderedTooltip(textRenderer, tooltipLines, mouseX, mouseY); // testing purposes - // else context.drawTooltip(textRenderer, Text.of(String.format("%s, %s", mouseX, mouseY)), mouseX, mouseY); + /* else context.drawTooltip(textRenderer, Text.of(String.format("%s, %s", mouseX, mouseY)), mouseX, mouseY); */ } } From 803607259ee662415a862d1c51aff3ff6de77902 Mon Sep 17 00:00:00 2001 From: maloryware Date: Tue, 31 Dec 2024 20:11:04 +0000 Subject: [PATCH 3/6] added javadocs for Server and Hidden annotations. --- .../eu/midnightdust/lib/config/MidnightConfig.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java index f65bebf..70cc323 100755 --- a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java +++ b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java @@ -555,7 +555,17 @@ public abstract class MidnightConfig { } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Client {} + + /** + * Hides the entry on singleplayer/client-side. + * Accessible through{@code /midnightconfig MOD_ID ENTRY} and through directly editing the config file. + */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Server {} + + /** + * Hides the entry entirely. + * Accessible only through directly editing the config file. + */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Hidden {} @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Comment { boolean centered() default false; From a32593ede0a71bd1ce3a69cc23877f05405b9eff Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Tue, 14 Jan 2025 10:27:35 +0100 Subject: [PATCH 4/6] feat: improve label tooltip code - Users can now define labels for text via this translation key pattern: "modid.midnightconfig.optionName.label.tooltip" --- .../lib/config/MidnightConfig.java | 59 ++++++------------- .../modid/{en_US.json => lang/en_us.json} | 1 + 2 files changed, 20 insertions(+), 40 deletions(-) rename test-fabric/src/main/resources/assets/modid/{en_US.json => lang/en_us.json} (94%) diff --git a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java index 70cc323..c1b2a78 100755 --- a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java +++ b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java @@ -13,7 +13,7 @@ import net.minecraft.client.render.RenderLayer; import net.minecraft.client.resource.language.I18n; import net.minecraft.registry.Registries; import net.minecraft.screen.ScreenTexts; -import net.minecraft.text.OrderedText; import net.minecraft.text.Style; import net.minecraft.text.Text; +import net.minecraft.text.Style; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; import org.jetbrains.annotations.Nullable; @@ -33,12 +33,6 @@ import static net.minecraft.client.MinecraftClient.IS_SYSTEM_MAC; * Based on ... * Credits to Minenash */ -// hi mots! - // TODO: - // define identifier for fetching the title tooltip text from lang - // fetch title tooltip from entry info? - // refactor at will :p - @SuppressWarnings("unchecked") public abstract class MidnightConfig { private static final Pattern INTEGER_ONLY = Pattern.compile("(-?[0-9]*)"); @@ -158,9 +152,9 @@ public abstract class MidnightConfig { } catch (NoSuchFieldException | IllegalAccessException ignored) { return listType; } } else return field.getType(); } - public static Tooltip getTooltip(EntryInfo info) { - String key = info.modid + ".midnightconfig."+info.field.getName()+".tooltip"; - return Tooltip.of(info.error != null ? info.error : I18n.hasTranslation(key) ? Text.translatable(key) : Text.empty()); + public static Tooltip getTooltip(EntryInfo info, boolean isButton) { + String key = info.modid + ".midnightconfig."+info.field.getName()+(!isButton ? ".label" : "" )+".tooltip"; + return Tooltip.of(isButton && info.error != null ? info.error : I18n.hasTranslation(key) ? Text.translatable(key) : Text.empty()); } // TODO: Maybe move this into the screen class itself to free up some RAM? @@ -178,7 +172,7 @@ public abstract class MidnightConfig { info.error = inLimits? null : Text.literal(value.doubleValue() < min ? "§cMinimum " + (isNumber? "value" : "length") + (cast? " is " + (int)min : " is " + min) : "§cMaximum " + (isNumber? "value" : "length") + (cast? " is " + (int)max : " is " + max)).formatted(Formatting.RED); - t.setTooltip(getTooltip(info)); + t.setTooltip(getTooltip(info, true)); } info.tempValue = s; @@ -268,7 +262,7 @@ public abstract class MidnightConfig { for (ButtonEntry entry : this.list.children()) { if (entry.buttons != null && entry.buttons.size() > 1) { if (entry.buttons.get(0) instanceof ClickableWidget widget) - if (widget.isFocused() || widget.isHovered()) widget.setTooltip(getTooltip(entry.info)); + if (widget.isFocused() || widget.isHovered()) widget.setTooltip(getTooltip(entry.info, true)); if (entry.buttons.get(1) instanceof ButtonWidget button) button.active = !Objects.equals(entry.info.value.toString(), entry.info.defaultValue.toString()); }}}} @@ -333,7 +327,7 @@ public abstract class MidnightConfig { var values = (Map.Entry>) info.function; if (info.dataType.isEnum()) values.setValue(value -> Text.translatable(translationPrefix + "enum." + info.field.getType().getSimpleName() + "." + info.value.toString())); - widget = ButtonWidget.builder(values.getValue().apply(info.value), values.getKey()).dimensions(width - 185, 0, 150, 20).tooltip(getTooltip(info)).build(); + widget = ButtonWidget.builder(values.getValue().apply(info.value), values.getKey()).dimensions(width - 185, 0, 150, 20).tooltip(getTooltip(info, true)).build(); } else if (e.isSlider()) widget = new MidnightSliderWidget(width - 185, 0, 150, 20, Text.of(info.tempValue), (Double.parseDouble(info.tempValue) - e.min()) / (e.max() - e.min()), info); @@ -344,7 +338,7 @@ public abstract class MidnightConfig { Predicate processor = ((BiFunction>) info.function).apply(textField, done); textField.setTextPredicate(processor); } - widget.setTooltip(getTooltip(info)); + widget.setTooltip(getTooltip(info, true)); ButtonWidget cycleButton = null; if (info.field.getType() == List.class) { @@ -442,54 +436,39 @@ public abstract class MidnightConfig { public final List buttons; public final EntryInfo info; public boolean centered = false; - public final Text tooltipText; - public final TextWidget entryText; public MultilineTextWidget title; - private final List tooltipLines; public ButtonEntry(List buttons, Text text, EntryInfo info) { this.buttons = buttons; this.text = text; this.info = info; - this.entryText = text != null ? new TextWidget(text, textRenderer) : null; if (info != null) this.centered = info.centered; - - this.tooltipText = Text.of("A very neat, original, thought out, yet badly worded tooltip, leading to uncomfortably long text."); - this.tooltipLines = textRenderer.wrapLines(tooltipText, MinecraftClient.getInstance().getWindow().getScaledWidth() / 2); + int scaledWidth = MinecraftClient.getInstance().getWindow().getScaledWidth(); // moved text declaration to constructor if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) { - //int wrappedY = y; title = new MultilineTextWidget( (centered) // x - ? (MinecraftClient.getInstance().getWindow().getScaledWidth() / 2 - (textRenderer.getWidth(text) / 2)) + ? (scaledWidth / 2 - (textRenderer.getWidth(text) / 2)) : 12, 0, // will be set on render - Text.of(text), - textRenderer + Text.of(text), textRenderer ); - title.setTooltip(Tooltip.of(Text.of("Test!"))); + if (info != null) title.setTooltip(getTooltip(info, false)); title.setMaxWidth(buttons.size() > 1 ? buttons.get(1).getX() - 24 - : MinecraftClient.getInstance().getWindow().getScaledWidth() - 24); + : scaledWidth - 24); } } public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { buttons.forEach(b -> { b.setY(y); b.render(context, mouseX, mouseY, tickDelta);}); - - if(title != null) { - + if (title != null) { title.setY(y + 9); + title.renderWidget(context, mouseX, mouseY, tickDelta); - title.render(context, mouseX, mouseY, tickDelta); - - - // isHovered and isMouseOver didn't work! sad! - if(mouseX >= title.getX() && mouseX < title.getWidth() + title.getX() - && mouseY >= title.getY() && mouseY < title.getHeight() + title.getY()) - context.drawOrderedTooltip(textRenderer, tooltipLines, mouseX, mouseY); - - // testing purposes - /* else context.drawTooltip(textRenderer, Text.of(String.format("%s, %s", mouseX, mouseY)), mouseX, mouseY); */ + boolean tooltipVisible = mouseX >= title.getX() && mouseX < title.getWidth() + title.getX() && mouseY >= title.getY() && mouseY < title.getHeight() + title.getY(); + if (tooltipVisible && title.getTooltip() != null) { + context.drawOrderedTooltip(textRenderer, title.getTooltip().getLines(MinecraftClient.getInstance()), mouseX, mouseY); + } } } diff --git a/test-fabric/src/main/resources/assets/modid/en_US.json b/test-fabric/src/main/resources/assets/modid/lang/en_us.json similarity index 94% rename from test-fabric/src/main/resources/assets/modid/en_US.json rename to test-fabric/src/main/resources/assets/modid/lang/en_us.json index 993eca1..8ac67dd 100644 --- a/test-fabric/src/main/resources/assets/modid/en_US.json +++ b/test-fabric/src/main/resources/assets/modid/lang/en_us.json @@ -3,6 +3,7 @@ "modid.midnightconfig.text1":"I am a comment *u*", "modid.midnightconfig.text2":"I am a centered comment (╯°□°)╯︵ ┻━┻", "modid.midnightconfig.name":"I am a string!", + "modid.midnightconfig.name.label.tooltip":"I am a label tooltip \nWohoo!", "modid.midnightconfig.name.tooltip":"I am a tooltip uwu \nI am a new line", "modid.midnightconfig.fabric":"I am an int", "modid.midnightconfig.world":"I am a double", From 6f35fb56584e051b93c869a79c59da19a7fe6d3a Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Tue, 14 Jan 2025 10:35:14 +0100 Subject: [PATCH 5/6] docs: improve javadocs --- .../java/eu/midnightdust/lib/config/MidnightConfig.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java index c1b2a78..1ec6c50 100755 --- a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java +++ b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java @@ -496,7 +496,7 @@ public abstract class MidnightConfig { /** * Entry Annotation
- * - width: The maximum character length of the {@link String}, {@link Identifier} or String/Identifier {@link List} field
+ * - width: The maximum character length of the {@link String}, {@link Identifier} or String/Identifier {@link List<>} field
* - min: The minimum value of the int, float or double field
* - max: The maximum value of the int, float or double field
* - name: The name of the field in the config screen
@@ -536,16 +536,18 @@ public abstract class MidnightConfig { @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Client {} /** - * Hides the entry on singleplayer/client-side. - * Accessible through{@code /midnightconfig MOD_ID ENTRY} and through directly editing the config file. + * Hides the entry in config screens, but still makes it + * accessible through the command {@code /midnightconfig MOD_ID ENTRY} and directly editing the config file. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Server {} /** * Hides the entry entirely. * Accessible only through directly editing the config file. + * Perfect for saving persistent internal data. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Hidden {} + @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Comment { boolean centered() default false; String category() default "default"; From 37fff5a9c5e7eba9430f05b3a9540bf79bbfb88f Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Tue, 14 Jan 2025 10:48:48 +0100 Subject: [PATCH 6/6] clean: compactify tooltip code --- .../lib/config/MidnightConfig.java | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java index 1ec6c50..dd313f4 100755 --- a/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java +++ b/common/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java @@ -79,7 +79,7 @@ public abstract class MidnightConfig { .registerTypeAdapter(Identifier.class, new Identifier.Serializer()) .setPrettyPrinting().create(); - @SuppressWarnings("unused") // shhhhhh... + @SuppressWarnings("unused") // Utility for mod authors public static @Nullable Object getDefaultValue(String modid, String entry) { for (EntryInfo e : entries) { if (modid.equals(e.modid) && entry.equals(e.field.getName())) return e.defaultValue; @@ -443,34 +443,21 @@ public abstract class MidnightConfig { if (info != null) this.centered = info.centered; int scaledWidth = MinecraftClient.getInstance().getWindow().getScaledWidth(); - // moved text declaration to constructor if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) { - title = new MultilineTextWidget( - (centered) // x - ? (scaledWidth / 2 - (textRenderer.getWidth(text) / 2)) - : 12, - 0, // will be set on render - Text.of(text), textRenderer - ); - + title = new MultilineTextWidget((centered) ? (scaledWidth / 2 - (textRenderer.getWidth(text) / 2)) : 12, 0, Text.of(text), textRenderer); if (info != null) title.setTooltip(getTooltip(info, false)); - title.setMaxWidth(buttons.size() > 1 - ? buttons.get(1).getX() - 24 - : scaledWidth - 24); + title.setMaxWidth(buttons.size() > 1 ? buttons.get(1).getX() - 24 : scaledWidth - 24); } } public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { buttons.forEach(b -> { b.setY(y); b.render(context, mouseX, mouseY, tickDelta);}); - if (title != null) { - title.setY(y + 9); - title.renderWidget(context, mouseX, mouseY, tickDelta); - - boolean tooltipVisible = mouseX >= title.getX() && mouseX < title.getWidth() + title.getX() && mouseY >= title.getY() && mouseY < title.getHeight() + title.getY(); - if (tooltipVisible && title.getTooltip() != null) { - context.drawOrderedTooltip(textRenderer, title.getTooltip().getLines(MinecraftClient.getInstance()), mouseX, mouseY); - } - } + if (title != null) { + title.setY(y + 9); + title.renderWidget(context, mouseX, mouseY, tickDelta); + boolean tooltipVisible = mouseX >= title.getX() && mouseX < title.getWidth() + title.getX() && mouseY >= title.getY() && mouseY < title.getHeight() + title.getY(); + if (tooltipVisible && title.getTooltip() != null) context.drawOrderedTooltip(textRenderer, title.getTooltip().getLines(MinecraftClient.getInstance()), mouseX, mouseY); + } } public List children() {return Lists.newArrayList(buttons);} public List selectableChildren() {return Lists.newArrayList(buttons);}