From 14343569fe6aa55c707e055736e6e4afaee0714e Mon Sep 17 00:00:00 2001 From: James Seibel Date: Tue, 21 May 2024 06:56:27 -0500 Subject: [PATCH] Fix neoforge config button position --- .../mixins/client/MixinOptionsScreen.java | 4 +- .../mixins/client/MixinOptionsScreen.java | 110 +++++++++++++----- 2 files changed, 86 insertions(+), 28 deletions(-) diff --git a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinOptionsScreen.java b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinOptionsScreen.java index 72f582380..ed2274cf6 100644 --- a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinOptionsScreen.java +++ b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinOptionsScreen.java @@ -94,7 +94,7 @@ public class MixinOptionsScreen extends Screen // TODO is there a better way to do this instead of using access transformers to inject into the exact UI elements? // TODO is there a way we can put the button on the left side of the FOV bar like before? LinearLayout layout = (LinearLayout) this.layout.headerFrame.children.get(0).child; - layout.wrapped.addChild(this.getOptionsButton(), 1, 2); + layout.wrapped.addChild(this.getOptionsButton(), 1, 2); layout.arrangeElements(); #endif @@ -125,7 +125,7 @@ public class MixinOptionsScreen extends Screen // Create the button and tell it where to go // For now it goes to the client option by default (buttonWidget) -> Objects.requireNonNull(this.minecraft).setScreen(GetConfigScreen.getScreen(this)), - // Add a title to the utton + // Add a title to the button #if MC_VER < MC_1_19_2 new TranslatableComponent(ModInfo.ID + ".title")); #else diff --git a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/client/MixinOptionsScreen.java b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/client/MixinOptionsScreen.java index 657b521f0..1270fc0f8 100644 --- a/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/client/MixinOptionsScreen.java +++ b/neoforge/src/main/java/com/seibel/distanthorizons/neoforge/mixins/client/MixinOptionsScreen.java @@ -30,52 +30,110 @@ import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TranslatableComponent; #endif import net.minecraft.resources.ResourceLocation; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.util.Objects; +#if MC_VER == MC_1_20_6 +import net.minecraft.client.gui.layouts.LinearLayout; +import net.minecraft.client.gui.layouts.HeaderAndFooterLayout; +#endif + /** * Adds a button to the menu to goto the config * * @author coolGi - * @version 12-02-2021 + * @version 2024-5-20 */ @Mixin(OptionsScreen.class) public class MixinOptionsScreen extends Screen { - // Get the texture for the button + /** Texture used for the config opening button */ + @Unique private static final ResourceLocation ICON_TEXTURE = new ResourceLocation(ModInfo.ID, "textures/gui/button.png"); - protected MixinOptionsScreen(Component title) - { - super(title); - } - @Inject(at = @At("HEAD"), method = "init") + + @Unique + private TexturedButtonWidget optionsButton = null; + + #if MC_VER == MC_1_20_6 + @Shadow + @Final + protected HeaderAndFooterLayout layout; + #endif + + + + //==============// + // constructors // + //==============// + + protected MixinOptionsScreen(Component title) { super(title); } + + @Inject(at = @At("RETURN"), method = "init") private void lodconfig$init(CallbackInfo ci) { if (Config.Client.optionsButton.get()) - this. #if MC_VER < MC_1_17_1 addButton #else addRenderableWidget #endif - (new TexturedButtonWidget( - // Where the button is on the screen - this.width / 2 - 180, this.height / 6 - 12, - // Width and height of the button - 20, 20, - // Offset - 0, 0, - // Some textuary stuff - 20, ICON_TEXTURE, 20, 40, - // Create the button and tell it where to go - // For now it goes to the client option by default - (buttonWidget) -> Objects.requireNonNull(minecraft).setScreen(GetConfigScreen.getScreen(this)), - // Add a title to the button - #if MC_VER < MC_1_19_2 - new TranslatableComponent(ModInfo.ID + ".title"))); - #else - Component.translatable(ModInfo.ID + ".title"))); - #endif + { + #if MC_VER < MC_1_17_1 + this.addButton(this.getOptionsButton()); + #elif MC_VER < MC_1_20_6 + this.addRenderableWidget(this.getOptionsButton()); + #else + + // add the button so it's rendered + this.addRenderableWidget(this.getOptionsButton()); + + // add the button to the correct location in the UI + // TODO is there a better way to do this instead of using access transformers to inject into the exact UI elements? + // TODO is there a way we can put the button on the left side of the FOV bar like before? + LinearLayout layout = (LinearLayout) this.layout.headerFrame.children.get(0).child; + layout.wrapped.addChild(this.getOptionsButton(), 1, 2); + layout.arrangeElements(); + + #endif + } + } + + + + //================// + // helper methods // + //================// + + @Unique + public TexturedButtonWidget getOptionsButton() + { + if (this.optionsButton == null) + { + this.optionsButton + = new TexturedButtonWidget( + // Where the button is on the screen + this.width / 2 - 180, this.height / 6 - 12, + // Width and height of the button + 20, 20, + // texture UV Offset + 0, 0, + // Some textuary stuff + 20, ICON_TEXTURE, 20, 40, + // Create the button and tell it where to go + // For now it goes to the client option by default + (buttonWidget) -> Objects.requireNonNull(this.minecraft).setScreen(GetConfigScreen.getScreen(this)), + // Add a title to the button + #if MC_VER < MC_1_19_2 + new TranslatableComponent(ModInfo.ID + ".title")); + #else + Component.translatable(ModInfo.ID + ".title")); + #endif + } + + return this.optionsButton; } }